>From 0f36fc9c51ca89c747e48e9425fdd429e2b31bc8 Mon Sep 17 00:00:00 2001
From: Tamas TEVESZ <[email protected]>
Date: Thu, 1 Apr 2010 03:30:05 +0200
Subject: [PATCH] Convert to getopt, continued

plus some assorted fixes:
 - sort headers, remove some unneeded ones
 - small bits of formatting and the like
---
 util/getstyle.c |   16 ++++++----
 util/seticons.c |    6 ++--
 util/wdread.c   |   87 +++++++++++++++++++++++++++++++++----------------------
 util/wdwrite.c  |   87 ++++++++++++++++++++++++++++++++----------------------
 4 files changed, 115 insertions(+), 81 deletions(-)

diff --git a/util/getstyle.c b/util/getstyle.c
index 6c97d39..9dd89a1 100644
--- a/util/getstyle.c
+++ b/util/getstyle.c
@@ -29,7 +29,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
@@ -286,12 +285,14 @@ void makeThemePack(WMPropList * style, char *themeName)
                        if (t == NULL)
                                continue;
 
-                       if (strcasecmp(t, "tpixmap") == 0
-                           || strcasecmp(t, "spixmap") == 0
-                           || strcasecmp(t, "cpixmap") == 0
-                           || strcasecmp(t, "mpixmap") == 0
-                           || strcasecmp(t, "tdgradient") == 0
-                           || strcasecmp(t, "tvgradient") == 0 || 
strcasecmp(t, "thgradient") == 0) {
+                       if (strcasecmp(t, "tpixmap") == 0 ||
+                           strcasecmp(t, "spixmap") == 0 ||
+                           strcasecmp(t, "cpixmap") == 0 ||
+                           strcasecmp(t, "mpixmap") == 0 ||
+                           strcasecmp(t, "tdgradient") == 0 ||
+                           strcasecmp(t, "tvgradient") == 0 ||
+                           strcasecmp(t, "thgradient") == 0) {
+
                                WMPropList *file;
                                char *p;
                                char *newPath;
@@ -310,6 +311,7 @@ void makeThemePack(WMPropList * style, char *themeName)
                                        findCopyFile(themeDir, 
WMGetFromPLString(file));
                                }
                        } else if (strcasecmp(t, "bitmap") == 0) {
+
                                WMPropList *file;
                                char *p;
                                char *newPath;
diff --git a/util/seticons.c b/util/seticons.c
index ffd38f4..84ea731 100644
--- a/util/seticons.c
+++ b/util/seticons.c
@@ -89,13 +89,13 @@ int main(int argc, char **argv)
        all_windows = WMReadPropListFromFile(path);
        if (!all_windows) {
                printf("%s: could not load WindowMaker configuration file 
\"%s\".\n", __progname, path);
-               exit(1);
+               return 1;
        }
 
        iconset = WMReadPropListFromFile(argv[0]);
        if (!iconset) {
                printf("%s: could not load icon set file \"%s\".\n", 
__progname, argv[0]);
-               exit(1);
+               return 1;
        }
 
        keylist = WMGetPLDictionaryKeys(iconset);
@@ -122,5 +122,5 @@ int main(int argc, char **argv)
 
        WMWritePropListToFile(all_windows, path);
 
-       exit(0);
+       return 0;
 }
diff --git a/util/wdread.c b/util/wdread.c
index a72fb22..b60fe07 100644
--- a/util/wdread.c
+++ b/util/wdread.c
@@ -23,66 +23,83 @@
 
 #define PROG_VERSION   "wdread (Window Maker) 0.2"
 
+#ifdef __GLIBC__
+#define _GNU_SOURCE            /* getopt_long */
+#endif
+
 /*
  * WindowMaker defaults DB reader
  */
 
-#include "../src/wconfig.h"
-
+#include <getopt.h>
+#include <limits.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <unistd.h>
 
 #include <WINGs/WUtil.h>
 
-#include <pwd.h>
+#include "../src/wconfig.h"
 
 extern char *__progname;
 
