(usage): Include one- or two-line synopsis in --help output.
[platform/upstream/coreutils.git] / src / chmod.c
1 /* chmod -- change permission modes of files
2    Copyright (C) 1989, 1990, 1991, 1995 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
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.
23
24    David MacKenzie <djm@gnu.ai.mit.edu> */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <sys/types.h>
30
31 #include "modechange.h"
32 #include "system.h"
33 #include "version.h"
34 #include "error.h"
35
36 void mode_string ();
37 char *savedir ();
38 void strip_trailing_slashes ();
39 char *xmalloc ();
40 char *xrealloc ();
41
42 static int change_file_mode ();
43 static int change_dir_mode ();
44 static void describe_change ();
45 static void usage ();
46
47 /* The name the program was run with. */
48 char *program_name;
49
50 /* If nonzero, change the modes of directories recursively. */
51 static int recurse;
52
53 /* If nonzero, force silence (no error messages). */
54 static int force_silent;
55
56 /* If nonzero, describe the modes we set. */
57 static int verbose;
58
59 /* If nonzero, describe only modes that change. */
60 static int changes_only;
61
62 /* If non-zero, display usage information and exit.  */
63 static int show_help;
64
65 /* If non-zero, print the version on standard output and exit.  */
66 static int show_version;
67
68 static struct option const long_options[] =
69 {
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}
78 };
79
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. */
82
83 void
84 main (argc, argv)
85      int argc;
86      char **argv;
87 {
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;
93
94   program_name = argv[0];
95   recurse = force_silent = verbose = changes_only = 0;
96
97   while (1)
98     {
99       thisind = optind ? optind : 1;
100
101       c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
102                        (int *) 0);
103       if (c == EOF)
104         break;
105
106       switch (c)
107         {
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);
143         }
144     }
145
146   if (show_version)
147     {
148       printf ("%s\n", version_string);
149       exit (0);
150     }
151
152   if (show_help)
153     usage (0);
154
155   if (modeind == 0)
156     modeind = optind++;
157
158   if (optind >= argc)
159     {
160       error (0, 0, "too few arguments");
161       usage (1);
162     }
163
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");
170
171   for (; optind < argc; ++optind)
172     {
173       strip_trailing_slashes (argv[optind]);
174       errors |= change_file_mode (argv[optind], changes, 1);
175     }
176
177   exit (errors);
178 }
179
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. */
184
185 static int
186 change_file_mode (file, changes, deref_symlink)
187      char *file;
188      struct mode_change *changes;
189      int deref_symlink;
190 {
191   struct stat file_stats;
192   unsigned short newmode;
193   int errors = 0;
194
195   if (lstat (file, &file_stats))
196     {
197       if (force_silent == 0)
198         error (0, errno, "%s", file);
199       return 1;
200     }
201 #ifdef S_ISLNK
202   if (S_ISLNK (file_stats.st_mode))
203     {
204       if (! deref_symlink)
205         return 0;
206       else
207         if (stat (file, &file_stats))
208           {
209             if (force_silent == 0)
210               error (0, errno, "%s", file);
211             return 1;
212           }
213     }
214 #endif
215
216   newmode = mode_adjust (file_stats.st_mode, changes);
217
218   if (newmode != (file_stats.st_mode & 07777))
219     {
220       if (verbose)
221         describe_change (file, newmode, 1);
222       if (chmod (file, (int) newmode))
223         {
224           if (force_silent == 0)
225             error (0, errno, "%s", file);
226           errors = 1;
227         }
228     }
229   else if (verbose && changes_only == 0)
230     describe_change (file, newmode, 0);
231
232   if (recurse && S_ISDIR (file_stats.st_mode))
233     errors |= change_dir_mode (file, changes, &file_stats);
234   return errors;
235 }
236
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. */
241
242 static int
243 change_dir_mode (dir, changes, statp)
244      char *dir;
245      struct mode_change *changes;
246      struct stat *statp;
247 {
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;
254
255   errno = 0;
256   name_space = savedir (dir, statp->st_size);
257   if (name_space == NULL)
258     {
259       if (errno)
260         {
261           if (force_silent == 0)
262             error (0, errno, "%s", dir);
263           return 1;
264         }
265       else
266         error (1, 0, "virtual memory exhausted");
267     }
268
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] = '/';
275
276   for (namep = name_space; *namep; namep += filelength - dirlength)
277     {
278       filelength = dirlength + strlen (namep) + 1;
279       if (filelength > pathlength)
280         {
281           pathlength = filelength * 2;
282           path = xrealloc (path, pathlength);
283         }
284       strcpy (path + dirlength, namep);
285       errors |= change_file_mode (path, changes, 0);
286     }
287   free (path);
288   free (name_space);
289   return errors;
290 }
291
292 /* Tell the user the mode MODE that file FILE has been set to;
293    if CHANGED is zero, FILE had that mode already. */
294
295 static void
296 describe_change (file, mode, changed)
297      char *file;
298      unsigned short mode;
299      int changed;
300 {
301   char perms[11];               /* "-rwxrwxrwx" ls-style modes. */
302
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]);
311 }
312
313 static void
314 usage (status)
315      int status;
316 {
317   if (status != 0)
318     fprintf (stderr, "Try `%s --help' for more information.\n",
319              program_name);
320   else
321     {
322       printf ("\
323 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
324   or:  %s [OPTION]... OCTAL_MODE FILE...\n\
325 ",
326               program_name, program_name);
327       printf ("\
328 \n\
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\
335 \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");
338     }
339   exit (status);
340 }