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