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=0644;
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";
51 uid_t my_getid(const char *filename, const char *name)
54 char *rname, *start, *end, buf[128];
57 stream=fopen(filename,"r");
59 while (fgets (buf, 128, stream) != NULL) {
64 end = strchr (start, ':');
71 end = strchr (start, ':');
76 rid = (uid_t) strtol (start, &end, 10);
81 if (0 == strcmp(rname, name))
90 my_getpwnam(char *name)
92 return my_getid("/etc/passwd", name);
96 my_getgrnam(char *name)
98 return my_getid("/etc/group", name);
101 static int fileAction(const char *fileName, struct stat* statbuf)
106 if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid,
107 (gid==-1)? statbuf->st_gid : gid) == 0) {
112 if (chmod(fileName, mode) == 0)
120 int chmod_chown_chgrp_main(int argc, char **argv)
122 int recursiveFlag=FALSE;
125 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP;
128 fprintf(stderr, "Usage: %s %s", *argv,
129 (whichApp==TRUE)? chown_usage : chgrp_usage);
132 invocationName=*argv;
137 while (**argv == '-') {
138 while (*++(*argv)) switch (**argv) {
140 recursiveFlag = TRUE;
143 fprintf(stderr, "Unknown option: %c\n", **argv);
150 if ( whichApp == CHMOD_APP ) {
151 /* Find the specified modes */
152 mode &= S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
153 if ( parse_mode(*argv, &mode) == FALSE ) {
154 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv);
159 /* Find the selected group */
160 if ( whichApp==CHGRP_APP && groupName ) {
162 gid = my_getgrnam(groupName);
166 groupName = strchr(*argv, '.');
169 gid = my_getgrnam(groupName);
177 /* Find the selected user (if appropriate) */
178 if (whichApp==CHOWN_APP) {
179 uid = my_getpwnam(*argv);
181 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
187 /* Ok, ready to do the deed now */
189 fprintf(stderr, "%s: too few arguments", invocationName);
193 if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE)
199 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);