2 * Mini chown/chmod/chgrp 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
31 static char* invocationName=NULL;
32 static mode_t mode=7777;
39 static const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n"
40 "Change the group membership of each FILE to GROUP.\n"
41 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
42 static const char chown_usage[] = "[OPTION]... OWNER[.[GROUP] FILE...\n"
43 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
44 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
45 static const char chmod_usage[] = "[-R] MODE[,MODE]... FILE...\n"
46 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
47 "one or more of the letters rwxst.\n\n"
48 "\t-R\tchange files and directories recursively.\n";
52 static int fileAction(const char *fileName)
55 if (stat(fileName, &statBuf) < 0) {
59 if (chown(fileName, ((whichApp==CHOWN_APP)? uid: statBuf.st_uid), gid) < 0)
62 if (chmod(fileName, mode))
70 /* [ugoa]{+|-|=}[rwxstl] */
71 int parse_mode( const char* s, mode_t* or, mode_t* and, int* group_execute)
74 mode_t groups = S_ISVTX;
84 groups |= S_ISUID|S_IRWXU;
87 groups |= S_ISGID|S_IRWXG;
93 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
99 if ( groups == S_ISVTX ) /* The default is "all" */
100 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
103 if ( c >= '0' && c <= '7' && mode == 0 && groups == S_ISVTX ) {
105 *or = strtol(--s, 0, 010);
114 while ( (c = *s++) != '\0' ) {
119 mode |= S_IRUSR|S_IRGRP|S_IROTH;
122 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
125 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
128 if ( group_execute != 0 && (groups & S_IRWXG) ) {
129 if ( *group_execute < 0 )
136 mode |= S_ISUID|S_ISGID;
139 if ( *group_execute > 0 )
161 *or |= mode & groups;
164 *and &= ~(mode & groups);
168 } while ( c == ',' );
173 int chmod_chown_chgrp_main(int argc, char **argv)
177 int recursiveFlag=FALSE;
181 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP;
184 fprintf(stderr, "Usage: %s %s", *argv,
185 (whichApp==TRUE)? chown_usage : chgrp_usage);
188 invocationName=*argv;
193 while (**argv == '-') {
194 while (*++(*argv)) switch (**argv) {
196 recursiveFlag = TRUE;
199 fprintf(stderr, "Unknown option: %c\n", **argv);
206 if ( whichApp == CHMOD_APP ) {
207 /* Find the specified modes */
208 if ( parse_mode(*argv, &orWithMode, &andWithMode, 0) == FALSE ) {
209 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv);
216 /* Find the selected group */
217 groupName = strchr(*argv, '.');
218 if ( whichApp==TRUE && groupName )
222 grp = getgrnam(groupName);
224 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
229 /* Find the selected user (if appropriate) */
230 if (whichApp==TRUE) {
231 pwd = getpwnam(*argv);
233 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
240 /* Ok, ready to do the deed now */
242 fprintf(stderr, "%s: too few arguments", invocationName);
247 recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction);