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