Deleted Added
sdiff udiff text old ( 71579 ) new ( 90045 )
full compact
1/*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Dave Borman at Cray Research, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 21 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
39#endif /* LIBC_SCCS and not lint */
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/lib/libc/gen/setmode.c 90045 2002-02-01 01:32:19Z obrien $");
42
43#include "namespace.h"
44#include <sys/types.h>
45#include <sys/stat.h>
46
47#include <ctype.h>
48#include <signal.h>
49#include <stddef.h>

--- 14 unchanged lines hidden (view full) ---

64} BITCMD;
65
66#define CMD2_CLR 0x01
67#define CMD2_SET 0x02
68#define CMD2_GBITS 0x04
69#define CMD2_OBITS 0x08
70#define CMD2_UBITS 0x10
71
72static BITCMD *addcmd(BITCMD *, int, int, int, u_int);
73static void compress_mode(BITCMD *);
74#ifdef SETMODE_DEBUG
75static void dumpmode(BITCMD *);
76#endif
77
78/*
79 * Given the old mode and an array of bitcmd structures, apply the operations
80 * described in the bitcmd structures to the old mode, and return the new mode.
81 * Note that there is no '=' command; a strict assignment is just a '-' (clear
82 * bits) followed by a '+' (set bits).
83 */
84mode_t
85getmode(bbox, omode)
86 void *bbox;
87 mode_t omode;
88{
89 BITCMD *set;
90 mode_t clrval, newmode, value;
91
92 set = (BITCMD *)bbox;
93 newmode = omode;
94 for (value = 0;; set++)
95 switch(set->cmd) {
96 /*
97 * When copying the user, group or other bits around, we "know"
98 * where the bits are in the mode so that we can do shifts to

--- 49 unchanged lines hidden (view full) ---

148 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
149#endif
150 return (newmode);
151 }
152}
153
154#define ADDCMD(a, b, c, d) \
155 if (set >= endset) { \
156 BITCMD *newset; \
157 setlen += SET_LEN_INCR; \
158 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
159 if (!saveset) \
160 return (NULL); \
161 set = newset + (set - saveset); \
162 saveset = newset; \
163 endset = newset + (setlen - 2); \
164 } \
165 set = addcmd(set, (a), (b), (c), (d))
166
167#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
168
169void *
170setmode(p)
171 char *p;
172{
173 int perm, who;
174 char op;
175 BITCMD *set, *saveset, *endset;
176 sigset_t sigset, sigoset;
177 mode_t mask;
178 int equalopdone=0, permXbits, setlen;
179
180 if (!*p)
181 return (NULL);
182

--- 151 unchanged lines hidden (view full) ---

334 dumpmode(saveset);
335#endif
336 return (saveset);
337}
338
339static BITCMD *
340addcmd(set, op, who, oparg, mask)
341 BITCMD *set;
342 int oparg, who;
343 int op;
344 u_int mask;
345{
346 switch (op) {
347 case '=':
348 set->cmd = '-';
349 set->bits = who ? who : STANDARD_BITS;
350 set++;
351

--- 29 unchanged lines hidden (view full) ---

381 break;
382 }
383 return (set + 1);
384}
385
386#ifdef SETMODE_DEBUG
387static void
388dumpmode(set)
389 BITCMD *set;
390{
391 for (; set->cmd; ++set)
392 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
393 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
394 set->cmd2 & CMD2_CLR ? " CLR" : "",
395 set->cmd2 & CMD2_SET ? " SET" : "",
396 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
397 set->cmd2 & CMD2_GBITS ? " GBITS" : "",

--- 4 unchanged lines hidden (view full) ---

402/*
403 * Given an array of bitcmd structures, compress by compacting consecutive
404 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
405 * 'g' and 'o' commands continue to be separate. They could probably be
406 * compacted, but it's not worth the effort.
407 */
408static void
409compress_mode(set)
410 BITCMD *set;
411{
412 BITCMD *nset;
413 int setbits, clrbits, Xbits, op;
414
415 for (nset = set;;) {
416 /* Copy over any 'u', 'g' and 'o' commands. */
417 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
418 *set++ = *nset++;
419 if (!op)
420 return;
421 }

--- 35 unchanged lines hidden ---