xref: /minix3/lib/libc/gen/setmode.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: setmode.c,v 1.34 2012/06/25 22:32:43 abs Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1989, 1993, 1994
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Dave Borman at Cray Research, Inc.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)setmode.c	8.2 (Berkeley) 3/25/94";
392fe8fb19SBen Gras #else
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: setmode.c,v 1.34 2012/06/25 22:32:43 abs Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include "namespace.h"
452fe8fb19SBen Gras #include <sys/types.h>
462fe8fb19SBen Gras #include <sys/stat.h>
472fe8fb19SBen Gras 
482fe8fb19SBen Gras #include <assert.h>
492fe8fb19SBen Gras #include <ctype.h>
502fe8fb19SBen Gras #include <errno.h>
512fe8fb19SBen Gras #include <signal.h>
522fe8fb19SBen Gras #include <stdlib.h>
532fe8fb19SBen Gras #include <limits.h>
542fe8fb19SBen Gras #include <unistd.h>
552fe8fb19SBen Gras 
562fe8fb19SBen Gras #ifdef SETMODE_DEBUG
572fe8fb19SBen Gras #include <stdio.h>
582fe8fb19SBen Gras #endif
592fe8fb19SBen Gras 
602fe8fb19SBen Gras #ifdef __weak_alias
612fe8fb19SBen Gras __weak_alias(getmode,_getmode)
622fe8fb19SBen Gras __weak_alias(setmode,_setmode)
632fe8fb19SBen Gras #endif
642fe8fb19SBen Gras 
652fe8fb19SBen Gras #define	SET_LEN	6		/* initial # of bitcmd struct to malloc */
662fe8fb19SBen Gras #define	SET_LEN_INCR 4		/* # of bitcmd structs to add as needed */
672fe8fb19SBen Gras 
682fe8fb19SBen Gras typedef struct bitcmd {
692fe8fb19SBen Gras 	char	cmd;
702fe8fb19SBen Gras 	char	cmd2;
712fe8fb19SBen Gras 	mode_t	bits;
722fe8fb19SBen Gras } BITCMD;
732fe8fb19SBen Gras 
742fe8fb19SBen Gras #define	CMD2_CLR	0x01
752fe8fb19SBen Gras #define	CMD2_SET	0x02
762fe8fb19SBen Gras #define	CMD2_GBITS	0x04
772fe8fb19SBen Gras #define	CMD2_OBITS	0x08
782fe8fb19SBen Gras #define	CMD2_UBITS	0x10
792fe8fb19SBen Gras 
80*f14fb602SLionel Sambuc static BITCMD	*addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
81*f14fb602SLionel Sambuc static void	 compress_mode(BITCMD *);
822fe8fb19SBen Gras #ifdef SETMODE_DEBUG
83*f14fb602SLionel Sambuc static void	 dumpmode(BITCMD *);
842fe8fb19SBen Gras #endif
852fe8fb19SBen Gras 
862fe8fb19SBen Gras /*
872fe8fb19SBen Gras  * Given the old mode and an array of bitcmd structures, apply the operations
882fe8fb19SBen Gras  * described in the bitcmd structures to the old mode, and return the new mode.
892fe8fb19SBen Gras  * Note that there is no '=' command; a strict assignment is just a '-' (clear
902fe8fb19SBen Gras  * bits) followed by a '+' (set bits).
912fe8fb19SBen Gras  */
922fe8fb19SBen Gras mode_t
getmode(const void * bbox,mode_t omode)932fe8fb19SBen Gras getmode(const void *bbox, mode_t omode)
942fe8fb19SBen Gras {
952fe8fb19SBen Gras 	const BITCMD *set;
962fe8fb19SBen Gras 	mode_t clrval, newmode, value;
972fe8fb19SBen Gras 
982fe8fb19SBen Gras 	_DIAGASSERT(bbox != NULL);
992fe8fb19SBen Gras 
1002fe8fb19SBen Gras 	set = (const BITCMD *)bbox;
1012fe8fb19SBen Gras 	newmode = omode;
1022fe8fb19SBen Gras 	for (value = 0;; set++)
1032fe8fb19SBen Gras 		switch(set->cmd) {
1042fe8fb19SBen Gras 		/*
1052fe8fb19SBen Gras 		 * When copying the user, group or other bits around, we "know"
1062fe8fb19SBen Gras 		 * where the bits are in the mode so that we can do shifts to
1072fe8fb19SBen Gras 		 * copy them around.  If we don't use shifts, it gets real
1082fe8fb19SBen Gras 		 * grundgy with lots of single bit checks and bit sets.
1092fe8fb19SBen Gras 		 */
1102fe8fb19SBen Gras 		case 'u':
1112fe8fb19SBen Gras 			value = (newmode & S_IRWXU) >> 6;
1122fe8fb19SBen Gras 			goto common;
1132fe8fb19SBen Gras 
1142fe8fb19SBen Gras 		case 'g':
1152fe8fb19SBen Gras 			value = (newmode & S_IRWXG) >> 3;
1162fe8fb19SBen Gras 			goto common;
1172fe8fb19SBen Gras 
1182fe8fb19SBen Gras 		case 'o':
1192fe8fb19SBen Gras 			value = newmode & S_IRWXO;
1202fe8fb19SBen Gras common:			if (set->cmd2 & CMD2_CLR) {
1212fe8fb19SBen Gras 				clrval =
1222fe8fb19SBen Gras 				    (set->cmd2 & CMD2_SET) ?  S_IRWXO : value;
1232fe8fb19SBen Gras 				if (set->cmd2 & CMD2_UBITS)
1242fe8fb19SBen Gras 					newmode &= ~((clrval<<6) & set->bits);
1252fe8fb19SBen Gras 				if (set->cmd2 & CMD2_GBITS)
1262fe8fb19SBen Gras 					newmode &= ~((clrval<<3) & set->bits);
1272fe8fb19SBen Gras 				if (set->cmd2 & CMD2_OBITS)
1282fe8fb19SBen Gras 					newmode &= ~(clrval & set->bits);
1292fe8fb19SBen Gras 			}
1302fe8fb19SBen Gras 			if (set->cmd2 & CMD2_SET) {
1312fe8fb19SBen Gras 				if (set->cmd2 & CMD2_UBITS)
1322fe8fb19SBen Gras 					newmode |= (value<<6) & set->bits;
1332fe8fb19SBen Gras 				if (set->cmd2 & CMD2_GBITS)
1342fe8fb19SBen Gras 					newmode |= (value<<3) & set->bits;
1352fe8fb19SBen Gras 				if (set->cmd2 & CMD2_OBITS)
1362fe8fb19SBen Gras 					newmode |= value & set->bits;
1372fe8fb19SBen Gras 			}
1382fe8fb19SBen Gras 			break;
1392fe8fb19SBen Gras 
1402fe8fb19SBen Gras 		case '+':
1412fe8fb19SBen Gras 			newmode |= set->bits;
1422fe8fb19SBen Gras 			break;
1432fe8fb19SBen Gras 
1442fe8fb19SBen Gras 		case '-':
1452fe8fb19SBen Gras 			newmode &= ~set->bits;
1462fe8fb19SBen Gras 			break;
1472fe8fb19SBen Gras 
1482fe8fb19SBen Gras 		case 'X':
1492fe8fb19SBen Gras 			if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
1502fe8fb19SBen Gras 				newmode |= set->bits;
1512fe8fb19SBen Gras 			break;
1522fe8fb19SBen Gras 
1532fe8fb19SBen Gras 		case '\0':
1542fe8fb19SBen Gras 		default:
1552fe8fb19SBen Gras #ifdef SETMODE_DEBUG
1562fe8fb19SBen Gras 			(void)printf("getmode:%04o -> %04o\n", omode, newmode);
1572fe8fb19SBen Gras #endif
1582fe8fb19SBen Gras 			return (newmode);
1592fe8fb19SBen Gras 		}
1602fe8fb19SBen Gras }
1612fe8fb19SBen Gras 
1622fe8fb19SBen Gras #define	ADDCMD(a, b, c, d) do {						\
1632fe8fb19SBen Gras 	if (set >= endset) {						\
1642fe8fb19SBen Gras 		BITCMD *newset;						\
1652fe8fb19SBen Gras 		setlen += SET_LEN_INCR;					\
1662fe8fb19SBen Gras 		newset = realloc(saveset, sizeof(BITCMD) * setlen);	\
1672fe8fb19SBen Gras 		if (newset == NULL)					\
1682fe8fb19SBen Gras 			goto out;					\
1692fe8fb19SBen Gras 		set = newset + (set - saveset);				\
1702fe8fb19SBen Gras 		saveset = newset;					\
1712fe8fb19SBen Gras 		endset = newset + (setlen - 2);				\
1722fe8fb19SBen Gras 	}								\
1732fe8fb19SBen Gras 	set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d));	\
1742fe8fb19SBen Gras } while (/*CONSTCOND*/0)
1752fe8fb19SBen Gras 
1762fe8fb19SBen Gras #define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
1772fe8fb19SBen Gras 
1782fe8fb19SBen Gras void *
setmode(const char * p)1792fe8fb19SBen Gras setmode(const char *p)
1802fe8fb19SBen Gras {
1812fe8fb19SBen Gras 	int serrno;
1822fe8fb19SBen Gras 	char op, *ep;
1832fe8fb19SBen Gras 	BITCMD *set, *saveset, *endset;
1842fe8fb19SBen Gras 	sigset_t signset, sigoset;
1852fe8fb19SBen Gras 	mode_t mask, perm, permXbits, who;
1862fe8fb19SBen Gras 	long lval;
1872fe8fb19SBen Gras 	int equalopdone = 0;	/* pacify gcc */
1882fe8fb19SBen Gras 	int setlen;
1892fe8fb19SBen Gras 
1902fe8fb19SBen Gras 	if (!*p) {
1912fe8fb19SBen Gras 		errno = EINVAL;
1922fe8fb19SBen Gras 		return NULL;
1932fe8fb19SBen Gras 	}
1942fe8fb19SBen Gras 
1952fe8fb19SBen Gras 	/*
1962fe8fb19SBen Gras 	 * Get a copy of the mask for the permissions that are mask relative.
1972fe8fb19SBen Gras 	 * Flip the bits, we want what's not set.  Since it's possible that
1982fe8fb19SBen Gras 	 * the caller is opening files inside a signal handler, protect them
1992fe8fb19SBen Gras 	 * as best we can.
2002fe8fb19SBen Gras 	 */
2012fe8fb19SBen Gras 	sigfillset(&signset);
2022fe8fb19SBen Gras 	(void)sigprocmask(SIG_BLOCK, &signset, &sigoset);
2032fe8fb19SBen Gras 	(void)umask(mask = umask(0));
2042fe8fb19SBen Gras 	mask = ~mask;
2052fe8fb19SBen Gras 	(void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
2062fe8fb19SBen Gras 
2072fe8fb19SBen Gras 	setlen = SET_LEN + 2;
2082fe8fb19SBen Gras 
2092fe8fb19SBen Gras 	if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
2102fe8fb19SBen Gras 		return (NULL);
2112fe8fb19SBen Gras 	saveset = set;
2122fe8fb19SBen Gras 	endset = set + (setlen - 2);
2132fe8fb19SBen Gras 
2142fe8fb19SBen Gras 	/*
2152fe8fb19SBen Gras 	 * If an absolute number, get it and return; disallow non-octal digits
2162fe8fb19SBen Gras 	 * or illegal bits.
2172fe8fb19SBen Gras 	 */
2182fe8fb19SBen Gras 	if (isdigit((unsigned char)*p)) {
2192fe8fb19SBen Gras 		errno = 0;
2202fe8fb19SBen Gras 		lval = strtol(p, &ep, 8);
2212fe8fb19SBen Gras 		if (*ep) {
2222fe8fb19SBen Gras 			errno = EINVAL;
2232fe8fb19SBen Gras 			goto out;
2242fe8fb19SBen Gras 		}
2252fe8fb19SBen Gras 		if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
2262fe8fb19SBen Gras 			goto out;
2272fe8fb19SBen Gras 		if (lval & ~(STANDARD_BITS|S_ISTXT)) {
2282fe8fb19SBen Gras 			errno = EINVAL;
2292fe8fb19SBen Gras 			goto out;
2302fe8fb19SBen Gras 		}
2312fe8fb19SBen Gras 		perm = (mode_t)lval;
2322fe8fb19SBen Gras 		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
2332fe8fb19SBen Gras 		set->cmd = 0;
2342fe8fb19SBen Gras 		return (saveset);
2352fe8fb19SBen Gras 	}
2362fe8fb19SBen Gras 
2372fe8fb19SBen Gras 	/*
2382fe8fb19SBen Gras 	 * Build list of structures to set/clear/copy bits as described by
2392fe8fb19SBen Gras 	 * each clause of the symbolic mode.
2402fe8fb19SBen Gras 	 */
2412fe8fb19SBen Gras 	for (;;) {
2422fe8fb19SBen Gras 		/* First, find out which bits might be modified. */
2432fe8fb19SBen Gras 		for (who = 0;; ++p) {
2442fe8fb19SBen Gras 			switch (*p) {
2452fe8fb19SBen Gras 			case 'a':
2462fe8fb19SBen Gras 				who |= STANDARD_BITS;
2472fe8fb19SBen Gras 				break;
2482fe8fb19SBen Gras 			case 'u':
2492fe8fb19SBen Gras 				who |= S_ISUID|S_IRWXU;
2502fe8fb19SBen Gras 				break;
2512fe8fb19SBen Gras 			case 'g':
2522fe8fb19SBen Gras 				who |= S_ISGID|S_IRWXG;
2532fe8fb19SBen Gras 				break;
2542fe8fb19SBen Gras 			case 'o':
2552fe8fb19SBen Gras 				who |= S_IRWXO;
2562fe8fb19SBen Gras 				break;
2572fe8fb19SBen Gras 			default:
2582fe8fb19SBen Gras 				goto getop;
2592fe8fb19SBen Gras 			}
2602fe8fb19SBen Gras 		}
2612fe8fb19SBen Gras 
2622fe8fb19SBen Gras getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
2632fe8fb19SBen Gras 			errno = EINVAL;
2642fe8fb19SBen Gras 			goto out;
2652fe8fb19SBen Gras 		}
2662fe8fb19SBen Gras 		if (op == '=')
2672fe8fb19SBen Gras 			equalopdone = 0;
2682fe8fb19SBen Gras 
2692fe8fb19SBen Gras 		who &= ~S_ISTXT;
2702fe8fb19SBen Gras 		for (perm = 0, permXbits = 0;; ++p) {
2712fe8fb19SBen Gras 			switch (*p) {
2722fe8fb19SBen Gras 			case 'r':
2732fe8fb19SBen Gras 				perm |= S_IRUSR|S_IRGRP|S_IROTH;
2742fe8fb19SBen Gras 				break;
2752fe8fb19SBen Gras 			case 's':
2762fe8fb19SBen Gras 				/*
2772fe8fb19SBen Gras 				 * If specific bits where requested and
2782fe8fb19SBen Gras 				 * only "other" bits ignore set-id.
2792fe8fb19SBen Gras 				 */
2802fe8fb19SBen Gras 				if (who == 0 || (who & ~S_IRWXO))
2812fe8fb19SBen Gras 					perm |= S_ISUID|S_ISGID;
2822fe8fb19SBen Gras 				break;
2832fe8fb19SBen Gras 			case 't':
2842fe8fb19SBen Gras 				/*
2852fe8fb19SBen Gras 				 * If specific bits where requested and
2862fe8fb19SBen Gras 				 * only "other" bits ignore set-id.
2872fe8fb19SBen Gras 				 */
2882fe8fb19SBen Gras 				if (who == 0 || (who & ~S_IRWXO)) {
2892fe8fb19SBen Gras 					who |= S_ISTXT;
2902fe8fb19SBen Gras 					perm |= S_ISTXT;
2912fe8fb19SBen Gras 				}
2922fe8fb19SBen Gras 				break;
2932fe8fb19SBen Gras 			case 'w':
2942fe8fb19SBen Gras 				perm |= S_IWUSR|S_IWGRP|S_IWOTH;
2952fe8fb19SBen Gras 				break;
2962fe8fb19SBen Gras 			case 'X':
2972fe8fb19SBen Gras 				permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
2982fe8fb19SBen Gras 				break;
2992fe8fb19SBen Gras 			case 'x':
3002fe8fb19SBen Gras 				perm |= S_IXUSR|S_IXGRP|S_IXOTH;
3012fe8fb19SBen Gras 				break;
3022fe8fb19SBen Gras 			case 'u':
3032fe8fb19SBen Gras 			case 'g':
3042fe8fb19SBen Gras 			case 'o':
3052fe8fb19SBen Gras 				/*
3062fe8fb19SBen Gras 				 * When ever we hit 'u', 'g', or 'o', we have
3072fe8fb19SBen Gras 				 * to flush out any partial mode that we have,
3082fe8fb19SBen Gras 				 * and then do the copying of the mode bits.
3092fe8fb19SBen Gras 				 */
3102fe8fb19SBen Gras 				if (perm) {
3112fe8fb19SBen Gras 					ADDCMD(op, who, perm, mask);
3122fe8fb19SBen Gras 					perm = 0;
3132fe8fb19SBen Gras 				}
3142fe8fb19SBen Gras 				if (op == '=')
3152fe8fb19SBen Gras 					equalopdone = 1;
3162fe8fb19SBen Gras 				if (op == '+' && permXbits) {
3172fe8fb19SBen Gras 					ADDCMD('X', who, permXbits, mask);
3182fe8fb19SBen Gras 					permXbits = 0;
3192fe8fb19SBen Gras 				}
3202fe8fb19SBen Gras 				ADDCMD(*p, who, op, mask);
3212fe8fb19SBen Gras 				break;
3222fe8fb19SBen Gras 
3232fe8fb19SBen Gras 			default:
3242fe8fb19SBen Gras 				/*
3252fe8fb19SBen Gras 				 * Add any permissions that we haven't already
3262fe8fb19SBen Gras 				 * done.
3272fe8fb19SBen Gras 				 */
3282fe8fb19SBen Gras 				if (perm || (op == '=' && !equalopdone)) {
3292fe8fb19SBen Gras 					if (op == '=')
3302fe8fb19SBen Gras 						equalopdone = 1;
3312fe8fb19SBen Gras 					ADDCMD(op, who, perm, mask);
3322fe8fb19SBen Gras 					perm = 0;
3332fe8fb19SBen Gras 				}
3342fe8fb19SBen Gras 				if (permXbits) {
3352fe8fb19SBen Gras 					ADDCMD('X', who, permXbits, mask);
3362fe8fb19SBen Gras 					permXbits = 0;
3372fe8fb19SBen Gras 				}
3382fe8fb19SBen Gras 				goto apply;
3392fe8fb19SBen Gras 			}
3402fe8fb19SBen Gras 		}
3412fe8fb19SBen Gras 
3422fe8fb19SBen Gras apply:		if (!*p)
3432fe8fb19SBen Gras 			break;
3442fe8fb19SBen Gras 		if (*p != ',')
3452fe8fb19SBen Gras 			goto getop;
3462fe8fb19SBen Gras 		++p;
3472fe8fb19SBen Gras 	}
3482fe8fb19SBen Gras 	set->cmd = 0;
3492fe8fb19SBen Gras #ifdef SETMODE_DEBUG
3502fe8fb19SBen Gras 	(void)printf("Before compress_mode()\n");
3512fe8fb19SBen Gras 	dumpmode(saveset);
3522fe8fb19SBen Gras #endif
3532fe8fb19SBen Gras 	compress_mode(saveset);
3542fe8fb19SBen Gras #ifdef SETMODE_DEBUG
3552fe8fb19SBen Gras 	(void)printf("After compress_mode()\n");
3562fe8fb19SBen Gras 	dumpmode(saveset);
3572fe8fb19SBen Gras #endif
3582fe8fb19SBen Gras 	return (saveset);
3592fe8fb19SBen Gras out:
3602fe8fb19SBen Gras 	serrno = errno;
3612fe8fb19SBen Gras 	free(saveset);
3622fe8fb19SBen Gras 	errno = serrno;
3632fe8fb19SBen Gras 	return NULL;
3642fe8fb19SBen Gras }
3652fe8fb19SBen Gras 
3662fe8fb19SBen Gras static BITCMD *
addcmd(BITCMD * set,mode_t op,mode_t who,mode_t oparg,mode_t mask)3672fe8fb19SBen Gras addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
3682fe8fb19SBen Gras {
3692fe8fb19SBen Gras 
3702fe8fb19SBen Gras 	_DIAGASSERT(set != NULL);
3712fe8fb19SBen Gras 
3722fe8fb19SBen Gras 	switch (op) {
3732fe8fb19SBen Gras 	case '=':
3742fe8fb19SBen Gras 		set->cmd = '-';
3752fe8fb19SBen Gras 		set->bits = who ? who : STANDARD_BITS;
3762fe8fb19SBen Gras 		set++;
3772fe8fb19SBen Gras 
3782fe8fb19SBen Gras 		op = '+';
3792fe8fb19SBen Gras 		/* FALLTHROUGH */
3802fe8fb19SBen Gras 	case '+':
3812fe8fb19SBen Gras 	case '-':
3822fe8fb19SBen Gras 	case 'X':
3832fe8fb19SBen Gras 		set->cmd = op;
3842fe8fb19SBen Gras 		set->bits = (who ? who : mask) & oparg;
3852fe8fb19SBen Gras 		break;
3862fe8fb19SBen Gras 
3872fe8fb19SBen Gras 	case 'u':
3882fe8fb19SBen Gras 	case 'g':
3892fe8fb19SBen Gras 	case 'o':
3902fe8fb19SBen Gras 		set->cmd = op;
3912fe8fb19SBen Gras 		if (who) {
3922fe8fb19SBen Gras 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
3932fe8fb19SBen Gras 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
3942fe8fb19SBen Gras 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
3952fe8fb19SBen Gras 			set->bits = (mode_t)~0;
3962fe8fb19SBen Gras 		} else {
3972fe8fb19SBen Gras 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
3982fe8fb19SBen Gras 			set->bits = mask;
3992fe8fb19SBen Gras 		}
4002fe8fb19SBen Gras 
4012fe8fb19SBen Gras 		if (oparg == '+')
4022fe8fb19SBen Gras 			set->cmd2 |= CMD2_SET;
4032fe8fb19SBen Gras 		else if (oparg == '-')
4042fe8fb19SBen Gras 			set->cmd2 |= CMD2_CLR;
4052fe8fb19SBen Gras 		else if (oparg == '=')
4062fe8fb19SBen Gras 			set->cmd2 |= CMD2_SET|CMD2_CLR;
4072fe8fb19SBen Gras 		break;
4082fe8fb19SBen Gras 	}
4092fe8fb19SBen Gras 	return (set + 1);
4102fe8fb19SBen Gras }
4112fe8fb19SBen Gras 
4122fe8fb19SBen Gras #ifdef SETMODE_DEBUG
4132fe8fb19SBen Gras static void
dumpmode(BITCMD * set)414*f14fb602SLionel Sambuc dumpmode(BITCMD *set)
4152fe8fb19SBen Gras {
4162fe8fb19SBen Gras 
4172fe8fb19SBen Gras 	_DIAGASSERT(set != NULL);
4182fe8fb19SBen Gras 
4192fe8fb19SBen Gras 	for (; set->cmd; ++set)
4202fe8fb19SBen Gras 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
4212fe8fb19SBen Gras 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
4222fe8fb19SBen Gras 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
4232fe8fb19SBen Gras 		    set->cmd2 & CMD2_SET ? " SET" : "",
4242fe8fb19SBen Gras 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
4252fe8fb19SBen Gras 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
4262fe8fb19SBen Gras 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
4272fe8fb19SBen Gras }
4282fe8fb19SBen Gras #endif
4292fe8fb19SBen Gras 
4302fe8fb19SBen Gras /*
4312fe8fb19SBen Gras  * Given an array of bitcmd structures, compress by compacting consecutive
4322fe8fb19SBen Gras  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
4332fe8fb19SBen Gras  * 'g' and 'o' commands continue to be separate.  They could probably be
4342fe8fb19SBen Gras  * compacted, but it's not worth the effort.
4352fe8fb19SBen Gras  */
4362fe8fb19SBen Gras static void
compress_mode(BITCMD * set)437*f14fb602SLionel Sambuc compress_mode(BITCMD *set)
4382fe8fb19SBen Gras {
4392fe8fb19SBen Gras 	BITCMD *nset;
4402fe8fb19SBen Gras 	int setbits, clrbits, Xbits, op;
4412fe8fb19SBen Gras 
4422fe8fb19SBen Gras 	_DIAGASSERT(set != NULL);
4432fe8fb19SBen Gras 
4442fe8fb19SBen Gras 	for (nset = set;;) {
4452fe8fb19SBen Gras 		/* Copy over any 'u', 'g' and 'o' commands. */
4462fe8fb19SBen Gras 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
4472fe8fb19SBen Gras 			*set++ = *nset++;
4482fe8fb19SBen Gras 			if (!op)
4492fe8fb19SBen Gras 				return;
4502fe8fb19SBen Gras 		}
4512fe8fb19SBen Gras 
4522fe8fb19SBen Gras 		for (setbits = clrbits = Xbits = 0;; nset++) {
4532fe8fb19SBen Gras 			if ((op = nset->cmd) == '-') {
4542fe8fb19SBen Gras 				clrbits |= nset->bits;
4552fe8fb19SBen Gras 				setbits &= ~nset->bits;
4562fe8fb19SBen Gras 				Xbits &= ~nset->bits;
4572fe8fb19SBen Gras 			} else if (op == '+') {
4582fe8fb19SBen Gras 				setbits |= nset->bits;
4592fe8fb19SBen Gras 				clrbits &= ~nset->bits;
4602fe8fb19SBen Gras 				Xbits &= ~nset->bits;
4612fe8fb19SBen Gras 			} else if (op == 'X')
4622fe8fb19SBen Gras 				Xbits |= nset->bits & ~setbits;
4632fe8fb19SBen Gras 			else
4642fe8fb19SBen Gras 				break;
4652fe8fb19SBen Gras 		}
4662fe8fb19SBen Gras 		if (clrbits) {
4672fe8fb19SBen Gras 			set->cmd = '-';
4682fe8fb19SBen Gras 			set->cmd2 = 0;
4692fe8fb19SBen Gras 			set->bits = clrbits;
4702fe8fb19SBen Gras 			set++;
4712fe8fb19SBen Gras 		}
4722fe8fb19SBen Gras 		if (setbits) {
4732fe8fb19SBen Gras 			set->cmd = '+';
4742fe8fb19SBen Gras 			set->cmd2 = 0;
4752fe8fb19SBen Gras 			set->bits = setbits;
4762fe8fb19SBen Gras 			set++;
4772fe8fb19SBen Gras 		}
4782fe8fb19SBen Gras 		if (Xbits) {
4792fe8fb19SBen Gras 			set->cmd = 'X';
4802fe8fb19SBen Gras 			set->cmd2 = 0;
4812fe8fb19SBen Gras 			set->bits = Xbits;
4822fe8fb19SBen Gras 			set++;
4832fe8fb19SBen Gras 		}
4842fe8fb19SBen Gras 	}
4852fe8fb19SBen Gras }
486