1 /* modechange.c -- file mode manipulation
2 Copyright (C) 1989, 1990 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@ai.mit.edu> */
20 /* The ASCII mode string is compiled into a linked list of `struct
21 modechange', which can then be applied to each file to be changed.
22 We do this instead of re-parsing the ASCII string for each file
23 because the compiled form requires less computation to use; when
24 changing the mode of many files, this probably results in a
28 #if defined (CONFIG_BROKETS)
29 /* We use <config.h> instead of "config.h" so that a compilation
30 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
31 (which it would do because it found this file in $srcdir). */
38 #include <sys/types.h>
40 #include "modechange.h"
52 #ifdef STAT_MACROS_BROKEN
56 #endif /* STAT_MACROS_BROKEN. */
58 #if !defined(S_ISDIR) && defined(S_IFDIR)
59 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
62 /* Return newly allocated memory to hold one element of type TYPE. */
63 #define talloc(type) ((type *) malloc (sizeof (type)))
65 #define isodigit(c) ((c) >= '0' && (c) <= '7')
69 /* Return a linked list of file mode change operations created from
70 MODE_STRING, an ASCII string that contains either an octal number
71 specifying an absolute mode, or symbolic mode change operations with
73 [ugoa...][[+-=][rwxXstugo...]...][,...]
74 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
75 should not affect bits set in the umask when no users are given.
76 Operators not selected in MASKED_OPS ignore the umask.
78 Return MODE_INVALID if `mode_string' does not contain a valid
79 representation of file mode change operations;
80 return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
83 mode_compile (mode_string, masked_ops)
84 register char *mode_string;
87 struct mode_change *head; /* First element of the linked list. */
88 struct mode_change *change; /* An element of the linked list. */
89 int i; /* General purpose temporary. */
90 int umask_value; /* The umask value (surprise). */
91 unsigned short affected_bits; /* Which bits in the mode are operated on. */
92 unsigned short affected_masked; /* `affected_bits' modified by umask. */
93 unsigned ops_to_mask; /* Operators to actually use umask on. */
95 i = oatoi (mode_string);
100 head = talloc (struct mode_change);
102 return MODE_MEMORY_EXHAUSTED;
107 head->affected = 07777; /* Affect all permissions. */
111 umask_value = umask (0);
112 umask (umask_value); /* Restore the old value. */
120 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
125 /* Turn on all the bits in `affected_bits' for each group given. */
126 for (++mode_string;; ++mode_string)
127 switch (*mode_string)
130 affected_bits |= 04700;
133 affected_bits |= 02070;
136 affected_bits |= 01007;
139 affected_bits |= 07777;
142 goto no_more_affected;
146 /* If none specified, affect all bits, except perhaps those
148 if (affected_bits == 0)
150 affected_bits = 07777;
151 ops_to_mask = masked_ops;
154 while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
156 /* Add the element to the tail of the list, so the operations
157 are performed in the correct order. */
160 head = talloc (struct mode_change);
162 return MODE_MEMORY_EXHAUSTED;
167 change->next = talloc (struct mode_change);
168 if (change->next == NULL)
171 return MODE_MEMORY_EXHAUSTED;
173 change = change->next;
177 change->op = *mode_string; /* One of "=+-". */
178 affected_masked = affected_bits;
179 if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
180 : *mode_string == '+' ? MODE_MASK_PLUS
182 affected_masked &= ~umask_value;
183 change->affected = affected_masked;
187 /* Set `value' according to the bits set in `affected_masked'. */
188 for (++mode_string;; ++mode_string)
189 switch (*mode_string)
192 change->value |= 00444 & affected_masked;
195 change->value |= 00222 & affected_masked;
198 change->flags |= MODE_X_IF_ANY_X;
201 change->value |= 00111 & affected_masked;
204 /* Set the setuid/gid bits if `u' or `g' is selected. */
205 change->value |= 06000 & affected_masked;
208 /* Set the "save text image" bit if `o' is selected. */
209 change->value |= 01000 & affected_masked;
212 /* Set the affected bits to the value of the `u' bits
216 change->value = 00700;
217 change->flags |= MODE_COPY_EXISTING;
220 /* Set the affected bits to the value of the `g' bits
224 change->value = 00070;
225 change->flags |= MODE_COPY_EXISTING;
228 /* Set the affected bits to the value of the `o' bits
232 change->value = 00007;
233 change->flags |= MODE_COPY_EXISTING;
240 } while (*mode_string == ',');
241 if (*mode_string == 0)
248 /* Return file mode OLDMODE, adjusted as indicated by the list of change
249 operations CHANGES. If OLDMODE is a directory, the type `X'
250 change affects it even if no execute bits were set in OLDMODE.
251 The returned value has the S_IFMT bits cleared. */
254 mode_adjust (oldmode, changes)
256 register struct mode_change *changes;
258 unsigned short newmode; /* The adjusted mode and one operand. */
259 unsigned short value; /* The other operand. */
261 newmode = oldmode & 07777;
263 for (; changes; changes = changes->next)
265 if (changes->flags & MODE_COPY_EXISTING)
267 /* Isolate in `value' the bits in `newmode' to copy, given in
268 the mask `changes->value'. */
269 value = newmode & changes->value;
271 if (changes->value & 00700)
272 /* Copy `u' permissions onto `g' and `o'. */
273 value |= (value >> 3) | (value >> 6);
274 else if (changes->value & 00070)
275 /* Copy `g' permissions onto `u' and `o'. */
276 value |= (value << 3) | (value >> 3);
278 /* Copy `o' permissions onto `u' and `g'. */
279 value |= (value << 3) | (value << 6);
281 /* In order to change only `u', `g', or `o' permissions,
282 or some combination thereof, clear unselected bits.
283 This can not be done in mode_compile because the value
284 to which the `changes->affected' mask is applied depends
285 on the old mode of each file. */
286 value &= changes->affected;
290 value = changes->value;
291 /* If `X', do not affect the execute bits if the file is not a
292 directory and no execute bits are already set. */
293 if ((changes->flags & MODE_X_IF_ANY_X)
294 && !S_ISDIR (oldmode)
295 && (newmode & 00111) == 0)
296 value &= ~00111; /* Clear the execute bits. */
302 /* Preserve the previous values in `newmode' of bits that are
303 not affected by this change operation. */
304 newmode = (newmode & ~changes->affected) | value;
317 /* Free the memory used by the list of file mode change operations
322 register struct mode_change *changes;
324 register struct mode_change *next;
328 next = changes->next;
334 /* Return a positive integer containing the value of the ASCII
335 octal number S. If S is not an octal number, return -1. */
345 for (i = 0; isodigit (*s); ++s)
346 i = i * 8 + *s - '0';