1 /* chmod -- change permission modes of files
2 Copyright (C) 1989, 1990, 1991 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> */
28 #include <sys/types.h>
29 #include "modechange.h"
40 static int change_file_mode ();
41 static int change_dir_mode ();
42 static void describe_change ();
45 /* The name the program was run with. */
48 /* If nonzero, change the modes of directories recursively. */
51 /* If nonzero, force silence (no error messages). */
52 static int force_silent;
54 /* If nonzero, describe the modes we set. */
57 /* If nonzero, describe only modes that change. */
58 static int changes_only;
60 /* Parse the ASCII mode given on the command line into a linked list
61 of `struct mode_change' and apply that to each file argument. */
68 struct mode_change *changes;
70 int modeind = 0; /* Index of the mode argument in `argv'. */
74 program_name = argv[0];
75 recurse = force_silent = verbose = changes_only = 0;
79 thisind = optind ? optind : 1;
81 c = getopt (argc, argv, "RcfvrwxXstugoa,+-=");
101 if (modeind != 0 && modeind != thisind)
102 error (1, 0, "invalid mode");
128 changes = mode_compile (argv[modeind],
129 MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
130 if (changes == MODE_INVALID)
131 error (1, 0, "invalid mode");
132 else if (changes == MODE_MEMORY_EXHAUSTED)
133 error (1, 0, "virtual memory exhausted");
135 for (; optind < argc; ++optind)
136 errors |= change_file_mode (argv[optind], changes);
141 /* Change the mode of FILE according to the list of operations CHANGES.
142 Return 0 if successful, 1 if errors occurred. */
145 change_file_mode (file, changes)
147 struct mode_change *changes;
149 struct stat file_stats;
150 unsigned short newmode;
153 if (lstat (file, &file_stats))
155 if (force_silent == 0)
156 error (0, errno, "%s", file);
160 if (S_ISLNK (file_stats.st_mode))
164 newmode = mode_adjust (file_stats.st_mode, changes);
166 if (newmode != (file_stats.st_mode & 07777))
169 describe_change (file, newmode, 1);
170 if (chmod (file, (int) newmode))
172 if (force_silent == 0)
173 error (0, errno, "%s", file);
177 else if (verbose && changes_only == 0)
178 describe_change (file, newmode, 0);
180 if (recurse && S_ISDIR (file_stats.st_mode))
181 errors |= change_dir_mode (file, changes, &file_stats);
185 /* Recursively change the modes of the files in directory DIR
186 according to the list of operations CHANGES.
187 STATP points to the results of lstat on DIR.
188 Return 0 if successful, 1 if errors occurred. */
191 change_dir_mode (dir, changes, statp)
193 struct mode_change *changes;
196 char *name_space, *namep;
197 char *path; /* Full path of each entry to process. */
198 unsigned dirlength; /* Length of DIR and '\0'. */
199 unsigned filelength; /* Length of each pathname to process. */
200 unsigned pathlength; /* Bytes allocated for `path'. */
204 name_space = savedir (dir, statp->st_size);
205 if (name_space == NULL)
209 if (force_silent == 0)
210 error (0, errno, "%s", dir);
214 error (1, 0, "virtual memory exhausted");
217 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
218 pathlength = dirlength + 1;
219 /* Give `path' a dummy value; it will be reallocated before first use. */
220 path = xmalloc (pathlength);
222 path[dirlength - 1] = '/';
224 for (namep = name_space; *namep; namep += filelength - dirlength)
226 filelength = dirlength + strlen (namep) + 1;
227 if (filelength > pathlength)
229 pathlength = filelength * 2;
230 path = xrealloc (path, pathlength);
232 strcpy (path + dirlength, namep);
233 errors |= change_file_mode (path, changes);
240 /* Tell the user the mode MODE that file FILE has been set to;
241 if CHANGED is zero, FILE had that mode already. */
244 describe_change (file, mode, changed)
249 char perms[11]; /* "-rwxrwxrwx" ls-style modes. */
251 mode_string (mode, perms);
252 perms[10] = '\0'; /* `mode_string' does not null terminate. */
254 printf ("mode of %s changed to %04o (%s)\n",
255 file, mode & 07777, &perms[1]);
257 printf ("mode of %s retained as %04o (%s)\n",
258 file, mode & 07777, &perms[1]);
265 Usage: %s [-Rcfv] mode file...\n\
266 mode is [ugoa...][[+-=][rwxXstugo...]...][,...] or octal number\n",