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";
52 static int fileAction(const char *fileName, struct stat* statbuf)
57 if (chown(fileName, ((whichApp==CHOWN_APP)? uid: statbuf->st_uid), gid) < 0)
60 fprintf(stderr, "%s, %d\n", fileName, mode);
61 if (chmod(fileName, mode))
68 int chmod_chown_chgrp_main(int argc, char **argv)
72 int recursiveFlag=FALSE;
75 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP;
78 fprintf(stderr, "Usage: %s %s", *argv,
79 (whichApp==TRUE)? chown_usage : chgrp_usage);
87 while (**argv == '-') {
88 while (*++(*argv)) switch (**argv) {
93 fprintf(stderr, "Unknown option: %c\n", **argv);
100 if ( whichApp == CHMOD_APP ) {
101 /* Find the specified modes */
102 mode &= S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
103 if ( parse_mode(*argv, &mode) == FALSE ) {
104 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv);
107 //mode &= andWithMode;
108 fprintf(stderr, "mode %d\n", mode);
111 /* Find the selected group */
112 groupName = strchr(*argv, '.');
113 if ( whichApp==TRUE && groupName )
117 grp = getgrnam(groupName);
119 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
124 /* Find the selected user (if appropriate) */
125 if (whichApp==TRUE) {
126 pwd = getpwnam(*argv);
128 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
135 /* Ok, ready to do the deed now */
137 fprintf(stderr, "%s: too few arguments", invocationName);
141 if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE)
174 #include "internal.h"
180 int my_getid(const char *filename, const char *name, uid_t *id)
184 char *rname, *start, *end, buf[128];
186 stream=fopen(filename,"r");
188 while (fgets (buf, 128, stream) != NULL) {
193 end = strchr (start, ':');
200 end = strchr (start, ':');
205 rid = (uid_t) strtol (start, &end, 10);
210 if (0 == strcmp(rname, name)) {
224 my_getpwuid(uid_t *uid)
226 return my_getid("/etc/passwd", NULL, uid);
230 my_getpwnam(char *name, uid_t *uid)
232 return my_getid("/etc/passwd", name, uid);
236 my_getgrgid(gid_t *gid)
238 return my_getid("/etc/group", NULL, gid);
242 my_getgrnam(char *name, gid_t *gid)
244 return my_getid("/etc/group", name, gid);
247 const char chown_usage[] = "chown [-R] user-name file [file ...]\n"
248 "\n\tThe group list is kept in the file /etc/groups.\n\n"
249 "\t-R:\tRecursively change the mode of all files and directories\n"
250 "\t\tunder the argument directory.";
253 parse_user_name(const char * s, struct FileInfo * i)
255 char * dot = strchr(s, '.');
260 dot = strchr(s, ':');
265 if ( my_getpwnam(s,&id) == -1 ) {
266 id = strtol(s,&end,10);
267 if ((*end != '\0') || ( my_getpwuid(&id) == -1 )) {
268 fprintf(stderr, "%s: no such user.\n", s);
275 if ( my_getgrnam(++dot,&id) == -1 ) {
276 id = strtol(dot,&end,10);
277 if ((*end != '\0') || ( my_getgrgid(&id) == -1 )) {
278 fprintf(stderr, "%s: no such group.\n", dot);
283 i->changeGroupID = 1;
289 chown_main(struct FileInfo * i, int argc, char * * argv)
293 while ( argc >= 3 && strcmp("-R", argv[1]) == 0 ) {
299 if ( (status = parse_user_name(argv[1], i)) != 0 )
306 i->complainInPostProcess = 1;
308 return monadic_main(i, argc, argv);