.
[coreutils.git] / src / chmod.c
blob4ddf6c45e35f7a4261d3fa5c8bdf1102db6a73cf
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_file_mode ();
43 static int change_dir_mode ();
44 static void describe_change ();
45 static void usage ();
47 /* The name the program was run with. */
48 char *program_name;
50 /* If nonzero, change the modes of directories recursively. */
51 static int recurse;
53 /* If nonzero, force silence (no error messages). */
54 static int force_silent;
56 /* If nonzero, describe the modes we set. */
57 static int verbose;
59 /* If nonzero, describe only modes that change. */
60 static int changes_only;
62 /* If non-zero, display usage information and exit. */
63 static int show_help;
65 /* If non-zero, print the version on standard output and exit. */
66 static int show_version;
68 static struct option const long_options[] =
70 {"recursive", no_argument, 0, 'R'},
71 {"changes", no_argument, 0, 'c'},
72 {"silent", no_argument, 0, 'f'},
73 {"quiet", no_argument, 0, 'f'},
74 {"verbose", no_argument, 0, 'v'},
75 {"help", no_argument, &show_help, 1},
76 {"version", no_argument, &show_version, 1},
77 {0, 0, 0, 0}
80 /* Parse the ASCII mode given on the command line into a linked list
81 of `struct mode_change' and apply that to each file argument. */
83 void
84 main (argc, argv)
85 int argc;
86 char **argv;
88 struct mode_change *changes;
89 int errors = 0;
90 int modeind = 0; /* Index of the mode argument in `argv'. */
91 int thisind;
92 int c;
94 program_name = argv[0];
95 recurse = force_silent = verbose = changes_only = 0;
97 while (1)
99 thisind = optind ? optind : 1;
101 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
102 (int *) 0);
103 if (c == EOF)
104 break;
106 switch (c)
108 case 0:
109 break;
110 case 'r':
111 case 'w':
112 case 'x':
113 case 'X':
114 case 's':
115 case 't':
116 case 'u':
117 case 'g':
118 case 'o':
119 case 'a':
120 case ',':
121 case '+':
122 case '-':
123 case '=':
124 if (modeind != 0 && modeind != thisind)
125 error (1, 0, "invalid mode");
126 modeind = thisind;
127 break;
128 case 'R':
129 recurse = 1;
130 break;
131 case 'c':
132 verbose = 1;
133 changes_only = 1;
134 break;
135 case 'f':
136 force_silent = 1;
137 break;
138 case 'v':
139 verbose = 1;
140 break;
141 default:
142 usage (1);
146 if (show_version)
148 printf ("chmod - %s\n", version_string);
149 exit (0);
152 if (show_help)
153 usage (0);
155 if (modeind == 0)
156 modeind = optind++;
158 if (optind >= argc)
160 error (0, 0, "too few arguments");
161 usage (1);
164 changes = mode_compile (argv[modeind],
165 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
166 if (changes == MODE_INVALID)
167 error (1, 0, "invalid mode");
168 else if (changes == MODE_MEMORY_EXHAUSTED)
169 error (1, 0, "virtual memory exhausted");
171 for (; optind < argc; ++optind)
173 strip_trailing_slashes (argv[optind]);
174 errors |= change_file_mode (argv[optind], changes, 1);
177 exit (errors);
180 /* Change the mode of FILE according to the list of operations CHANGES.
181 If DEREF_SYMLINK is non-zero and FILE is a symbolic link, change the
182 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
183 links. Return 0 if successful, 1 if errors occurred. */
185 static int
186 change_file_mode (file, changes, deref_symlink)
187 char *file;
188 struct mode_change *changes;
189 int deref_symlink;
191 struct stat file_stats;
192 unsigned short newmode;
193 int errors = 0;
195 if (lstat (file, &file_stats))
197 if (force_silent == 0)
198 error (0, errno, "%s", file);
199 return 1;
201 #ifdef S_ISLNK
202 if (S_ISLNK (file_stats.st_mode))
204 if (! deref_symlink)
205 return 0;
206 else
207 if (stat (file, &file_stats))
209 if (force_silent == 0)
210 error (0, errno, "%s", file);
211 return 1;
214 #endif
216 newmode = mode_adjust (file_stats.st_mode, changes);
218 if (newmode != (file_stats.st_mode & 07777))
220 if (verbose)
221 describe_change (file, newmode, 1);
222 if (chmod (file, (int) newmode))
224 if (force_silent == 0)
225 error (0, errno, "%s", file);
226 errors = 1;
229 else if (verbose && changes_only == 0)
230 describe_change (file, newmode, 0);
232 if (recurse && S_ISDIR (file_stats.st_mode))
233 errors |= change_dir_mode (file, changes, &file_stats);
234 return errors;
237 /* Recursively change the modes of the files in directory DIR
238 according to the list of operations CHANGES.
239 STATP points to the results of lstat on DIR.
240 Return 0 if successful, 1 if errors occurred. */
242 static int
243 change_dir_mode (dir, changes, statp)
244 char *dir;
245 struct mode_change *changes;
246 struct stat *statp;
248 char *name_space, *namep;
249 char *path; /* Full path of each entry to process. */
250 unsigned dirlength; /* Length of DIR and '\0'. */
251 unsigned filelength; /* Length of each pathname to process. */
252 unsigned pathlength; /* Bytes allocated for `path'. */
253 int errors = 0;
255 errno = 0;
256 name_space = savedir (dir, statp->st_size);
257 if (name_space == NULL)
259 if (errno)
261 if (force_silent == 0)
262 error (0, errno, "%s", dir);
263 return 1;
265 else
266 error (1, 0, "virtual memory exhausted");
269 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
270 pathlength = dirlength + 1;
271 /* Give `path' a dummy value; it will be reallocated before first use. */
272 path = xmalloc (pathlength);
273 strcpy (path, dir);
274 path[dirlength - 1] = '/';
276 for (namep = name_space; *namep; namep += filelength - dirlength)
278 filelength = dirlength + strlen (namep) + 1;
279 if (filelength > pathlength)
281 pathlength = filelength * 2;
282 path = xrealloc (path, pathlength);
284 strcpy (path + dirlength, namep);
285 errors |= change_file_mode (path, changes, 0);
287 free (path);
288 free (name_space);
289 return errors;
292 /* Tell the user the mode MODE that file FILE has been set to;
293 if CHANGED is zero, FILE had that mode already. */
295 static void
296 describe_change (file, mode, changed)
297 char *file;
298 unsigned short mode;
299 int changed;
301 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
303 mode_string (mode, perms);
304 perms[10] = '\0'; /* `mode_string' does not null terminate. */
305 if (changed)
306 printf ("mode of %s changed to %04o (%s)\n",
307 file, mode & 07777, &perms[1]);
308 else
309 printf ("mode of %s retained as %04o (%s)\n",
310 file, mode & 07777, &perms[1]);
313 static void
314 usage (status)
315 int status;
317 if (status != 0)
318 fprintf (stderr, "Try `%s --help' for more information.\n",
319 program_name);
320 else
322 printf ("\
323 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
324 or: %s [OPTION]... OCTAL_MODE FILE...\n\
326 program_name, program_name);
327 printf ("\
329 -c, --changes like verbose but report only when a change is made\n\
330 -f, --silent, --quiet suppress most error messages\n\
331 -v, --verbose output a diagnostic for every file processed\n\
332 -R, --recursive change files and directories recursively\n\
333 --help display this help and exit\n\
334 --version output version information and exit\n\
336 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
337 one or more of the letters rwxXstugo.\n");
339 exit (status);