1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 1995-2000 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>
28 #include "modechange.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "chmod"
35 #define AUTHORS "David MacKenzie"
41 CH_NO_CHANGE_REQUESTED
46 /* Print a message for each file that is processed. */
49 /* Print a message for each file whose attributes we change. */
52 /* Do not be verbose. This is the default. */
56 void strip_trailing_slashes ();
58 static int change_dir_mode PARAMS ((const char *dir,
59 const struct mode_change *changes,
60 const struct stat *statp));
62 /* The name the program was run with. */
65 /* If nonzero, change the modes of directories recursively. */
68 /* If nonzero, force silence (no error messages). */
69 static int force_silent;
71 /* Level of verbosity. */
72 static enum Verbosity verbosity = V_off;
74 /* The argument to the --reference option. Use the owner and group IDs
75 of this file. This file must exist. */
76 static char *reference_file;
78 /* For long options that have no equivalent short option, use a
79 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
82 REFERENCE_FILE_OPTION = CHAR_MAX + 1
85 static struct option const long_options[] =
87 {"recursive", no_argument, 0, 'R'},
88 {"changes", no_argument, 0, 'c'},
89 {"silent", no_argument, 0, 'f'},
90 {"quiet", no_argument, 0, 'f'},
91 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
92 {"verbose", no_argument, 0, 'v'},
93 {GETOPT_HELP_OPTION_DECL},
94 {GETOPT_VERSION_OPTION_DECL},
99 mode_changed (const char *file, mode_t old_mode)
101 struct stat new_stats;
103 if (stat (file, &new_stats))
105 if (force_silent == 0)
106 error (0, errno, _("getting new attributes of %s"), quote (file));
110 return old_mode != new_stats.st_mode;
113 /* Tell the user how/if the MODE of FILE has been changed.
114 CHANGED describes what (if anything) has happened. */
117 describe_change (const char *file, mode_t mode,
118 enum Change_status changed)
120 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
123 mode_string (mode, perms);
124 perms[10] = '\0'; /* `mode_string' does not null terminate. */
128 fmt = _("mode of %s changed to %04lo (%s)\n");
131 fmt = _("failed to change mode of %s to %04lo (%s)\n");
133 case CH_NO_CHANGE_REQUESTED:
134 fmt = _("mode of %s retained as %04lo (%s)\n");
139 printf (fmt, quote (file),
140 (unsigned long) (mode & CHMOD_MODE_BITS), &perms[1]);
143 /* Change the mode of FILE according to the list of operations CHANGES.
144 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
145 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
146 links. Return 0 if successful, 1 if errors occurred. */
149 change_file_mode (const char *file, const struct mode_change *changes,
150 const int deref_symlink)
152 struct stat file_stats;
158 if (deref_symlink ? stat (file, &file_stats) : lstat (file, &file_stats))
160 if (force_silent == 0)
161 error (0, errno, _("getting attributes of %s"), quote (file));
166 if (S_ISLNK (file_stats.st_mode))
170 newmode = mode_adjust (file_stats.st_mode, changes);
172 fail = chmod (file, newmode);
175 if (verbosity == V_high
176 || (verbosity == V_changes_only
177 && !fail && mode_changed (file, file_stats.st_mode)))
178 describe_change (file, newmode, (fail ? CH_FAILED : CH_SUCCEEDED));
182 if (force_silent == 0)
183 error (0, saved_errno, _("changing permissions of %s"),
188 if (recurse && S_ISDIR (file_stats.st_mode))
189 errors |= change_dir_mode (file, changes, &file_stats);
193 /* Recursively change the modes of the files in directory DIR
194 according to the list of operations CHANGES.
195 STATP points to the results of lstat on DIR.
196 Return 0 if successful, 1 if errors occurred. */
199 change_dir_mode (const char *dir, const struct mode_change *changes,
200 const struct stat *statp)
202 char *name_space, *namep;
203 char *path; /* Full path of each entry to process. */
204 unsigned dirlength; /* Length of DIR and '\0'. */
205 unsigned filelength; /* Length of each pathname to process. */
206 unsigned pathlength; /* Bytes allocated for `path'. */
209 name_space = savedir (dir, statp->st_size);
210 if (name_space == NULL)
212 if (force_silent == 0)
213 error (0, errno, "%s", quote (dir));
217 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
218 pathlength = dirlength + 1;
219 /* Give `path' a dummy value; it will be reallocated before first use. */
220 path = xmalloc (pathlength);
222 path[dirlength - 1] = '/';
224 for (namep = name_space; *namep; namep += filelength - dirlength)
226 filelength = dirlength + strlen (namep) + 1;
227 if (filelength > pathlength)
229 pathlength = filelength * 2;
230 path = xrealloc (path, pathlength);
232 strcpy (path + dirlength, namep);
233 errors |= change_file_mode (path, changes, 0);
244 fprintf (stderr, _("Try `%s --help' for more information.\n"),
249 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
250 or: %s [OPTION]... OCTAL-MODE FILE...\n\
251 or: %s [OPTION]... --reference=RFILE FILE...\n\
253 program_name, program_name, program_name);
255 Change the mode of each FILE to MODE.\n\
257 -c, --changes like verbose but report only when a change is made\n\
258 -f, --silent, --quiet suppress most error messages\n\
259 -v, --verbose output a diagnostic for every file processed\n\
260 --reference=RFILE use RFILE's mode instead of MODE values\n\
261 -R, --recursive change files and directories recursively\n\
262 --help display this help and exit\n\
263 --version output version information and exit\n\
265 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
266 one or more of the letters rwxXstugo.\n\
268 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
273 /* Parse the ASCII mode given on the command line into a linked list
274 of `struct mode_change' and apply that to each file argument. */
277 main (int argc, char **argv)
279 struct mode_change *changes;
281 int modeind = 0; /* Index of the mode argument in `argv'. */
285 program_name = argv[0];
286 setlocale (LC_ALL, "");
287 bindtextdomain (PACKAGE, LOCALEDIR);
288 textdomain (PACKAGE);
290 atexit (close_stdout);
292 recurse = force_silent = 0;
296 thisind = optind ? optind : 1;
298 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
320 if (modeind != 0 && modeind != thisind)
322 static char char_string[2] = {0, 0};
324 error (1, 0, _("invalid character %s in mode string %s"),
325 quote_n (0, char_string), quote_n (1, argv[thisind]));
329 case REFERENCE_FILE_OPTION:
330 reference_file = optarg;
336 verbosity = V_changes_only;
344 case_GETOPT_HELP_CHAR;
345 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
351 if (modeind == 0 && reference_file == NULL)
356 error (0, 0, _("too few arguments"));
360 changes = (reference_file ? mode_create_from_ref (reference_file)
361 : mode_compile (argv[modeind], MODE_MASK_ALL));
363 if (changes == MODE_INVALID)
364 error (1, 0, _("invalid mode string: %s"), quote (argv[modeind]));
365 else if (changes == MODE_MEMORY_EXHAUSTED)
367 else if (changes == MODE_BAD_REFERENCE)
368 error (1, errno, _("getting attributes of %s"), quote (reference_file));
370 for (; optind < argc; ++optind)
372 strip_trailing_slashes (argv[optind]);
373 errors |= change_file_mode (argv[optind], changes, 1);