-void wAbort()
-{
-       exit(0);
-}
-
-void print_help()
+void print_help(int print_usage, int exitval)
 {
-       printf("Usage: %s [OPTIONS] <domain> <option>\n", __progname);
-       puts("");
-       puts("  -h, --help              display this help message");
-       puts("  -v, --version           output version information and exit");
-       exit(1);
+       printf("Usage: %s [OPTIONS] <domain> <key>\n", __progname);
+       if (print_usage) {
+               puts("Read <key> from <domain>'s database");
+               puts("");
+               puts("  -h, --help              display this help message");
+               puts("  -v, --version           output version information and 
exit");
+       }
+       exit(exitval);
 }
 
 int main(int argc, char **argv)
 {
-       char path[256];
+       char path[PATH_MAX];
        WMPropList *key, *value, *dict;
-       int i;
-
-       for (i = 1; i < argc; i++) {
-               if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 
0) {
-                       print_help();
-                       exit(0);
-               } else if (strcmp("-v", argv[i]) == 0 || strcmp("--version", 
argv[i]) == 0) {
-                       puts(PROG_VERSION);
-                       exit(0);
+       int ch;
+
+       struct option longopts[] = {
+               { "version",    no_argument,            NULL,                   
'v' },
+               { "help",       no_argument,            NULL,                   
'h' },
+               { NULL,         0,                      NULL,                   
0 }
+       };
+
+       while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
+               switch(ch) {
+                       case 'v':
+                               puts(PROG_VERSION);
+                               return 0;
+                               /* NOTREACHED */
+                       case 'h':
+                               print_help(1, 0);
+                               /* NOTREACHED */
+                       case 0:
+                               break;
+                       default:
+                               print_help(0, 1);
+                               /* NOTREACHED */
                }
-       }
 
-       if (argc < 3) {
-               printf("%s: invalid argument format\n", __progname);
-               printf("Try '%s --help' for more information\n", __progname);
-               exit(1);
-       }
+       argc -= optind;
+       argv += optind;
+
+       if (argc != 2)
+               print_help(0, 1);
 
-       key = WMCreatePLString(argv[2]);
+       key = WMCreatePLString(argv[1]);
 
-       snprintf(path, sizeof(path), wdefaultspathfordomain(argv[1]));
+       snprintf(path, sizeof(path), "%s", wdefaultspathfordomain(argv[0]));
 
-       if ((dict = WMReadPropListFromFile(path)) == NULL)
+       dict = WMReadPropListFromFile(path);
+       if (dict == NULL)
                return 1;       /* bad domain */
-       if ((value = WMGetFromPLDictionary(dict, key)) == NULL)
+
+       value = WMGetFromPLDictionary(dict, key);
+       if (value == NULL)
                return 2;       /* bad key */
 
        printf("%s\n", WMGetPropListDescription(value, True));
diff --git a/util/wdwrite.c b/util/wdwrite.c
index 494cd7f..017e711 100644
--- a/util/wdwrite.c
+++ b/util/wdwrite.c
@@ -22,68 +22,83 @@
 
 #define PROG_VERSION   "wdwrite (Window Maker) 0.2"
 
+#ifdef __GLIBC__
+#define _GNU_SOURCE            /* getopt_long */
+#endif
+
 /*
  * WindowMaker defaults DB writer
  */
 
-#include "../src/wconfig.h"
 
+#include <getopt.h>
+#include <limits.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <unistd.h>
 
 #include <WINGs/WUtil.h>
 
-#include <pwd.h>
+#include "../src/wconfig.h"
 
 extern char *__progname;
 
-void wAbort()
-{
-       exit(0);
-}
-
-void print_help()
+void print_help(int print_usage, int exitval)
 {
-       printf("Usage: %s [OPTIONS] <domain> <option> <value>\n", __progname);
-       puts("");
-       puts("  -h, --help        display this help message");
-       puts("  -v, --version     output version information and exit");
-       exit(1);
+       printf("Usage: %s [OPTIONS] <domain> <key> <value>\n", __progname);
+       if (print_usage) {
+               puts("Write <value> for <key> in <domain>'s database");
+               puts("");
+               puts("  -h, --help        display this help message");
+               puts("  -v, --version     output version information and exit");
+       }
+       exit(exitval);
 }
 
 int main(int argc, char **argv)
 {
-       char path[256];
+       char path[PATH_MAX];
        WMPropList *dom, *key, *value, *dict;
-       int i;
-
-       for (i = 1; i < argc; i++) {
-               if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 
0) {
-                       print_help();
-                       exit(0);
-               } else if (strcmp("-v", argv[i]) == 0 || strcmp("--version", 
argv[i]) == 0) {
-                       puts(PROG_VERSION);
-                       exit(0);
+       int ch;
+
+       struct option longopts[] = {
+               { "version",    no_argument,            NULL,                   
'v' },
+               { "help",       no_argument,            NULL,                   
'h' },
+               { NULL,         0,                      NULL,                   
0 }
+       };
+
+       while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
+               switch(ch) {
+                       case 'v':
+                               puts(PROG_VERSION);
+                               return 0;
+                               /* NOTREACHED */
+                       case 'h':
+                               print_help(1, 0);
+                               /* NOTREACHED */
+                       case 0:
+                               break;
+                       default:
+                               print_help(0, 1);
+                               /* NOTREACHED */
                }
-       }
 
-       if (argc < 4) {
-               printf("%s: invalid argument format\n", __progname);
-               printf("Try '%s --help' for more information\n", __progname);
-               exit(1);
-       }
+       argc -= optind;
+       argv += optind;
+
+       if (argc != 3)
+               print_help(0, 1);
 
-       dom = WMCreatePLString(argv[1]);
-       key = WMCreatePLString(argv[2]);
-       value = WMCreatePropListFromDescription(argv[3]);
+       dom = WMCreatePLString(argv[0]);
+       key = WMCreatePLString(argv[1]);
+       value = WMCreatePropListFromDescription(argv[2]);
        if (!value) {
-               printf("%s: syntax error in value \"%s\"", __progname, argv[3]);
-               exit(1);
+               printf("%s: syntax error in value \"%s\"", __progname, argv[2]);
+               return 1;
        }
 
-       snprintf(path, sizeof(path), wdefaultspathfordomain(argv[1]));
+       snprintf(path, sizeof(path), "%s", wdefaultspathfordomain(argv[0]));
 
        dict = WMReadPropListFromFile(path);
        if (!dict) {
-- 
1.7.0


-- 
[-]

mkdir /nonexistent
From 0f36fc9c51ca89c747e48e9425fdd429e2b31bc8 Mon Sep 17 00:00:00 2001
From: Tamas TEVESZ <[email protected]>
Date: Thu, 1 Apr 2010 03:30:05 +0200
Subject: [PATCH] Convert to getopt, continued

plus some assorted fixes:
 - sort headers, remove some unneeded ones
 - small bits of formatting and the like
---
 util/getstyle.c |   16 ++++++----
 util/seticons.c |    6 ++--
 util/wdread.c   |   87 +++++++++++++++++++++++++++++++++----------------------
 util/wdwrite.c  |   87 ++++++++++++++++++++++++++++++++----------------------
 4 files changed, 115 insertions(+), 81 deletions(-)

diff --git a/util/getstyle.c b/util/getstyle.c
index 6c97d39..9dd89a1 100644
--- a/util/getstyle.c
+++ b/util/getstyle.c
@@ -29,7 +29,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
@@ -286,12 +285,14 @@ void makeThemePack(WMPropList * style, char *themeName)
 			if (t == NULL)
 				continue;
 
-			if (strcasecmp(t, "tpixmap") == 0
-			    || strcasecmp(t, "spixmap") == 0
-			    || strcasecmp(t, "cpixmap") == 0
-			    || strcasecmp(t, "mpixmap") == 0
-			    || strcasecmp(t, "tdgradient") == 0
-			    || strcasecmp(t, "tvgradient") == 0 || strcasecmp(t, "thgradient") == 0) {
+			if (strcasecmp(t, "tpixmap") == 0 ||
+			    strcasecmp(t, "spixmap") == 0 ||
+			    strcasecmp(t, "cpixmap") == 0 ||
+			    strcasecmp(t, "mpixmap") == 0 ||
+			    strcasecmp(t, "tdgradient") == 0 ||
+			    strcasecmp(t, "tvgradient") == 0 ||
+			    strcasecmp(t, "thgradient") == 0) {
+
 				WMPropList *file;
 				char *p;
 				char *newPath;
@@ -310,6 +311,7 @@ void makeThemePack(WMPropList * style, char *themeName)
 					findCopyFile(themeDir, WMGetFromPLString(file));
 				}
 			} else if (strcasecmp(t, "bitmap") == 0) {
+
 				WMPropList *file;
 				char *p;
 				char *newPath;
diff --git a/util/seticons.c b/util/seticons.c
index ffd38f4..84ea731 100644
--- a/util/seticons.c
+++ b/util/seticons.c
@@ -89,13 +89,13 @@ int main(int argc, char **argv)
 	all_windows = WMReadPropListFromFile(path);
 	if (!all_windows) {
 		printf("%s: could not load WindowMaker configuration file \"%s\".\n", __progname, path);
-		exit(1);
+		return 1;
 	}
 
 	iconset = WMReadPropListFromFile(argv[0]);
 	if (!iconset) {
 		printf("%s: could not load icon set file \"%s\".\n", __progname, argv[0]);
-		exit(1);
+		return 1;
 	}
 
 	keylist = WMGetPLDictionaryKeys(iconset);
@@ -122,5 +122,5 @@ int main(int argc, char **argv)
 
 	WMWritePropListToFile(all_windows, path);
 
-	exit(0);
+	return 0;
 }
diff --git a/util/wdread.c b/util/wdread.c
index a72fb22..b60fe07 100644
--- a/util/wdread.c
+++ b/util/wdread.c
@@ -23,66 +23,83 @@
 
 #define PROG_VERSION	"wdread (Window Maker) 0.2"
 
+#ifdef __GLIBC__
+#define _GNU_SOURCE		/* getopt_long */
+#endif
+
 /*
  * WindowMaker defaults DB reader
  */
 
-#include "../src/wconfig.h"
-
+#include <getopt.h>
+#include <limits.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <unistd.h>
 
 #include <WINGs/WUtil.h>
 
-#include <pwd.h>
+#include "../src/wconfig.h"
 
 extern char *__progname;
 
-void wAbort()
-{
-	exit(0);
-}
-
-void print_help()
+void print_help(int print_usage, int exitval)
 {
-	printf("Usage: %s [OPTIONS] <domain> <option>\n", __progname);
-	puts("");
-	puts("  -h, --help              display this help message");
-	puts("  -v, --version           output version information and exit");
-	exit(1);
+	printf("Usage: %s [OPTIONS] <domain> <key>\n", __progname);
+	if (print_usage) {
+		puts("Read <key> from <domain>'s database");
+		puts("");
+		puts("  -h, --help              display this help message");
+		puts("  -v, --version           output version information and exit");
+	}
+	exit(exitval);
 }
 
 int main(int argc, char **argv)
 {
-	char path[256];
+	char path[PATH_MAX];
 	WMPropList *key, *value, *dict;
-	int i;
-
-	for (i = 1; i < argc; i++) {
-		if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
-			print_help();
-			exit(0);
-		} else if (strcmp("-v", argv[i]) == 0 || strcmp("--version", argv[i]) == 0) {
-			puts(PROG_VERSION);
-			exit(0);
+	int ch;
+
+	struct option longopts[] = {
+		{ "version",	no_argument,		NULL,			'v' },
+		{ "help",	no_argument,		NULL,			'h' },
+		{ NULL,		0,			NULL,			0 }
+	};
+
+	while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
+		switch(ch) {
+			case 'v':
+				puts(PROG_VERSION);
+				return 0;
+				/* NOTREACHED */
+			case 'h':
+				print_help(1, 0);
+				/* NOTREACHED */
+			case 0:
+				break;
+			default:
+				print_help(0, 1);
+				/* NOTREACHED */
 		}
-	}
 
-	if (argc < 3) {
-		printf("%s: invalid argument format\n", __progname);
-		printf("Try '%s --help' for more information\n", __progname);
-		exit(1);
-	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc != 2)
+		print_help(0, 1);
 
-	key = WMCreatePLString(argv[2]);
+	key = WMCreatePLString(argv[1]);
 
-	snprintf(path, sizeof(path), wdefaultspathfordomain(argv[1]));
+	snprintf(path, sizeof(path), "%s", wdefaultspathfordomain(argv[0]));
 
-	if ((dict = WMReadPropListFromFile(path)) == NULL)
+	dict = WMReadPropListFromFile(path);
+	if (dict == NULL)
 		return 1;	/* bad domain */
-	if ((value = WMGetFromPLDictionary(dict, key)) == NULL)
+
+	value = WMGetFromPLDictionary(dict, key);
+	if (value == NULL)
 		return 2;	/* bad key */
 
 	printf("%s\n", WMGetPropListDescription(value, True));
diff --git a/util/wdwrite.c b/util/wdwrite.c
index 494cd7f..017e711 100644
--- a/util/wdwrite.c
+++ b/util/wdwrite.c
@@ -22,68 +22,83 @@
 
 #define PROG_VERSION	"wdwrite (Window Maker) 0.2"
 
+#ifdef __GLIBC__
+#define _GNU_SOURCE		/* getopt_long */
+#endif
+
 /*
  * WindowMaker defaults DB writer
  */
 
-#include "../src/wconfig.h"
 
+#include <getopt.h>
+#include <limits.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <unistd.h>
 
 #include <WINGs/WUtil.h>
 
-#include <pwd.h>
+#include "../src/wconfig.h"
 
 extern char *__progname;
 
-void wAbort()
-{
-	exit(0);
-}
-
-void print_help()
+void print_help(int print_usage, int exitval)
 {
-	printf("Usage: %s [OPTIONS] <domain> <option> <value>\n", __progname);
-	puts("");
-	puts("  -h, --help        display this help message");
-	puts("  -v, --version     output version information and exit");
-	exit(1);
+	printf("Usage: %s [OPTIONS] <domain> <key> <value>\n", __progname);
+	if (print_usage) {
+		puts("Write <value> for <key> in <domain>'s database");
+		puts("");
+		puts("  -h, --help        display this help message");
+		puts("  -v, --version     output version information and exit");
+	}
+	exit(exitval);
 }
 
 int main(int argc, char **argv)
 {
-	char path[256];
+	char path[PATH_MAX];
 	WMPropList *dom, *key, *value, *dict;
-	int i;
-
-	for (i = 1; i < argc; i++) {
-		if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
-			print_help();
-			exit(0);
-		} else if (strcmp("-v", argv[i]) == 0 || strcmp("--version", argv[i]) == 0) {
-			puts(PROG_VERSION);
-			exit(0);
+	int ch;
+
+	struct option longopts[] = {
+		{ "version",	no_argument,		NULL,			'v' },
+		{ "help",	no_argument,		NULL,			'h' },
+		{ NULL,		0,			NULL,			0 }
+	};
+
+	while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
+		switch(ch) {
+			case 'v':
+				puts(PROG_VERSION);
+				return 0;
+				/* NOTREACHED */
+			case 'h':
+				print_help(1, 0);
+				/* NOTREACHED */
+			case 0:
+				break;
+			default:
+				print_help(0, 1);
+				/* NOTREACHED */
 		}
-	}
 
-	if (argc < 4) {
-		printf("%s: invalid argument format\n", __progname);
-		printf("Try '%s --help' for more information\n", __progname);
-		exit(1);
-	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc != 3)
+		print_help(0, 1);
 
-	dom = WMCreatePLString(argv[1]);
-	key = WMCreatePLString(argv[2]);
-	value = WMCreatePropListFromDescription(argv[3]);
+	dom = WMCreatePLString(argv[0]);
+	key = WMCreatePLString(argv[1]);
+	value = WMCreatePropListFromDescription(argv[2]);
 	if (!value) {
-		printf("%s: syntax error in value \"%s\"", __progname, argv[3]);
-		exit(1);
+		printf("%s: syntax error in value \"%s\"", __progname, argv[2]);
+		return 1;
 	}
 
-	snprintf(path, sizeof(path), wdefaultspathfordomain(argv[1]));
+	snprintf(path, sizeof(path), "%s", wdefaultspathfordomain(argv[0]));
 
 	dict = WMReadPropListFromFile(path);
 	if (!dict) {
-- 
1.7.0

Reply via email to