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 <hiltjo@codemadness.org>
This commit is contained in:
Hiltjo Posthuma 2014-04-23 21:28:59 +02:00 committed by sin
parent a1b62b2282
commit bd99b92e91
4 changed files with 68 additions and 51 deletions

22
chmod.c
View File

@ -9,8 +9,8 @@
static void chmodr(const char *); static void chmodr(const char *);
static bool rflag = false; static bool rflag = false;
static int oper = '='; static char *modestr = "";
static mode_t mode = 0; static mode_t mask = 0;
static void static void
usage(void) usage(void)
@ -45,7 +45,8 @@ main(int argc, char *argv[])
} }
done: done:
parsemode(argv[0], &mode, &oper); mask = getumask();
modestr = argv[0];
argv++; argv++;
argc--; argc--;
@ -61,22 +62,13 @@ void
chmodr(const char *path) chmodr(const char *path)
{ {
struct stat st; struct stat st;
mode_t m;
if(stat(path, &st) == -1) if(stat(path, &st) == -1)
eprintf("stat %s:", path); eprintf("stat %s:", path);
switch(oper) { m = parsemode(modestr, st.st_mode, mask);
case '+': if(chmod(path, m) == -1)
st.st_mode |= mode;
break;
case '-':
st.st_mode &= ~mode;
break;
case '=':
st.st_mode = mode;
break;
}
if(chmod(path, st.st_mode) == -1)
weprintf("chmod %s:", path); weprintf("chmod %s:", path);
if(rflag) if(rflag)
recurse(path, chmodr); recurse(path, chmodr);

3
util.h
View File

@ -27,4 +27,5 @@ size_t strlcat(char *, const char *, size_t);
size_t strlcpy(char *, const char *, size_t); size_t strlcpy(char *, const char *, size_t);
void weprintf(const char *, ...); void weprintf(const char *, ...);
void parsemode(const char *, mode_t *, int *); mode_t getumask(void);
mode_t parsemode(const char *, mode_t, mode_t);

View File

@ -4,74 +4,98 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "../util.h" #include "../util.h"
void mode_t
parsemode(const char *str, mode_t *mode, int *oper) 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; char *end;
const char *p; const char *p;
int octal; int octal, op = '+';
mode_t mask = 0; mode_t gmask = 0, m = 0;
octal = strtol(str, &end, 8); octal = strtol(str, &end, 8);
if(*end == '\0') { if(*end == '\0') {
if(octal < 0 || octal > 07777) if(octal < 0 || octal > 07777) {
eprintf("%s: invalid mode\n", str); eprintf("%s: invalid mode\n", str);
if(octal & 04000) *mode |= S_ISUID; return -1;
if(octal & 02000) *mode |= S_ISGID;
if(octal & 01000) *mode |= S_ISVTX;
if(octal & 00400) *mode |= S_IRUSR;
if(octal & 00200) *mode |= S_IWUSR;
if(octal & 00100) *mode |= S_IXUSR;
if(octal & 00040) *mode |= S_IRGRP;
if(octal & 00020) *mode |= S_IWGRP;
if(octal & 00010) *mode |= S_IXGRP;
if(octal & 00004) *mode |= S_IROTH;
if(octal & 00002) *mode |= S_IWOTH;
if(octal & 00001) *mode |= S_IXOTH;
return;
} }
for(p = str; *p; p++) mode = 0;
if(octal & 04000) mode |= S_ISUID;
if(octal & 02000) mode |= S_ISGID;
if(octal & 01000) mode |= S_ISVTX;
if(octal & 00400) mode |= S_IRUSR;
if(octal & 00200) mode |= S_IWUSR;
if(octal & 00100) mode |= S_IXUSR;
if(octal & 00040) mode |= S_IRGRP;
if(octal & 00020) mode |= S_IWGRP;
if(octal & 00010) mode |= S_IXGRP;
if(octal & 00004) mode |= S_IROTH;
if(octal & 00002) mode |= S_IWOTH;
if(octal & 00001) mode |= S_IXOTH;
return mode;
}
for(p = str; *p; p++) {
switch(*p) { switch(*p) {
/* masks */ /* masks */
case 'u': case 'u':
mask |= S_IRWXU; gmask |= S_IRWXU;
break; break;
case 'g': case 'g':
mask |= S_IRWXG; gmask |= S_IRWXG;
break; break;
case 'o': case 'o':
mask |= S_IRWXO; gmask |= S_IRWXO;
break; break;
case 'a': case 'a':
mask |= S_IRWXU|S_IRWXG|S_IRWXO; gmask |= S_IRWXU|S_IRWXG|S_IRWXO;
break; break;
/* opers */ /* opers */
case '=':
case '+': case '+':
case '-': case '-':
case '=': op = (int)*p;
if(oper)
*oper = (int)*p;
break; break;
/* modes */ /* modes */
case 'r': case 'r':
*mode |= S_IRUSR|S_IRGRP|S_IROTH; m |= S_IRUSR|S_IRGRP|S_IROTH;
break; break;
case 'w': case 'w':
*mode |= S_IWUSR|S_IWGRP|S_IWOTH; m |= S_IWUSR|S_IWGRP|S_IWOTH;
break; break;
case 'x': case 'x':
*mode |= S_IXUSR|S_IXGRP|S_IXOTH; m |= S_IXUSR|S_IXGRP|S_IXOTH;
break; break;
case 's': case 's':
*mode |= S_ISUID|S_ISGID; m |= S_ISUID|S_ISGID;
break; break;
case 't': case 't':
*mode |= S_ISVTX; m |= S_ISVTX;
break; break;
/* error */
default: default:
eprintf("%s: invalid mode\n", str); eprintf("%s: invalid mode\n", str);
return -1;
} }
if(mask) /* apply */
*mode &= mask; switch(op) {
case '+':
mode |= (m & ((~mask) & 0777));
break;
case '-':
mode &= (~(m & ((~mask) & 0777)));
break;
case '=':
mode = (m & ((~mask) & 0777));
break;
}
}
if(gmask && op != '=')
mode &= ~gmask;
return mode;
} }

View File

@ -107,7 +107,7 @@ parseheader(FILE *fp, const char *s, const char *header, mode_t *mode, char **fn
eprintf("malformed mode string in header\n"); eprintf("malformed mode string in header\n");
*q++ = '\0'; *q++ = '\0';
/* now mode should be null terminated, q points to fname */ /* now mode should be null terminated, q points to fname */
parsemode(p, mode, NULL); *mode = parsemode(p, *mode, 0);
n = strlen(q); n = strlen(q);
while (n > 0 && (q[n - 1] == '\n' || q[n - 1] == '\r')) while (n > 0 && (q[n - 1] == '\n' || q[n - 1] == '\r'))
q[--n] = '\0'; q[--n] = '\0';