Deleted Added
full compact
setmode.c (71579) setmode.c (90045)
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
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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)
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
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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)
38#if 0
39static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
38static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
40#endif
41static const char rcsid[] =
42 "$FreeBSD: head/lib/libc/gen/setmode.c 71579 2001-01-24 13:01:12Z deischen $";
43#endif /* LIBC_SCCS and not lint */
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 $");
44
45#include "namespace.h"
46#include <sys/types.h>
47#include <sys/stat.h>
48
49#include <ctype.h>
50#include <signal.h>
51#include <stddef.h>
52#include <stdlib.h>
53
54#ifdef SETMODE_DEBUG
55#include <stdio.h>
56#endif
57#include "un-namespace.h"
58
59#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
60#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
61
62typedef struct bitcmd {
63 char cmd;
64 char cmd2;
65 mode_t bits;
66} BITCMD;
67
68#define CMD2_CLR 0x01
69#define CMD2_SET 0x02
70#define CMD2_GBITS 0x04
71#define CMD2_OBITS 0x08
72#define CMD2_UBITS 0x10
73
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>
50#include <stdlib.h>
51
52#ifdef SETMODE_DEBUG
53#include <stdio.h>
54#endif
55#include "un-namespace.h"
56
57#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
58#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
59
60typedef struct bitcmd {
61 char cmd;
62 char cmd2;
63 mode_t bits;
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
74static BITCMD *addcmd __P((BITCMD *, int, int, int, u_int));
75static void compress_mode __P((BITCMD *));
72static BITCMD *addcmd(BITCMD *, int, int, int, u_int);
73static void compress_mode(BITCMD *);
76#ifdef SETMODE_DEBUG
74#ifdef SETMODE_DEBUG
77static void dumpmode __P((BITCMD *));
75static void dumpmode(BITCMD *);
78#endif
79
80/*
81 * Given the old mode and an array of bitcmd structures, apply the operations
82 * described in the bitcmd structures to the old mode, and return the new mode.
83 * Note that there is no '=' command; a strict assignment is just a '-' (clear
84 * bits) followed by a '+' (set bits).
85 */
86mode_t
87getmode(bbox, omode)
88 void *bbox;
89 mode_t omode;
90{
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{
91 register BITCMD *set;
92 register mode_t clrval, newmode, value;
89 BITCMD *set;
90 mode_t clrval, newmode, value;
93
94 set = (BITCMD *)bbox;
95 newmode = omode;
96 for (value = 0;; set++)
97 switch(set->cmd) {
98 /*
99 * When copying the user, group or other bits around, we "know"
100 * where the bits are in the mode so that we can do shifts to
101 * copy them around. If we don't use shifts, it gets real
102 * grundgy with lots of single bit checks and bit sets.
103 */
104 case 'u':
105 value = (newmode & S_IRWXU) >> 6;
106 goto common;
107
108 case 'g':
109 value = (newmode & S_IRWXG) >> 3;
110 goto common;
111
112 case 'o':
113 value = newmode & S_IRWXO;
114common: if (set->cmd2 & CMD2_CLR) {
115 clrval =
116 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
117 if (set->cmd2 & CMD2_UBITS)
118 newmode &= ~((clrval<<6) & set->bits);
119 if (set->cmd2 & CMD2_GBITS)
120 newmode &= ~((clrval<<3) & set->bits);
121 if (set->cmd2 & CMD2_OBITS)
122 newmode &= ~(clrval & set->bits);
123 }
124 if (set->cmd2 & CMD2_SET) {
125 if (set->cmd2 & CMD2_UBITS)
126 newmode |= (value<<6) & set->bits;
127 if (set->cmd2 & CMD2_GBITS)
128 newmode |= (value<<3) & set->bits;
129 if (set->cmd2 & CMD2_OBITS)
130 newmode |= value & set->bits;
131 }
132 break;
133
134 case '+':
135 newmode |= set->bits;
136 break;
137
138 case '-':
139 newmode &= ~set->bits;
140 break;
141
142 case 'X':
143 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
144 newmode |= set->bits;
145 break;
146
147 case '\0':
148 default:
149#ifdef SETMODE_DEBUG
150 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
151#endif
152 return (newmode);
153 }
154}
155
156#define ADDCMD(a, b, c, d) \
157 if (set >= endset) { \
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
99 * copy them around. If we don't use shifts, it gets real
100 * grundgy with lots of single bit checks and bit sets.
101 */
102 case 'u':
103 value = (newmode & S_IRWXU) >> 6;
104 goto common;
105
106 case 'g':
107 value = (newmode & S_IRWXG) >> 3;
108 goto common;
109
110 case 'o':
111 value = newmode & S_IRWXO;
112common: if (set->cmd2 & CMD2_CLR) {
113 clrval =
114 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
115 if (set->cmd2 & CMD2_UBITS)
116 newmode &= ~((clrval<<6) & set->bits);
117 if (set->cmd2 & CMD2_GBITS)
118 newmode &= ~((clrval<<3) & set->bits);
119 if (set->cmd2 & CMD2_OBITS)
120 newmode &= ~(clrval & set->bits);
121 }
122 if (set->cmd2 & CMD2_SET) {
123 if (set->cmd2 & CMD2_UBITS)
124 newmode |= (value<<6) & set->bits;
125 if (set->cmd2 & CMD2_GBITS)
126 newmode |= (value<<3) & set->bits;
127 if (set->cmd2 & CMD2_OBITS)
128 newmode |= value & set->bits;
129 }
130 break;
131
132 case '+':
133 newmode |= set->bits;
134 break;
135
136 case '-':
137 newmode &= ~set->bits;
138 break;
139
140 case 'X':
141 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
142 newmode |= set->bits;
143 break;
144
145 case '\0':
146 default:
147#ifdef SETMODE_DEBUG
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) { \
158 register BITCMD *newset; \
156 BITCMD *newset; \
159 setlen += SET_LEN_INCR; \
160 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
161 if (!saveset) \
162 return (NULL); \
163 set = newset + (set - saveset); \
164 saveset = newset; \
165 endset = newset + (setlen - 2); \
166 } \
167 set = addcmd(set, (a), (b), (c), (d))
168
169#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
170
171void *
172setmode(p)
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)
173 register char *p;
171 char *p;
174{
172{
175 register int perm, who;
176 register char op;
173 int perm, who;
174 char op;
177 BITCMD *set, *saveset, *endset;
178 sigset_t sigset, sigoset;
179 mode_t mask;
180 int equalopdone=0, permXbits, setlen;
181
182 if (!*p)
183 return (NULL);
184
185 /*
186 * Get a copy of the mask for the permissions that are mask relative.
187 * Flip the bits, we want what's not set. Since it's possible that
188 * the caller is opening files inside a signal handler, protect them
189 * as best we can.
190 */
191 sigfillset(&sigset);
192 (void)_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
193 (void)umask(mask = umask(0));
194 mask = ~mask;
195 (void)_sigprocmask(SIG_SETMASK, &sigoset, NULL);
196
197 setlen = SET_LEN + 2;
198
199 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
200 return (NULL);
201 saveset = set;
202 endset = set + (setlen - 2);
203
204 /*
205 * If an absolute number, get it and return; disallow non-octal digits
206 * or illegal bits.
207 */
208 if (isdigit((unsigned char)*p)) {
209 perm = (mode_t)strtol(p, NULL, 8);
210 if (perm & ~(STANDARD_BITS|S_ISTXT)) {
211 free(saveset);
212 return (NULL);
213 }
214 while (*++p)
215 if (*p < '0' || *p > '7') {
216 free(saveset);
217 return (NULL);
218 }
219 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
220 return (saveset);
221 }
222
223 /*
224 * Build list of structures to set/clear/copy bits as described by
225 * each clause of the symbolic mode.
226 */
227 for (;;) {
228 /* First, find out which bits might be modified. */
229 for (who = 0;; ++p) {
230 switch (*p) {
231 case 'a':
232 who |= STANDARD_BITS;
233 break;
234 case 'u':
235 who |= S_ISUID|S_IRWXU;
236 break;
237 case 'g':
238 who |= S_ISGID|S_IRWXG;
239 break;
240 case 'o':
241 who |= S_IRWXO;
242 break;
243 default:
244 goto getop;
245 }
246 }
247
248getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
249 free(saveset);
250 return (NULL);
251 }
252 if (op == '=')
253 equalopdone = 0;
254
255 who &= ~S_ISTXT;
256 for (perm = 0, permXbits = 0;; ++p) {
257 switch (*p) {
258 case 'r':
259 perm |= S_IRUSR|S_IRGRP|S_IROTH;
260 break;
261 case 's':
262 /* If only "other" bits ignore set-id. */
263 if (!who || who & ~S_IRWXO)
264 perm |= S_ISUID|S_ISGID;
265 break;
266 case 't':
267 /* If only "other" bits ignore sticky. */
268 if (!who || who & ~S_IRWXO) {
269 who |= S_ISTXT;
270 perm |= S_ISTXT;
271 }
272 break;
273 case 'w':
274 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
275 break;
276 case 'X':
277 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
278 break;
279 case 'x':
280 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
281 break;
282 case 'u':
283 case 'g':
284 case 'o':
285 /*
286 * When ever we hit 'u', 'g', or 'o', we have
287 * to flush out any partial mode that we have,
288 * and then do the copying of the mode bits.
289 */
290 if (perm) {
291 ADDCMD(op, who, perm, mask);
292 perm = 0;
293 }
294 if (op == '=')
295 equalopdone = 1;
296 if (op == '+' && permXbits) {
297 ADDCMD('X', who, permXbits, mask);
298 permXbits = 0;
299 }
300 ADDCMD(*p, who, op, mask);
301 break;
302
303 default:
304 /*
305 * Add any permissions that we haven't already
306 * done.
307 */
308 if (perm || (op == '=' && !equalopdone)) {
309 if (op == '=')
310 equalopdone = 1;
311 ADDCMD(op, who, perm, mask);
312 perm = 0;
313 }
314 if (permXbits) {
315 ADDCMD('X', who, permXbits, mask);
316 permXbits = 0;
317 }
318 goto apply;
319 }
320 }
321
322apply: if (!*p)
323 break;
324 if (*p != ',')
325 goto getop;
326 ++p;
327 }
328 set->cmd = 0;
329#ifdef SETMODE_DEBUG
330 (void)printf("Before compress_mode()\n");
331 dumpmode(saveset);
332#endif
333 compress_mode(saveset);
334#ifdef SETMODE_DEBUG
335 (void)printf("After compress_mode()\n");
336 dumpmode(saveset);
337#endif
338 return (saveset);
339}
340
341static BITCMD *
342addcmd(set, op, who, oparg, mask)
343 BITCMD *set;
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
183 /*
184 * Get a copy of the mask for the permissions that are mask relative.
185 * Flip the bits, we want what's not set. Since it's possible that
186 * the caller is opening files inside a signal handler, protect them
187 * as best we can.
188 */
189 sigfillset(&sigset);
190 (void)_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
191 (void)umask(mask = umask(0));
192 mask = ~mask;
193 (void)_sigprocmask(SIG_SETMASK, &sigoset, NULL);
194
195 setlen = SET_LEN + 2;
196
197 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
198 return (NULL);
199 saveset = set;
200 endset = set + (setlen - 2);
201
202 /*
203 * If an absolute number, get it and return; disallow non-octal digits
204 * or illegal bits.
205 */
206 if (isdigit((unsigned char)*p)) {
207 perm = (mode_t)strtol(p, NULL, 8);
208 if (perm & ~(STANDARD_BITS|S_ISTXT)) {
209 free(saveset);
210 return (NULL);
211 }
212 while (*++p)
213 if (*p < '0' || *p > '7') {
214 free(saveset);
215 return (NULL);
216 }
217 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
218 return (saveset);
219 }
220
221 /*
222 * Build list of structures to set/clear/copy bits as described by
223 * each clause of the symbolic mode.
224 */
225 for (;;) {
226 /* First, find out which bits might be modified. */
227 for (who = 0;; ++p) {
228 switch (*p) {
229 case 'a':
230 who |= STANDARD_BITS;
231 break;
232 case 'u':
233 who |= S_ISUID|S_IRWXU;
234 break;
235 case 'g':
236 who |= S_ISGID|S_IRWXG;
237 break;
238 case 'o':
239 who |= S_IRWXO;
240 break;
241 default:
242 goto getop;
243 }
244 }
245
246getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
247 free(saveset);
248 return (NULL);
249 }
250 if (op == '=')
251 equalopdone = 0;
252
253 who &= ~S_ISTXT;
254 for (perm = 0, permXbits = 0;; ++p) {
255 switch (*p) {
256 case 'r':
257 perm |= S_IRUSR|S_IRGRP|S_IROTH;
258 break;
259 case 's':
260 /* If only "other" bits ignore set-id. */
261 if (!who || who & ~S_IRWXO)
262 perm |= S_ISUID|S_ISGID;
263 break;
264 case 't':
265 /* If only "other" bits ignore sticky. */
266 if (!who || who & ~S_IRWXO) {
267 who |= S_ISTXT;
268 perm |= S_ISTXT;
269 }
270 break;
271 case 'w':
272 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
273 break;
274 case 'X':
275 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
276 break;
277 case 'x':
278 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
279 break;
280 case 'u':
281 case 'g':
282 case 'o':
283 /*
284 * When ever we hit 'u', 'g', or 'o', we have
285 * to flush out any partial mode that we have,
286 * and then do the copying of the mode bits.
287 */
288 if (perm) {
289 ADDCMD(op, who, perm, mask);
290 perm = 0;
291 }
292 if (op == '=')
293 equalopdone = 1;
294 if (op == '+' && permXbits) {
295 ADDCMD('X', who, permXbits, mask);
296 permXbits = 0;
297 }
298 ADDCMD(*p, who, op, mask);
299 break;
300
301 default:
302 /*
303 * Add any permissions that we haven't already
304 * done.
305 */
306 if (perm || (op == '=' && !equalopdone)) {
307 if (op == '=')
308 equalopdone = 1;
309 ADDCMD(op, who, perm, mask);
310 perm = 0;
311 }
312 if (permXbits) {
313 ADDCMD('X', who, permXbits, mask);
314 permXbits = 0;
315 }
316 goto apply;
317 }
318 }
319
320apply: if (!*p)
321 break;
322 if (*p != ',')
323 goto getop;
324 ++p;
325 }
326 set->cmd = 0;
327#ifdef SETMODE_DEBUG
328 (void)printf("Before compress_mode()\n");
329 dumpmode(saveset);
330#endif
331 compress_mode(saveset);
332#ifdef SETMODE_DEBUG
333 (void)printf("After compress_mode()\n");
334 dumpmode(saveset);
335#endif
336 return (saveset);
337}
338
339static BITCMD *
340addcmd(set, op, who, oparg, mask)
341 BITCMD *set;
344 register int oparg, who;
345 register int op;
342 int oparg, who;
343 int op;
346 u_int mask;
347{
348 switch (op) {
349 case '=':
350 set->cmd = '-';
351 set->bits = who ? who : STANDARD_BITS;
352 set++;
353
354 op = '+';
355 /* FALLTHROUGH */
356 case '+':
357 case '-':
358 case 'X':
359 set->cmd = op;
360 set->bits = (who ? who : mask) & oparg;
361 break;
362
363 case 'u':
364 case 'g':
365 case 'o':
366 set->cmd = op;
367 if (who) {
368 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
369 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
370 ((who & S_IROTH) ? CMD2_OBITS : 0);
371 set->bits = ~0;
372 } else {
373 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
374 set->bits = mask;
375 }
376
377 if (oparg == '+')
378 set->cmd2 |= CMD2_SET;
379 else if (oparg == '-')
380 set->cmd2 |= CMD2_CLR;
381 else if (oparg == '=')
382 set->cmd2 |= CMD2_SET|CMD2_CLR;
383 break;
384 }
385 return (set + 1);
386}
387
388#ifdef SETMODE_DEBUG
389static void
390dumpmode(set)
344 u_int mask;
345{
346 switch (op) {
347 case '=':
348 set->cmd = '-';
349 set->bits = who ? who : STANDARD_BITS;
350 set++;
351
352 op = '+';
353 /* FALLTHROUGH */
354 case '+':
355 case '-':
356 case 'X':
357 set->cmd = op;
358 set->bits = (who ? who : mask) & oparg;
359 break;
360
361 case 'u':
362 case 'g':
363 case 'o':
364 set->cmd = op;
365 if (who) {
366 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
367 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
368 ((who & S_IROTH) ? CMD2_OBITS : 0);
369 set->bits = ~0;
370 } else {
371 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
372 set->bits = mask;
373 }
374
375 if (oparg == '+')
376 set->cmd2 |= CMD2_SET;
377 else if (oparg == '-')
378 set->cmd2 |= CMD2_CLR;
379 else if (oparg == '=')
380 set->cmd2 |= CMD2_SET|CMD2_CLR;
381 break;
382 }
383 return (set + 1);
384}
385
386#ifdef SETMODE_DEBUG
387static void
388dumpmode(set)
391 register BITCMD *set;
389 BITCMD *set;
392{
393 for (; set->cmd; ++set)
394 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
395 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
396 set->cmd2 & CMD2_CLR ? " CLR" : "",
397 set->cmd2 & CMD2_SET ? " SET" : "",
398 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
399 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
400 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
401}
402#endif
403
404/*
405 * Given an array of bitcmd structures, compress by compacting consecutive
406 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
407 * 'g' and 'o' commands continue to be separate. They could probably be
408 * compacted, but it's not worth the effort.
409 */
410static void
411compress_mode(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" : "",
398 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
399}
400#endif
401
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)
412 register BITCMD *set;
410 BITCMD *set;
413{
411{
414 register BITCMD *nset;
415 register int setbits, clrbits, Xbits, op;
412 BITCMD *nset;
413 int setbits, clrbits, Xbits, op;
416
417 for (nset = set;;) {
418 /* Copy over any 'u', 'g' and 'o' commands. */
419 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
420 *set++ = *nset++;
421 if (!op)
422 return;
423 }
424
425 for (setbits = clrbits = Xbits = 0;; nset++) {
426 if ((op = nset->cmd) == '-') {
427 clrbits |= nset->bits;
428 setbits &= ~nset->bits;
429 Xbits &= ~nset->bits;
430 } else if (op == '+') {
431 setbits |= nset->bits;
432 clrbits &= ~nset->bits;
433 Xbits &= ~nset->bits;
434 } else if (op == 'X')
435 Xbits |= nset->bits & ~setbits;
436 else
437 break;
438 }
439 if (clrbits) {
440 set->cmd = '-';
441 set->cmd2 = 0;
442 set->bits = clrbits;
443 set++;
444 }
445 if (setbits) {
446 set->cmd = '+';
447 set->cmd2 = 0;
448 set->bits = setbits;
449 set++;
450 }
451 if (Xbits) {
452 set->cmd = 'X';
453 set->cmd2 = 0;
454 set->bits = Xbits;
455 set++;
456 }
457 }
458}
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 }
422
423 for (setbits = clrbits = Xbits = 0;; nset++) {
424 if ((op = nset->cmd) == '-') {
425 clrbits |= nset->bits;
426 setbits &= ~nset->bits;
427 Xbits &= ~nset->bits;
428 } else if (op == '+') {
429 setbits |= nset->bits;
430 clrbits &= ~nset->bits;
431 Xbits &= ~nset->bits;
432 } else if (op == 'X')
433 Xbits |= nset->bits & ~setbits;
434 else
435 break;
436 }
437 if (clrbits) {
438 set->cmd = '-';
439 set->cmd2 = 0;
440 set->bits = clrbits;
441 set++;
442 }
443 if (setbits) {
444 set->cmd = '+';
445 set->cmd2 = 0;
446 set->bits = setbits;
447 set++;
448 }
449 if (Xbits) {
450 set->cmd = 'X';
451 set->cmd2 = 0;
452 set->bits = Xbits;
453 set++;
454 }
455 }
456}