commit eff58b553123206677cc8cdde8c7c539662c469e
Author: FRIGN <[email protected]>
Date:   Sat Jan 17 00:01:51 2015 +0100

    Refactor chmod(1)
    
    Clean the mess.

diff --git a/chmod.c b/chmod.c
index 9f3ab9e..93b97a3 100644
--- a/chmod.c
+++ b/chmod.c
@@ -6,58 +6,10 @@
 
 #include "util.h"
 
-static void chmodr(const char *);
-
-static int rflag = 0;
-static char *modestr = "";
-static mode_t mask = 0;
-static int ret = 0;
-
-static void
-usage(void)
-{
-       eprintf("usage: %s [-R] mode [file...]\n", argv0);
-}
-
-int
-main(int argc, char *argv[])
-{
-       int c;
-       argv0 = argv[0];
-
-       while (--argc > 0 && (*++argv)[0] == '-') {
-               while ((c = *++argv[0])) {
-                       switch (c) {
-                       case 'R':
-                               rflag = 1;
-                               break;
-                       case 'r': case 'w': case 'x': case 's': case 't':
-                               /*
-                                * -[rwxst] are valid modes so do not interpret
-                                * them as options - in any case we are done if
-                                * we hit this case
-                                */
-                               --argv[0];
-                               goto done;
-                       default:
-                               usage();
-                       }
-               }
-       }
-
-done:
-       mask = getumask();
-       modestr = argv[0];
-       argv++;
-       argc--;
-
-       if (argc < 1)
-               usage();
-
-       for (; argc > 0; argc--, argv++)
-               chmodr(argv[0]);
-       return ret;
-}
+static int    rflag   = 0;
+static char  *modestr = "";
+static mode_t mask    = 0;
+static int    ret     = 0;
 
 void
 chmodr(const char *path)
@@ -79,3 +31,44 @@ chmodr(const char *path)
        if (rflag)
                recurse(path, chmodr);
 }
+
+static void
+usage(void)
+{
+       eprintf("usage: %s [-R] mode [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+       size_t i;
+
+       argv0 = argv[0];
+       for (i = 1; i < argc && argv[i][0] == '-'; i++) {
+               switch (argv[i][1]) {
+               case 'R':
+                       rflag = 1;
+                       break;
+               case 'r': case 'w': case 'x': case 's': case 't':
+                       /*
+                        * -[rwxst] are valid modes so do not interpret
+                        * them as options - in any case we are done if
+                        * we hit this case
+                        */
+                       goto done;
+               default:
+                       usage();
+               }
+       }
+done:
+       mask = getumask();
+       modestr = argv[i];
+
+       if (argc - i - 1 < 1)
+               usage();
+
+       for (++i; i < argc; i++)
+               chmodr(argv[i]);
+
+       return ret;
+}

Reply via email to