r1476: Options to forward button-3 clicks on the pinboard to the window manager
[rox-filer.git] / ROX-Filer / src / modechange.c
blob227195c912cf2e22fea3e7d834732c12bb5a8ce6
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
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>
41 #include <string.h>
43 #include <glib.h>
45 #include "modechange.h"
47 #define isodigit(c) ((c) >= '0' && (c) <= '7')
49 /* Return a positive integer containing the value of the ASCII
50 octal number S. If S is not an octal number or is more than
51 4 digits, return -1. */
53 static int
54 oatoi (const char *s)
56 register int i;
58 if (*s == 0 || strlen (s) > 4)
59 return -1;
60 for (i = 0; isodigit (*s); ++s)
61 i = i * 8 + *s - '0';
62 if (*s)
63 return -1;
64 return i;
67 /* Return a linked list of file mode change operations created from
68 MODE_STRING, an ASCII string that contains either an octal number
69 specifying an absolute mode, or symbolic mode change operations with
70 the form:
71 [ugoa...][[+-=][rwxXstugo...]...][,...]
72 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
73 should not affect bits set in the umask when no users are given.
74 Operators not selected in MASKED_OPS ignore the umask.
76 Return NULL if `mode_string' does not contain a valid
77 representation of file mode change operations. */
79 struct mode_change *
80 mode_compile (const char *mode_string, unsigned int masked_ops)
82 struct mode_change *head; /* First element of the linked list. */
83 struct mode_change *change; /* An element of the linked list. */
84 int i; /* General purpose temporary. */
85 int umask_value; /* The umask value (surprise). */
86 unsigned short affected_bits; /* Which bits in the mode are operated on. */
87 unsigned short affected_masked; /* `affected_bits' modified by umask. */
88 unsigned ops_to_mask; /* Operators to actually use umask on. */
90 i = oatoi (mode_string);
91 if (i >= 0)
93 if (i > 07777)
94 return NULL;
95 head = g_new(struct mode_change, 1);
96 head->next = NULL;
97 head->op = '=';
98 head->flags = 0;
99 head->value = i;
100 head->affected = 07777; /* Affect all permissions. */
101 return head;
104 umask_value = umask (0);
105 umask (umask_value); /* Restore the old value. */
107 head = NULL;
108 change = NULL;
109 --mode_string;
111 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
114 affected_bits = 0;
115 ops_to_mask = 0;
116 /* Turn on all the bits in `affected_bits' for each group given. */
117 for (++mode_string;; ++mode_string)
118 switch (*mode_string)
120 case 'u':
121 affected_bits |= 04700;
122 break;
123 case 'g':
124 affected_bits |= 02070;
125 break;
126 case 'o':
127 affected_bits |= 01007;
128 break;
129 case 'a':
130 affected_bits |= 07777;
131 break;
132 default:
133 goto no_more_affected;
136 no_more_affected:
137 /* If none specified, affect all bits, except perhaps those
138 set in the umask. */
139 if (affected_bits == 0)
141 affected_bits = 07777;
142 ops_to_mask = masked_ops;
145 while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
147 /* Add the element to the tail of the list, so the operations
148 are performed in the correct order. */
149 if (head == NULL)
151 head = g_new(struct mode_change, 1);
152 change = head;
154 else
156 change->next = g_new(struct mode_change, 1);
157 change = change->next;
160 change->next = NULL;
161 change->op = *mode_string; /* One of "=+-". */
162 affected_masked = affected_bits;
163 if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
164 : *mode_string == '+' ? MODE_MASK_PLUS
165 : MODE_MASK_MINUS))
166 affected_masked &= ~umask_value;
167 change->affected = affected_masked;
168 change->value = 0;
169 change->flags = 0;
171 /* Set `value' according to the bits set in `affected_masked'. */
172 for (++mode_string;; ++mode_string)
173 switch (*mode_string)
175 case 'r':
176 change->value |= 00444 & affected_masked;
177 break;
178 case 'w':
179 change->value |= 00222 & affected_masked;
180 break;
181 case 'X':
182 change->flags |= MODE_X_IF_ANY_X;
183 /* Fall through. */
184 case 'x':
185 change->value |= 00111 & affected_masked;
186 break;
187 case 's':
188 /* Set the setuid/gid bits if `u' or `g' is selected. */
189 change->value |= 06000 & affected_masked;
190 break;
191 case 't':
192 /* Set the "save text image" bit if `o' is selected. */
193 change->value |= 01000 & affected_masked;
194 break;
195 case 'u':
196 /* Set the affected bits to the value of the `u' bits
197 on the same file. */
198 if (change->value)
199 goto invalid;
200 change->value = 00700;
201 change->flags |= MODE_COPY_EXISTING;
202 break;
203 case 'g':
204 /* Set the affected bits to the value of the `g' bits
205 on the same file. */
206 if (change->value)
207 goto invalid;
208 change->value = 00070;
209 change->flags |= MODE_COPY_EXISTING;
210 break;
211 case 'o':
212 /* Set the affected bits to the value of the `o' bits
213 on the same file. */
214 if (change->value)
215 goto invalid;
216 change->value = 00007;
217 change->flags |= MODE_COPY_EXISTING;
218 break;
219 default:
220 goto no_more_values;
222 no_more_values:;
224 } while (*mode_string == ',');
225 if (*mode_string == 0)
226 return head;
227 invalid:
228 mode_free (head);
229 return NULL;
232 /* Return file mode OLDMODE, adjusted as indicated by the list of change
233 operations CHANGES. If OLDMODE is a directory, the type `X'
234 change affects it even if no execute bits were set in OLDMODE.
235 The returned value has the S_IFMT bits cleared. */
237 unsigned short
238 mode_adjust (unsigned int oldmode, const struct mode_change *changes)
240 unsigned short newmode; /* The adjusted mode and one operand. */
241 unsigned short value; /* The other operand. */
243 newmode = oldmode & 07777;
245 for (; changes; changes = changes->next)
247 if (changes->flags & MODE_COPY_EXISTING)
249 /* Isolate in `value' the bits in `newmode' to copy, given in
250 the mask `changes->value'. */
251 value = newmode & changes->value;
253 if (changes->value & 00700)
254 /* Copy `u' permissions onto `g' and `o'. */
255 value |= (value >> 3) | (value >> 6);
256 else if (changes->value & 00070)
257 /* Copy `g' permissions onto `u' and `o'. */
258 value |= (value << 3) | (value >> 3);
259 else
260 /* Copy `o' permissions onto `u' and `g'. */
261 value |= (value << 3) | (value << 6);
263 /* In order to change only `u', `g', or `o' permissions,
264 or some combination thereof, clear unselected bits.
265 This can not be done in mode_compile because the value
266 to which the `changes->affected' mask is applied depends
267 on the old mode of each file. */
268 value &= changes->affected;
270 else
272 value = changes->value;
273 /* If `X', do not affect the execute bits if the file is not a
274 directory and no execute bits are already set. */
275 if ((changes->flags & MODE_X_IF_ANY_X)
276 && !S_ISDIR (oldmode)
277 && (newmode & 00111) == 0)
278 value &= ~00111; /* Clear the execute bits. */
281 switch (changes->op)
283 case '=':
284 /* Preserve the previous values in `newmode' of bits that are
285 not affected by this change operation. */
286 newmode = (newmode & ~changes->affected) | value;
287 break;
288 case '+':
289 newmode |= value;
290 break;
291 case '-':
292 newmode &= ~value;
293 break;
296 return newmode;
299 /* Free the memory used by the list of file mode change operations
300 CHANGES. */
302 void
303 mode_free (register struct mode_change *changes)
305 register struct mode_change *next;
307 while (changes)
309 next = changes->next;
310 g_free (changes);
311 changes = next;