* find/defs.h: move lstat declarations into defs.h
[findutils.git] / lib / modechange.c
blob321dbe561c16f35cbceddd6973cbd16b79c04068
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)
7 any later version.
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
25 performance gain. */
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include "modechange.h"
35 #ifdef STDC_HEADERS
36 #include <stdlib.h>
37 #endif
39 #ifndef NULL
40 #define NULL 0
41 #endif
43 #ifdef STAT_MACROS_BROKEN
44 #undef S_ISDIR
45 #endif /* STAT_MACROS_BROKEN. */
47 #if !defined(S_ISDIR) && defined(S_IFDIR)
48 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
49 #endif
51 /* Return newly allocated memory to hold one element of type TYPE. */
52 #define talloc(type) ((type *) malloc (sizeof (type)))
54 #define isodigit(c) ((c) >= '0' && (c) <= '7')
56 static int oatoi ();
58 /* Return a linked list of file mode change operations created from
59 MODE_STRING, an ASCII string that contains either an octal number
60 specifying an absolute mode, or symbolic mode change operations with
61 the form:
62 [ugoa...][[+-=][rwxXstugo...]...][,...]
63 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
64 should not affect bits set in the umask when no users are given.
65 Operators not selected in MASKED_OPS ignore the umask.
67 Return MODE_INVALID if `mode_string' does not contain a valid
68 representation of file mode change operations;
69 return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
71 struct mode_change *
72 mode_compile (mode_string, masked_ops)
73 register char *mode_string;
74 unsigned masked_ops;
76 struct mode_change *head; /* First element of the linked list. */
77 struct mode_change *change; /* An element of the linked list. */
78 int i; /* General purpose temporary. */
79 unsigned short umask_value; /* The umask value (surprise). */
80 unsigned short affected_bits; /* Which bits in the mode are operated on. */
81 unsigned short affected_masked; /* `affected_bits' modified by umask. */
82 unsigned ops_to_mask; /* Operators to actually use umask on. */
84 i = oatoi (mode_string);
85 if (i >= 0)
87 if (i > 07777)
88 return MODE_INVALID;
89 head = talloc (struct mode_change);
90 if (head == NULL)
91 return MODE_MEMORY_EXHAUSTED;
92 head->next = NULL;
93 head->op = '=';
94 head->flags = 0;
95 head->value = i;
96 head->affected = 07777; /* Affect all permissions. */
97 return head;
100 umask_value = umask (0);
101 umask (umask_value); /* Restore the old value. */
103 head = NULL;
104 #ifdef lint
105 change = NULL;
106 #endif
107 --mode_string;
109 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
112 affected_bits = 0;
113 ops_to_mask = 0;
114 /* Turn on all the bits in `affected_bits' for each group given. */
115 for (++mode_string;; ++mode_string)
116 switch (*mode_string)
118 case 'u':
119 affected_bits |= 04700;
120 break;
121 case 'g':
122 affected_bits |= 02070;
123 break;
124 case 'o':
125 affected_bits |= 01007;
126 break;
127 case 'a':
128 affected_bits |= 07777;
129 break;
130 default:
131 goto no_more_affected;
134 no_more_affected:
135 /* If none specified, affect all bits, except perhaps those
136 set in the umask. */
137 if (affected_bits == 0)
139 affected_bits = 07777;
140 ops_to_mask = masked_ops;
143 while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
145 /* Add the element to the tail of the list, so the operations
146 are performed in the correct order. */
147 if (head == NULL)
149 head = talloc (struct mode_change);
150 if (head == NULL)
151 return MODE_MEMORY_EXHAUSTED;
152 change = head;
154 else
156 change->next = talloc (struct mode_change);
157 if (change->next == NULL)
159 mode_free (change);
160 return MODE_MEMORY_EXHAUSTED;
162 change = change->next;
165 change->next = NULL;
166 change->op = *mode_string; /* One of "=+-". */
167 affected_masked = affected_bits;
168 if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
169 : *mode_string == '+' ? MODE_MASK_PLUS
170 : MODE_MASK_MINUS))
171 affected_masked &= ~umask_value;
172 change->affected = affected_masked;
173 change->value = 0;
174 change->flags = 0;
176 /* Set `value' according to the bits set in `affected_masked'. */
177 for (++mode_string;; ++mode_string)
178 switch (*mode_string)
180 case 'r':
181 change->value |= 00444 & affected_masked;
182 break;
183 case 'w':
184 change->value |= 00222 & affected_masked;
185 break;
186 case 'X':
187 change->flags |= MODE_X_IF_ANY_X;
188 /* Fall through. */
189 case 'x':
190 change->value |= 00111 & affected_masked;
191 break;
192 case 's':
193 /* Set the setuid/gid bits if `u' or `g' is selected. */
194 change->value |= 06000 & affected_masked;
195 break;
196 case 't':
197 /* Set the "save text image" bit if `o' is selected. */
198 change->value |= 01000 & affected_masked;
199 break;
200 case 'u':
201 /* Set the affected bits to the value of the `u' bits
202 on the same file. */
203 if (change->value)
204 goto invalid;
205 change->value = 00700;
206 change->flags |= MODE_COPY_EXISTING;
207 break;
208 case 'g':
209 /* Set the affected bits to the value of the `g' bits
210 on the same file. */
211 if (change->value)
212 goto invalid;
213 change->value = 00070;
214 change->flags |= MODE_COPY_EXISTING;
215 break;
216 case 'o':
217 /* Set the affected bits to the value of the `o' bits
218 on the same file. */
219 if (change->value)
220 goto invalid;
221 change->value = 00007;
222 change->flags |= MODE_COPY_EXISTING;
223 break;
224 default:
225 goto no_more_values;
227 no_more_values:;
229 } while (*mode_string == ',');
230 if (*mode_string == 0)
231 return head;
232 invalid:
233 mode_free (head);
234 return MODE_INVALID;
237 /* Return file mode OLDMODE, adjusted as indicated by the list of change
238 operations CHANGES. If OLDMODE is a directory, the type `X'
239 change affects it even if no execute bits were set in OLDMODE.
240 The returned value has the S_IFMT bits cleared. */
242 unsigned short
243 mode_adjust (oldmode, changes)
244 unsigned oldmode;
245 register struct mode_change *changes;
247 unsigned short newmode; /* The adjusted mode and one operand. */
248 unsigned short value; /* The other operand. */
250 newmode = oldmode & 07777;
252 for (; changes; changes = changes->next)
254 if (changes->flags & MODE_COPY_EXISTING)
256 /* Isolate in `value' the bits in `newmode' to copy, given in
257 the mask `changes->value'. */
258 value = newmode & changes->value;
260 if (changes->value & 00700)
261 /* Copy `u' permissions onto `g' and `o'. */
262 value |= (value >> 3) | (value >> 6);
263 else if (changes->value & 00070)
264 /* Copy `g' permissions onto `u' and `o'. */
265 value |= (value << 3) | (value >> 3);
266 else
267 /* Copy `o' permissions onto `u' and `g'. */
268 value |= (value << 3) | (value << 6);
270 /* In order to change only `u', `g', or `o' permissions,
271 or some combination thereof, clear unselected bits.
272 This can not be done in mode_compile because the value
273 to which the `changes->affected' mask is applied depends
274 on the old mode of each file. */
275 value &= changes->affected;
277 else
279 value = changes->value;
280 /* If `X', do not affect the execute bits if the file is not a
281 directory and no execute bits are already set. */
282 if ((changes->flags & MODE_X_IF_ANY_X)
283 && !S_ISDIR (oldmode)
284 && (newmode & 00111) == 0)
285 value &= ~00111; /* Clear the execute bits. */
288 switch (changes->op)
290 case '=':
291 /* Preserve the previous values in `newmode' of bits that are
292 not affected by this change operation. */
293 newmode = (newmode & ~changes->affected) | value;
294 break;
295 case '+':
296 newmode |= value;
297 break;
298 case '-':
299 newmode &= ~value;
300 break;
303 return newmode;
306 /* Free the memory used by the list of file mode change operations
307 CHANGES. */
309 void
310 mode_free (changes)
311 register struct mode_change *changes;
313 register struct mode_change *next;
315 while (changes)
317 next = changes->next;
318 free (changes);
319 changes = next;
323 /* Return a positive integer containing the value of the ASCII
324 octal number S. If S is not an octal number, return -1. */
326 static int
327 oatoi (s)
328 char *s;
330 register int i;
332 if (*s == 0)
333 return -1;
334 for (i = 0; isodigit (*s); ++s)
335 i = i * 8 + *s - '0';
336 if (*s)
337 return -1;
338 return i;