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>
29 #include "modechange.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "chmod"
36 #define AUTHORS "David MacKenzie"
42 CH_NO_CHANGE_REQUESTED
47 /* Print a message for each file that is processed. */
50 /* Print a message for each file whose attributes we change. */
53 /* Do not be verbose. This is the default. */
57 static int change_dir_mode PARAMS ((const char *dir,
58 const struct mode_change *changes));
60 /* The name the program was run with. */
63 /* If nonzero, change the modes of directories recursively. */
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 /* For long options that have no equivalent short option, use a
77 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
80 REFERENCE_FILE_OPTION = CHAR_MAX + 1
83 static struct option const long_options[] =
85 {"recursive", no_argument, 0, 'R'},
86 {"changes", no_argument, 0, 'c'},
87 {"silent", no_argument, 0, 'f'},
88 {"quiet", no_argument, 0, 'f'},
89 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
90 {"verbose", no_argument, 0, 'v'},
91 {GETOPT_HELP_OPTION_DECL},
92 {GETOPT_VERSION_OPTION_DECL},
97 mode_changed (const char *file, mode_t old_mode)
99 struct stat new_stats;
101 if (stat (file, &new_stats))
103 if (force_silent == 0)
104 error (0, errno, _("getting new attributes of %s"), quote (file));
108 return old_mode != new_stats.st_mode;
111 /* Tell the user how/if the MODE of FILE has been changed.
112 CHANGED describes what (if anything) has happened. */
115 describe_change (const char *file, mode_t mode,
116 enum Change_status changed)
118 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
121 mode_string (mode, perms);
122 perms[10] = '\0'; /* `mode_string' does not null terminate. */
126 fmt = _("mode of %s changed to %04lo (%s)\n");
129 fmt = _("failed to change mode of %s to %04lo (%s)\n");
131 case CH_NO_CHANGE_REQUESTED:
132 fmt = _("mode of %s retained as %04lo (%s)\n");
137 printf (fmt, quote (file),
138 (unsigned long) (mode & CHMOD_MODE_BITS), &perms[1]);
141 /* Change the mode of FILE according to the list of operations CHANGES.
142 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
143 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
144 links. Return 0 if successful, 1 if errors occurred. */
147 change_file_mode (const char *file, const struct mode_change *changes,
148 const int deref_symlink)
150 struct stat file_stats;
156 if (deref_symlink ? stat (file, &file_stats) : lstat (file, &file_stats))
158 if (force_silent == 0)
159 error (0, errno, _("getting attributes of %s"), quote (file));
164 if (S_ISLNK (file_stats.st_mode))
168 newmode = mode_adjust (file_stats.st_mode, changes);
170 fail = chmod (file, newmode);
173 if (verbosity == V_high
174 || (verbosity == V_changes_only
175 && !fail && mode_changed (file, file_stats.st_mode)))
176 describe_change (file, newmode, (fail ? CH_FAILED : CH_SUCCEEDED));
180 if (force_silent == 0)
181 error (0, saved_errno, _("changing permissions of %s"),
186 if (recurse && S_ISDIR (file_stats.st_mode))
187 errors |= change_dir_mode (file, changes);
191 /* Recursively change the modes of the files in directory DIR
192 according to the list of operations CHANGES.
193 Return 0 if successful, 1 if errors occurred. */
196 change_dir_mode (const char *dir, const struct mode_change *changes)
198 char *name_space, *namep;
199 char *path; /* Full path of each entry to process. */
200 unsigned dirlength; /* Length of DIR and '\0'. */
201 unsigned filelength; /* Length of each pathname to process. */
202 unsigned pathlength; /* Bytes allocated for `path'. */
205 name_space = savedir (dir);
206 if (name_space == NULL)
208 if (force_silent == 0)
209 error (0, errno, "%s", quote (dir));
213 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
214 pathlength = dirlength + 1;
215 /* Give `path' a dummy value; it will be reallocated before first use. */
216 path = xmalloc (pathlength);
218 path[dirlength - 1] = '/';
220 for (namep = name_space; *namep; namep += filelength - dirlength)
222 filelength = dirlength + strlen (namep) + 1;
223 if (filelength > pathlength)
225 pathlength = filelength * 2;
226 path = xrealloc (path, pathlength);
228 strcpy (path + dirlength, namep);
229 errors |= change_file_mode (path, changes, 0);
240 fprintf (stderr, _("Try `%s --help' for more information.\n"),
245 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
246 or: %s [OPTION]... OCTAL-MODE FILE...\n\
247 or: %s [OPTION]... --reference=RFILE FILE...\n\
249 program_name, program_name, program_name);
251 Change the mode of each FILE to MODE.\n\
253 -c, --changes like verbose but report only when a change is made\n\
254 -f, --silent, --quiet suppress most error messages\n\
255 -v, --verbose output a diagnostic for every file processed\n\
256 --reference=RFILE use RFILE's mode instead of MODE values\n\
257 -R, --recursive change files and directories recursively\n\
258 --help display this help and exit\n\
259 --version output version information and exit\n\
261 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
262 one or more of the letters rwxXstugo.\n\
264 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
269 /* Parse the ASCII mode given on the command line into a linked list
270 of `struct mode_change' and apply that to each file argument. */
273 main (int argc, char **argv)
275 struct mode_change *changes;
277 int modeind = 0; /* Index of the mode argument in `argv'. */
281 program_name = argv[0];
282 setlocale (LC_ALL, "");
283 bindtextdomain (PACKAGE, LOCALEDIR);
284 textdomain (PACKAGE);
286 atexit (close_stdout);
288 recurse = force_silent = 0;
292 thisind = optind ? optind : 1;
294 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
316 if (modeind != 0 && modeind != thisind)
318 static char char_string[2] = {0, 0};
320 error (1, 0, _("invalid character %s in mode string %s"),
321 quote_n (0, char_string), quote_n (1, argv[thisind]));
325 case REFERENCE_FILE_OPTION:
326 reference_file = optarg;
332 verbosity = V_changes_only;
340 case_GETOPT_HELP_CHAR;
341 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
347 if (modeind == 0 && reference_file == NULL)
352 error (0, 0, _("too few arguments"));
356 changes = (reference_file ? mode_create_from_ref (reference_file)
357 : mode_compile (argv[modeind], MODE_MASK_ALL));
359 if (changes == MODE_INVALID)
360 error (1, 0, _("invalid mode string: %s"), quote (argv[modeind]));
361 else if (changes == MODE_MEMORY_EXHAUSTED)
363 else if (changes == MODE_BAD_REFERENCE)
364 error (1, errno, _("getting attributes of %s"), quote (reference_file));
366 for (; optind < argc; ++optind)
368 strip_trailing_slashes (argv[optind]);
369 errors |= change_file_mode (argv[optind], changes, 1);