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)
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> */
23 #include <sys/types.h>
28 #include "modechange.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "chmod"
35 #define AUTHORS "David MacKenzie"
41 CH_NO_CHANGE_REQUESTED
46 /* Print a message for each file that is processed. */
49 /* Print a message for each file whose attributes we change. */
52 /* Do not be verbose. This is the default. */
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. */
65 /* If nonzero, change the modes of directories recursively. */
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. */
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
},
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
));
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
));
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. */
127 describe_change (const char *file
, mode_t mode
,
128 enum Change_status changed
)
130 char perms
[11]; /* "-rwxrwxrwx" ls-style modes. */
133 mode_string (mode
, perms
);
134 perms
[10] = '\0'; /* `mode_string' does not null terminate. */
138 fmt
= _("mode of %s changed to %04lo (%s)\n");
141 fmt
= _("failed to change mode of %s to %04lo (%s)\n");
143 case CH_NO_CHANGE_REQUESTED
:
144 fmt
= _("mode of %s retained as %04lo (%s)\n");
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. */
159 change_file_mode (const char *file
, const struct mode_change
*changes
,
160 const int deref_symlink
)
162 struct stat file_stats
;
168 if (lstat (file
, &file_stats
))
170 if (force_silent
== 0)
171 error (0, errno
, _("getting attributes of %s"), quote (file
));
175 if (S_ISLNK (file_stats
.st_mode
))
180 if (stat (file
, &file_stats
))
182 if (force_silent
== 0)
183 error (0, errno
, _("getting attributes of %s"), quote (file
));
189 newmode
= mode_adjust (file_stats
.st_mode
, changes
);
191 fail
= chmod (file
, newmode
);
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
));
201 if (force_silent
== 0)
202 error (0, saved_errno
, _("changing permissions of %s"),
207 if (recurse
&& S_ISDIR (file_stats
.st_mode
))
208 errors
|= change_dir_mode (file
, changes
, &file_stats
);
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. */
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'. */
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
));
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
);
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);
263 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
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
);
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\
287 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
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
;
300 int modeind
= 0; /* Index of the mode argument in `argv'. */
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;
315 thisind
= optind
? optind
: 1;
317 c
= getopt_long (argc
, argv
, "RcfvrwxXstugoa,+-=", long_options
, NULL
);
339 if (modeind
!= 0 && modeind
!= thisind
)
341 static char char_string
[2] = {0, 0};
343 error (1, 0, _("invalid character %s in mode string %s"),
344 quote_n (0, char_string
), quote_n (1, argv
[thisind
]));
348 case REFERENCE_FILE_OPTION
:
349 reference_file
= optarg
;
355 verbosity
= V_changes_only
;
363 case_GETOPT_HELP_CHAR
;
364 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
370 if (modeind
== 0 && reference_file
== NULL
)
375 error (0, 0, _("too few arguments"));
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
)
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);