(main): Don't set verbose flag for --changes.
[coreutils.git] / src / chmod.c
blob63a54e3b06cc0230bdec2b639b914b1744479458
1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 95, 1996 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 /* Options:
19 -R Recursively change modes of directory contents.
20 -c Verbosely describe only files whose modes actually change.
21 -f Do not print error messages about files.
22 -v Verbosely describe changed modes.
24 David MacKenzie <djm@gnu.ai.mit.edu> */
26 #include <config.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <sys/types.h>
31 #include "modechange.h"
32 #include "system.h"
33 #include "error.h"
35 void mode_string ();
36 char *savedir ();
37 void strip_trailing_slashes ();
38 char *xmalloc ();
39 char *xrealloc ();
41 static int change_dir_mode __P ((const char *dir,
42 const struct mode_change *changes,
43 const struct stat *statp));
45 /* The name the program was run with. */
46 char *program_name;
48 /* If nonzero, change the modes of directories recursively. */
49 static int recurse;
51 /* If nonzero, force silence (no error messages). */
52 static int force_silent;
54 /* If nonzero, describe the modes we set. */
55 static int verbose;
57 /* If nonzero, describe only modes that change. */
58 static int changes_only;
60 /* If nonzero, display usage information and exit. */
61 static int show_help;
63 /* If nonzero, print the version on standard output and exit. */
64 static int show_version;
66 static struct option const long_options[] =
68 {"recursive", no_argument, 0, 'R'},
69 {"changes", no_argument, 0, 'c'},
70 {"silent", no_argument, 0, 'f'},
71 {"quiet", no_argument, 0, 'f'},
72 {"verbose", no_argument, 0, 'v'},
73 {"help", no_argument, &show_help, 1},
74 {"version", no_argument, &show_version, 1},
75 {0, 0, 0, 0}
78 /* Tell the user the mode MODE that file FILE has been set to;
79 if CHANGED is zero, FILE had that mode already. */
81 static void
82 describe_change (const char *file, short unsigned int mode, int changed)
84 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
86 mode_string (mode, perms);
87 perms[10] = '\0'; /* `mode_string' does not null terminate. */
88 if (changed)
89 printf (_("mode of %s changed to %04o (%s)\n"),
90 file, mode & 07777, &perms[1]);
91 else
92 printf (_("mode of %s retained as %04o (%s)\n"),
93 file, mode & 07777, &perms[1]);
96 /* Change the mode of FILE according to the list of operations CHANGES.
97 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
98 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
99 links. Return 0 if successful, 1 if errors occurred. */
101 static int
102 change_file_mode (const char *file, const struct mode_change *changes,
103 const int deref_symlink)
105 struct stat file_stats;
106 unsigned short newmode;
107 int errors = 0;
109 if (lstat (file, &file_stats))
111 if (force_silent == 0)
112 error (0, errno, "%s", file);
113 return 1;
115 #ifdef S_ISLNK
116 if (S_ISLNK (file_stats.st_mode))
118 if (! deref_symlink)
119 return 0;
120 else
121 if (stat (file, &file_stats))
123 if (force_silent == 0)
124 error (0, errno, "%s", file);
125 return 1;
128 #endif
130 newmode = mode_adjust (file_stats.st_mode, changes);
132 if (newmode != (file_stats.st_mode & 07777))
134 if (verbose)
135 describe_change (file, newmode, 1);
136 if (chmod (file, (int) newmode) == 0)
138 if (changes_only)
139 describe_change (file, newmode, 1);
141 else
143 if (force_silent == 0)
144 error (0, errno, "%s", file);
145 errors = 1;
148 else if (verbose && changes_only == 0)
149 describe_change (file, newmode, 0);
151 if (recurse && S_ISDIR (file_stats.st_mode))
152 errors |= change_dir_mode (file, changes, &file_stats);
153 return errors;
156 /* Recursively change the modes of the files in directory DIR
157 according to the list of operations CHANGES.
158 STATP points to the results of lstat on DIR.
159 Return 0 if successful, 1 if errors occurred. */
161 static int
162 change_dir_mode (const char *dir, const struct mode_change *changes,
163 const struct stat *statp)
165 char *name_space, *namep;
166 char *path; /* Full path of each entry to process. */
167 unsigned dirlength; /* Length of DIR and '\0'. */
168 unsigned filelength; /* Length of each pathname to process. */
169 unsigned pathlength; /* Bytes allocated for `path'. */
170 int errors = 0;
172 errno = 0;
173 name_space = savedir (dir, statp->st_size);
174 if (name_space == NULL)
176 if (errno)
178 if (force_silent == 0)
179 error (0, errno, "%s", dir);
180 return 1;
182 else
183 error (1, 0, _("virtual memory exhausted"));
186 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
187 pathlength = dirlength + 1;
188 /* Give `path' a dummy value; it will be reallocated before first use. */
189 path = xmalloc (pathlength);
190 strcpy (path, dir);
191 path[dirlength - 1] = '/';
193 for (namep = name_space; *namep; namep += filelength - dirlength)
195 filelength = dirlength + strlen (namep) + 1;
196 if (filelength > pathlength)
198 pathlength = filelength * 2;
199 path = xrealloc (path, pathlength);
201 strcpy (path + dirlength, namep);
202 errors |= change_file_mode (path, changes, 0);
204 free (path);
205 free (name_space);
206 return errors;
209 static void
210 usage (int status)
212 if (status != 0)
213 fprintf (stderr, _("Try `%s --help' for more information.\n"),
214 program_name);
215 else
217 printf (_("\
218 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
219 or: %s [OPTION]... OCTAL_MODE FILE...\n\
221 program_name, program_name);
222 printf (_("\
224 -c, --changes like verbose but report only when a change is made\n\
225 -f, --silent, --quiet suppress most error messages\n\
226 -v, --verbose output a diagnostic for every file processed\n\
227 -R, --recursive change files and directories recursively\n\
228 --help display this help and exit\n\
229 --version output version information and exit\n\
231 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
232 one or more of the letters rwxXstugo.\n\
233 "));
234 puts (_("\nReport bugs to bug-gnu-utils@gnu.ai.mit.edu"));
236 exit (status);
239 /* Parse the ASCII mode given on the command line into a linked list
240 of `struct mode_change' and apply that to each file argument. */
243 main (int argc, char **argv)
245 struct mode_change *changes;
246 int errors = 0;
247 int modeind = 0; /* Index of the mode argument in `argv'. */
248 int thisind;
249 int c;
251 program_name = argv[0];
252 setlocale (LC_ALL, "");
253 bindtextdomain (PACKAGE, LOCALEDIR);
254 textdomain (PACKAGE);
256 recurse = force_silent = verbose = changes_only = 0;
258 while (1)
260 thisind = optind ? optind : 1;
262 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
263 (int *) 0);
264 if (c == EOF)
265 break;
267 switch (c)
269 case 0:
270 break;
271 case 'r':
272 case 'w':
273 case 'x':
274 case 'X':
275 case 's':
276 case 't':
277 case 'u':
278 case 'g':
279 case 'o':
280 case 'a':
281 case ',':
282 case '+':
283 case '-':
284 case '=':
285 if (modeind != 0 && modeind != thisind)
286 error (1, 0, _("invalid mode"));
287 modeind = thisind;
288 break;
289 case 'R':
290 recurse = 1;
291 break;
292 case 'c':
293 changes_only = 1;
294 break;
295 case 'f':
296 force_silent = 1;
297 break;
298 case 'v':
299 verbose = 1;
300 break;
301 default:
302 usage (1);
306 if (show_version)
308 printf ("chmod - %s\n", PACKAGE_VERSION);
309 exit (0);
312 if (show_help)
313 usage (0);
315 if (modeind == 0)
316 modeind = optind++;
318 if (optind >= argc)
320 error (0, 0, _("too few arguments"));
321 usage (1);
324 changes = mode_compile (argv[modeind],
325 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
326 if (changes == MODE_INVALID)
327 error (1, 0, _("invalid mode"));
328 else if (changes == MODE_MEMORY_EXHAUSTED)
329 error (1, 0, _("virtual memory exhausted"));
331 for (; optind < argc; ++optind)
333 strip_trailing_slashes (argv[optind]);
334 errors |= change_file_mode (argv[optind], changes, 1);
337 exit (errors);