1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 1995-2003 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)
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> */
23 #include <sys/types.h>
30 #include "modechange.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "chmod"
38 #define AUTHORS "David MacKenzie", "Jim Meyering"
44 CH_NO_CHANGE_REQUESTED
49 /* Print a message for each file that is processed. */
52 /* Print a message for each file whose attributes we change. */
55 /* Do not be verbose. This is the default. */
59 /* The name the program was run with. */
62 /* If nonzero, change the modes of directories recursively. */
65 /* If nonzero, force silence (no error messages). */
66 static int force_silent;
68 /* Level of verbosity. */
69 static enum Verbosity verbosity = V_off;
71 /* The argument to the --reference option. Use the owner and group IDs
72 of this file. This file must exist. */
73 static char *reference_file;
75 /* Pointer to the device and inode numbers of `/', when --recursive.
77 static struct dev_ino *root_dev_ino;
79 /* For long options that have no equivalent short option, use a
80 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
83 NO_PRESERVE_ROOT = CHAR_MAX + 1,
88 static struct option const long_options[] =
90 {"recursive", no_argument, 0, 'R'},
91 {"changes", no_argument, 0, 'c'},
92 {"preserve-root", no_argument, 0, PRESERVE_ROOT},
93 {"no-preserve-root", no_argument, 0, NO_PRESERVE_ROOT},
94 {"quiet", no_argument, 0, 'f'},
95 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
96 {"silent", no_argument, 0, 'f'},
97 {"verbose", no_argument, 0, 'v'},
98 {GETOPT_HELP_OPTION_DECL},
99 {GETOPT_VERSION_OPTION_DECL},
104 mode_changed (const char *file, mode_t old_mode)
106 struct stat new_stats;
108 if (stat (file, &new_stats))
110 if (force_silent == 0)
111 error (0, errno, _("getting new attributes of %s"), quote (file));
115 return old_mode != new_stats.st_mode;
118 /* Tell the user how/if the MODE of FILE has been changed.
119 CHANGED describes what (if anything) has happened. */
122 describe_change (const char *file, mode_t mode,
123 enum Change_status changed)
125 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
128 mode_string (mode, perms);
129 perms[10] = '\0'; /* `mode_string' does not null terminate. */
133 fmt = _("mode of %s changed to %04lo (%s)\n");
136 fmt = _("failed to change mode of %s to %04lo (%s)\n");
138 case CH_NO_CHANGE_REQUESTED:
139 fmt = _("mode of %s retained as %04lo (%s)\n");
144 printf (fmt, quote (file),
145 (unsigned long) (mode & CHMOD_MODE_BITS), &perms[1]);
148 /* Change the mode of FILE according to the list of operations CHANGES.
149 Return 0 if successful, 1 if errors occurred. This function is called
150 once for every file system object that fts encounters. */
153 process_file (FTS *fts, FTSENT *ent, const struct mode_change *changes)
155 const char *file_full_name = ent->fts_path;
156 const char *file = ent->fts_accpath;
157 const struct stat *sb = ent->fts_statp;
163 switch (ent->fts_info)
166 error (0, ent->fts_errno, _("cannot access %s"), quote (file_full_name));
170 /* if (S_ISDIR (ent->fts_statp->st_mode) && FIXME */
171 error (0, ent->fts_errno, _("%s"), quote (file_full_name));
175 error (0, ent->fts_errno, _("cannot read directory %s"),
176 quote (file_full_name));
183 /* If this is the second (post-order) encounter with a directory,
184 then return right away. */
185 if (ent->fts_info == FTS_DP)
188 if (root_dev_ino && SAME_INODE (*sb, *root_dev_ino))
190 if (STREQ (file_full_name, "/"))
191 error (0, 0, _("it is dangerous to operate recursively on %s"),
192 quote (file_full_name));
195 _("it is dangerous to operate recursively on %s (same as %s)"),
196 quote_n (0, file_full_name), quote_n (1, "/"));
197 error (0, 0, _("use --no-preserve-root to override this failsafe"));
201 if (S_ISLNK (sb->st_mode))
204 newmode = mode_adjust (sb->st_mode, changes);
206 fail = chmod (file, newmode);
209 if (verbosity == V_high
210 || (verbosity == V_changes_only
211 && !fail && mode_changed (file, sb->st_mode)))
212 describe_change (file_full_name, newmode,
213 (fail ? CH_FAILED : CH_SUCCEEDED));
217 if (force_silent == 0)
218 error (0, saved_errno, _("changing permissions of %s"),
219 quote (file_full_name));
224 fts_set (fts, ent, FTS_SKIP);
229 /* Recursively change the modes of the specified FILES (the last entry
230 of which is NULL) according to the list of operations CHANGES.
231 BIT_FLAGS controls how fts works.
232 If the fts_open call fails, exit nonzero.
233 Otherwise, return nonzero upon error. */
236 process_files (char **files, int bit_flags, const struct mode_change *changes)
240 FTS *fts = xfts_open (files, bit_flags, NULL);
246 ent = fts_read (fts);
251 /* FIXME: try to give a better message */
252 error (0, errno, _("fts_read failed"));
258 fail |= process_file (fts, ent, changes);
261 /* Ignore failure, since the only way it can do so is in failing to
262 return to the original directory, and since we're about to exit,
263 that doesn't matter. */
273 fprintf (stderr, _("Try `%s --help' for more information.\n"),
278 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
279 or: %s [OPTION]... OCTAL-MODE FILE...\n\
280 or: %s [OPTION]... --reference=RFILE FILE...\n\
282 program_name, program_name, program_name);
284 Change the mode of each FILE to MODE.\n\
286 -c, --changes like verbose but report only when a change is made\n\
287 --preserve-root fail to operate recursively on `/'\n\
288 --no-preserve-root do not treat `/' specially (the default)\n\
289 -f, --silent, --quiet suppress most error messages\n\
290 -v, --verbose output a diagnostic for every file processed\n\
291 --reference=RFILE use RFILE's mode instead of MODE values\n\
292 -R, --recursive change files and directories recursively\n\
294 fputs (HELP_OPTION_DESCRIPTION, stdout);
295 fputs (VERSION_OPTION_DESCRIPTION, stdout);
298 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
299 one or more of the letters rwxXstugo.\n\
301 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
306 /* Call lstat to get the device and inode numbers for `/'.
307 Upon failure, return NULL. Otherwise, set the members of
308 *ROOT_D_I accordingly and return ROOT_D_I. */
309 static struct dev_ino *
310 get_root_dev_ino (struct dev_ino *root_d_i)
313 if (lstat ("/", &statbuf))
315 root_d_i->st_ino = statbuf.st_ino;
316 root_d_i->st_dev = statbuf.st_dev;
320 /* Parse the ASCII mode given on the command line into a linked list
321 of `struct mode_change' and apply that to each file argument. */
324 main (int argc, char **argv)
326 struct mode_change *changes;
328 int modeind = 0; /* Index of the mode argument in `argv'. */
330 bool preserve_root = false;
333 initialize_main (&argc, &argv);
334 program_name = argv[0];
335 setlocale (LC_ALL, "");
336 bindtextdomain (PACKAGE, LOCALEDIR);
337 textdomain (PACKAGE);
339 atexit (close_stdout);
341 recurse = force_silent = 0;
345 thisind = optind ? optind : 1;
347 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
369 if (modeind != 0 && modeind != thisind)
371 static char char_string[2] = {0, 0};
373 error (EXIT_FAILURE, 0,
374 _("invalid character %s in mode string %s"),
375 quote_n (0, char_string), quote_n (1, argv[thisind]));
379 case NO_PRESERVE_ROOT:
380 preserve_root = false;
383 preserve_root = true;
385 case REFERENCE_FILE_OPTION:
386 reference_file = optarg;
392 verbosity = V_changes_only;
400 case_GETOPT_HELP_CHAR;
401 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
403 usage (EXIT_FAILURE);
407 if (modeind == 0 && reference_file == NULL)
412 error (0, 0, _("too few arguments"));
413 usage (EXIT_FAILURE);
416 changes = (reference_file ? mode_create_from_ref (reference_file)
417 : mode_compile (argv[modeind], MODE_MASK_ALL));
419 if (changes == MODE_INVALID)
420 error (EXIT_FAILURE, 0,
421 _("invalid mode string: %s"), quote (argv[modeind]));
422 else if (changes == MODE_MEMORY_EXHAUSTED)
424 else if (changes == MODE_BAD_REFERENCE)
425 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
426 quote (reference_file));
428 if (recurse && preserve_root)
430 static struct dev_ino dev_ino_buf;
431 root_dev_ino = get_root_dev_ino (&dev_ino_buf);
432 if (root_dev_ino == NULL)
433 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
441 fail = process_files (argv + optind, FTS_COMFOLLOW, changes);
443 exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);