1 /* chmod -- change permission modes of files
2 Copyright (C) 1989, 1990, 1991, 1995 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)
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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.
24 David MacKenzie <djm@gnu.ai.mit.edu> */
29 #include <sys/types.h>
31 #include "modechange.h"
38 void strip_trailing_slashes ();
42 static int change_file_mode ();
43 static int change_dir_mode ();
44 static void describe_change ();
47 /* The name the program was run with. */
50 /* If nonzero, change the modes of directories recursively. */
53 /* If nonzero, force silence (no error messages). */
54 static int force_silent
;
56 /* If nonzero, describe the modes we set. */
59 /* If nonzero, describe only modes that change. */
60 static int changes_only
;
62 /* If non-zero, display usage information and exit. */
65 /* If non-zero, print the version on standard output and exit. */
66 static int show_version
;
68 static struct option
const long_options
[] =
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},
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. */
88 struct mode_change
*changes
;
90 int modeind
= 0; /* Index of the mode argument in `argv'. */
94 program_name
= argv
[0];
95 recurse
= force_silent
= verbose
= changes_only
= 0;
99 thisind
= optind
? optind
: 1;
101 c
= getopt_long (argc
, argv
, "RcfvrwxXstugoa,+-=", long_options
,
124 if (modeind
!= 0 && modeind
!= thisind
)
125 error (1, 0, "invalid mode");
148 printf ("chmod - %s\n", version_string
);
160 error (0, 0, "too few arguments");
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");
171 for (; optind
< argc
; ++optind
)
173 strip_trailing_slashes (argv
[optind
]);
174 errors
|= change_file_mode (argv
[optind
], changes
, 1);
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. */
186 change_file_mode (file
, changes
, deref_symlink
)
188 struct mode_change
*changes
;
191 struct stat file_stats
;
192 unsigned short newmode
;
195 if (lstat (file
, &file_stats
))
197 if (force_silent
== 0)
198 error (0, errno
, "%s", file
);
202 if (S_ISLNK (file_stats
.st_mode
))
207 if (stat (file
, &file_stats
))
209 if (force_silent
== 0)
210 error (0, errno
, "%s", file
);
216 newmode
= mode_adjust (file_stats
.st_mode
, changes
);
218 if (newmode
!= (file_stats
.st_mode
& 07777))
221 describe_change (file
, newmode
, 1);
222 if (chmod (file
, (int) newmode
))
224 if (force_silent
== 0)
225 error (0, errno
, "%s", file
);
229 else if (verbose
&& changes_only
== 0)
230 describe_change (file
, newmode
, 0);
232 if (recurse
&& S_ISDIR (file_stats
.st_mode
))
233 errors
|= change_dir_mode (file
, changes
, &file_stats
);
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. */
243 change_dir_mode (dir
, changes
, statp
)
245 struct mode_change
*changes
;
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'. */
256 name_space
= savedir (dir
, statp
->st_size
);
257 if (name_space
== NULL
)
261 if (force_silent
== 0)
262 error (0, errno
, "%s", dir
);
266 error (1, 0, "virtual memory exhausted");
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
);
274 path
[dirlength
- 1] = '/';
276 for (namep
= name_space
; *namep
; namep
+= filelength
- dirlength
)
278 filelength
= dirlength
+ strlen (namep
) + 1;
279 if (filelength
> pathlength
)
281 pathlength
= filelength
* 2;
282 path
= xrealloc (path
, pathlength
);
284 strcpy (path
+ dirlength
, namep
);
285 errors
|= change_file_mode (path
, changes
, 0);
292 /* Tell the user the mode MODE that file FILE has been set to;
293 if CHANGED is zero, FILE had that mode already. */
296 describe_change (file
, mode
, changed
)
301 char perms
[11]; /* "-rwxrwxrwx" ls-style modes. */
303 mode_string (mode
, perms
);
304 perms
[10] = '\0'; /* `mode_string' does not null terminate. */
306 printf ("mode of %s changed to %04o (%s)\n",
307 file
, mode
& 07777, &perms
[1]);
309 printf ("mode of %s retained as %04o (%s)\n",
310 file
, mode
& 07777, &perms
[1]);
318 fprintf (stderr
, "Try `%s --help' for more information.\n",
323 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
324 or: %s [OPTION]... OCTAL_MODE FILE...\n\
326 program_name
, program_name
);
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\
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");