(noinst_HEADERS): Add nanosleep.h.
[coreutils.git] / src / chmod.c
blob14ad4d5cd5a65fb6f209419361c7a508ea8d0aff
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 /* For long options that have no equivalent short option, use a
78 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
79 enum
81 REFERENCE_FILE_OPTION = CHAR_MAX + 1
84 static struct option const long_options[] =
86 {"recursive", no_argument, 0, 'R'},
87 {"changes", no_argument, 0, 'c'},
88 {"silent", no_argument, 0, 'f'},
89 {"quiet", no_argument, 0, 'f'},
90 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
91 {"verbose", no_argument, 0, 'v'},
92 {GETOPT_HELP_OPTION_DECL},
93 {GETOPT_VERSION_OPTION_DECL},
94 {0, 0, 0, 0}
97 /* Tell the user how/if the MODE of FILE has been changed.
98 CHANGED describes what (if anything) has happened. */
100 static void
101 describe_change (const char *file, mode_t mode,
102 enum Change_status changed)
104 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
105 const char *fmt;
107 mode_string (mode, perms);
108 perms[10] = '\0'; /* `mode_string' does not null terminate. */
109 switch (changed)
111 case CH_SUCCEEDED:
112 fmt = _("mode of %s changed to %04lo (%s)\n");
113 break;
114 case CH_FAILED:
115 fmt = _("failed to change mode of %s to %04lo (%s)\n");
116 break;
117 case CH_NO_CHANGE_REQUESTED:
118 fmt = _("mode of %s retained as %04lo (%s)\n");
119 break;
120 default:
121 abort ();
123 printf (fmt, file, (unsigned long) (mode & CHMOD_MODE_BITS), &perms[1]);
126 /* Change the mode of FILE according to the list of operations CHANGES.
127 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
128 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
129 links. Return 0 if successful, 1 if errors occurred. */
131 static int
132 change_file_mode (const char *file, const struct mode_change *changes,
133 const int deref_symlink)
135 struct stat file_stats;
136 mode_t newmode;
137 int errors = 0;
139 if (lstat (file, &file_stats))
141 if (force_silent == 0)
142 error (0, errno, "%s", file);
143 return 1;
145 #ifdef S_ISLNK
146 if (S_ISLNK (file_stats.st_mode))
148 if (! deref_symlink)
149 return 0;
150 else
151 if (stat (file, &file_stats))
153 if (force_silent == 0)
154 error (0, errno, "%s", file);
155 return 1;
158 #endif
160 newmode = mode_adjust (file_stats.st_mode, changes);
162 if (newmode != (file_stats.st_mode & CHMOD_MODE_BITS))
164 int fail = chmod (file, newmode);
166 if (verbosity == V_high || (verbosity == V_changes_only && !fail))
167 describe_change (file, newmode, (fail ? CH_FAILED : CH_SUCCEEDED));
169 if (fail)
171 if (force_silent == 0)
172 error (0, errno, "%s", file);
173 errors = 1;
176 else if (verbosity == V_high)
177 describe_change (file, newmode, CH_NO_CHANGE_REQUESTED);
179 if (recurse && S_ISDIR (file_stats.st_mode))
180 errors |= change_dir_mode (file, changes, &file_stats);
181 return errors;
184 /* Recursively change the modes of the files in directory DIR
185 according to the list of operations CHANGES.
186 STATP points to the results of lstat on DIR.
187 Return 0 if successful, 1 if errors occurred. */
189 static int
190 change_dir_mode (const char *dir, const struct mode_change *changes,
191 const struct stat *statp)
193 char *name_space, *namep;
194 char *path; /* Full path of each entry to process. */
195 unsigned dirlength; /* Length of DIR and '\0'. */
196 unsigned filelength; /* Length of each pathname to process. */
197 unsigned pathlength; /* Bytes allocated for `path'. */
198 int errors = 0;
200 errno = 0;
201 name_space = savedir (dir, statp->st_size);
202 if (name_space == NULL)
204 if (errno)
206 if (force_silent == 0)
207 error (0, errno, "%s", dir);
208 return 1;
210 else
211 error (1, 0, _("virtual memory exhausted"));
214 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
215 pathlength = dirlength + 1;
216 /* Give `path' a dummy value; it will be reallocated before first use. */
217 path = xmalloc (pathlength);
218 strcpy (path, dir);
219 path[dirlength - 1] = '/';
221 for (namep = name_space; *namep; namep += filelength - dirlength)
223 filelength = dirlength + strlen (namep) + 1;
224 if (filelength > pathlength)
226 pathlength = filelength * 2;
227 path = xrealloc (path, pathlength);
229 strcpy (path + dirlength, namep);
230 errors |= change_file_mode (path, changes, 0);
232 free (path);
233 free (name_space);
234 return errors;
237 void
238 usage (int status)
240 if (status != 0)
241 fprintf (stderr, _("Try `%s --help' for more information.\n"),
242 program_name);
243 else
245 printf (_("\
246 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
247 or: %s [OPTION]... OCTAL-MODE FILE...\n\
248 or: %s [OPTION]... --reference=RFILE FILE...\n\
250 program_name, program_name, program_name);
251 printf (_("\
252 Change the mode of each FILE to MODE.\n\
254 -c, --changes like verbose but report only when a change is made\n\
255 -f, --silent, --quiet suppress most error messages\n\
256 -v, --verbose output a diagnostic for every file processed\n\
257 --reference=RFILE use RFILE's mode instead of MODE values\n\
258 -R, --recursive change files and directories recursively\n\
259 --help display this help and exit\n\
260 --version output version information and exit\n\
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 "));
265 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
266 close_stdout ();
268 exit (status);
271 /* Parse the ASCII mode given on the command line into a linked list
272 of `struct mode_change' and apply that to each file argument. */
275 main (int argc, char **argv)
277 struct mode_change *changes;
278 int errors = 0;
279 int modeind = 0; /* Index of the mode argument in `argv'. */
280 int thisind;
281 int c;
283 program_name = argv[0];
284 setlocale (LC_ALL, "");
285 bindtextdomain (PACKAGE, LOCALEDIR);
286 textdomain (PACKAGE);
288 recurse = force_silent = 0;
290 while (1)
292 thisind = optind ? optind : 1;
294 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options, NULL);
295 if (c == -1)
296 break;
298 switch (c)
300 case 0:
301 break;
302 case 'r':
303 case 'w':
304 case 'x':
305 case 'X':
306 case 's':
307 case 't':
308 case 'u':
309 case 'g':
310 case 'o':
311 case 'a':
312 case ',':
313 case '+':
314 case '-':
315 case '=':
316 if (modeind != 0 && modeind != thisind)
317 error (1, 0, _("invalid mode"));
318 modeind = thisind;
319 break;
320 case REFERENCE_FILE_OPTION:
321 reference_file = optarg;
322 break;
323 case 'R':
324 recurse = 1;
325 break;
326 case 'c':
327 verbosity = V_changes_only;
328 break;
329 case 'f':
330 force_silent = 1;
331 break;
332 case 'v':
333 verbosity = V_high;
334 break;
335 case_GETOPT_HELP_CHAR;
336 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
337 default:
338 usage (1);
342 if (modeind == 0 && reference_file == NULL)
343 modeind = optind++;
345 if (optind >= argc)
347 error (0, 0, _("too few arguments"));
348 usage (1);
351 changes = (reference_file ? mode_create_from_ref (reference_file)
352 : mode_compile (argv[modeind], MODE_MASK_ALL));
354 if (changes == MODE_INVALID)
355 error (1, 0, _("invalid mode"));
356 else if (changes == MODE_MEMORY_EXHAUSTED)
357 error (1, 0, _("virtual memory exhausted"));
358 else if (changes == MODE_BAD_REFERENCE)
359 error (1, errno, "%s", reference_file);
361 for (; optind < argc; ++optind)
363 strip_trailing_slashes (argv[optind]);
364 errors |= change_file_mode (argv[optind], changes, 1);
367 if (verbosity != V_off)
368 close_stdout ();
369 exit (errors);