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
27 #include <sys/types.h>
29 #include "modechange.h"
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
45 /* Return newly allocated memory to hold one element of type TYPE. */
46 #define talloc(type) ((type *) malloc (sizeof (type)))
48 #define isodigit(c) ((c) >= '0' && (c) <= '7')
52 /* Return a linked list of file mode change operations created from
53 MODE_STRING, an ASCII string that contains either an octal number
54 specifying an absolute mode, or symbolic mode change operations with
56 [ugoa...][[+-=][rwxXstugo...]...][,...]
57 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
58 should not affect bits set in the umask when no users are given.
59 Operators not selected in MASKED_OPS ignore the umask.
61 Return MODE_INVALID if `mode_string' does not contain a valid
62 representation of file mode change operations;
63 return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
66 mode_compile (mode_string
, masked_ops
)
67 register char *mode_string
;
70 struct mode_change
*head
; /* First element of the linked list. */
71 struct mode_change
*change
; /* An element of the linked list. */
72 int i
; /* General purpose temporary. */
73 int umask_value
; /* The umask value (surprise). */
74 unsigned short affected_bits
; /* Which bits in the mode are operated on. */
75 unsigned short affected_masked
; /* `affected_bits' modified by umask. */
76 unsigned ops_to_mask
; /* Operators to actually use umask on. */
78 i
= oatoi (mode_string
);
83 head
= talloc (struct mode_change
);
85 return MODE_MEMORY_EXHAUSTED
;
90 head
->affected
= 07777; /* Affect all permissions. */
94 umask_value
= umask (0);
95 umask (umask_value
); /* Restore the old value. */
100 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
105 /* Turn on all the bits in `affected_bits' for each group given. */
106 for (++mode_string
;; ++mode_string
)
107 switch (*mode_string
)
110 affected_bits
|= 04700;
113 affected_bits
|= 02070;
116 affected_bits
|= 01007;
119 affected_bits
|= 07777;
122 goto no_more_affected
;
126 /* If none specified, affect all bits, except perhaps those
128 if (affected_bits
== 0)
130 affected_bits
= 07777;
131 ops_to_mask
= masked_ops
;
134 while (*mode_string
== '=' || *mode_string
== '+' || *mode_string
== '-')
136 /* Add the element to the tail of the list, so the operations
137 are performed in the correct order. */
140 head
= talloc (struct mode_change
);
142 return MODE_MEMORY_EXHAUSTED
;
147 change
->next
= talloc (struct mode_change
);
148 if (change
->next
== NULL
)
151 return MODE_MEMORY_EXHAUSTED
;
153 change
= change
->next
;
157 change
->op
= *mode_string
; /* One of "=+-". */
158 affected_masked
= affected_bits
;
159 if (ops_to_mask
& (*mode_string
== '=' ? MODE_MASK_EQUALS
160 : *mode_string
== '+' ? MODE_MASK_PLUS
162 affected_masked
&= ~umask_value
;
163 change
->affected
= affected_masked
;
167 /* Set `value' according to the bits set in `affected_masked'. */
168 for (++mode_string
;; ++mode_string
)
169 switch (*mode_string
)
172 change
->value
|= 00444 & affected_masked
;
175 change
->value
|= 00222 & affected_masked
;
178 change
->flags
|= MODE_X_IF_ANY_X
;
181 change
->value
|= 00111 & affected_masked
;
184 /* Set the setuid/gid bits if `u' or `g' is selected. */
185 change
->value
|= 06000 & affected_masked
;
188 /* Set the "save text image" bit if `o' is selected. */
189 change
->value
|= 01000 & affected_masked
;
192 /* Set the affected bits to the value of the `u' bits
196 change
->value
= 00700;
197 change
->flags
|= MODE_COPY_EXISTING
;
200 /* Set the affected bits to the value of the `g' bits
204 change
->value
= 00070;
205 change
->flags
|= MODE_COPY_EXISTING
;
208 /* Set the affected bits to the value of the `o' bits
212 change
->value
= 00007;
213 change
->flags
|= MODE_COPY_EXISTING
;
220 } while (*mode_string
== ',');
221 if (*mode_string
== 0)
228 /* Return file mode OLDMODE, adjusted as indicated by the list of change
229 operations CHANGES. If OLDMODE is a directory, the type `X'
230 change affects it even if no execute bits were set in OLDMODE.
231 The returned value has the S_IFMT bits cleared. */
234 mode_adjust (oldmode
, changes
)
236 register struct mode_change
*changes
;
238 unsigned short newmode
; /* The adjusted mode and one operand. */
239 unsigned short value
; /* The other operand. */
241 newmode
= oldmode
& 07777;
243 for (; changes
; changes
= changes
->next
)
245 if (changes
->flags
& MODE_COPY_EXISTING
)
247 /* Isolate in `value' the bits in `newmode' to copy, given in
248 the mask `changes->value'. */
249 value
= newmode
& changes
->value
;
251 if (changes
->value
& 00700)
252 /* Copy `u' permissions onto `g' and `o'. */
253 value
|= (value
>> 3) | (value
>> 6);
254 else if (changes
->value
& 00070)
255 /* Copy `g' permissions onto `u' and `o'. */
256 value
|= (value
<< 3) | (value
>> 3);
258 /* Copy `o' permissions onto `u' and `g'. */
259 value
|= (value
<< 3) | (value
<< 6);
261 /* In order to change only `u', `g', or `o' permissions,
262 or some combination thereof, clear unselected bits.
263 This can not be done in mode_compile because the value
264 to which the `changes->affected' mask is applied depends
265 on the old mode of each file. */
266 value
&= changes
->affected
;
270 value
= changes
->value
;
271 /* If `X', do not affect the execute bits if the file is not a
272 directory and no execute bits are already set. */
273 if ((changes
->flags
& MODE_X_IF_ANY_X
)
274 && !S_ISDIR (oldmode
)
275 && (newmode
& 00111) == 0)
276 value
&= ~00111; /* Clear the execute bits. */
282 /* Preserve the previous values in `newmode' of bits that are
283 not affected by this change operation. */
284 newmode
= (newmode
& ~changes
->affected
) | value
;
297 /* Free the memory used by the list of file mode change operations
302 register struct mode_change
*changes
;
304 register struct mode_change
*next
;
308 next
= changes
->next
;
314 /* Return a positive integer containing the value of the ASCII
315 octal number S. If S is not an octal number, return -1. */
325 for (i
= 0; isodigit (*s
); ++s
)
326 i
= i
* 8 + *s
- '0';