Hi,

This is a quick fix for chmod(1).  Doing something like chmod -x file would fail
as it would try to interpret 'x' as a command line option rather than as a mode.

Let me know if this makes sense or if there is a simpler way to fix it.

Thanks,
sin
>From 73121ad2f267a95e13a023dcda0b7794389ed9b3 Mon Sep 17 00:00:00 2001
From: sin <[email protected]>
Date: Thu, 10 Oct 2013 14:50:52 +0100
Subject: [PATCH] Do not interpret -[rwxs] as options in chmod(1)

To chmod recursively use `-R' as opposed to `-r' so we can
distinguish it from the mode `-r'.
---
 chmod.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/chmod.c b/chmod.c
index 9cdb50e..6d0f8a7 100644
--- a/chmod.c
+++ b/chmod.c
@@ -16,26 +16,44 @@ static mode_t mode = 0;
 static void
 usage(void)
 {
-       eprintf("usage: %s [-r] mode [file...]\n", argv0);
+       eprintf("usage: %s [-R] mode [file...]\n", argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
+       int c;
+       argv0 = argv[0];
 
-       ARGBEGIN {
-       case 'r':
-               rflag = true;
-               break;
-       default:
-               usage();
-       } ARGEND;
+       while (--argc > 0 && (*++argv)[0] == '-') {
+               while ((c = *++argv[0])) {
+                       switch (c) {
+                       case 'R':
+                               rflag = true;
+                               break;
+                       case 'r': case 'w': case 'x': case 's':
+                               /*
+                                * -[rwxs] 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:
+       parsemode(argv[0]);
+       argv++;
+       argc--;
 
        if(argc < 1)
                usage();
 
-       parsemode(argv[0]);
-       for(++argv; argc > 0; argc--)
+       for (; argc > 0; argc--, argv++)
                chmodr(argv[0]);
        return EXIT_SUCCESS;
 }
-- 
1.8.3.4

Reply via email to