1 /* modechange.c -- file mode manipulation
2 Copyright (C) 1989, 1990, 1997, 1998, 1999 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 /* Create a mode_change entry with the specified `=ddd'-style
76 mode change operation, where NEW_MODE is `ddd'. Return the
77 new entry, or NULL upon failure. */
79 static struct mode_change
*
80 make_node_op_equals (int new_mode
)
82 struct mode_change
*p
;
83 p
= talloc (struct mode_change
);
90 p
->affected
= 07777; /* Affect all permissions. */
94 /* Append entry E to the end of the link list with the specified
98 mode_append_entry (struct mode_change
**head
,
99 struct mode_change
**tail
,
100 struct mode_change
*e
)
111 /* Return a linked list of file mode change operations created from
112 MODE_STRING, an ASCII string that contains either an octal number
113 specifying an absolute mode, or symbolic mode change operations with
115 [ugoa...][[+-=][rwxXstugo...]...][,...]
116 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
117 should not affect bits set in the umask when no users are given.
118 Operators not selected in MASKED_OPS ignore the umask.
120 Return MODE_INVALID if `mode_string' does not contain a valid
121 representation of file mode change operations;
122 return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
125 mode_compile (const char *mode_string
, unsigned int masked_ops
)
127 struct mode_change
*head
; /* First element of the linked list. */
128 struct mode_change
*tail
; /* An element of the linked list. */
129 int i
; /* General purpose temporary. */
130 int umask_value
; /* The umask value (surprise). */
137 i
= oatoi (mode_string
);
140 struct mode_change
*p
;
143 p
= make_node_op_equals (i
);
145 return MODE_MEMORY_EXHAUSTED
;
146 mode_append_entry (&head
, &tail
, p
);
150 umask_value
= umask (0);
151 umask (umask_value
); /* Restore the old value. */
154 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
157 /* Which bits in the mode are operated on. */
158 unsigned short affected_bits
= 0;
159 /* `affected_bits' modified by umask. */
160 unsigned short affected_masked
;
161 /* Operators to actually use umask on. */
162 unsigned ops_to_mask
= 0;
168 /* Turn on all the bits in `affected_bits' for each group given. */
169 for (++mode_string
;; ++mode_string
)
170 switch (*mode_string
)
173 affected_bits
|= 04700;
176 affected_bits
|= 02070;
179 affected_bits
|= 01007;
182 affected_bits
|= 07777;
185 goto no_more_affected
;
189 /* If none specified, affect all bits, except perhaps those
196 affected_bits
= 07777;
197 ops_to_mask
= masked_ops
;
200 while (*mode_string
== '=' || *mode_string
== '+' || *mode_string
== '-')
202 struct mode_change
*change
= talloc (struct mode_change
);
206 return MODE_MEMORY_EXHAUSTED
;
210 change
->op
= *mode_string
; /* One of "=+-". */
211 affected_masked
= affected_bits
;
213 /* Per the Single Unix Spec, if `who' is not specified and the
214 `=' operator is used, then clear all the bits first. */
215 if (!who_specified_p
&&
216 ops_to_mask
& (*mode_string
== '=' ? MODE_MASK_EQUALS
: 0))
218 struct mode_change
*p
= make_node_op_equals (0);
220 return MODE_MEMORY_EXHAUSTED
;
221 mode_append_entry (&head
, &tail
, p
);
224 if (ops_to_mask
& (*mode_string
== '=' ? MODE_MASK_EQUALS
225 : *mode_string
== '+' ? MODE_MASK_PLUS
227 affected_masked
&= ~umask_value
;
228 change
->affected
= affected_masked
;
232 /* Add the element to the tail of the list, so the operations
233 are performed in the correct order. */
234 mode_append_entry (&head
, &tail
, change
);
236 /* Set `value' according to the bits set in `affected_masked'. */
237 for (++mode_string
;; ++mode_string
)
238 switch (*mode_string
)
241 change
->value
|= 00444 & affected_masked
;
244 change
->value
|= 00222 & affected_masked
;
247 change
->flags
|= MODE_X_IF_ANY_X
;
250 change
->value
|= 00111 & affected_masked
;
253 /* Set the setuid/gid bits if `u' or `g' is selected. */
254 change
->value
|= 06000 & affected_masked
;
257 /* Set the "save text image" bit if `o' is selected. */
258 change
->value
|= 01000 & affected_masked
;
261 /* Set the affected bits to the value of the `u' bits
265 change
->value
= 00700;
266 change
->flags
|= MODE_COPY_EXISTING
;
269 /* Set the affected bits to the value of the `g' bits
273 change
->value
= 00070;
274 change
->flags
|= MODE_COPY_EXISTING
;
277 /* Set the affected bits to the value of the `o' bits
281 change
->value
= 00007;
282 change
->flags
|= MODE_COPY_EXISTING
;
289 } while (*mode_string
== ',');
290 if (*mode_string
== 0)
297 /* Return a file mode change operation that sets permissions to match those
298 of REF_FILE. Return MODE_BAD_REFERENCE if REF_FILE can't be accessed. */
301 mode_create_from_ref (const char *ref_file
)
303 struct mode_change
*change
; /* the only change element */
304 struct stat ref_stats
;
306 if (stat (ref_file
, &ref_stats
))
307 return MODE_BAD_REFERENCE
;
309 change
= talloc (struct mode_change
);
312 return MODE_MEMORY_EXHAUSTED
;
316 change
->affected
= 07777;
317 change
->value
= ref_stats
.st_mode
;
323 /* Return file mode OLDMODE, adjusted as indicated by the list of change
324 operations CHANGES. If OLDMODE is a directory, the type `X'
325 change affects it even if no execute bits were set in OLDMODE.
326 The returned value has the S_IFMT bits cleared. */
329 mode_adjust (unsigned int oldmode
, const struct mode_change
*changes
)
331 unsigned short newmode
; /* The adjusted mode and one operand. */
332 unsigned short value
; /* The other operand. */
334 newmode
= oldmode
& 07777;
336 for (; changes
; changes
= changes
->next
)
338 if (changes
->flags
& MODE_COPY_EXISTING
)
340 /* Isolate in `value' the bits in `newmode' to copy, given in
341 the mask `changes->value'. */
342 value
= newmode
& changes
->value
;
344 if (changes
->value
& 00700)
345 /* Copy `u' permissions onto `g' and `o'. */
346 value
|= (value
>> 3) | (value
>> 6);
347 else if (changes
->value
& 00070)
348 /* Copy `g' permissions onto `u' and `o'. */
349 value
|= (value
<< 3) | (value
>> 3);
351 /* Copy `o' permissions onto `u' and `g'. */
352 value
|= (value
<< 3) | (value
<< 6);
354 /* In order to change only `u', `g', or `o' permissions,
355 or some combination thereof, clear unselected bits.
356 This can not be done in mode_compile because the value
357 to which the `changes->affected' mask is applied depends
358 on the old mode of each file. */
359 value
&= changes
->affected
;
363 value
= changes
->value
;
364 /* If `X', do not affect the execute bits if the file is not a
365 directory and no execute bits are already set. */
366 if ((changes
->flags
& MODE_X_IF_ANY_X
)
367 && !S_ISDIR (oldmode
)
368 && (newmode
& 00111) == 0)
369 value
&= ~00111; /* Clear the execute bits. */
375 /* Preserve the previous values in `newmode' of bits that are
376 not affected by this change operation. */
377 newmode
= (newmode
& ~changes
->affected
) | value
;
390 /* Free the memory used by the list of file mode change operations
394 mode_free (register struct mode_change
*changes
)
396 register struct mode_change
*next
;
400 next
= changes
->next
;