make parsemode() generic

use for uudecode and chmod

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
This commit is contained in:
Hiltjo Posthuma 2014-04-09 15:17:20 +02:00 committed by sin
parent ff474a8cbc
commit 560340341f
5 changed files with 84 additions and 104 deletions

View File

@ -17,6 +17,7 @@ LIB = \
util/fnck.o \ util/fnck.o \
util/getlines.o \ util/getlines.o \
util/md5.o \ util/md5.o \
util/mode.o \
util/putword.o \ util/putword.o \
util/recurse.o \ util/recurse.o \
util/rm.o \ util/rm.o \

75
chmod.c
View File

@ -7,10 +7,9 @@
#include "util.h" #include "util.h"
static void chmodr(const char *); static void chmodr(const char *);
static void parsemode(const char *);
static bool rflag = false; static bool rflag = false;
static char oper = '='; static int oper = '=';
static mode_t mode = 0; static mode_t mode = 0;
static void static void
@ -46,7 +45,7 @@ main(int argc, char *argv[])
} }
done: done:
parsemode(argv[0]); parsemode(argv[0], &mode, &oper);
argv++; argv++;
argc--; argc--;
@ -82,73 +81,3 @@ chmodr(const char *path)
if(rflag) if(rflag)
recurse(path, chmodr); recurse(path, chmodr);
} }
void
parsemode(const char *str)
{
char *end;
const char *p;
int octal;
mode_t mask = 0;
octal = strtol(str, &end, 8);
if(*end == '\0') {
if( octal < 0 || octal > 07777) eprintf("invalid mode\n");
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;
}
for(p = str; *p; p++)
switch(*p) {
/* masks */
case 'u':
mask |= S_IRWXU;
break;
case 'g':
mask |= S_IRWXG;
break;
case 'o':
mask |= S_IRWXO;
break;
case 'a':
mask |= S_IRWXU|S_IRWXG|S_IRWXO;
break;
/* opers */
case '+':
case '-':
case '=':
oper = *p;
break;
/* modes */
case 'r':
mode |= S_IRUSR|S_IRGRP|S_IROTH;
break;
case 'w':
mode |= S_IWUSR|S_IWGRP|S_IWOTH;
break;
case 'x':
mode |= S_IXUSR|S_IXGRP|S_IXOTH;
break;
case 's':
mode |= S_ISUID|S_ISGID;
break;
case 't':
mode |= S_ISVTX;
break;
/* error */
default:
eprintf("%s: invalid mode\n", str);
}
if(mask)
mode &= mask;
}

3
util.h
View File

@ -1,5 +1,6 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stddef.h> #include <stddef.h>
#include <sys/stat.h>
#include "arg.h" #include "arg.h"
#define UTF8_POINT(c) (((c) & 0xc0) != 0x80) #define UTF8_POINT(c) (((c) & 0xc0) != 0x80)
@ -25,3 +26,5 @@ size_t strlcat(char *, const char *, size_t);
#undef strlcpy #undef strlcpy
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 *);

77
util/mode.c Normal file
View File

@ -0,0 +1,77 @@
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "../util.h"
void
parsemode(const char *str, mode_t *mode, int *oper)
{
char *end;
const char *p;
int octal;
mode_t mask = 0;
octal = strtol(str, &end, 8);
if(*end == '\0') {
if(octal < 0 || octal > 07777)
eprintf("%s: invalid mode\n", str);
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;
}
for(p = str; *p; p++)
switch(*p) {
/* masks */
case 'u':
mask |= S_IRWXU;
break;
case 'g':
mask |= S_IRWXG;
break;
case 'o':
mask |= S_IRWXO;
break;
case 'a':
mask |= S_IRWXU|S_IRWXG|S_IRWXO;
break;
/* opers */
case '+':
case '-':
case '=':
if(oper)
*oper = (int)*p;
break;
/* modes */
case 'r':
*mode |= S_IRUSR|S_IRGRP|S_IROTH;
break;
case 'w':
*mode |= S_IWUSR|S_IWGRP|S_IWOTH;
break;
case 'x':
*mode |= S_IXUSR|S_IXGRP|S_IXOTH;
break;
case 's':
*mode |= S_ISUID|S_ISGID;
break;
case 't':
*mode |= S_ISVTX;
break;
/* error */
default:
eprintf("%s: invalid mode\n", str);
}
if(mask)
*mode &= mask;
}

View File

@ -12,7 +12,6 @@
static void uudecode(FILE *, FILE *); static void uudecode(FILE *, FILE *);
static void parseheader(FILE *, const char *, const char *, mode_t *, char **); static void parseheader(FILE *, const char *, const char *, mode_t *, char **);
static void parsemode(const char *, mode_t *);
static FILE *parsefile(const char *); static FILE *parsefile(const char *);
static void static void
@ -108,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); parsemode(p, mode, NULL);
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';
@ -116,35 +115,6 @@ parseheader(FILE *fp, const char *s, const char *header, mode_t *mode, char **fn
*fname = q; *fname = q;
} }
static void
parsemode(const char *str, mode_t *validmode)
{
char *end;
int octal;
if (str == NULL || str == '\0')
eprintf("invalid mode\n");
octal = strtol(str, &end, 8);
if (*end == '\0') {
if (octal >= 0 && octal <= 07777) {
if(octal & 04000) *validmode |= S_ISUID;
if(octal & 02000) *validmode |= S_ISGID;
if(octal & 01000) *validmode |= S_ISVTX;
if(octal & 00400) *validmode |= S_IRUSR;
if(octal & 00200) *validmode |= S_IWUSR;
if(octal & 00100) *validmode |= S_IXUSR;
if(octal & 00040) *validmode |= S_IRGRP;
if(octal & 00020) *validmode |= S_IWGRP;
if(octal & 00010) *validmode |= S_IXGRP;
if(octal & 00004) *validmode |= S_IROTH;
if(octal & 00002) *validmode |= S_IWOTH;
if(octal & 00001) *validmode |= S_IXOTH;
} else {
eprintf("invalid mode\n");
}
}
}
static void static void
uudecode(FILE *fp, FILE *outfp) uudecode(FILE *fp, FILE *outfp)
{ {