[hackers] [sbase] Eliminating the getopt disgrace. || Christoph Lohmann

From: <git_AT_suckless.org>
Date: Fri, 14 Jun 2013 20:22:18 +0200

commit 4d38f6068500104693796059da3950a2de296af3
Author: Christoph Lohmann <20h_AT_r-36.net>
Date: Fri Jun 14 20:20:47 2013 +0200

    Eliminating the getopt disgrace.

diff --git a/chmod.c b/chmod.c
index 950c2f7..805ecdc 100644
--- a/chmod.c
+++ b/chmod.c
_AT_@ -13,26 +13,32 @@ static bool rflag = false;
 static char oper = '=';
 static mode_t mode = 0;
 
+static void
+usage(void)
+{
+ eprintf("usage: %s [-r] mode [file...]
", argv0);
+ exit(1);
+}
+
 int
 main(int argc, char *argv[])
 {
- char c;
 
- while((c = getopt(argc, argv, "r")) != -1)
- switch(c) {
- case 'r':
- rflag = true;
- break;
- default:
- exit(EXIT_FAILURE);
- }
- if(optind == argc)
- eprintf("usage: %s [-r] mode [file...]
", argv[0]);
+ ARGBEGIN {
+ case 'r':
+ rflag = true;
+ break;
+ default:
+ usage();
+ } ARGEND;
+
+ if(argc < 1)
+ usage();
 
- parsemode(argv[optind++]);
- for(; optind < argc; optind++)
- chmodr(argv[optind]);
- return EXIT_SUCCESS;
+ parsemode(argv[0]);
+ for(++argv; argc > 0; argc--)
+ chmodr(argv[0]);
+ return 0;
 }
 
 void
diff --git a/chown.c b/chown.c
index 57da60a..867785c 100644
--- a/chown.c
+++ b/chown.c
_AT_@ -14,22 +14,30 @@ static bool rflag = false;
 static struct passwd *pw = NULL;
 static struct group *gr = NULL;
 
+static void
+usage(void)
+{
+ eprintf("usage: %s [-r] [owner][:[group]] file...
", argv0);
+ exit(1);
+}
+
 int
 main(int argc, char *argv[])
 {
- char c, *owner, *group;
+ char *owner, *group;
 
- while((c = getopt(argc, argv, "r")) != -1)
- switch(c) {
- case 'r':
- rflag = true;
- break;
- default:
- exit(EXIT_FAILURE);
- }
- if(optind == argc)
- eprintf("usage: %s [-r] [owner][:group] [file...]
", argv[0]);
- owner = argv[optind++];
+ ARGBEGIN {
+ case 'r':
+ rflag = true;
+ break;
+ default:
+ usage();
+ } ARGEND;
+ if(argc == 0)
+ usage();
+
+ owner = argv[0];
+ argv++;
         if((group = strchr(owner, ':')))
                 *group++ = '
Received on Fri Jun 14 2013 - 20:22:18 CEST

This archive was generated by hypermail 2.3.0 : Fri Jun 14 2013 - 20:24:13 CEST