(add_tabstop): Give correct size when reallocating tab_list buffer.
[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 "safe-stat.h"
35 #include "safe-lstat.h"
36 #include "error.h"
37
38 void mode_string ();
39 char *savedir ();
40 void strip_trailing_slashes ();
41 char *xmalloc ();
42 char *xrealloc ();
43
44 static int change_file_mode ();
45 static int change_dir_mode ();
46 static void describe_change ();
47 static void usage ();
48
49 /* The name the program was run with. */
50 char *program_name;
51
52 /* If nonzero, change the modes of directories recursively. */
53 static int recurse;
54
55 /* If nonzero, force silence (no error messages). */
56 static int force_silent;
57
58 /* If nonzero, describe the modes we set. */
59 static int verbose;
60
61 /* If nonzero, describe only modes that change. */
62 static int changes_only;
63
64 /* If non-zero, display usage information and exit.  */
65 static int show_help;
66
67 /* If non-zero, print the version on standard output and exit.  */
68 static int show_version;
69
70 static struct option const long_options[] =
71 {
72   {"recursive", no_argument, 0, 'R'},
73   {"changes", no_argument, 0, 'c'},
74   {"silent", no_argument, 0, 'f'},
75   {"quiet", no_argument, 0, 'f'},
76   {"verbose", no_argument, 0, 'v'},
77   {"help", no_argument, &show_help, 1},
78   {"version", no_argument, &show_version, 1},
79   {0, 0, 0, 0}
80 };
81
82 /* Parse the ASCII mode given on the command line into a linked list
83    of `struct mode_change' and apply that to each file argument. */
84
85 void
86 main (argc, argv)
87      int argc;
88      char **argv;
89 {
90   struct mode_change *changes;
91   int errors = 0;
92   int modeind = 0;              /* Index of the mode argument in `argv'. */
93   int thisind;
94   int c;
95
96   program_name = argv[0];
97   recurse = force_silent = verbose = changes_only = 0;
98
99   while (1)
100     {
101       thisind = optind ? optind : 1;
102
103       c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
104                        (int *) 0);
105       if (c == EOF)
106         break;
107
108       switch (c)
109         {
110         case 0:
111           break;
112         case 'r':
113         case 'w':
114         case 'x':
115         case 'X':
116         case 's':
117         case 't':
118         case 'u':
119         case 'g':
120         case 'o':
121         case 'a':
122         case ',':
123         case '+':
124         case '-':
125         case '=':
126           if (modeind != 0 && modeind != thisind)
127             error (1, 0, "invalid mode");
128           modeind = thisind;
129           break;
130         case 'R':
131           recurse = 1;
132           break;
133         case 'c':
134           verbose = 1;
135           changes_only = 1;
136           break;
137         case 'f':
138           force_silent = 1;
139           break;
140         case 'v':
141           verbose = 1;
142           break;
143         default:
144           usage (1);
145         }
146     }
147
148   if (show_version)
149     {
150       printf ("%s\n", version_string);
151       exit (0);
152     }
153
154   if (show_help)
155     usage (0);
156
157   if (modeind == 0)
158     modeind = optind++;
159
160   if (optind >= argc)
161     {
162       error (0, 0, "too few arguments");
163       usage (1);
164     }
165
166   changes = mode_compile (argv[modeind],
167                           MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
168   if (changes == MODE_INVALID)
169     error (1, 0, "invalid mode");
170   else if (changes == MODE_MEMORY_EXHAUSTED)
171     error (1, 0, "virtual memory exhausted");
172
173   for (; optind < argc; ++optind)
174     {
175       strip_trailing_slashes (argv[optind]);
176       errors |= change_file_mode (argv[optind], changes, 1);
177     }
178
179   exit (errors);
180 }
181
182 /* Change the mode of FILE according to the list of operations CHANGES.
183    If DEREF_SYMLINK is non-zero and FILE is a symbolic link, change the
184    mode of the referenced file.  If DEREF_SYMLINK is zero, ignore symbolic
185    links.  Return 0 if successful, 1 if errors occurred. */
186
187 static int
188 change_file_mode (file, changes, deref_symlink)
189      char *file;
190      struct mode_change *changes;
191      int deref_symlink;
192 {
193   struct stat file_stats;
194   unsigned short newmode;
195   int errors = 0;
196
197   if (safe_lstat (file, &file_stats))
198     {
199       if (force_silent == 0)
200         error (0, errno, "%s", file);
201       return 1;
202     }
203 #ifdef S_ISLNK
204   if (S_ISLNK (file_stats.st_mode))
205     {
206       if (! deref_symlink)
207         return 0;
208       else 
209         if (safe_stat (file, &file_stats))
210           {
211             if (force_silent == 0)
212               error (0, errno, "%s", file);
213             return 1;
214           }
215     }
216 #endif
217
218   newmode = mode_adjust (file_stats.st_mode, changes);
219
220   if (newmode != (file_stats.st_mode & 07777))
221     {
222       if (verbose)
223         describe_change (file, newmode, 1);
224       if (chmod (file, (int) newmode))
225         {
226           if (force_silent == 0)
227             error (0, errno, "%s", file);
228           errors = 1;
229         }
230     }
231   else if (verbose && changes_only == 0)
232     describe_change (file, newmode, 0);
233
234   if (recurse && S_ISDIR (file_stats.st_mode))
235     errors |= change_dir_mode (file, changes, &file_stats);
236   return errors;
237 }
238
239 /* Recursively change the modes of the files in directory DIR
240    according to the list of operations CHANGES.
241    STATP points to the results of lstat on DIR.
242    Return 0 if successful, 1 if errors occurred. */
243
244 static int
245 change_dir_mode (dir, changes, statp)
246      char *dir;
247      struct mode_change *changes;
248      struct stat *statp;
249 {
250   char *name_space, *namep;
251   char *path;                   /* Full path of each entry to process. */
252   unsigned dirlength;           /* Length of DIR and '\0'. */
253   unsigned filelength;          /* Length of each pathname to process. */
254   unsigned pathlength;          /* Bytes allocated for `path'. */
255   int errors = 0;
256
257   errno = 0;
258   name_space = savedir (dir, statp->st_size);
259   if (name_space == NULL)
260     {
261       if (errno)
262         {
263           if (force_silent == 0)
264             error (0, errno, "%s", dir);
265           return 1;
266         }
267       else
268         error (1, 0, "virtual memory exhausted");
269     }
270
271   dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
272   pathlength = dirlength + 1;
273   /* Give `path' a dummy value; it will be reallocated before first use. */
274   path = xmalloc (pathlength);
275   strcpy (path, dir);
276   path[dirlength - 1] = '/';
277
278   for (namep = name_space; *namep; namep += filelength - dirlength)
279     {
280       filelength = dirlength + strlen (namep) + 1;
281       if (filelength > pathlength)
282         {
283           pathlength = filelength * 2;
284           path = xrealloc (path, pathlength);
285         }
286       strcpy (path + dirlength, namep);
287       errors |= change_file_mode (path, changes, 0);
288     }
289   free (path);
290   free (name_space);
291   return errors;
292 }
293
294 /* Tell the user the mode MODE that file FILE has been set to;
295    if CHANGED is zero, FILE had that mode already. */
296
297 static void
298 describe_change (file, mode, changed)
299      char *file;
300      unsigned short mode;
301      int changed;
302 {
303   char perms[11];               /* "-rwxrwxrwx" ls-style modes. */
304
305   mode_string (mode, perms);
306   perms[10] = '\0';             /* `mode_string' does not null terminate. */
307   if (changed)
308     printf ("mode of %s changed to %04o (%s)\n",
309             file, mode & 07777, &perms[1]);
310   else
311     printf ("mode of %s retained as %04o (%s)\n",
312             file, mode & 07777, &perms[1]);
313 }
314
315 static void
316 usage (status)
317      int status;
318 {
319   if (status != 0)
320     fprintf (stderr, "Try `%s --help' for more information.\n",
321              program_name);
322   else
323     {
324       printf ("\
325 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
326   or:  %s [OPTION]... OCTAL_MODE FILE...\n\
327 ",
328               program_name, program_name);
329       printf ("\
330 \n\
331   -c, --changes           like verbose but report only when a change is made\n\
332   -f, --silent, --quiet   suppress most error messages\n\
333   -v, --verbose           output a diagnostic for every file processed\n\
334   -R, --recursive         change files and directories recursively\n\
335       --help              display this help and exit\n\
336       --version           output version information and exit\n\
337 \n\
338 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
339 one or more of the letters rwxXstugo.\n");
340     }
341   exit (status);
342 }