1 /* chmod -- change permission modes of files
2 Copyright (C) 1989, 1990, 1991, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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> */
29 #include <sys/types.h>
31 #include "modechange.h"
34 #include "safe-stat.h"
35 #include "safe-lstat.h"
40 void strip_trailing_slashes ();
44 static int change_file_mode ();
45 static int change_dir_mode ();
46 static void describe_change ();
49 /* The name the program was run with. */
52 /* If nonzero, change the modes of directories recursively. */
55 /* If nonzero, force silence (no error messages). */
56 static int force_silent;
58 /* If nonzero, describe the modes we set. */
61 /* If nonzero, describe only modes that change. */
62 static int changes_only;
64 /* If non-zero, display usage information and exit. */
67 /* If non-zero, print the version on standard output and exit. */
68 static int show_version;
70 static struct option const long_options[] =
72 {"recursive", no_argument, 0, 'R'},
73 {"changes", no_argument, 0, 'c'},
74 {"silent", no_argument, 0, 'f'},
75 {"quiet", no_argument, 0, 'f'},
76 {"verbose", no_argument, 0, 'v'},
77 {"help", no_argument, &show_help, 1},
78 {"version", no_argument, &show_version, 1},
82 /* Parse the ASCII mode given on the command line into a linked list
83 of `struct mode_change' and apply that to each file argument. */
90 struct mode_change *changes;
92 int modeind = 0; /* Index of the mode argument in `argv'. */
96 program_name = argv[0];
97 recurse = force_silent = verbose = changes_only = 0;
101 thisind = optind ? optind : 1;
103 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
126 if (modeind != 0 && modeind != thisind)
127 error (1, 0, "invalid mode");
150 printf ("%s\n", version_string);
162 error (0, 0, "too few arguments");
166 changes = mode_compile (argv[modeind],
167 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
168 if (changes == MODE_INVALID)
169 error (1, 0, "invalid mode");
170 else if (changes == MODE_MEMORY_EXHAUSTED)
171 error (1, 0, "virtual memory exhausted");
173 for (; optind < argc; ++optind)
175 strip_trailing_slashes (argv[optind]);
176 errors |= change_file_mode (argv[optind], changes, 1);
182 /* Change the mode of FILE according to the list of operations CHANGES.
183 If DEREF_SYMLINK is non-zero and FILE is a symbolic link, change the
184 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
185 links. Return 0 if successful, 1 if errors occurred. */
188 change_file_mode (file, changes, deref_symlink)
190 struct mode_change *changes;
193 struct stat file_stats;
194 unsigned short newmode;
197 if (safe_lstat (file, &file_stats))
199 if (force_silent == 0)
200 error (0, errno, "%s", file);
204 if (S_ISLNK (file_stats.st_mode))
209 if (safe_stat (file, &file_stats))
211 if (force_silent == 0)
212 error (0, errno, "%s", file);
218 newmode = mode_adjust (file_stats.st_mode, changes);
220 if (newmode != (file_stats.st_mode & 07777))
223 describe_change (file, newmode, 1);
224 if (chmod (file, (int) newmode))
226 if (force_silent == 0)
227 error (0, errno, "%s", file);
231 else if (verbose && changes_only == 0)
232 describe_change (file, newmode, 0);
234 if (recurse && S_ISDIR (file_stats.st_mode))
235 errors |= change_dir_mode (file, changes, &file_stats);
239 /* Recursively change the modes of the files in directory DIR
240 according to the list of operations CHANGES.
241 STATP points to the results of lstat on DIR.
242 Return 0 if successful, 1 if errors occurred. */
245 change_dir_mode (dir, changes, statp)
247 struct mode_change *changes;
250 char *name_space, *namep;
251 char *path; /* Full path of each entry to process. */
252 unsigned dirlength; /* Length of DIR and '\0'. */
253 unsigned filelength; /* Length of each pathname to process. */
254 unsigned pathlength; /* Bytes allocated for `path'. */
258 name_space = savedir (dir, statp->st_size);
259 if (name_space == NULL)
263 if (force_silent == 0)
264 error (0, errno, "%s", dir);
268 error (1, 0, "virtual memory exhausted");
271 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
272 pathlength = dirlength + 1;
273 /* Give `path' a dummy value; it will be reallocated before first use. */
274 path = xmalloc (pathlength);
276 path[dirlength - 1] = '/';
278 for (namep = name_space; *namep; namep += filelength - dirlength)
280 filelength = dirlength + strlen (namep) + 1;
281 if (filelength > pathlength)
283 pathlength = filelength * 2;
284 path = xrealloc (path, pathlength);
286 strcpy (path + dirlength, namep);
287 errors |= change_file_mode (path, changes, 0);
294 /* Tell the user the mode MODE that file FILE has been set to;
295 if CHANGED is zero, FILE had that mode already. */
298 describe_change (file, mode, changed)
303 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
305 mode_string (mode, perms);
306 perms[10] = '\0'; /* `mode_string' does not null terminate. */
308 printf ("mode of %s changed to %04o (%s)\n",
309 file, mode & 07777, &perms[1]);
311 printf ("mode of %s retained as %04o (%s)\n",
312 file, mode & 07777, &perms[1]);
320 fprintf (stderr, "Try `%s --help' for more information.\n",
325 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
326 or: %s [OPTION]... OCTAL_MODE FILE...\n\
328 program_name, program_name);
331 -c, --changes like verbose but report only when a change is made\n\
332 -f, --silent, --quiet suppress most error messages\n\
333 -v, --verbose output a diagnostic for every file processed\n\
334 -R, --recursive change files and directories recursively\n\
335 --help display this help and exit\n\
336 --version output version information and exit\n\
338 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
339 one or more of the letters rwxXstugo.\n");