source: trunk/src/gmake/kmkbuiltin/setmode.c@ 370

Last change on this file since 370 was 370, checked in by bird, 20 years ago

o Ported all kmk builtins to win32.
o Fixed serveral bugs in kmk builtins.
o Probably broke both linux, bsd and OS/2. :-)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
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)
38static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
39#include <sys/cdefs.h>
40//__FBSDID("$FreeBSD: src/lib/libc/gen/setmode.c,v 1.9 2003/02/23 00:24:03 mikeh Exp $");
41#endif /* LIBC_SCCS and not lint */
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#ifndef _MSC_VER
52#include <unistd.h>
53#else
54#include "mscfakes.h"
55#endif
56
57#ifdef SETMODE_DEBUG
58#include <stdio.h>
59#endif
60//#include "un-namespace.h"
61
62#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
63#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
64
65typedef struct bitcmd {
66 char cmd;
67 char cmd2;
68 mode_t bits;
69} BITCMD;
70
71#define CMD2_CLR 0x01
72#define CMD2_SET 0x02
73#define CMD2_GBITS 0x04
74#define CMD2_OBITS 0x08
75#define CMD2_UBITS 0x10
76
77static BITCMD *addcmd(BITCMD *, int, int, int, u_int);
78static void compress_mode(BITCMD *);
79#ifdef SETMODE_DEBUG
80static void dumpmode(BITCMD *);
81#endif
82
83#ifndef S_ISTXT
84#ifdef S_ISVTX
85#define S_ISTXT S_ISVTX
86#else
87#define S_ISTXT 0
88#endif
89#endif /* !S_ISTXT */
90
91/*
92 * Given the old mode and an array of bitcmd structures, apply the operations
93 * described in the bitcmd structures to the old mode, and return the new mode.
94 * Note that there is no '=' command; a strict assignment is just a '-' (clear
95 * bits) followed by a '+' (set bits).
96 */
97mode_t
98getmode(bbox, omode)
99 const void *bbox;
100 mode_t omode;
101{
102 const BITCMD *set;
103 mode_t clrval, newmode, value;
104
105 set = (const BITCMD *)bbox;
106 newmode = omode;
107 for (value = 0;; set++)
108 switch(set->cmd) {
109 /*
110 * When copying the user, group or other bits around, we "know"
111 * where the bits are in the mode so that we can do shifts to
112 * copy them around. If we don't use shifts, it gets real
113 * grundgy with lots of single bit checks and bit sets.
114 */
115 case 'u':
116 value = (newmode & S_IRWXU) >> 6;
117 goto common;
118
119 case 'g':
120 value = (newmode & S_IRWXG) >> 3;
121 goto common;
122
123 case 'o':
124 value = newmode & S_IRWXO;
125common: if (set->cmd2 & CMD2_CLR) {
126 clrval =
127 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
128 if (set->cmd2 & CMD2_UBITS)
129 newmode &= ~((clrval<<6) & set->bits);
130 if (set->cmd2 & CMD2_GBITS)
131 newmode &= ~((clrval<<3) & set->bits);
132 if (set->cmd2 & CMD2_OBITS)
133 newmode &= ~(clrval & set->bits);
134 }
135 if (set->cmd2 & CMD2_SET) {
136 if (set->cmd2 & CMD2_UBITS)
137 newmode |= (value<<6) & set->bits;
138 if (set->cmd2 & CMD2_GBITS)
139 newmode |= (value<<3) & set->bits;
140 if (set->cmd2 & CMD2_OBITS)
141 newmode |= value & set->bits;
142 }
143 break;
144
145 case '+':
146 newmode |= set->bits;
147 break;
148
149 case '-':
150 newmode &= ~set->bits;
151 break;
152
153 case 'X':
154 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
155 newmode |= set->bits;
156 break;
157
158 case '\0':
159 default:
160#ifdef SETMODE_DEBUG
161 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
162#endif
163 return (newmode);
164 }
165}
166
167#define ADDCMD(a, b, c, d) \
168 if (set >= endset) { \
169 BITCMD *newset; \
170 setlen += SET_LEN_INCR; \
171 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
172 if (!newset) { \
173 if (saveset) \
174 free(saveset); \
175 saveset = NULL; \
176 return (NULL); \
177 } \
178 set = newset + (set - saveset); \
179 saveset = newset; \
180 endset = newset + (setlen - 2); \
181 } \
182 set = addcmd(set, (a), (b), (c), (d))
183
184#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
185
186void *
187setmode(p)
188 const char *p;
189{
190 int perm, who;
191 char op, *ep;
192 BITCMD *set, *saveset, *endset;
193#ifndef _MSC_VER
194 sigset_t sigset, sigoset;
195#endif
196 mode_t mask;
197 int equalopdone=0, permXbits, setlen;
198 long perml;
199
200 if (!*p)
201 return (NULL);
202
203 /*
204 * Get a copy of the mask for the permissions that are mask relative.
205 * Flip the bits, we want what's not set. Since it's possible that
206 * the caller is opening files inside a signal handler, protect them
207 * as best we can.
208 */
209#ifndef _MSC_VER
210 sigfillset(&sigset);
211 (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset);
212#endif
213 (void)umask(mask = umask(0));
214 mask = ~mask;
215#ifndef _MSC_VER
216 (void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
217#endif
218
219 setlen = SET_LEN + 2;
220
221 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
222 return (NULL);
223 saveset = set;
224 endset = set + (setlen - 2);
225
226 /*
227 * If an absolute number, get it and return; disallow non-octal digits
228 * or illegal bits.
229 */
230 if (isdigit((unsigned char)*p)) {
231 perml = strtol(p, &ep, 8);
232 if (*ep || perml < 0 || perml & ~(STANDARD_BITS|S_ISTXT)) {
233 free(saveset);
234 return (NULL);
235 }
236 perm = (mode_t)perml;
237 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
238 set->cmd = 0;
239 return (saveset);
240 }
241
242 /*
243 * Build list of structures to set/clear/copy bits as described by
244 * each clause of the symbolic mode.
245 */
246 for (;;) {
247 /* First, find out which bits might be modified. */
248 for (who = 0;; ++p) {
249 switch (*p) {
250 case 'a':
251 who |= STANDARD_BITS;
252 break;
253 case 'u':
254 who |= S_ISUID|S_IRWXU;
255 break;
256 case 'g':
257 who |= S_ISGID|S_IRWXG;
258 break;
259 case 'o':
260 who |= S_IRWXO;
261 break;
262 default:
263 goto getop;
264 }
265 }
266
267getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
268 free(saveset);
269 return (NULL);
270 }
271 if (op == '=')
272 equalopdone = 0;
273
274 who &= ~S_ISTXT;
275 for (perm = 0, permXbits = 0;; ++p) {
276 switch (*p) {
277 case 'r':
278 perm |= S_IRUSR|S_IRGRP|S_IROTH;
279 break;
280 case 's':
281 /* If only "other" bits ignore set-id. */
282 if (!who || who & ~S_IRWXO)
283 perm |= S_ISUID|S_ISGID;
284 break;
285 case 't':
286 /* If only "other" bits ignore sticky. */
287 if (!who || who & ~S_IRWXO) {
288 who |= S_ISTXT;
289 perm |= S_ISTXT;
290 }
291 break;
292 case 'w':
293 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
294 break;
295 case 'X':
296 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
297 break;
298 case 'x':
299 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
300 break;
301 case 'u':
302 case 'g':
303 case 'o':
304 /*
305 * When ever we hit 'u', 'g', or 'o', we have
306 * to flush out any partial mode that we have,
307 * and then do the copying of the mode bits.
308 */
309 if (perm) {
310 ADDCMD(op, who, perm, mask);
311 perm = 0;
312 }
313 if (op == '=')
314 equalopdone = 1;
315 if (op == '+' && permXbits) {
316 ADDCMD('X', who, permXbits, mask);
317 permXbits = 0;
318 }
319 ADDCMD(*p, who, op, mask);
320 break;
321
322 default:
323 /*
324 * Add any permissions that we haven't already
325 * done.
326 */
327 if (perm || (op == '=' && !equalopdone)) {
328 if (op == '=')
329 equalopdone = 1;
330 ADDCMD(op, who, perm, mask);
331 perm = 0;
332 }
333 if (permXbits) {
334 ADDCMD('X', who, permXbits, mask);
335 permXbits = 0;
336 }
337 goto apply;
338 }
339 }
340
341apply: if (!*p)
342 break;
343 if (*p != ',')
344 goto getop;
345 ++p;
346 }
347 set->cmd = 0;
348#ifdef SETMODE_DEBUG
349 (void)printf("Before compress_mode()\n");
350 dumpmode(saveset);
351#endif
352 compress_mode(saveset);
353#ifdef SETMODE_DEBUG
354 (void)printf("After compress_mode()\n");
355 dumpmode(saveset);
356#endif
357 return (saveset);
358}
359
360static BITCMD *
361addcmd(set, op, who, oparg, mask)
362 BITCMD *set;
363 int oparg, who;
364 int op;
365 u_int mask;
366{
367 switch (op) {
368 case '=':
369 set->cmd = '-';
370 set->bits = who ? who : STANDARD_BITS;
371 set++;
372
373 op = '+';
374 /* FALLTHROUGH */
375 case '+':
376 case '-':
377 case 'X':
378 set->cmd = op;
379 set->bits = (who ? who : mask) & oparg;
380 break;
381
382 case 'u':
383 case 'g':
384 case 'o':
385 set->cmd = op;
386 if (who) {
387 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
388 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
389 ((who & S_IROTH) ? CMD2_OBITS : 0);
390 set->bits = (mode_t)~0;
391 } else {
392 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
393 set->bits = mask;
394 }
395
396 if (oparg == '+')
397 set->cmd2 |= CMD2_SET;
398 else if (oparg == '-')
399 set->cmd2 |= CMD2_CLR;
400 else if (oparg == '=')
401 set->cmd2 |= CMD2_SET|CMD2_CLR;
402 break;
403 }
404 return (set + 1);
405}
406
407#ifdef SETMODE_DEBUG
408static void
409dumpmode(set)
410 BITCMD *set;
411{
412 for (; set->cmd; ++set)
413 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
414 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
415 set->cmd2 & CMD2_CLR ? " CLR" : "",
416 set->cmd2 & CMD2_SET ? " SET" : "",
417 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
418 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
419 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
420}
421#endif
422
423/*
424 * Given an array of bitcmd structures, compress by compacting consecutive
425 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
426 * 'g' and 'o' commands continue to be separate. They could probably be
427 * compacted, but it's not worth the effort.
428 */
429static void
430compress_mode(set)
431 BITCMD *set;
432{
433 BITCMD *nset;
434 int setbits, clrbits, Xbits, op;
435
436 for (nset = set;;) {
437 /* Copy over any 'u', 'g' and 'o' commands. */
438 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
439 *set++ = *nset++;
440 if (!op)
441 return;
442 }
443
444 for (setbits = clrbits = Xbits = 0;; nset++) {
445 if ((op = nset->cmd) == '-') {
446 clrbits |= nset->bits;
447 setbits &= ~nset->bits;
448 Xbits &= ~nset->bits;
449 } else if (op == '+') {
450 setbits |= nset->bits;
451 clrbits &= ~nset->bits;
452 Xbits &= ~nset->bits;
453 } else if (op == 'X')
454 Xbits |= nset->bits & ~setbits;
455 else
456 break;
457 }
458 if (clrbits) {
459 set->cmd = '-';
460 set->cmd2 = 0;
461 set->bits = clrbits;
462 set++;
463 }
464 if (setbits) {
465 set->cmd = '+';
466 set->cmd2 = 0;
467 set->bits = setbits;
468 set++;
469 }
470 if (Xbits) {
471 set->cmd = 'X';
472 set->cmd2 = 0;
473 set->bits = Xbits;
474 set++;
475 }
476 }
477}
Note: See TracBrowser for help on using the repository browser.