1 /* chmod -- change permission modes of files
2 Copyright (C) 89, 90, 91, 95, 1996 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. */
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"
37 void strip_trailing_slashes ();
41 static int change_dir_mode __P ((char *dir, struct mode_change *changes,
44 /* The name the program was run with. */
47 /* If nonzero, change the modes of directories recursively. */
50 /* If nonzero, force silence (no error messages). */
51 static int force_silent;
53 /* If nonzero, describe the modes we set. */
56 /* If nonzero, describe only modes that change. */
57 static int changes_only;
59 /* If nonzero, display usage information and exit. */
62 /* If nonzero, print the version on standard output and exit. */
63 static int show_version;
65 static struct option const long_options[] =
67 {"recursive", no_argument, 0, 'R'},
68 {"changes", no_argument, 0, 'c'},
69 {"silent", no_argument, 0, 'f'},
70 {"quiet", no_argument, 0, 'f'},
71 {"verbose", no_argument, 0, 'v'},
72 {"help", no_argument, &show_help, 1},
73 {"version", no_argument, &show_version, 1},
77 /* Tell the user the mode MODE that file FILE has been set to;
78 if CHANGED is zero, FILE had that mode already. */
81 describe_change (char *file, short unsigned int mode, int changed)
83 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
85 mode_string (mode, perms);
86 perms[10] = '\0'; /* `mode_string' does not null terminate. */
88 printf (_("mode of %s changed to %04o (%s)\n"),
89 file, mode & 07777, &perms[1]);
91 printf (_("mode of %s retained as %04o (%s)\n"),
92 file, mode & 07777, &perms[1]);
95 /* Change the mode of FILE according to the list of operations CHANGES.
96 If DEREF_SYMLINK is nonzero and FILE is a symbolic link, change the
97 mode of the referenced file. If DEREF_SYMLINK is zero, ignore symbolic
98 links. Return 0 if successful, 1 if errors occurred. */
101 change_file_mode (char *file, struct mode_change *changes, int deref_symlink)
103 struct stat file_stats;
104 unsigned short newmode;
107 if (lstat (file, &file_stats))
109 if (force_silent == 0)
110 error (0, errno, "%s", file);
114 if (S_ISLNK (file_stats.st_mode))
119 if (stat (file, &file_stats))
121 if (force_silent == 0)
122 error (0, errno, "%s", file);
128 newmode = mode_adjust (file_stats.st_mode, changes);
130 if (newmode != (file_stats.st_mode & 07777))
133 describe_change (file, newmode, 1);
134 if (chmod (file, (int) newmode))
136 if (force_silent == 0)
137 error (0, errno, "%s", file);
141 else if (verbose && changes_only == 0)
142 describe_change (file, newmode, 0);
144 if (recurse && S_ISDIR (file_stats.st_mode))
145 errors |= change_dir_mode (file, changes, &file_stats);
149 /* Recursively change the modes of the files in directory DIR
150 according to the list of operations CHANGES.
151 STATP points to the results of lstat on DIR.
152 Return 0 if successful, 1 if errors occurred. */
155 change_dir_mode (char *dir, struct mode_change *changes, struct stat *statp)
157 char *name_space, *namep;
158 char *path; /* Full path of each entry to process. */
159 unsigned dirlength; /* Length of DIR and '\0'. */
160 unsigned filelength; /* Length of each pathname to process. */
161 unsigned pathlength; /* Bytes allocated for `path'. */
165 name_space = savedir (dir, statp->st_size);
166 if (name_space == NULL)
170 if (force_silent == 0)
171 error (0, errno, "%s", dir);
175 error (1, 0, _("virtual memory exhausted"));
178 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
179 pathlength = dirlength + 1;
180 /* Give `path' a dummy value; it will be reallocated before first use. */
181 path = xmalloc (pathlength);
183 path[dirlength - 1] = '/';
185 for (namep = name_space; *namep; namep += filelength - dirlength)
187 filelength = dirlength + strlen (namep) + 1;
188 if (filelength > pathlength)
190 pathlength = filelength * 2;
191 path = xrealloc (path, pathlength);
193 strcpy (path + dirlength, namep);
194 errors |= change_file_mode (path, changes, 0);
205 fprintf (stderr, _("Try `%s --help' for more information.\n"),
210 Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
211 or: %s [OPTION]... OCTAL_MODE FILE...\n\
213 program_name, program_name);
216 -c, --changes like verbose but report only when a change is made\n\
217 -f, --silent, --quiet suppress most error messages\n\
218 -v, --verbose output a diagnostic for every file processed\n\
219 -R, --recursive change files and directories recursively\n\
220 --help display this help and exit\n\
221 --version output version information and exit\n\
223 Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
224 one or more of the letters rwxXstugo.\n"));
229 /* Parse the ASCII mode given on the command line into a linked list
230 of `struct mode_change' and apply that to each file argument. */
233 main (int argc, char **argv)
235 struct mode_change *changes;
237 int modeind = 0; /* Index of the mode argument in `argv'. */
241 program_name = argv[0];
242 setlocale (LC_ALL, "");
243 bindtextdomain (PACKAGE, LOCALEDIR);
244 textdomain (PACKAGE);
246 recurse = force_silent = verbose = changes_only = 0;
250 thisind = optind ? optind : 1;
252 c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
275 if (modeind != 0 && modeind != thisind)
276 error (1, 0, _("invalid mode"));
299 printf ("chmod - %s\n", PACKAGE_VERSION);
311 error (0, 0, _("too few arguments"));
315 changes = mode_compile (argv[modeind],
316 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
317 if (changes == MODE_INVALID)
318 error (1, 0, _("invalid mode"));
319 else if (changes == MODE_MEMORY_EXHAUSTED)
320 error (1, 0, _("virtual memory exhausted"));
322 for (; optind < argc; ++optind)
324 strip_trailing_slashes (argv[optind]);
325 errors |= change_file_mode (argv[optind], changes, 1);