2 * Mini chown/chmod/chgrp implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define BB_DECLARE_EXTERN
26 #define bb_need_invalid_option
34 static uid_t uid = -1;
35 static gid_t gid = -1;
37 static char* invocationName=NULL;
38 static char* theMode=NULL;
45 static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n"
46 "Change the group membership of each FILE to GROUP.\n"
47 "\nOptions:\n\t-R\tchange files and directories recursively\n";
48 static const char chown_usage[] = "chown [OPTION]... OWNER[.[GROUP] FILE...\n\n"
49 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
50 "\nOptions:\n\t-R\tchange files and directories recursively\n";
51 static const char chmod_usage[] = "chmod [-R] MODE[,MODE]... FILE...\n\n"
52 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
53 "one or more of the letters rwxst.\n\n"
54 "\nOptions:\n\t-R\tchange files and directories recursively.\n";
57 static int fileAction(const char *fileName, struct stat* statbuf)
62 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
63 if (lchown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid,
64 (gid==-1)? statbuf->st_gid : gid) == 0)
66 if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid,
67 (gid==-1)? statbuf->st_gid : gid) == 0)
74 /* Parse the specified modes */
75 if ( parse_mode(theMode, &(statbuf->st_mode)) == FALSE ) {
76 fprintf(stderr, "%s: unknown mode: %s\n", invocationName, theMode);
79 if (chmod(fileName, statbuf->st_mode) == 0)
87 int chmod_chown_chgrp_main(int argc, char **argv)
89 int recursiveFlag=FALSE;
94 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP;
96 appUsage = (whichApp==CHOWN_APP)? chown_usage : (whichApp==CHMOD_APP)? chmod_usage : chgrp_usage;
100 invocationName = *argv;
105 while (**argv == '-') {
106 while (*++(*argv)) switch (**argv) {
108 recursiveFlag = TRUE;
111 fprintf(stderr, invalid_option, invocationName, **argv);
118 if ( whichApp == CHMOD_APP ) {
122 /* Find the selected group */
123 if ( whichApp==CHGRP_APP ) {
125 gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */
127 gid = my_getgrnam(groupName);
131 groupName = strchr(*argv, '.');
134 gid = strtoul(groupName, &p, 10);
136 gid = my_getgrnam(groupName);
144 /* Find the selected user (if appropriate) */
145 if (whichApp==CHOWN_APP) {
146 uid = strtoul(*argv, &p, 10); /* if numeric ...*/
148 uid = my_getpwnam(*argv);
150 fprintf(stderr, "%s: unknown user name: %s\n", invocationName, *argv);
156 /* Ok, ready to do the deed now */
158 fprintf(stderr, "%s: too few arguments\n", invocationName);
162 if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE)
168 fprintf(stderr, "%s: unknown group name: %s\n", invocationName, groupName);