clean up, reorganize, etc.

This commit is contained in:
cgd 1994-02-10 23:36:59 +00:00
parent d69870dda1
commit 29270058b2

View File

@ -33,12 +33,14 @@
#if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)setmode.c 5.6 (Berkeley) 5/27/91";*/
static char *rcsid = "$Id: setmode.c,v 1.5 1994/01/04 05:05:36 cgd Exp $";
static char *rcsid = "$Id: setmode.c,v 1.6 1994/02/10 23:36:59 cgd Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <signal.h>
#include <errno.h>
#include <stddef.h>
#ifdef SETMODE_DEBUG
#include <stdio.h>
#endif
@ -48,11 +50,11 @@ static char *rcsid = "$Id: setmode.c,v 1.5 1994/01/04 05:05:36 cgd Exp $";
#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
struct bitcmd {
typedef struct bitcmd {
char cmd;
char cmd2;
mode_t bits;
};
} BITCMD;
#define CMD2_CLR 0x01
#define CMD2_SET 0x02
@ -60,6 +62,12 @@ struct bitcmd {
#define CMD2_OBITS 0x08
#define CMD2_UBITS 0x10
static BITCMD *addcmd __P((BITCMD *, int, int, int, u_int));
static int compress_mode __P((BITCMD *));
#ifdef SETMODE_DEBUG
static void dumpmode __P((BITCMD *));
#endif
/*
* Given the old mode and an array of bitcmd structures, apply the operations
* described in the bitcmd structures to the old mode, and return the new mode.
@ -71,16 +79,16 @@ getmode(bbox, omode)
void *bbox;
mode_t omode;
{
register struct bitcmd *set;
register BITCMD *set;
register mode_t newmode, value;
set = (struct bitcmd *)bbox;
set = (BITCMD *)bbox;
newmode = omode;
for (value = 0;; set++)
switch(set->cmd) {
/*
* When copying the user, group or other bits around, we "know"
* where the bit are in the mode so that we can do shifts to
* where the bits are in the mode so that we can do shifts to
* copy them around. If we don't use shifts, it gets real
* grundgy with lots of single bit checks and bit sets.
*/
@ -94,8 +102,7 @@ getmode(bbox, omode)
case 'o':
value = newmode & S_IRWXO;
common:
if (set->cmd2 & CMD2_CLR) {
common: if (set->cmd2 & CMD2_CLR) {
if (set->cmd2 & CMD2_UBITS)
newmode &= ~(S_IRWXU & set->bits);
if (set->cmd2 & CMD2_GBITS)
@ -129,107 +136,57 @@ getmode(bbox, omode)
case '\0':
default:
#ifdef SETMODE_DEBUG
(void)printf("getmode(, %04o) -> %04o\n",
omode, newmode);
(void)printf("getmode:%04o -> %04o\n", omode, newmode);
#endif
return(newmode);
return (newmode);
}
}
#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
static struct bitcmd *
addcmd(set, op, who, oparg, mask)
struct bitcmd *set;
register int oparg, who;
register int op;
mode_t mask;
{
switch (op) {
case '+':
case 'X':
set->cmd = op;
set->bits = (who ? who : mask) & oparg;
break;
case '-':
set->cmd = '-';
set->bits = (who ? who : (S_IRWXU|S_IRWXG|S_IRWXO)) & oparg;
break;
case '=':
set->cmd = '-';
if (!who) {
set->bits = STANDARD_BITS;
who = mask;
} else
set->bits = who;
set++;
set->cmd = '+';
set->bits = who & oparg;
break;
case 'u':
case 'g':
case 'o':
set->cmd = op;
if (who) {
set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
((who & S_IRGRP) ? CMD2_GBITS : 0) |
((who & S_IROTH) ? CMD2_OBITS : 0);
set->bits = ~0;
} else {
set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
set->bits = mask;
}
if (oparg == '+')
set->cmd2 |= CMD2_SET;
else if (oparg == '-')
set->cmd2 |= CMD2_CLR;
else if (oparg == '=')
set->cmd2 |= CMD2_SET|CMD2_CLR;
break;
}
return(set+1);
}
#define ADDCMD(a, b, c, d) \
if (set >= endset) { \
register struct bitcmd *newset; \
register BITCMD *newset; \
setlen += SET_LEN_INCR; \
newset = realloc(saveset, sizeof(struct bitcmd) * setlen); \
newset = realloc(saveset, sizeof(BITCMD) * setlen); \
if (!saveset) \
return(NULL); \
return (NULL); \
set = newset + (set - saveset); \
saveset = newset; \
endset = newset + (setlen - 2); \
} \
set = addcmd(set, (a), (b), (c), (d))
#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
void *
setmode(p)
register char *p;
{
register int perm, who;
register char op;
BITCMD *set, *saveset, *endset;
sigset_t sigset, sigoset;
mode_t mask;
struct bitcmd *set, *saveset, *endset;
int permXbits, setlen;
static void compress_mode();
if (!*p)
return (NULL);
/*
* Get a copy of the mask for the permissions that are mask relative.
* Flip the bits, we want what's not set.
* Flip the bits, we want what's not set. Since it's possible that
* the caller is opening files inside a signal handler, protect them
* as best we can.
*/
sigfillset(&sigset);
(void)sigprocmask(SIG_BLOCK, &sigset, &sigoset);
(void)umask(mask = umask(0));
mask = ~mask;
(void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
setlen = SET_LEN + 2;
set = (struct bitcmd *)malloc((u_int)(sizeof(struct bitcmd) * setlen));
if (!set)
return(NULL);
if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
return (NULL);
saveset = set;
endset = set + (setlen - 2);
@ -241,21 +198,17 @@ setmode(p)
perm = (mode_t)strtol(p, (char **)0, 8);
if (perm & ~(STANDARD_BITS|S_ISTXT)) {
free(saveset);
return(NULL);
return (NULL);
}
while (*++p)
if (*p < '0' || *p > '7') {
free(saveset);
return(NULL);
return (NULL);
}
ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
return((void *)saveset);
return (saveset);
}
if (!*p) {
free(saveset);
return(NULL);
}
/*
* Build list of structures to set/clear/copy bits as described by
* each clause of the symbolic mode.
@ -280,11 +233,10 @@ setmode(p)
goto getop;
}
}
getop:
if ((op = *p++) != '+' && op != '-' && op != '=') {
getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
free(saveset);
return(NULL);
return (NULL);
}
who &= ~S_ISTXT;
@ -366,12 +318,69 @@ apply: if (!*p)
(void)printf("After compress_mode()\n");
dumpmode(saveset);
#endif
return((void *)saveset);
return (saveset);
}
static BITCMD *
addcmd(set, op, who, oparg, mask)
BITCMD *set;
register int oparg, who;
register int op;
u_int mask;
{
switch (op) {
case '+':
case 'X':
set->cmd = op;
set->bits = (who ? who : mask) & oparg;
break;
case '-':
set->cmd = '-';
set->bits = (who ? who : (S_IRWXU|S_IRWXG|S_IRWXO)) & oparg;
break;
case '=':
set->cmd = '-';
if (!who) {
set->bits = STANDARD_BITS;
who = mask;
} else
set->bits = who;
set++;
set->cmd = '+';
set->bits = who & oparg;
break;
case 'u':
case 'g':
case 'o':
set->cmd = op;
if (who) {
set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
((who & S_IRGRP) ? CMD2_GBITS : 0) |
((who & S_IROTH) ? CMD2_OBITS : 0);
set->bits = ~0;
} else {
set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
set->bits = mask;
}
if (oparg == '+')
set->cmd2 |= CMD2_SET;
else if (oparg == '-')
set->cmd2 |= CMD2_CLR;
else if (oparg == '=')
set->cmd2 |= CMD2_SET|CMD2_CLR;
break;
}
return (set + 1);
}
#ifdef SETMODE_DEBUG
static void
dumpmode(set)
register struct bitcmd *set;
register BITCMD *set;
{
for (; set->cmd; ++set)
(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
@ -390,11 +399,11 @@ dumpmode(set)
* 'g' and 'o' commands continue to be separate. They could probably be
* compacted, but it's not worth the effort.
*/
static void
static int
compress_mode(set)
register struct bitcmd *set;
register BITCMD *set;
{
register struct bitcmd *nset;
register BITCMD *nset;
register int setbits, clrbits, Xbits, op;
for (nset = set;;) {