(make_node_op_equals): New function.
[gnulib.git] / lib / modechange.c
1 /* modechange.c -- file mode manipulation
2    Copyright (C) 1989, 1990, 1997, 1998, 1999 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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 #if HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include "modechange.h"
34
35 #if STDC_HEADERS
36 # include <stdlib.h>
37 #else
38 char *malloc ();
39 #endif
40
41 #ifndef NULL
42 # define NULL 0
43 #endif
44
45 #if STAT_MACROS_BROKEN
46 # undef S_ISDIR
47 #endif
48
49 #if !defined(S_ISDIR) && defined(S_IFDIR)
50 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
51 #endif
52
53 /* Return newly allocated memory to hold one element of type TYPE. */
54 #define talloc(type) ((type *) malloc (sizeof (type)))
55
56 #define isodigit(c) ((c) >= '0' && (c) <= '7')
57
58 /* Return a positive integer containing the value of the ASCII
59    octal number S.  If S is not an octal number, return -1.  */
60
61 static int
62 oatoi (const char *s)
63 {
64   register int i;
65
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;
73 }
74
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.  */
78
79 static struct mode_change *
80 make_node_op_equals (int new_mode)
81 {
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;
92 }
93
94 /* Append entry E to the end of the link list with the specified
95    HEAD and TAIL.  */
96
97 static void
98 mode_append_entry (struct mode_change **head,
99                    struct mode_change **tail,
100                    struct mode_change *e)
101 {
102   if (*head == NULL)
103     *head = *tail = e;
104   else
105     {
106       (*tail)->next = e;
107       *tail = e;
108     }
109 }
110
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.
119
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. */
123
124 struct mode_change *
125 mode_compile (const char *mode_string, unsigned int masked_ops)
126 {
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). */
131
132   head = NULL;
133 #ifdef lint
134   tail = NULL;
135 #endif
136
137   i = oatoi (mode_string);
138   if (i >= 0)
139     {
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;
148     }
149
150   umask_value = umask (0);
151   umask (umask_value);          /* Restore the old value. */
152   --mode_string;
153
154   /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
155   do
156     {
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;
163
164       int who_specified_p;
165
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)
171           {
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;
186           }
187
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
194         {
195           who_specified_p = 0;
196           affected_bits = 07777;
197           ops_to_mask = masked_ops;
198         }
199
200       while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
201         {
202           struct mode_change *change = talloc (struct mode_change);
203           if (change == NULL)
204             {
205               mode_free (head);
206               return MODE_MEMORY_EXHAUSTED;
207             }
208
209           change->next = NULL;
210           change->op = *mode_string;    /* One of "=+-". */
211           affected_masked = affected_bits;
212
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))
217             {
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);
222             }
223
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;
231
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);
235
236           /* Set `value' according to the bits set in `affected_masked'. */
237           for (++mode_string;; ++mode_string)
238             switch (*mode_string)
239               {
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;
286               }
287         no_more_values:;
288         }
289   } while (*mode_string == ',');
290   if (*mode_string == 0)
291     return head;
292 invalid:
293   mode_free (head);
294   return MODE_INVALID;
295 }
296
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.  */
299
300 struct mode_change *
301 mode_create_from_ref (const char *ref_file)
302 {
303   struct mode_change *change;   /* the only change element */
304   struct stat ref_stats;
305
306   if (stat (ref_file, &ref_stats))
307     return MODE_BAD_REFERENCE;
308
309   change = talloc (struct mode_change);
310
311   if (change == NULL)
312     return MODE_MEMORY_EXHAUSTED;
313
314   change->op = '=';
315   change->flags = 0;
316   change->affected = 07777;
317   change->value = ref_stats.st_mode;
318   change->next = NULL;
319
320   return change;
321 }
322
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. */
327
328 unsigned short
329 mode_adjust (unsigned int oldmode, const struct mode_change *changes)
330 {
331   unsigned short newmode;       /* The adjusted mode and one operand. */
332   unsigned short value;         /* The other operand. */
333
334   newmode = oldmode & 07777;
335
336   for (; changes; changes = changes->next)
337     {
338       if (changes->flags & MODE_COPY_EXISTING)
339         {
340           /* Isolate in `value' the bits in `newmode' to copy, given in
341              the mask `changes->value'. */
342           value = newmode & changes->value;
343
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);
353
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;
360         }
361       else
362         {
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. */
370         }
371
372       switch (changes->op)
373         {
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;
385         }
386     }
387   return newmode;
388 }
389
390 /* Free the memory used by the list of file mode change operations
391    CHANGES. */
392
393 void
394 mode_free (register struct mode_change *changes)
395 {
396   register struct mode_change *next;
397
398   while (changes)
399     {
400       next = changes->next;
401       free (changes);
402       changes = next;
403     }
404 }