1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 1995-1999 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 "long-options.h"
30 #include "modechange.h"
37 CH_NO_CHANGE_REQUESTED
42 /* Print a message for each file that is processed. */
45 /* Print a message for each file whose attributes we change. */
48 /* Do not be verbose. This is the default. */
52 void strip_trailing_slashes ();
54 static int change_dir_mode PARAMS ((const char *dir,
55 const struct mode_change *changes,
56 const struct stat *statp));
58 /* The name the program was run with. */
61 /* If nonzero, change the modes of directories recursively. */
64 /* If nonzero, force silence (no error messages). */
65 static int force_silent;
67 /* Level of verbosity. */
68 static enum Verbosity verbosity = V_off;
70 /* The argument to the --reference option. Use the owner and group IDs
71 of this file. This file must exist. */
72 static char *reference_file;
74 static struct option const long_options[] =
76 {"recursive", no_argument, 0, 'R'},
77 {"changes", no_argument, 0, 'c'},
78 {"silent", no_argument, 0, 'f'},
79 {"quiet", no_argument, 0, 'f'},
80 {"reference", required_argument, 0, CHAR_MAX + 1},
81 {"verbose", no_argument, 0, 'v'},
85 /* Tell the user how/if the MODE of FILE has been changed.
86 CHANGED describes what (if anything) has happened. */
89 describe_change (const char *file, short unsigned int mode,
90 enum Change_status changed)
92 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
95 mode_string (mode, perms);
96 perms[10] = '\0'; /* `mode_string' does not null terminate. */
100 fmt = _("mode of %s changed to %04o (%s)\n");
103 fmt = _("failed to change mode of %s to %04o (%s)\n");
105 case CH_NO_CHANGE_REQUESTED:
106 fmt = _("mode of %s retained as %04o (%s)\n");
111 printf (fmt, file, mode & 07777, &perms[1]);
114 /* Change the mode of FILE according to the list of operations CHANGES.
115 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
116 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
117 links. Return 0 if successful, 1 if errors occurred. */
120 change_file_mode (const char *file, const struct mode_change *changes,
121 const int deref_symlink)
123 struct stat file_stats;
124 unsigned short newmode;
127 if (lstat (file, &file_stats))
129 if (force_silent == 0)
130 error (0, errno, "%s", file);
134 if (S_ISLNK (file_stats.st_mode))
139 if (stat (file, &file_stats))
141 if (force_silent == 0)
142 error (0, errno, "%s", file);
148 newmode = mode_adjust (file_stats.st_mode, changes);
150 if (newmode != (file_stats.st_mode & 07777))
152 int fail = chmod (file, (int) newmode);
154 if (verbosity == V_high || (verbosity == V_changes_only && !fail))
155 describe_change (file, newmode, (fail ? CH_FAILED : CH_SUCCEEDED));
159 if (force_silent == 0)
160 error (0, errno, "%s", file);
164 else if (verbosity == V_high)
165 describe_change (file, newmode, CH_NO_CHANGE_REQUESTED);
167 if (recurse && S_ISDIR (file_stats.st_mode))
168 errors |= change_dir_mode (file, changes, &file_stats);
172 /* Recursively change the modes of the files in directory DIR
173 according to the list of operations CHANGES.
174 STATP points to the results of lstat on DIR.
175 Return 0 if successful, 1 if errors occurred. */
178 change_dir_mode (const char *dir, const struct mode_change *changes,
179 const struct stat *statp)
181 char *name_space, *namep;
182 char *path; /* Full path of each entry to process. */
183 unsigned dirlength; /* Length of DIR and '\0'. */
184 unsigned filelength; /* Length of each pathname to process. */
185 unsigned pathlength; /* Bytes allocated for `path'. */
189 name_space = savedir (dir, (unsigned int) statp->st_size);
190 if (name_space == NULL)
194 if (force_silent == 0)
195 error (0, errno, "%s", dir);
199 error (1, 0, _("virtual memory exhausted"));
202 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
203 pathlength = dirlength + 1;
204 /* Give `path' a dummy value; it will be reallocated before first use. */
205 path = xmalloc (pathlength);
207 path[dirlength - 1] = '/';
209 for (namep = name_space; *namep; namep += filelength - dirlength)
211 filelength = dirlength + strlen (namep) + 1;
212 if (filelength > pathlength)
214 pathlength = filelength * 2;
215 path = xrealloc (path, pathlength);
217 strcpy (path + dirlength, namep);
218 errors |= change_file_mode (path, changes, 0);
229 fprintf (stderr, _("Try `%s --help' for more information.\n"),
234 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
235 or: %s [OPTION]... OCTAL-MODE FILE...\n\
236 or: %s [OPTION]... --reference=RFILE FILE...\n\
238 program_name, program_name, program_name);
240 Change the mode of each FILE to MODE.\n\
242 -c, --changes like verbose but report only when a change is made\n\
243 -f, --silent, --quiet suppress most error messages\n\
244 -v, --verbose output a diagnostic for every file processed\n\
245 --reference=RFILE use RFILE's mode instead of MODE values\n\
246 -R, --recursive change files and directories recursively\n\
247 --help display this help and exit\n\
248 --version output version information and exit\n\
250 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
251 one or more of the letters rwxXstugo.\n\
253 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
259 /* Parse the ASCII mode given on the command line into a linked list
260 of `struct mode_change' and apply that to each file argument. */
263 main (int argc, char **argv)
265 struct mode_change *changes;
267 int modeind = 0; /* Index of the mode argument in `argv'. */
271 program_name = argv[0];
272 setlocale (LC_ALL, "");
273 bindtextdomain (PACKAGE, LOCALEDIR);
274 textdomain (PACKAGE);
276 parse_long_options (argc, argv, "chmod", GNU_PACKAGE, VERSION,
277 "David MacKenzie", usage);
279 recurse = force_silent = 0;
283 thisind = optind ? optind : 1;
285 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
307 if (modeind != 0 && modeind != thisind)
308 error (1, 0, _("invalid mode"));
312 reference_file = optarg;
318 verbosity = V_changes_only;
331 if (modeind == 0 && reference_file == NULL)
336 error (0, 0, _("too few arguments"));
340 changes = (reference_file ? mode_create_from_ref (reference_file)
341 : mode_compile (argv[modeind], MODE_MASK_ALL));
343 if (changes == MODE_INVALID)
344 error (1, 0, _("invalid mode"));
345 else if (changes == MODE_MEMORY_EXHAUSTED)
346 error (1, 0, _("virtual memory exhausted"));
347 else if (changes == MODE_BAD_REFERENCE)
348 error (1, errno, "%s", reference_file);
350 for (; optind < argc; ++optind)
352 strip_trailing_slashes (argv[optind]);
353 errors |= change_file_mode (argv[optind], changes, 1);
356 if (verbosity != V_off)