.
[coreutils.git] / src / chmod.c
blobd3650cf332d0fc2d137d0b1a2e47b9e4ae61fae8
1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 1995-2004 Free Software Foundation, Inc.
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.
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.
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. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "dev-ino.h"
27 #include "dirname.h"
28 #include "error.h"
29 #include "filemode.h"
30 #include "modechange.h"
31 #include "quote.h"
32 #include "root-dev-ino.h"
33 #include "savedir.h"
34 #include "xfts.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "chmod"
39 #define AUTHORS "David MacKenzie", "Jim Meyering"
41 enum Change_status
43 CH_SUCCEEDED,
44 CH_FAILED,
45 CH_NO_CHANGE_REQUESTED
48 enum Verbosity
50 /* Print a message for each file that is processed. */
51 V_high,
53 /* Print a message for each file whose attributes we change. */
54 V_changes_only,
56 /* Do not be verbose. This is the default. */
57 V_off
60 /* The name the program was run with. */
61 char *program_name;
63 /* If nonzero, change the modes of directories recursively. */
64 static int recurse;
66 /* If nonzero, force silence (no error messages). */
67 static int force_silent;
69 /* Level of verbosity. */
70 static enum Verbosity verbosity = V_off;
72 /* The argument to the --reference option. Use the owner and group IDs
73 of this file. This file must exist. */
74 static char *reference_file;
76 /* Pointer to the device and inode numbers of `/', when --recursive.
77 Otherwise NULL. */
78 static struct dev_ino *root_dev_ino;
80 /* For long options that have no equivalent short option, use a
81 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
82 enum
84 NO_PRESERVE_ROOT = CHAR_MAX + 1,
85 PRESERVE_ROOT,
86 REFERENCE_FILE_OPTION
89 static struct option const long_options[] =
91 {"changes", no_argument, 0, 'c'},
92 {"recursive", no_argument, 0, 'R'},
93 {"no-preserve-root", no_argument, 0, NO_PRESERVE_ROOT},
94 {"preserve-root", no_argument, 0, PRESERVE_ROOT},
95 {"quiet", no_argument, 0, 'f'},
96 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
97 {"silent", no_argument, 0, 'f'},
98 {"verbose", no_argument, 0, 'v'},
99 {GETOPT_HELP_OPTION_DECL},
100 {GETOPT_VERSION_OPTION_DECL},
101 {0, 0, 0, 0}
104 static int
105 mode_changed (const char *file, mode_t old_mode)
107 struct stat new_stats;
109 if (stat (file, &new_stats))
111 if (force_silent == 0)
112 error (0, errno, _("getting new attributes of %s"), quote (file));
113 return 0;
116 return old_mode != new_stats.st_mode;
119 /* Tell the user how/if the MODE of FILE has been changed.
120 CHANGED describes what (if anything) has happened. */
122 static void
123 describe_change (const char *file, mode_t mode,
124 enum Change_status changed)
126 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
127 const char *fmt;
129 mode_string (mode, perms);
130 perms[10] = '\0'; /* `mode_string' does not null terminate. */
131 switch (changed)
133 case CH_SUCCEEDED:
134 fmt = _("mode of %s changed to %04lo (%s)\n");
135 break;
136 case CH_FAILED:
137 fmt = _("failed to change mode of %s to %04lo (%s)\n");
138 break;
139 case CH_NO_CHANGE_REQUESTED:
140 fmt = _("mode of %s retained as %04lo (%s)\n");
141 break;
142 default:
143 abort ();
145 printf (fmt, quote (file),
146 (unsigned long) (mode & CHMOD_MODE_BITS), &perms[1]);
149 /* Change the mode of FILE according to the list of operations CHANGES.
150 Return 0 if successful, 1 if errors occurred. This function is called
151 once for every file system object that fts encounters. */
153 static int
154 process_file (FTS *fts, FTSENT *ent, const struct mode_change *changes)
156 const char *file_full_name = ent->fts_path;
157 const char *file = ent->fts_accpath;
158 const struct stat *sb = ent->fts_statp;
159 mode_t newmode;
160 int errors = 0;
161 int fail;
162 int saved_errno;
164 switch (ent->fts_info)
166 case FTS_NS:
167 error (0, ent->fts_errno, _("cannot access %s"), quote (file_full_name));
168 return 1;
170 case FTS_ERR:
171 error (0, ent->fts_errno, _("%s"), quote (file_full_name));
172 return 1;
174 case FTS_DNR:
175 error (0, ent->fts_errno, _("cannot read directory %s"),
176 quote (file_full_name));
177 return 1;
179 default:
180 break;
183 /* If this is the second (post-order) encounter with a directory,
184 then return right away. */
185 if (ent->fts_info == FTS_DP)
186 return 0;
188 if (ROOT_DEV_INO_CHECK (root_dev_ino, sb))
190 ROOT_DEV_INO_WARN (file_full_name);
191 return 1;
194 if (S_ISLNK (sb->st_mode))
195 return 0;
197 newmode = mode_adjust (sb->st_mode, changes);
199 fail = chmod (file, newmode);
200 saved_errno = errno;
202 if (verbosity == V_high
203 || (verbosity == V_changes_only
204 && !fail && mode_changed (file, sb->st_mode)))
205 describe_change (file_full_name, newmode,
206 (fail ? CH_FAILED : CH_SUCCEEDED));
208 if (fail)
210 if (force_silent == 0)
211 error (0, saved_errno, _("changing permissions of %s"),
212 quote (file_full_name));
213 errors = 1;
216 if ( ! recurse)
217 fts_set (fts, ent, FTS_SKIP);
219 return errors;
222 /* Recursively change the modes of the specified FILES (the last entry
223 of which is NULL) according to the list of operations CHANGES.
224 BIT_FLAGS controls how fts works.
225 If the fts_open call fails, exit nonzero.
226 Otherwise, return nonzero upon error. */
228 static int
229 process_files (char **files, int bit_flags, const struct mode_change *changes)
231 int fail = 0;
233 FTS *fts = xfts_open (files, bit_flags, NULL);
235 while (1)
237 FTSENT *ent;
239 ent = fts_read (fts);
240 if (ent == NULL)
242 if (errno != 0)
244 /* FIXME: try to give a better message */
245 error (0, errno, _("fts_read failed"));
246 fail = 1;
248 break;
251 fail |= process_file (fts, ent, changes);
254 /* Ignore failure, since the only way it can do so is in failing to
255 return to the original directory, and since we're about to exit,
256 that doesn't matter. */
257 fts_close (fts);
259 return fail;
262 void
263 usage (int status)
265 if (status != EXIT_SUCCESS)
266 fprintf (stderr, _("Try `%s --help' for more information.\n"),
267 program_name);
268 else
270 printf (_("\
271 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
272 or: %s [OPTION]... OCTAL-MODE FILE...\n\
273 or: %s [OPTION]... --reference=RFILE FILE...\n\
275 program_name, program_name, program_name);
276 fputs (_("\
277 Change the mode of each FILE to MODE.\n\
279 -c, --changes like verbose but report only when a change is made\n\
280 "), stdout);
281 fputs (_("\
282 --no-preserve-root do not treat `/' specially (the default)\n\
283 --preserve-root fail to operate recursively on `/'\n\
284 "), stdout);
285 fputs (_("\
286 -f, --silent, --quiet suppress most error messages\n\
287 -v, --verbose output a diagnostic for every file processed\n\
288 --reference=RFILE use RFILE's mode instead of MODE values\n\
289 -R, --recursive change files and directories recursively\n\
290 "), stdout);
291 fputs (HELP_OPTION_DESCRIPTION, stdout);
292 fputs (VERSION_OPTION_DESCRIPTION, stdout);
293 fputs (_("\
295 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
296 one or more of the letters rwxXstugo.\n\
297 "), stdout);
298 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
300 exit (status);
303 /* Parse the ASCII mode given on the command line into a linked list
304 of `struct mode_change' and apply that to each file argument. */
307 main (int argc, char **argv)
309 struct mode_change *changes;
310 int fail = 0;
311 int modeind = 0; /* Index of the mode argument in `argv'. */
312 int thisind;
313 bool preserve_root = false;
314 int c;
316 initialize_main (&argc, &argv);
317 program_name = argv[0];
318 setlocale (LC_ALL, "");
319 bindtextdomain (PACKAGE, LOCALEDIR);
320 textdomain (PACKAGE);
322 atexit (close_stdout);
324 recurse = force_silent = 0;
326 while (1)
328 thisind = optind ? optind : 1;
330 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
331 if (c == -1)
332 break;
334 switch (c)
336 case 0:
337 break;
338 case 'r':
339 case 'w':
340 case 'x':
341 case 'X':
342 case 's':
343 case 't':
344 case 'u':
345 case 'g':
346 case 'o':
347 case 'a':
348 case ',':
349 case '+':
350 case '-':
351 case '=':
352 if (modeind != 0 && modeind != thisind)
354 static char char_string[2] = {0, 0};
355 char_string[0] = c;
356 error (EXIT_FAILURE, 0,
357 _("invalid character %s in mode string %s"),
358 quote_n (0, char_string), quote_n (1, argv[thisind]));
360 modeind = thisind;
361 break;
362 case NO_PRESERVE_ROOT:
363 preserve_root = false;
364 break;
365 case PRESERVE_ROOT:
366 preserve_root = true;
367 break;
368 case REFERENCE_FILE_OPTION:
369 reference_file = optarg;
370 break;
371 case 'R':
372 recurse = 1;
373 break;
374 case 'c':
375 verbosity = V_changes_only;
376 break;
377 case 'f':
378 force_silent = 1;
379 break;
380 case 'v':
381 verbosity = V_high;
382 break;
383 case_GETOPT_HELP_CHAR;
384 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
385 default:
386 usage (EXIT_FAILURE);
390 if (modeind == 0 && reference_file == NULL)
391 modeind = optind++;
393 if (optind >= argc)
395 error (0, 0, _("too few arguments"));
396 usage (EXIT_FAILURE);
399 changes = (reference_file ? mode_create_from_ref (reference_file)
400 : mode_compile (argv[modeind], MODE_MASK_ALL));
402 if (changes == MODE_INVALID)
403 error (EXIT_FAILURE, 0,
404 _("invalid mode string: %s"), quote (argv[modeind]));
405 else if (changes == MODE_MEMORY_EXHAUSTED)
406 xalloc_die ();
407 else if (changes == MODE_BAD_REFERENCE)
408 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
409 quote (reference_file));
411 if (recurse && preserve_root)
413 static struct dev_ino dev_ino_buf;
414 root_dev_ino = get_root_dev_ino (&dev_ino_buf);
415 if (root_dev_ino == NULL)
416 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
417 quote ("/"));
419 else
421 root_dev_ino = NULL;
424 fail = process_files (argv + optind, FTS_COMFOLLOW, changes);
426 exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);