.
[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 #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 #ifdef lint
116   change = NULL;
117 #endif
118   --mode_string;
119
120   /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
121   do
122     {
123       affected_bits = 0;
124       ops_to_mask = 0;
125       /* Turn on all the bits in `affected_bits' for each group given. */
126       for (++mode_string;; ++mode_string)
127         switch (*mode_string)
128           {
129           case 'u':
130             affected_bits |= 04700;
131             break;
132           case 'g':
133             affected_bits |= 02070;
134             break;
135           case 'o':
136             affected_bits |= 01007;
137             break;
138           case 'a':
139             affected_bits |= 07777;
140             break;
141           default:
142             goto no_more_affected;
143           }
144
145     no_more_affected:
146       /* If none specified, affect all bits, except perhaps those
147          set in the umask. */
148       if (affected_bits == 0)
149         {
150           affected_bits = 07777;
151           ops_to_mask = masked_ops;
152         }
153
154       while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
155         {
156           /* Add the element to the tail of the list, so the operations
157              are performed in the correct order. */
158           if (head == NULL)
159             {
160               head = talloc (struct mode_change);
161               if (head == NULL)
162                 return MODE_MEMORY_EXHAUSTED;
163               change = head;
164             }
165           else
166             {
167               change->next = talloc (struct mode_change);
168               if (change->next == NULL)
169                 {
170                   mode_free (change);
171                   return MODE_MEMORY_EXHAUSTED;
172                 }
173               change = change->next;
174             }
175
176           change->next = NULL;
177           change->op = *mode_string;    /* One of "=+-". */
178           affected_masked = affected_bits;
179           if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
180                              : *mode_string == '+' ? MODE_MASK_PLUS
181                              : MODE_MASK_MINUS))
182             affected_masked &= ~umask_value;
183           change->affected = affected_masked;
184           change->value = 0;
185           change->flags = 0;
186
187           /* Set `value' according to the bits set in `affected_masked'. */
188           for (++mode_string;; ++mode_string)
189             switch (*mode_string)
190               {
191               case 'r':
192                 change->value |= 00444 & affected_masked;
193                 break;
194               case 'w':
195                 change->value |= 00222 & affected_masked;
196                 break;
197               case 'X':
198                 change->flags |= MODE_X_IF_ANY_X;
199                 /* Fall through. */
200               case 'x':
201                 change->value |= 00111 & affected_masked;
202                 break;
203               case 's':
204                 /* Set the setuid/gid bits if `u' or `g' is selected. */
205                 change->value |= 06000 & affected_masked;
206                 break;
207               case 't':
208                 /* Set the "save text image" bit if `o' is selected. */
209                 change->value |= 01000 & affected_masked;
210                 break;
211               case 'u':
212                 /* Set the affected bits to the value of the `u' bits
213                    on the same file.  */
214                 if (change->value)
215                   goto invalid;
216                 change->value = 00700;
217                 change->flags |= MODE_COPY_EXISTING;
218                 break;
219               case 'g':
220                 /* Set the affected bits to the value of the `g' bits
221                    on the same file.  */
222                 if (change->value)
223                   goto invalid;
224                 change->value = 00070;
225                 change->flags |= MODE_COPY_EXISTING;
226                 break;
227               case 'o':
228                 /* Set the affected bits to the value of the `o' bits
229                    on the same file.  */
230                 if (change->value)
231                   goto invalid;
232                 change->value = 00007;
233                 change->flags |= MODE_COPY_EXISTING;
234                 break;
235               default:
236                 goto no_more_values;
237               }
238         no_more_values:;
239         }
240   } while (*mode_string == ',');
241   if (*mode_string == 0)
242     return head;
243 invalid:
244   mode_free (head);
245   return MODE_INVALID;
246 }
247
248 /* Return file mode OLDMODE, adjusted as indicated by the list of change
249    operations CHANGES.  If OLDMODE is a directory, the type `X'
250    change affects it even if no execute bits were set in OLDMODE.
251    The returned value has the S_IFMT bits cleared. */
252
253 unsigned short
254 mode_adjust (oldmode, changes)
255      unsigned oldmode;
256      register struct mode_change *changes;
257 {
258   unsigned short newmode;       /* The adjusted mode and one operand. */
259   unsigned short value;         /* The other operand. */
260
261   newmode = oldmode & 07777;
262
263   for (; changes; changes = changes->next)
264     {
265       if (changes->flags & MODE_COPY_EXISTING)
266         {
267           /* Isolate in `value' the bits in `newmode' to copy, given in
268              the mask `changes->value'. */
269           value = newmode & changes->value;
270
271           if (changes->value & 00700)
272             /* Copy `u' permissions onto `g' and `o'. */
273             value |= (value >> 3) | (value >> 6);
274           else if (changes->value & 00070)
275             /* Copy `g' permissions onto `u' and `o'. */
276             value |= (value << 3) | (value >> 3);
277           else
278             /* Copy `o' permissions onto `u' and `g'. */
279             value |= (value << 3) | (value << 6);
280
281           /* In order to change only `u', `g', or `o' permissions,
282              or some combination thereof, clear unselected bits.
283              This can not be done in mode_compile because the value
284              to which the `changes->affected' mask is applied depends
285              on the old mode of each file. */
286           value &= changes->affected;
287         }
288       else
289         {
290           value = changes->value;
291           /* If `X', do not affect the execute bits if the file is not a
292              directory and no execute bits are already set. */
293           if ((changes->flags & MODE_X_IF_ANY_X)
294               && !S_ISDIR (oldmode)
295               && (newmode & 00111) == 0)
296             value &= ~00111;    /* Clear the execute bits. */
297         }
298
299       switch (changes->op)
300         {
301         case '=':
302           /* Preserve the previous values in `newmode' of bits that are
303              not affected by this change operation. */
304           newmode = (newmode & ~changes->affected) | value;
305           break;
306         case '+':
307           newmode |= value;
308           break;
309         case '-':
310           newmode &= ~value;
311           break;
312         }
313     }
314   return newmode;
315 }
316
317 /* Free the memory used by the list of file mode change operations
318    CHANGES. */
319
320 void
321 mode_free (changes)
322      register struct mode_change *changes;
323 {
324   register struct mode_change *next;
325
326   while (changes)
327     {
328       next = changes->next;
329       free (changes);
330       changes = next;
331     }
332 }
333
334 /* Return a positive integer containing the value of the ASCII
335    octal number S.  If S is not an octal number, return -1.  */
336
337 static int
338 oatoi (s)
339      char *s;
340 {
341   register int i;
342
343   if (*s == 0)
344     return -1;
345   for (i = 0; isodigit (*s); ++s)
346     i = i * 8 + *s - '0';
347   if (*s)
348     return -1;
349   return i;
350 }