2 * Mini chmod implementation for busybox
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 static mode_t mode=7777;
31 static const char chmod_usage[] = "[-R] MODE[,MODE]... FILE...\n"
32 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
33 "one or more of the letters rwxst.\n\n"
34 "\t-R\tchange files and directories recursively.\n";
38 static int fileAction(const char *fileName)
41 if ((stat(fileName, &statBuf) < 0) ||
42 (chmod(fileName, mode)) < 0) {
49 /* [ugoa]{+|-|=}[rwxstl] */
50 int parse_mode( const char* s, mode_t* or, mode_t* and, int* group_execute)
53 mode_t groups = S_ISVTX;
63 groups |= S_ISUID|S_IRWXU;
66 groups |= S_ISGID|S_IRWXG;
72 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
78 if ( groups == S_ISVTX ) /* The default is "all" */
79 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
82 if ( c >= '0' && c <= '7' && mode == 0 && groups == S_ISVTX ) {
84 *or = strtol(--s, 0, 010);
93 while ( (c = *s++) != '\0' ) {
98 mode |= S_IRUSR|S_IRGRP|S_IROTH;
101 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
104 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
107 if ( group_execute != 0 && (groups & S_IRWXG) ) {
108 if ( *group_execute < 0 )
115 mode |= S_ISUID|S_ISGID;
118 if ( *group_execute > 0 )
140 *or |= mode & groups;
143 *and &= ~(mode & groups);
147 } while ( c == ',' );
152 int chmod_main(int argc, char **argv)
154 int recursiveFlag=FALSE;
155 mode_t andWithMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
156 mode_t orWithMode = 0;
157 char *invocationName=*argv;
160 fprintf(stderr, "Usage: %s %s", invocationName, chmod_usage);
167 while (**argv == '-') {
168 while (*++(*argv)) switch (**argv) {
170 recursiveFlag = TRUE;
173 fprintf(stderr, "Unknown option: %c\n", **argv);
180 /* Find the selected group */
181 if ( parse_mode(*argv, &orWithMode, &andWithMode, 0) == FALSE ) {
182 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv);
188 /* Ok, ready to do the deed now */
190 fprintf(stderr, "%s: too few arguments", invocationName);
195 recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction);