*** empty log message ***
[coreutils.git] / src / chmod.c
blob4486b53053033aa0ce957bda53d9cb7baeda1b4b
1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 1995-1999 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 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
25 #include "system.h"
26 #include "error.h"
27 #include "filemode.h"
28 #include "modechange.h"
29 #include "savedir.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "chmod"
34 #define AUTHORS "David MacKenzie"
36 enum Change_status
38 CH_SUCCEEDED,
39 CH_FAILED,
40 CH_NO_CHANGE_REQUESTED
43 enum Verbosity
45 /* Print a message for each file that is processed. */
46 V_high,
48 /* Print a message for each file whose attributes we change. */
49 V_changes_only,
51 /* Do not be verbose. This is the default. */
52 V_off
55 void strip_trailing_slashes ();
57 static int change_dir_mode PARAMS ((const char *dir,
58 const struct mode_change *changes,
59 const struct stat *statp));
61 /* The name the program was run with. */
62 char *program_name;
64 /* If nonzero, change the modes of directories recursively. */
65 static int recurse;
67 /* If nonzero, force silence (no error messages). */
68 static int force_silent;
70 /* Level of verbosity. */
71 static enum Verbosity verbosity = V_off;
73 /* The argument to the --reference option. Use the owner and group IDs
74 of this file. This file must exist. */
75 static char *reference_file;
77 static struct option const long_options[] =
79 {"recursive", no_argument, 0, 'R'},
80 {"changes", no_argument, 0, 'c'},
81 {"silent", no_argument, 0, 'f'},
82 {"quiet", no_argument, 0, 'f'},
83 {"reference", required_argument, 0, CHAR_MAX + 1},
84 {"verbose", no_argument, 0, 'v'},
85 {GETOPT_HELP_OPTION_DECL},
86 {GETOPT_VERSION_OPTION_DECL},
87 {0, 0, 0, 0}
90 /* Tell the user how/if the MODE of FILE has been changed.
91 CHANGED describes what (if anything) has happened. */
93 static void
94 describe_change (const char *file, mode_t mode,
95 enum Change_status changed)
97 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
98 const char *fmt;
100 mode_string (mode, perms);
101 perms[10] = '\0'; /* `mode_string' does not null terminate. */
102 switch (changed)
104 case CH_SUCCEEDED:
105 fmt = _("mode of %s changed to %04lo (%s)\n");
106 break;
107 case CH_FAILED:
108 fmt = _("failed to change mode of %s to %04lo (%s)\n");
109 break;
110 case CH_NO_CHANGE_REQUESTED:
111 fmt = _("mode of %s retained as %04lo (%s)\n");
112 break;
113 default:
114 abort ();
116 printf (fmt, file, (unsigned long) (mode & CHMOD_MODE_BITS), &perms[1]);
119 /* Change the mode of FILE according to the list of operations CHANGES.
120 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
121 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
122 links. Return 0 if successful, 1 if errors occurred. */
124 static int
125 change_file_mode (const char *file, const struct mode_change *changes,
126 const int deref_symlink)
128 struct stat file_stats;
129 mode_t newmode;
130 int errors = 0;
132 if (lstat (file, &file_stats))
134 if (force_silent == 0)
135 error (0, errno, "%s", file);
136 return 1;
138 #ifdef S_ISLNK
139 if (S_ISLNK (file_stats.st_mode))
141 if (! deref_symlink)
142 return 0;
143 else
144 if (stat (file, &file_stats))
146 if (force_silent == 0)
147 error (0, errno, "%s", file);
148 return 1;
151 #endif
153 newmode = mode_adjust (file_stats.st_mode, changes);
155 if (newmode != (file_stats.st_mode & CHMOD_MODE_BITS))
157 int fail = chmod (file, newmode);
159 if (verbosity == V_high || (verbosity == V_changes_only && !fail))
160 describe_change (file, newmode, (fail ? CH_FAILED : CH_SUCCEEDED));
162 if (fail)
164 if (force_silent == 0)
165 error (0, errno, "%s", file);
166 errors = 1;
169 else if (verbosity == V_high)
170 describe_change (file, newmode, CH_NO_CHANGE_REQUESTED);
172 if (recurse && S_ISDIR (file_stats.st_mode))
173 errors |= change_dir_mode (file, changes, &file_stats);
174 return errors;
177 /* Recursively change the modes of the files in directory DIR
178 according to the list of operations CHANGES.
179 STATP points to the results of lstat on DIR.
180 Return 0 if successful, 1 if errors occurred. */
182 static int
183 change_dir_mode (const char *dir, const struct mode_change *changes,
184 const struct stat *statp)
186 char *name_space, *namep;
187 char *path; /* Full path of each entry to process. */
188 unsigned dirlength; /* Length of DIR and '\0'. */
189 unsigned filelength; /* Length of each pathname to process. */
190 unsigned pathlength; /* Bytes allocated for `path'. */
191 int errors = 0;
193 errno = 0;
194 name_space = savedir (dir, (unsigned int) statp->st_size);
195 if (name_space == NULL)
197 if (errno)
199 if (force_silent == 0)
200 error (0, errno, "%s", dir);
201 return 1;
203 else
204 error (1, 0, _("virtual memory exhausted"));
207 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
208 pathlength = dirlength + 1;
209 /* Give `path' a dummy value; it will be reallocated before first use. */
210 path = xmalloc (pathlength);
211 strcpy (path, dir);
212 path[dirlength - 1] = '/';
214 for (namep = name_space; *namep; namep += filelength - dirlength)
216 filelength = dirlength + strlen (namep) + 1;
217 if (filelength > pathlength)
219 pathlength = filelength * 2;
220 path = xrealloc (path, pathlength);
222 strcpy (path + dirlength, namep);
223 errors |= change_file_mode (path, changes, 0);
225 free (path);
226 free (name_space);
227 return errors;
230 void
231 usage (int status)
233 if (status != 0)
234 fprintf (stderr, _("Try `%s --help' for more information.\n"),
235 program_name);
236 else
238 printf (_("\
239 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
240 or: %s [OPTION]... OCTAL-MODE FILE...\n\
241 or: %s [OPTION]... --reference=RFILE FILE...\n\
243 program_name, program_name, program_name);
244 printf (_("\
245 Change the mode of each FILE to MODE.\n\
247 -c, --changes like verbose but report only when a change is made\n\
248 -f, --silent, --quiet suppress most error messages\n\
249 -v, --verbose output a diagnostic for every file processed\n\
250 --reference=RFILE use RFILE's mode instead of MODE values\n\
251 -R, --recursive change files and directories recursively\n\
252 --help display this help and exit\n\
253 --version output version information and exit\n\
255 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
256 one or more of the letters rwxXstugo.\n\
257 "));
258 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
259 close_stdout ();
261 exit (status);
264 /* Parse the ASCII mode given on the command line into a linked list
265 of `struct mode_change' and apply that to each file argument. */
268 main (int argc, char **argv)
270 struct mode_change *changes;
271 int errors = 0;
272 int modeind = 0; /* Index of the mode argument in `argv'. */
273 int thisind;
274 int c;
276 program_name = argv[0];
277 setlocale (LC_ALL, "");
278 bindtextdomain (PACKAGE, LOCALEDIR);
279 textdomain (PACKAGE);
281 recurse = force_silent = 0;
283 while (1)
285 thisind = optind ? optind : 1;
287 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
288 if (c == -1)
289 break;
291 switch (c)
293 case 0:
294 break;
295 case 'r':
296 case 'w':
297 case 'x':
298 case 'X':
299 case 's':
300 case 't':
301 case 'u':
302 case 'g':
303 case 'o':
304 case 'a':
305 case ',':
306 case '+':
307 case '-':
308 case '=':
309 if (modeind != 0 && modeind != thisind)
310 error (1, 0, _("invalid mode"));
311 modeind = thisind;
312 break;
313 case CHAR_MAX + 1:
314 reference_file = optarg;
315 break;
316 case 'R':
317 recurse = 1;
318 break;
319 case 'c':
320 verbosity = V_changes_only;
321 break;
322 case 'f':
323 force_silent = 1;
324 break;
325 case 'v':
326 verbosity = V_high;
327 break;
328 case_GETOPT_HELP_CHAR;
329 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
330 default:
331 usage (1);
335 if (modeind == 0 && reference_file == NULL)
336 modeind = optind++;
338 if (optind >= argc)
340 error (0, 0, _("too few arguments"));
341 usage (1);
344 changes = (reference_file ? mode_create_from_ref (reference_file)
345 : mode_compile (argv[modeind], MODE_MASK_ALL));
347 if (changes == MODE_INVALID)
348 error (1, 0, _("invalid mode"));
349 else if (changes == MODE_MEMORY_EXHAUSTED)
350 error (1, 0, _("virtual memory exhausted"));
351 else if (changes == MODE_BAD_REFERENCE)
352 error (1, errno, "%s", reference_file);
354 for (; optind < argc; ++optind)
356 strip_trailing_slashes (argv[optind]);
357 errors |= change_file_mode (argv[optind], changes, 1);
360 if (verbosity != V_off)
361 close_stdout ();
362 exit (errors);