.
[coreutils.git] / src / chmod.c
blob29510712c73277372b4343a580507bea250b5ae1
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)
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 "version.h"
34 #include "error.h"
36 void mode_string ();
37 char *savedir ();
38 void strip_trailing_slashes ();
39 char *xmalloc ();
40 char *xrealloc ();
42 static int change_dir_mode __P ((char *dir, struct mode_change *changes,
43 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 (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 (char *file, struct mode_change *changes, int deref_symlink)
104 struct stat file_stats;
105 unsigned short newmode;
106 int errors = 0;
108 if (lstat (file, &file_stats))
110 if (force_silent == 0)
111 error (0, errno, "%s", file);
112 return 1;
114 #ifdef S_ISLNK
115 if (S_ISLNK (file_stats.st_mode))
117 if (! deref_symlink)
118 return 0;
119 else
120 if (stat (file, &file_stats))
122 if (force_silent == 0)
123 error (0, errno, "%s", file);
124 return 1;
127 #endif
129 newmode = mode_adjust (file_stats.st_mode, changes);
131 if (newmode != (file_stats.st_mode & 07777))
133 if (verbose)
134 describe_change (file, newmode, 1);
135 if (chmod (file, (int) newmode))
137 if (force_silent == 0)
138 error (0, errno, "%s", file);
139 errors = 1;
142 else if (verbose && changes_only == 0)
143 describe_change (file, newmode, 0);
145 if (recurse && S_ISDIR (file_stats.st_mode))
146 errors |= change_dir_mode (file, changes, &file_stats);
147 return errors;
150 /* Recursively change the modes of the files in directory DIR
151 according to the list of operations CHANGES.
152 STATP points to the results of lstat on DIR.
153 Return 0 if successful, 1 if errors occurred. */
155 static int
156 change_dir_mode (char *dir, struct mode_change *changes, struct stat *statp)
158 char *name_space, *namep;
159 char *path; /* Full path of each entry to process. */
160 unsigned dirlength; /* Length of DIR and '\0'. */
161 unsigned filelength; /* Length of each pathname to process. */
162 unsigned pathlength; /* Bytes allocated for `path'. */
163 int errors = 0;
165 errno = 0;
166 name_space = savedir (dir, statp->st_size);
167 if (name_space == NULL)
169 if (errno)
171 if (force_silent == 0)
172 error (0, errno, "%s", dir);
173 return 1;
175 else
176 error (1, 0, _("virtual memory exhausted"));
179 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
180 pathlength = dirlength + 1;
181 /* Give `path' a dummy value; it will be reallocated before first use. */
182 path = xmalloc (pathlength);
183 strcpy (path, dir);
184 path[dirlength - 1] = '/';
186 for (namep = name_space; *namep; namep += filelength - dirlength)
188 filelength = dirlength + strlen (namep) + 1;
189 if (filelength > pathlength)
191 pathlength = filelength * 2;
192 path = xrealloc (path, pathlength);
194 strcpy (path + dirlength, namep);
195 errors |= change_file_mode (path, changes, 0);
197 free (path);
198 free (name_space);
199 return errors;
202 static void
203 usage (int status)
205 if (status != 0)
206 fprintf (stderr, _("Try `%s --help' for more information.\n"),
207 program_name);
208 else
210 printf (_("\
211 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
212 or: %s [OPTION]... OCTAL_MODE FILE...\n\
214 program_name, program_name);
215 printf (_("\
217 -c, --changes like verbose but report only when a change is made\n\
218 -f, --silent, --quiet suppress most error messages\n\
219 -v, --verbose output a diagnostic for every file processed\n\
220 -R, --recursive change files and directories recursively\n\
221 --help display this help and exit\n\
222 --version output version information and exit\n\
224 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
225 one or more of the letters rwxXstugo.\n"));
227 exit (status);
230 /* Parse the ASCII mode given on the command line into a linked list
231 of `struct mode_change' and apply that to each file argument. */
233 void
234 main (int argc, char **argv)
236 struct mode_change *changes;
237 int errors = 0;
238 int modeind = 0; /* Index of the mode argument in `argv'. */
239 int thisind;
240 int c;
242 program_name = argv[0];
243 setlocale (LC_ALL, "");
244 bindtextdomain (PACKAGE, LOCALEDIR);
245 textdomain (PACKAGE);
247 recurse = force_silent = verbose = changes_only = 0;
249 while (1)
251 thisind = optind ? optind : 1;
253 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
254 (int *) 0);
255 if (c == EOF)
256 break;
258 switch (c)
260 case 0:
261 break;
262 case 'r':
263 case 'w':
264 case 'x':
265 case 'X':
266 case 's':
267 case 't':
268 case 'u':
269 case 'g':
270 case 'o':
271 case 'a':
272 case ',':
273 case '+':
274 case '-':
275 case '=':
276 if (modeind != 0 && modeind != thisind)
277 error (1, 0, _("invalid mode"));
278 modeind = thisind;
279 break;
280 case 'R':
281 recurse = 1;
282 break;
283 case 'c':
284 verbose = 1;
285 changes_only = 1;
286 break;
287 case 'f':
288 force_silent = 1;
289 break;
290 case 'v':
291 verbose = 1;
292 break;
293 default:
294 usage (1);
298 if (show_version)
300 printf ("chmod - %s\n", version_string);
301 exit (0);
304 if (show_help)
305 usage (0);
307 if (modeind == 0)
308 modeind = optind++;
310 if (optind >= argc)
312 error (0, 0, _("too few arguments"));
313 usage (1);
316 changes = mode_compile (argv[modeind],
317 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
318 if (changes == MODE_INVALID)
319 error (1, 0, _("invalid mode"));
320 else if (changes == MODE_MEMORY_EXHAUSTED)
321 error (1, 0, _("virtual memory exhausted"));
323 for (; optind < argc; ++optind)
325 strip_trailing_slashes (argv[optind]);
326 errors |= change_file_mode (argv[optind], changes, 1);
329 exit (errors);