commit bd99b92e9107dd4af44d57e6a31da153d64df99c
Author: Hiltjo Posthuma <[email protected]>
Date:   Wed Apr 23 21:28:59 2014 +0200

    parsemode: rework
    
    - for octal input: reset mode to 0.
    - take umask into account.
    - make '=rwx' etc work.
    - we wont support crazy but valid modes like "a+rw,g=x,o=g"
    - uudecode: use parsemode, mask is 0.
    
    Signed-off-by: Hiltjo Posthuma <[email protected]>

diff --git a/chmod.c b/chmod.c
index ddda633..63d800a 100644
--- a/chmod.c
+++ b/chmod.c
@@ -9,8 +9,8 @@
 static void chmodr(const char *);
 
 static bool rflag = false;
-static int oper = '=';
-static mode_t mode = 0;
+static char *modestr = "";
+static mode_t mask = 0;
 
 static void
 usage(void)
@@ -45,7 +45,8 @@ main(int argc, char *argv[])
        }
 
 done:
-       parsemode(argv[0], &mode, &oper);
+       mask = getumask();
+       modestr = argv[0];
        argv++;
        argc--;
 
@@ -61,22 +62,13 @@ void
 chmodr(const char *path)
 {
        struct stat st;
+       mode_t m;
 
        if(stat(path, &st) == -1)
                eprintf("stat %s:", path);
 
-       switch(oper) {
-       case '+':
-               st.st_mode |= mode;
-               break;
-       case '-':
-               st.st_mode &= ~mode;
-               break;
-       case '=':
-               st.st_mode = mode;
-               break;
-       }
-       if(chmod(path, st.st_mode) == -1)
+       m = parsemode(modestr, st.st_mode, mask);
+       if(chmod(path, m) == -1)
                weprintf("chmod %s:", path);
        if(rflag)
                recurse(path, chmodr);
diff --git a/util.h b/util.h
index 6c7f659..4f0cbbf 100644
--- a/util.h
+++ b/util.h
@@ -27,4 +27,5 @@ size_t strlcat(char *, const char *, size_t);
 size_t strlcpy(char *, const char *, size_t);
 void weprintf(const char *, ...);
 
-void parsemode(const char *, mode_t *, int *);
+mode_t getumask(void);
+mode_t parsemode(const char *, mode_t, mode_t);
diff --git a/util/mode.c b/util/mode.c
index 69e106f..cd29e65 100644
--- a/util/mode.c
+++ b/util/mode.c
@@ -4,74 +4,98 @@
 #include <sys/stat.h>
 #include "../util.h"
 
-void
-parsemode(const char *str, mode_t *mode, int *oper)
+mode_t
+getumask(void)
+{
+    mode_t mask = umask(0);
+    umask(mask);
+    return mask;
+}
+
+mode_t
+parsemode(const char *str, mode_t mode, mode_t mask)
 {
        char *end;
        const char *p;
-       int octal;
-       mode_t mask = 0;
+       int octal, op = '+';
+       mode_t gmask = 0, m = 0;
 
        octal = strtol(str, &end, 8);
        if(*end == '

Reply via email to