*** empty log message ***
[coreutils.git] / src / chmod.c
blob852fe47e6327eef763e54e0981af90eb649bd29b
1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 1995-2001 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 "quote.h"
30 #include "savedir.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "chmod"
35 #define AUTHORS "David MacKenzie"
37 enum Change_status
39 CH_SUCCEEDED,
40 CH_FAILED,
41 CH_NO_CHANGE_REQUESTED
44 enum Verbosity
46 /* Print a message for each file that is processed. */
47 V_high,
49 /* Print a message for each file whose attributes we change. */
50 V_changes_only,
52 /* Do not be verbose. This is the default. */
53 V_off
56 static int change_dir_mode PARAMS ((const char *dir,
57 const struct mode_change *changes));
59 /* The name the program was run with. */
60 char *program_name;
62 /* If nonzero, change the modes of directories recursively. */
63 static int recurse;
65 /* If nonzero, force silence (no error messages). */
66 static int force_silent;
68 /* Level of verbosity. */
69 static enum Verbosity verbosity = V_off;
71 /* The argument to the --reference option. Use the owner and group IDs
72 of this file. This file must exist. */
73 static char *reference_file;
75 /* For long options that have no equivalent short option, use a
76 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
77 enum
79 REFERENCE_FILE_OPTION = CHAR_MAX + 1
82 static struct option const long_options[] =
84 {"recursive", no_argument, 0, 'R'},
85 {"changes", no_argument, 0, 'c'},
86 {"silent", no_argument, 0, 'f'},
87 {"quiet", no_argument, 0, 'f'},
88 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
89 {"verbose", no_argument, 0, 'v'},
90 {GETOPT_HELP_OPTION_DECL},
91 {GETOPT_VERSION_OPTION_DECL},
92 {0, 0, 0, 0}
95 static int
96 mode_changed (const char *file, mode_t old_mode)
98 struct stat new_stats;
100 if (stat (file, &new_stats))
102 if (force_silent == 0)
103 error (0, errno, _("getting new attributes of %s"), quote (file));
104 return 0;
107 return old_mode != new_stats.st_mode;
110 /* Tell the user how/if the MODE of FILE has been changed.
111 CHANGED describes what (if anything) has happened. */
113 static void
114 describe_change (const char *file, mode_t mode,
115 enum Change_status changed)
117 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
118 const char *fmt;
120 mode_string (mode, perms);
121 perms[10] = '\0'; /* `mode_string' does not null terminate. */
122 switch (changed)
124 case CH_SUCCEEDED:
125 fmt = _("mode of %s changed to %04lo (%s)\n");
126 break;
127 case CH_FAILED:
128 fmt = _("failed to change mode of %s to %04lo (%s)\n");
129 break;
130 case CH_NO_CHANGE_REQUESTED:
131 fmt = _("mode of %s retained as %04lo (%s)\n");
132 break;
133 default:
134 abort ();
136 printf (fmt, quote (file),
137 (unsigned long) (mode & CHMOD_MODE_BITS), &perms[1]);
140 /* Change the mode of FILE according to the list of operations CHANGES.
141 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
142 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
143 links. Return 0 if successful, 1 if errors occurred. */
145 static int
146 change_file_mode (const char *file, const struct mode_change *changes,
147 const int deref_symlink)
149 struct stat file_stats;
150 mode_t newmode;
151 int errors = 0;
152 int fail;
153 int saved_errno;
155 if (deref_symlink ? stat (file, &file_stats) : lstat (file, &file_stats))
157 if (force_silent == 0)
158 error (0, errno, _("failed to get attributes of %s"), quote (file));
159 return 1;
162 #ifdef S_ISLNK
163 if (S_ISLNK (file_stats.st_mode))
164 return 0;
165 #endif
167 newmode = mode_adjust (file_stats.st_mode, changes);
169 fail = chmod (file, newmode);
170 saved_errno = errno;
172 if (verbosity == V_high
173 || (verbosity == V_changes_only
174 && !fail && mode_changed (file, file_stats.st_mode)))
175 describe_change (file, newmode, (fail ? CH_FAILED : CH_SUCCEEDED));
177 if (fail)
179 if (force_silent == 0)
180 error (0, saved_errno, _("changing permissions of %s"),
181 quote (file));
182 errors = 1;
185 if (recurse && S_ISDIR (file_stats.st_mode))
186 errors |= change_dir_mode (file, changes);
187 return errors;
190 /* Recursively change the modes of the files in directory DIR
191 according to the list of operations CHANGES.
192 Return 0 if successful, 1 if errors occurred. */
194 static int
195 change_dir_mode (const char *dir, const struct mode_change *changes)
197 char *name_space, *namep;
198 char *path; /* Full path of each entry to process. */
199 unsigned dirlength; /* Length of DIR and '\0'. */
200 unsigned filelength; /* Length of each pathname to process. */
201 unsigned pathlength; /* Bytes allocated for `path'. */
202 int errors = 0;
204 name_space = savedir (dir);
205 if (name_space == NULL)
207 if (force_silent == 0)
208 error (0, errno, "%s", quote (dir));
209 return 1;
212 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
213 pathlength = dirlength + 1;
214 /* Give `path' a dummy value; it will be reallocated before first use. */
215 path = xmalloc (pathlength);
216 strcpy (path, dir);
217 path[dirlength - 1] = '/';
219 for (namep = name_space; *namep; namep += filelength - dirlength)
221 filelength = dirlength + strlen (namep) + 1;
222 if (filelength > pathlength)
224 pathlength = filelength * 2;
225 path = xrealloc (path, pathlength);
227 strcpy (path + dirlength, namep);
228 errors |= change_file_mode (path, changes, 0);
230 free (path);
231 free (name_space);
232 return errors;
235 void
236 usage (int status)
238 if (status != 0)
239 fprintf (stderr, _("Try `%s --help' for more information.\n"),
240 program_name);
241 else
243 printf (_("\
244 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
245 or: %s [OPTION]... OCTAL-MODE FILE...\n\
246 or: %s [OPTION]... --reference=RFILE FILE...\n\
248 program_name, program_name, program_name);
249 fputs (_("\
250 Change the mode of each FILE to MODE.\n\
252 -c, --changes like verbose but report only when a change is made\n\
253 -f, --silent, --quiet suppress most error messages\n\
254 -v, --verbose output a diagnostic for every file processed\n\
255 --reference=RFILE use RFILE's mode instead of MODE values\n\
256 -R, --recursive change files and directories recursively\n\
257 "), stdout);
258 fputs (HELP_OPTION_DESCRIPTION, stdout);
259 fputs (VERSION_OPTION_DESCRIPTION, stdout);
260 fputs (_("\
262 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
263 one or more of the letters rwxXstugo.\n\
264 "), stdout);
265 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
267 exit (status);
270 /* Parse the ASCII mode given on the command line into a linked list
271 of `struct mode_change' and apply that to each file argument. */
274 main (int argc, char **argv)
276 struct mode_change *changes;
277 int errors = 0;
278 int modeind = 0; /* Index of the mode argument in `argv'. */
279 int thisind;
280 int c;
282 program_name = argv[0];
283 setlocale (LC_ALL, "");
284 bindtextdomain (PACKAGE, LOCALEDIR);
285 textdomain (PACKAGE);
287 atexit (close_stdout);
289 recurse = force_silent = 0;
291 while (1)
293 thisind = optind ? optind : 1;
295 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
296 if (c == -1)
297 break;
299 switch (c)
301 case 0:
302 break;
303 case 'r':
304 case 'w':
305 case 'x':
306 case 'X':
307 case 's':
308 case 't':
309 case 'u':
310 case 'g':
311 case 'o':
312 case 'a':
313 case ',':
314 case '+':
315 case '-':
316 case '=':
317 if (modeind != 0 && modeind != thisind)
319 static char char_string[2] = {0, 0};
320 char_string[0] = c;
321 error (1, 0, _("invalid character %s in mode string %s"),
322 quote_n (0, char_string), quote_n (1, argv[thisind]));
324 modeind = thisind;
325 break;
326 case REFERENCE_FILE_OPTION:
327 reference_file = optarg;
328 break;
329 case 'R':
330 recurse = 1;
331 break;
332 case 'c':
333 verbosity = V_changes_only;
334 break;
335 case 'f':
336 force_silent = 1;
337 break;
338 case 'v':
339 verbosity = V_high;
340 break;
341 case_GETOPT_HELP_CHAR;
342 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
343 default:
344 usage (1);
348 if (modeind == 0 && reference_file == NULL)
349 modeind = optind++;
351 if (optind >= argc)
353 error (0, 0, _("too few arguments"));
354 usage (1);
357 changes = (reference_file ? mode_create_from_ref (reference_file)
358 : mode_compile (argv[modeind], MODE_MASK_ALL));
360 if (changes == MODE_INVALID)
361 error (1, 0, _("invalid mode string: %s"), quote (argv[modeind]));
362 else if (changes == MODE_MEMORY_EXHAUSTED)
363 xalloc_die ();
364 else if (changes == MODE_BAD_REFERENCE)
365 error (1, errno, _("failed to get attributes of %s"),
366 quote (reference_file));
368 for (; optind < argc; ++optind)
369 errors |= change_file_mode (argv[optind], changes, 1);
371 exit (errors);