r345: Fixed a bug that prevented i18n from working.
[rox-filer/dt.git] / ROX-Filer / src / modechange.c
blob89c282b55b590bfe6f6d468950edfb715652fe1f
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@users.sourceforge.net>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* modechange.c -- file mode manipulation
24 * This rest of this file was taken from GNU FileUtils, with minor
25 * modifications (eg changing the memory handling to use GLib).
28 /* Written by David MacKenzie <djm@ai.mit.edu> */
30 /* The ASCII mode string is compiled into a linked list of `struct
31 modechange', which can then be applied to each file to be changed.
32 We do this instead of re-parsing the ASCII string for each file
33 because the compiled form requires less computation to use; when
34 changing the mode of many files, this probably results in a
35 performance gain. */
37 #include <config.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
42 #include <glib.h>
44 #include "modechange.h"
46 #ifndef NULL
47 # define NULL 0
48 #endif
50 #if STAT_MACROS_BROKEN
51 # undef S_ISDIR
52 #endif
54 #if !defined(S_ISDIR) && defined(S_IFDIR)
55 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
56 #endif
58 #define isodigit(c) ((c) >= '0' && (c) <= '7')
60 /* Return a positive integer containing the value of the ASCII
61 octal number S. If S is not an octal number, return -1. */
63 static int
64 oatoi (const char *s)
66 register int i;
68 if (*s == 0)
69 return -1;
70 for (i = 0; isodigit (*s); ++s)
71 i = i * 8 + *s - '0';
72 if (*s)
73 return -1;
74 return i;
77 /* Return a linked list of file mode change operations created from
78 MODE_STRING, an ASCII string that contains either an octal number
79 specifying an absolute mode, or symbolic mode change operations with
80 the form:
81 [ugoa...][[+-=][rwxXstugo...]...][,...]
82 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
83 should not affect bits set in the umask when no users are given.
84 Operators not selected in MASKED_OPS ignore the umask.
86 Return NULL if `mode_string' does not contain a valid
87 representation of file mode change operations. */
89 struct mode_change *
90 mode_compile (const char *mode_string, unsigned int masked_ops)
92 struct mode_change *head; /* First element of the linked list. */
93 struct mode_change *change; /* An element of the linked list. */
94 int i; /* General purpose temporary. */
95 int umask_value; /* The umask value (surprise). */
96 unsigned short affected_bits; /* Which bits in the mode are operated on. */
97 unsigned short affected_masked; /* `affected_bits' modified by umask. */
98 unsigned ops_to_mask; /* Operators to actually use umask on. */
100 i = oatoi (mode_string);
101 if (i >= 0)
103 if (i > 07777)
104 return NULL;
105 head = g_new(struct mode_change, 1);
106 head->next = NULL;
107 head->op = '=';
108 head->flags = 0;
109 head->value = i;
110 head->affected = 07777; /* Affect all permissions. */
111 return head;
114 umask_value = umask (0);
115 umask (umask_value); /* Restore the old value. */
117 head = NULL;
118 change = NULL;
119 --mode_string;
121 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
124 affected_bits = 0;
125 ops_to_mask = 0;
126 /* Turn on all the bits in `affected_bits' for each group given. */
127 for (++mode_string;; ++mode_string)
128 switch (*mode_string)
130 case 'u':
131 affected_bits |= 04700;
132 break;
133 case 'g':
134 affected_bits |= 02070;
135 break;
136 case 'o':
137 affected_bits |= 01007;
138 break;
139 case 'a':
140 affected_bits |= 07777;
141 break;
142 default:
143 goto no_more_affected;
146 no_more_affected:
147 /* If none specified, affect all bits, except perhaps those
148 set in the umask. */
149 if (affected_bits == 0)
151 affected_bits = 07777;
152 ops_to_mask = masked_ops;
155 while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
157 /* Add the element to the tail of the list, so the operations
158 are performed in the correct order. */
159 if (head == NULL)
161 head = g_new(struct mode_change, 1);
162 change = head;
164 else
166 change->next = g_new(struct mode_change, 1);
167 change = change->next;
170 change->next = NULL;
171 change->op = *mode_string; /* One of "=+-". */
172 affected_masked = affected_bits;
173 if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
174 : *mode_string == '+' ? MODE_MASK_PLUS
175 : MODE_MASK_MINUS))
176 affected_masked &= ~umask_value;
177 change->affected = affected_masked;
178 change->value = 0;
179 change->flags = 0;
181 /* Set `value' according to the bits set in `affected_masked'. */
182 for (++mode_string;; ++mode_string)
183 switch (*mode_string)
185 case 'r':
186 change->value |= 00444 & affected_masked;
187 break;
188 case 'w':
189 change->value |= 00222 & affected_masked;
190 break;
191 case 'X':
192 change->flags |= MODE_X_IF_ANY_X;
193 /* Fall through. */
194 case 'x':
195 change->value |= 00111 & affected_masked;
196 break;
197 case 's':
198 /* Set the setuid/gid bits if `u' or `g' is selected. */
199 change->value |= 06000 & affected_masked;
200 break;
201 case 't':
202 /* Set the "save text image" bit if `o' is selected. */
203 change->value |= 01000 & affected_masked;
204 break;
205 case 'u':
206 /* Set the affected bits to the value of the `u' bits
207 on the same file. */
208 if (change->value)
209 goto invalid;
210 change->value = 00700;
211 change->flags |= MODE_COPY_EXISTING;
212 break;
213 case 'g':
214 /* Set the affected bits to the value of the `g' bits
215 on the same file. */
216 if (change->value)
217 goto invalid;
218 change->value = 00070;
219 change->flags |= MODE_COPY_EXISTING;
220 break;
221 case 'o':
222 /* Set the affected bits to the value of the `o' bits
223 on the same file. */
224 if (change->value)
225 goto invalid;
226 change->value = 00007;
227 change->flags |= MODE_COPY_EXISTING;
228 break;
229 default:
230 goto no_more_values;
232 no_more_values:;
234 } while (*mode_string == ',');
235 if (*mode_string == 0)
236 return head;
237 invalid:
238 mode_free (head);
239 return NULL;
242 /* Return file mode OLDMODE, adjusted as indicated by the list of change
243 operations CHANGES. If OLDMODE is a directory, the type `X'
244 change affects it even if no execute bits were set in OLDMODE.
245 The returned value has the S_IFMT bits cleared. */
247 unsigned short
248 mode_adjust (unsigned int oldmode, const struct mode_change *changes)
250 unsigned short newmode; /* The adjusted mode and one operand. */
251 unsigned short value; /* The other operand. */
253 newmode = oldmode & 07777;
255 for (; changes; changes = changes->next)
257 if (changes->flags & MODE_COPY_EXISTING)
259 /* Isolate in `value' the bits in `newmode' to copy, given in
260 the mask `changes->value'. */
261 value = newmode & changes->value;
263 if (changes->value & 00700)
264 /* Copy `u' permissions onto `g' and `o'. */
265 value |= (value >> 3) | (value >> 6);
266 else if (changes->value & 00070)
267 /* Copy `g' permissions onto `u' and `o'. */
268 value |= (value << 3) | (value >> 3);
269 else
270 /* Copy `o' permissions onto `u' and `g'. */
271 value |= (value << 3) | (value << 6);
273 /* In order to change only `u', `g', or `o' permissions,
274 or some combination thereof, clear unselected bits.
275 This can not be done in mode_compile because the value
276 to which the `changes->affected' mask is applied depends
277 on the old mode of each file. */
278 value &= changes->affected;
280 else
282 value = changes->value;
283 /* If `X', do not affect the execute bits if the file is not a
284 directory and no execute bits are already set. */
285 if ((changes->flags & MODE_X_IF_ANY_X)
286 && !S_ISDIR (oldmode)
287 && (newmode & 00111) == 0)
288 value &= ~00111; /* Clear the execute bits. */
291 switch (changes->op)
293 case '=':
294 /* Preserve the previous values in `newmode' of bits that are
295 not affected by this change operation. */
296 newmode = (newmode & ~changes->affected) | value;
297 break;
298 case '+':
299 newmode |= value;
300 break;
301 case '-':
302 newmode &= ~value;
303 break;
306 return newmode;
309 /* Free the memory used by the list of file mode change operations
310 CHANGES. */
312 void
313 mode_free (register struct mode_change *changes)
315 register struct mode_change *next;
317 while (changes)
319 next = changes->next;
320 g_free (changes);
321 changes = next;