(AC_CHECK_FUNCS): Remove strtoull, strtoumax, strtouq.
[coreutils.git] / lib / modechange.c
blobcbb1bb905bfe0c30db193024a7cdc853f5b804c0
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)
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 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
25 performance gain. */
27 #if HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include "modechange.h"
35 #if STDC_HEADERS
36 # include <stdlib.h>
37 #else
38 char *malloc ();
39 #endif
41 #ifndef NULL
42 # define NULL 0
43 #endif
45 #if STAT_MACROS_BROKEN
46 # undef S_ISDIR
47 #endif
49 #if !defined(S_ISDIR) && defined(S_IFDIR)
50 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
51 #endif
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. */
61 static int
62 oatoi (const char *s)
64 register int i;
66 if (*s == 0)
67 return -1;
68 for (i = 0; isodigit (*s); ++s)
69 i = i * 8 + *s - '0';
70 if (*s)
71 return -1;
72 return i;
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);
84 if (p == NULL)
85 return p;
86 p->next = NULL;
87 p->op = '=';
88 p->flags = 0;
89 p->value = new_mode;
90 p->affected = 07777; /* Affect all permissions. */
91 return p;
94 /* Append entry E to the end of the link list with the specified
95 HEAD and TAIL. */
97 static void
98 mode_append_entry (struct mode_change **head,
99 struct mode_change **tail,
100 struct mode_change *e)
102 if (*head == NULL)
103 *head = *tail = e;
104 else
106 (*tail)->next = e;
107 *tail = 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
114 the form:
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. */
124 struct mode_change *
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). */
132 head = NULL;
133 #ifdef lint
134 tail = NULL;
135 #endif
137 i = oatoi (mode_string);
138 if (i >= 0)
140 struct mode_change *p;
141 if (i > 07777)
142 return MODE_INVALID;
143 p = make_node_op_equals (i);
144 if (p == NULL)
145 return MODE_MEMORY_EXHAUSTED;
146 mode_append_entry (&head, &tail, p);
147 return head;
150 umask_value = umask (0);
151 umask (umask_value); /* Restore the old value. */
152 --mode_string;
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;
164 int who_specified_p;
166 affected_bits = 0;
167 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)
172 case 'u':
173 affected_bits |= 04700;
174 break;
175 case 'g':
176 affected_bits |= 02070;
177 break;
178 case 'o':
179 affected_bits |= 01007;
180 break;
181 case 'a':
182 affected_bits |= 07777;
183 break;
184 default:
185 goto no_more_affected;
188 no_more_affected:
189 /* If none specified, affect all bits, except perhaps those
190 set in the umask. */
191 if (affected_bits)
192 who_specified_p = 1;
193 else
195 who_specified_p = 0;
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);
203 if (change == NULL)
205 mode_free (head);
206 return MODE_MEMORY_EXHAUSTED;
209 change->next = NULL;
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);
219 if (p == NULL)
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
226 : MODE_MASK_MINUS))
227 affected_masked &= ~umask_value;
228 change->affected = affected_masked;
229 change->value = 0;
230 change->flags = 0;
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)
240 case 'r':
241 change->value |= 00444 & affected_masked;
242 break;
243 case 'w':
244 change->value |= 00222 & affected_masked;
245 break;
246 case 'X':
247 change->flags |= MODE_X_IF_ANY_X;
248 /* Fall through. */
249 case 'x':
250 change->value |= 00111 & affected_masked;
251 break;
252 case 's':
253 /* Set the setuid/gid bits if `u' or `g' is selected. */
254 change->value |= 06000 & affected_masked;
255 break;
256 case 't':
257 /* Set the "save text image" bit if `o' is selected. */
258 change->value |= 01000 & affected_masked;
259 break;
260 case 'u':
261 /* Set the affected bits to the value of the `u' bits
262 on the same file. */
263 if (change->value)
264 goto invalid;
265 change->value = 00700;
266 change->flags |= MODE_COPY_EXISTING;
267 break;
268 case 'g':
269 /* Set the affected bits to the value of the `g' bits
270 on the same file. */
271 if (change->value)
272 goto invalid;
273 change->value = 00070;
274 change->flags |= MODE_COPY_EXISTING;
275 break;
276 case 'o':
277 /* Set the affected bits to the value of the `o' bits
278 on the same file. */
279 if (change->value)
280 goto invalid;
281 change->value = 00007;
282 change->flags |= MODE_COPY_EXISTING;
283 break;
284 default:
285 goto no_more_values;
287 no_more_values:;
289 } while (*mode_string == ',');
290 if (*mode_string == 0)
291 return head;
292 invalid:
293 mode_free (head);
294 return MODE_INVALID;
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. */
300 struct mode_change *
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);
311 if (change == NULL)
312 return MODE_MEMORY_EXHAUSTED;
314 change->op = '=';
315 change->flags = 0;
316 change->affected = 07777;
317 change->value = ref_stats.st_mode;
318 change->next = NULL;
320 return change;
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. */
328 unsigned short
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);
350 else
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;
361 else
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. */
372 switch (changes->op)
374 case '=':
375 /* Preserve the previous values in `newmode' of bits that are
376 not affected by this change operation. */
377 newmode = (newmode & ~changes->affected) | value;
378 break;
379 case '+':
380 newmode |= value;
381 break;
382 case '-':
383 newmode &= ~value;
384 break;
387 return newmode;
390 /* Free the memory used by the list of file mode change operations
391 CHANGES. */
393 void
394 mode_free (register struct mode_change *changes)
396 register struct mode_change *next;
398 while (changes)
400 next = changes->next;
401 free (changes);
402 changes = next;