1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 95, 96, 1997 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>
25 #include "modechange.h"
34 CH_NO_CHANGE_REQUESTED
38 void strip_trailing_slashes ();
42 static int change_dir_mode __P ((const char *dir,
43 const struct mode_change *changes,
44 const struct stat *statp));
46 /* The name the program was run with. */
49 /* If nonzero, change the modes of directories recursively. */
52 /* If nonzero, force silence (no error messages). */
53 static int force_silent;
55 /* If nonzero, describe the modes we set. */
58 /* The argument to the --reference option. Use the owner and group IDs
59 of this file. This file must exist. */
60 static char *reference_file;
62 /* If nonzero, describe only modes that change. */
63 static int changes_only;
65 /* If nonzero, display usage information and exit. */
68 /* If nonzero, print the version on standard output and exit. */
69 static int show_version;
71 static struct option const long_options[] =
73 {"recursive", no_argument, 0, 'R'},
74 {"changes", no_argument, 0, 'c'},
75 {"silent", no_argument, 0, 'f'},
76 {"quiet", no_argument, 0, 'f'},
77 {"reference", required_argument, 0, 12},
78 {"verbose", no_argument, 0, 'v'},
79 {"help", no_argument, &show_help, 1},
80 {"version", no_argument, &show_version, 1},
84 /* Tell the user how/if the MODE of FILE has been changed.
85 CHANGED describes what (if anything) has happened. */
88 describe_change (const char *file, short unsigned int mode,
89 enum Change_status changed)
91 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
94 mode_string (mode, perms);
95 perms[10] = '\0'; /* `mode_string' does not null terminate. */
99 fmt = _("mode of %s changed to %04o (%s)\n");
102 fmt = _("failed to change mode of %s to %04o (%s)\n");
104 case CH_NO_CHANGE_REQUESTED:
105 fmt = _("mode of %s retained as %04o (%s)\n");
110 printf (fmt, file, mode & 07777, &perms[1]);
113 /* Change the mode of FILE according to the list of operations CHANGES.
114 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
115 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
116 links. Return 0 if successful, 1 if errors occurred. */
119 change_file_mode (const char *file, const struct mode_change *changes,
120 const int deref_symlink)
122 struct stat file_stats;
123 unsigned short newmode;
126 if (lstat (file, &file_stats))
128 if (force_silent == 0)
129 error (0, errno, "%s", file);
133 if (S_ISLNK (file_stats.st_mode))
138 if (stat (file, &file_stats))
140 if (force_silent == 0)
141 error (0, errno, "%s", file);
147 newmode = mode_adjust (file_stats.st_mode, changes);
149 if (newmode != (file_stats.st_mode & 07777))
151 int fail = chmod (file, (int) newmode);
153 if (verbose || (changes_only && !fail))
154 describe_change (file, newmode, (fail ? CH_FAILED : CH_SUCCEEDED));
158 if (force_silent == 0)
159 error (0, errno, "%s", file);
163 else if (verbose && changes_only == 0)
164 describe_change (file, newmode, CH_NO_CHANGE_REQUESTED);
166 if (recurse && S_ISDIR (file_stats.st_mode))
167 errors |= change_dir_mode (file, changes, &file_stats);
171 /* Recursively change the modes of the files in directory DIR
172 according to the list of operations CHANGES.
173 STATP points to the results of lstat on DIR.
174 Return 0 if successful, 1 if errors occurred. */
177 change_dir_mode (const char *dir, const struct mode_change *changes,
178 const struct stat *statp)
180 char *name_space, *namep;
181 char *path; /* Full path of each entry to process. */
182 unsigned dirlength; /* Length of DIR and '\0'. */
183 unsigned filelength; /* Length of each pathname to process. */
184 unsigned pathlength; /* Bytes allocated for `path'. */
188 name_space = savedir (dir, statp->st_size);
189 if (name_space == NULL)
193 if (force_silent == 0)
194 error (0, errno, "%s", dir);
198 error (1, 0, _("virtual memory exhausted"));
201 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
202 pathlength = dirlength + 1;
203 /* Give `path' a dummy value; it will be reallocated before first use. */
204 path = xmalloc (pathlength);
206 path[dirlength - 1] = '/';
208 for (namep = name_space; *namep; namep += filelength - dirlength)
210 filelength = dirlength + strlen (namep) + 1;
211 if (filelength > pathlength)
213 pathlength = filelength * 2;
214 path = xrealloc (path, pathlength);
216 strcpy (path + dirlength, namep);
217 errors |= change_file_mode (path, changes, 0);
228 fprintf (stderr, _("Try `%s --help' for more information.\n"),
233 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
234 or: %s [OPTION]... OCTAL_MODE FILE...\n\
235 or: %s [OPTION]... --reference=RFILE FILE...\n\
237 program_name, program_name, program_name);
240 -c, --changes like verbose but report only when a change is made\n\
241 -f, --silent, --quiet suppress most error messages\n\
242 -v, --verbose output a diagnostic for every file processed\n\
243 --reference=RFILE use RFILE's mode instead of MODE values\n\
244 -R, --recursive change files and directories recursively\n\
245 --help display this help and exit\n\
246 --version output version information and exit\n\
248 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
249 one or more of the letters rwxXstugo.\n\
251 puts (_("\nReport bugs to <fileutils-bugs@gnu.ai.mit.edu>."));
256 /* Parse the ASCII mode given on the command line into a linked list
257 of `struct mode_change' and apply that to each file argument. */
260 main (int argc, char **argv)
262 struct mode_change *changes;
264 int modeind = 0; /* Index of the mode argument in `argv'. */
268 program_name = argv[0];
269 setlocale (LC_ALL, "");
270 bindtextdomain (PACKAGE, LOCALEDIR);
271 textdomain (PACKAGE);
273 recurse = force_silent = verbose = changes_only = 0;
277 thisind = optind ? optind : 1;
279 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
301 if (modeind != 0 && modeind != thisind)
302 error (1, 0, _("invalid mode"));
306 reference_file = optarg;
327 printf ("chmod (%s) %s\n", GNU_PACKAGE, VERSION);
334 if (modeind == 0 && reference_file == NULL)
339 error (0, 0, _("too few arguments"));
343 changes = (reference_file ? mode_create_from_ref (reference_file)
344 : mode_compile (argv[modeind], MODE_MASK_ALL));
346 if (changes == MODE_INVALID)
347 error (1, 0, _("invalid mode"));
348 else if (changes == MODE_MEMORY_EXHAUSTED)
349 error (1, 0, _("virtual memory exhausted"));
350 else if (changes == MODE_BAD_REFERENCE)
351 error (1, errno, "%s", reference_file);
353 for (; optind < argc; ++optind)
355 strip_trailing_slashes (argv[optind]);
356 errors |= change_file_mode (argv[optind], changes, 1);