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