*** empty log message ***
[coreutils.git] / src / chmod.c
blobdd3c42dcdf2021ff635fd3cb19be5f29736dc519
1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 1995-2000 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 void strip_trailing_slashes ();
58 static int change_dir_mode PARAMS ((const char *dir,
59 const struct mode_change *changes,
60 const struct stat *statp));
62 /* The name the program was run with. */
63 char *program_name;
65 /* If nonzero, change the modes of directories recursively. */
66 static int recurse;
68 /* If nonzero, force silence (no error messages). */
69 static int force_silent;
71 /* Level of verbosity. */
72 static enum Verbosity verbosity = V_off;
74 /* The argument to the --reference option. Use the owner and group IDs
75 of this file. This file must exist. */
76 static char *reference_file;
78 /* For long options that have no equivalent short option, use a
79 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
80 enum
82 REFERENCE_FILE_OPTION = CHAR_MAX + 1
85 static struct option const long_options[] =
87 {"recursive", no_argument, 0, 'R'},
88 {"changes", no_argument, 0, 'c'},
89 {"silent", no_argument, 0, 'f'},
90 {"quiet", no_argument, 0, 'f'},
91 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
92 {"verbose", no_argument, 0, 'v'},
93 {GETOPT_HELP_OPTION_DECL},
94 {GETOPT_VERSION_OPTION_DECL},
95 {0, 0, 0, 0}
98 static int
99 mode_changed (const char *file, mode_t old_mode)
101 struct stat new_stats;
103 if (lstat (file, &new_stats))
105 if (force_silent == 0)
106 error (0, errno, _("getting new attributes of %s"), quote (file));
107 return 0;
110 #ifdef S_ISLNK
111 if (S_ISLNK (new_stats.st_mode)
112 && stat (file, &new_stats))
114 if (force_silent == 0)
115 error (0, errno, _("getting new attributes of %s"), quote (file));
116 return 0;
118 #endif
120 return old_mode != new_stats.st_mode;
123 /* Tell the user how/if the MODE of FILE has been changed.
124 CHANGED describes what (if anything) has happened. */
126 static void
127 describe_change (const char *file, mode_t mode,
128 enum Change_status changed)
130 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
131 const char *fmt;
133 mode_string (mode, perms);
134 perms[10] = '\0'; /* `mode_string' does not null terminate. */
135 switch (changed)
137 case CH_SUCCEEDED:
138 fmt = _("mode of %s changed to %04lo (%s)\n");
139 break;
140 case CH_FAILED:
141 fmt = _("failed to change mode of %s to %04lo (%s)\n");
142 break;
143 case CH_NO_CHANGE_REQUESTED:
144 fmt = _("mode of %s retained as %04lo (%s)\n");
145 break;
146 default:
147 abort ();
149 printf (fmt, quote (file),
150 (unsigned long) (mode & CHMOD_MODE_BITS), &perms[1]);
153 /* Change the mode of FILE according to the list of operations CHANGES.
154 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
155 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
156 links. Return 0 if successful, 1 if errors occurred. */
158 static int
159 change_file_mode (const char *file, const struct mode_change *changes,
160 const int deref_symlink)
162 struct stat file_stats;
163 mode_t newmode;
164 int errors = 0;
165 int fail;
166 int saved_errno;
168 if (lstat (file, &file_stats))
170 if (force_silent == 0)
171 error (0, errno, _("getting attributes of %s"), quote (file));
172 return 1;
174 #ifdef S_ISLNK
175 if (S_ISLNK (file_stats.st_mode))
177 if (! deref_symlink)
178 return 0;
179 else
180 if (stat (file, &file_stats))
182 if (force_silent == 0)
183 error (0, errno, _("getting attributes of %s"), quote (file));
184 return 1;
187 #endif
189 newmode = mode_adjust (file_stats.st_mode, changes);
191 fail = chmod (file, newmode);
192 saved_errno = errno;
194 if (verbosity == V_high
195 || (verbosity == V_changes_only
196 && !fail && mode_changed (file, file_stats.st_mode)))
197 describe_change (file, newmode, (fail ? CH_FAILED : CH_SUCCEEDED));
199 if (fail)
201 if (force_silent == 0)
202 error (0, saved_errno, _("changing permissions of %s"),
203 quote (file));
204 errors = 1;
207 if (recurse && S_ISDIR (file_stats.st_mode))
208 errors |= change_dir_mode (file, changes, &file_stats);
209 return errors;
212 /* Recursively change the modes of the files in directory DIR
213 according to the list of operations CHANGES.
214 STATP points to the results of lstat on DIR.
215 Return 0 if successful, 1 if errors occurred. */
217 static int
218 change_dir_mode (const char *dir, const struct mode_change *changes,
219 const struct stat *statp)
221 char *name_space, *namep;
222 char *path; /* Full path of each entry to process. */
223 unsigned dirlength; /* Length of DIR and '\0'. */
224 unsigned filelength; /* Length of each pathname to process. */
225 unsigned pathlength; /* Bytes allocated for `path'. */
226 int errors = 0;
228 name_space = savedir (dir, statp->st_size);
229 if (name_space == NULL)
231 if (force_silent == 0)
232 error (0, errno, "%s", quote (dir));
233 return 1;
236 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
237 pathlength = dirlength + 1;
238 /* Give `path' a dummy value; it will be reallocated before first use. */
239 path = xmalloc (pathlength);
240 strcpy (path, dir);
241 path[dirlength - 1] = '/';
243 for (namep = name_space; *namep; namep += filelength - dirlength)
245 filelength = dirlength + strlen (namep) + 1;
246 if (filelength > pathlength)
248 pathlength = filelength * 2;
249 path = xrealloc (path, pathlength);
251 strcpy (path + dirlength, namep);
252 errors |= change_file_mode (path, changes, 0);
254 free (path);
255 free (name_space);
256 return errors;
259 void
260 usage (int status)
262 if (status != 0)
263 fprintf (stderr, _("Try `%s --help' for more information.\n"),
264 program_name);
265 else
267 printf (_("\
268 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
269 or: %s [OPTION]... OCTAL-MODE FILE...\n\
270 or: %s [OPTION]... --reference=RFILE FILE...\n\
272 program_name, program_name, program_name);
273 printf (_("\
274 Change the mode of each FILE to MODE.\n\
276 -c, --changes like verbose but report only when a change is made\n\
277 -f, --silent, --quiet suppress most error messages\n\
278 -v, --verbose output a diagnostic for every file processed\n\
279 --reference=RFILE use RFILE's mode instead of MODE values\n\
280 -R, --recursive change files and directories recursively\n\
281 --help display this help and exit\n\
282 --version output version information and exit\n\
284 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
285 one or more of the letters rwxXstugo.\n\
286 "));
287 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
289 exit (status);
292 /* Parse the ASCII mode given on the command line into a linked list
293 of `struct mode_change' and apply that to each file argument. */
296 main (int argc, char **argv)
298 struct mode_change *changes;
299 int errors = 0;
300 int modeind = 0; /* Index of the mode argument in `argv'. */
301 int thisind;
302 int c;
304 program_name = argv[0];
305 setlocale (LC_ALL, "");
306 bindtextdomain (PACKAGE, LOCALEDIR);
307 textdomain (PACKAGE);
309 atexit (close_stdout);
311 recurse = force_silent = 0;
313 while (1)
315 thisind = optind ? optind : 1;
317 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
318 if (c == -1)
319 break;
321 switch (c)
323 case 0:
324 break;
325 case 'r':
326 case 'w':
327 case 'x':
328 case 'X':
329 case 's':
330 case 't':
331 case 'u':
332 case 'g':
333 case 'o':
334 case 'a':
335 case ',':
336 case '+':
337 case '-':
338 case '=':
339 if (modeind != 0 && modeind != thisind)
341 static char char_string[2] = {0, 0};
342 char_string[0] = c;
343 error (1, 0, _("invalid character %s in mode string %s"),
344 quote_n (0, char_string), quote_n (1, argv[thisind]));
346 modeind = thisind;
347 break;
348 case REFERENCE_FILE_OPTION:
349 reference_file = optarg;
350 break;
351 case 'R':
352 recurse = 1;
353 break;
354 case 'c':
355 verbosity = V_changes_only;
356 break;
357 case 'f':
358 force_silent = 1;
359 break;
360 case 'v':
361 verbosity = V_high;
362 break;
363 case_GETOPT_HELP_CHAR;
364 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
365 default:
366 usage (1);
370 if (modeind == 0 && reference_file == NULL)
371 modeind = optind++;
373 if (optind >= argc)
375 error (0, 0, _("too few arguments"));
376 usage (1);
379 changes = (reference_file ? mode_create_from_ref (reference_file)
380 : mode_compile (argv[modeind], MODE_MASK_ALL));
382 if (changes == MODE_INVALID)
383 error (1, 0, _("invalid mode string: %s"), quote (argv[modeind]));
384 else if (changes == MODE_MEMORY_EXHAUSTED)
385 xalloc_die ();
386 else if (changes == MODE_BAD_REFERENCE)
387 error (1, errno, _("getting attributes of %s"), quote (reference_file));
389 for (; optind < argc; ++optind)
391 strip_trailing_slashes (argv[optind]);
392 errors |= change_file_mode (argv[optind], changes, 1);
395 exit (errors);