1 /* modechange.c -- file mode manipulation
2 Copyright (C) 1989, 1990, 1997, 1998 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
31 #include <sys/types.h>
33 #include "modechange.h"
45 #if STAT_MACROS_BROKEN
49 #if !defined(S_ISDIR) && defined(S_IFDIR)
50 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
53 /* Return newly allocated memory to hold one element of type TYPE. */
54 #define talloc(type) ((type *) malloc (sizeof (type)))
56 #define isodigit(c) ((c) >= '0' && (c) <= '7')
58 /* Return a positive integer containing the value of the ASCII
59 octal number S. If S is not an octal number, return -1. */
68 for (i = 0; isodigit (*s); ++s)
75 /* Return a linked list of file mode change operations created from
76 MODE_STRING, an ASCII string that contains either an octal number
77 specifying an absolute mode, or symbolic mode change operations with
79 [ugoa...][[+-=][rwxXstugo...]...][,...]
80 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
81 should not affect bits set in the umask when no users are given.
82 Operators not selected in MASKED_OPS ignore the umask.
84 Return MODE_INVALID if `mode_string' does not contain a valid
85 representation of file mode change operations;
86 return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
89 mode_compile (const char *mode_string, unsigned int masked_ops)
91 struct mode_change *head; /* First element of the linked list. */
92 struct mode_change *change; /* An element of the linked list. */
93 int i; /* General purpose temporary. */
94 int umask_value; /* The umask value (surprise). */
95 unsigned short affected_bits; /* Which bits in the mode are operated on. */
96 unsigned short affected_masked; /* `affected_bits' modified by umask. */
97 unsigned ops_to_mask; /* Operators to actually use umask on. */
99 i = oatoi (mode_string);
104 head = talloc (struct mode_change);
106 return MODE_MEMORY_EXHAUSTED;
111 head->affected = 07777; /* Affect all permissions. */
115 umask_value = umask (0);
116 umask (umask_value); /* Restore the old value. */
124 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
129 /* Turn on all the bits in `affected_bits' for each group given. */
130 for (++mode_string;; ++mode_string)
131 switch (*mode_string)
134 affected_bits |= 04700;
137 affected_bits |= 02070;
140 affected_bits |= 01007;
143 affected_bits |= 07777;
146 goto no_more_affected;
150 /* If none specified, affect all bits, except perhaps those
152 if (affected_bits == 0)
154 affected_bits = 07777;
155 ops_to_mask = masked_ops;
158 while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
160 /* Add the element to the tail of the list, so the operations
161 are performed in the correct order. */
164 head = talloc (struct mode_change);
166 return MODE_MEMORY_EXHAUSTED;
171 change->next = talloc (struct mode_change);
172 if (change->next == NULL)
175 return MODE_MEMORY_EXHAUSTED;
177 change = change->next;
181 change->op = *mode_string; /* One of "=+-". */
182 affected_masked = affected_bits;
183 if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
184 : *mode_string == '+' ? MODE_MASK_PLUS
186 affected_masked &= ~umask_value;
187 change->affected = affected_masked;
191 /* Set `value' according to the bits set in `affected_masked'. */
192 for (++mode_string;; ++mode_string)
193 switch (*mode_string)
196 change->value |= 00444 & affected_masked;
199 change->value |= 00222 & affected_masked;
202 change->flags |= MODE_X_IF_ANY_X;
205 change->value |= 00111 & affected_masked;
208 /* Set the setuid/gid bits if `u' or `g' is selected. */
209 change->value |= 06000 & affected_masked;
212 /* Set the "save text image" bit if `o' is selected. */
213 change->value |= 01000 & affected_masked;
216 /* Set the affected bits to the value of the `u' bits
220 change->value = 00700;
221 change->flags |= MODE_COPY_EXISTING;
224 /* Set the affected bits to the value of the `g' bits
228 change->value = 00070;
229 change->flags |= MODE_COPY_EXISTING;
232 /* Set the affected bits to the value of the `o' bits
236 change->value = 00007;
237 change->flags |= MODE_COPY_EXISTING;
244 } while (*mode_string == ',');
245 if (*mode_string == 0)
252 /* Return a file mode change operation that sets permissions to match those
253 of REF_FILE. Return MODE_BAD_REFERENCE if REF_FILE can't be accessed. */
256 mode_create_from_ref (const char *ref_file)
258 struct mode_change *change; /* the only change element */
259 struct stat ref_stats;
261 if (stat (ref_file, &ref_stats))
262 return MODE_BAD_REFERENCE;
264 change = talloc (struct mode_change);
267 return MODE_MEMORY_EXHAUSTED;
271 change->affected = 07777;
272 change->value = ref_stats.st_mode;
278 /* Return file mode OLDMODE, adjusted as indicated by the list of change
279 operations CHANGES. If OLDMODE is a directory, the type `X'
280 change affects it even if no execute bits were set in OLDMODE.
281 The returned value has the S_IFMT bits cleared. */
284 mode_adjust (unsigned int oldmode, const struct mode_change *changes)
286 unsigned short newmode; /* The adjusted mode and one operand. */
287 unsigned short value; /* The other operand. */
289 newmode = oldmode & 07777;
291 for (; changes; changes = changes->next)
293 if (changes->flags & MODE_COPY_EXISTING)
295 /* Isolate in `value' the bits in `newmode' to copy, given in
296 the mask `changes->value'. */
297 value = newmode & changes->value;
299 if (changes->value & 00700)
300 /* Copy `u' permissions onto `g' and `o'. */
301 value |= (value >> 3) | (value >> 6);
302 else if (changes->value & 00070)
303 /* Copy `g' permissions onto `u' and `o'. */
304 value |= (value << 3) | (value >> 3);
306 /* Copy `o' permissions onto `u' and `g'. */
307 value |= (value << 3) | (value << 6);
309 /* In order to change only `u', `g', or `o' permissions,
310 or some combination thereof, clear unselected bits.
311 This can not be done in mode_compile because the value
312 to which the `changes->affected' mask is applied depends
313 on the old mode of each file. */
314 value &= changes->affected;
318 value = changes->value;
319 /* If `X', do not affect the execute bits if the file is not a
320 directory and no execute bits are already set. */
321 if ((changes->flags & MODE_X_IF_ANY_X)
322 && !S_ISDIR (oldmode)
323 && (newmode & 00111) == 0)
324 value &= ~00111; /* Clear the execute bits. */
330 /* Preserve the previous values in `newmode' of bits that are
331 not affected by this change operation. */
332 newmode = (newmode & ~changes->affected) | value;
345 /* Free the memory used by the list of file mode change operations
349 mode_free (register struct mode_change *changes)
351 register struct mode_change *next;
355 next = changes->next;