* Copyright (c) 1989 The Regents of the University of California.
* %sccs.include.redist.c%
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)setmode.c 5.5 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
* 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.
* Note that there is no '=' command; a strict assignment is just a '-' (clear
* bits) followed by a '+' (set bits).
register struct bitcmd
*set
;
register mode_t newmode
, value
;
set
= (struct bitcmd
*)bbox
;
* 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
* copy them around. If we don't use shifts, it gets real
* grundgy with lots of single bit checks and bit sets.
value
= (newmode
& S_IRWXU
) >> 6;
value
= (newmode
& S_IRWXG
) >> 3;
value
= newmode
& S_IRWXO
;
if (set
->cmd2
& CMD2_CLR
) {
if (set
->cmd2
& CMD2_UBITS
)
newmode
&= ~(S_IRWXU
& set
->bits
);
if (set
->cmd2
& CMD2_GBITS
)
newmode
&= ~(S_IRWXG
& set
->bits
);
if (set
->cmd2
& CMD2_OBITS
)
newmode
&= ~(S_IRWXO
& set
->bits
);
if (set
->cmd2
& CMD2_SET
) {
if (set
->cmd2
& CMD2_UBITS
)
newmode
|= (value
<<6) & set
->bits
;
if (set
->cmd2
& CMD2_GBITS
)
newmode
|= (value
<<3) & set
->bits
;
if (set
->cmd2
& CMD2_OBITS
)
newmode
|= value
& set
->bits
;
if (omode
& (S_IFDIR
|S_IXUSR
|S_IXGRP
|S_IXOTH
))
(void)printf("getmode(, %04o) -> %04o\n",
#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
addcmd(set
, op
, who
, oparg
, mask
)
set
->bits
= (who
? who
: mask
) & oparg
;
set
->bits
= (who
? who
: (S_IRWXU
|S_IRWXG
|S_IRWXO
)) & oparg
;
set
->bits
= STANDARD_BITS
;
set
->cmd2
= ((who
& S_IRUSR
) ? CMD2_UBITS
: 0) |
((who
& S_IRGRP
) ? CMD2_GBITS
: 0) |
((who
& S_IROTH
) ? CMD2_OBITS
: 0);
set
->cmd2
= CMD2_UBITS
| CMD2_GBITS
| CMD2_OBITS
;
set
->cmd2
|= CMD2_SET
|CMD2_CLR
;
#define ADDCMD(a, b, c, d) \
register struct bitcmd *newset; \
setlen += SET_LEN_INCR; \
newset = realloc(saveset, sizeof(struct bitcmd) * setlen); \
set = newset + (set - saveset); \
endset = newset + (setlen - 2); \
set = addcmd(set, (a), (b), (c), (d))
struct bitcmd
*set
, *saveset
, *endset
;
static int compress_mode();
* Get a copy of the mask for the permissions that are mask relative.
* Flip the bits, we want what's not set.
(void)umask(mask
= umask(0));
set
= (struct bitcmd
*)malloc((u_int
)(sizeof(struct bitcmd
) * setlen
));
endset
= set
+ (setlen
- 2);
* If an absolute number, get it and return; disallow non-octal digits
perm
= (mode_t
)strtol(p
, (char **)0, 8);
if (perm
& ~(STANDARD_BITS
|S_ISTXT
)) {
if (*p
< '0' || *p
> '7') {
ADDCMD('=', (STANDARD_BITS
|S_ISTXT
), perm
, mask
);
* Build list of structures to set/clear/copy bits as described by
* each clause of the symbolic mode.
/* First, find out which bits might be modified. */
if ((op
= *p
++) != '+' && op
!= '-' && op
!= '=') {
for (perm
= 0, permXbits
= 0;; ++p
) {
perm
|= S_IRUSR
|S_IRGRP
|S_IROTH
;
/* If only "other" bits ignore set-id. */
/* If only "other" bits ignore sticky. */
perm
|= S_IWUSR
|S_IWGRP
|S_IWOTH
;
permXbits
= S_IXUSR
|S_IXGRP
|S_IXOTH
;
perm
|= S_IXUSR
|S_IXGRP
|S_IXOTH
;
* When ever we hit 'u', 'g', or 'o', we have
* to flush out any partial mode that we have,
* and then do the copying of the mode bits.
ADDCMD(op
, who
, perm
, mask
);
if (op
== '+' && permXbits
) {
ADDCMD('X', who
, permXbits
, mask
);
ADDCMD(*p
, who
, op
, mask
);
* Add any permissions that we haven't already
ADDCMD(op
, who
, perm
, mask
);
ADDCMD('X', who
, permXbits
, mask
);
(void)printf("Before compress_mode()\n");
(void)printf("After compress_mode()\n");
register struct bitcmd
*set
;
(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
set
->cmd
, set
->bits
, set
->cmd2
? " cmd2:" : "",
set
->cmd2
& CMD2_CLR
? " CLR" : "",
set
->cmd2
& CMD2_SET
? " SET" : "",
set
->cmd2
& CMD2_UBITS
? " UBITS" : "",
set
->cmd2
& CMD2_GBITS
? " GBITS" : "",
set
->cmd2
& CMD2_OBITS
? " OBITS" : "");
* Given an array of bitcmd structures, compress by compacting consecutive
* '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
* 'g' and 'o' commands continue to be separate. They could probably be
* compacted, but it's not worth the effort.
register struct bitcmd
*set
;
register struct bitcmd
*nset
;
register int setbits
, clrbits
, Xbits
, op
;
/* Copy over any 'u', 'g' and 'o' commands. */
while ((op
= nset
->cmd
) != '+' && op
!= '-' && op
!= 'X') {
for (setbits
= clrbits
= Xbits
= 0;; nset
++) {
if ((op
= nset
->cmd
) == '-') {
Xbits
|= nset
->bits
& ~setbits
;