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
33 static char* invocationName=NULL;
34 static char* theMode=NULL;
41 static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n"
42 "Change the group membership of each FILE to GROUP.\n"
43 "\nOptions:\n\t-R\tchange files and directories recursively\n";
44 static const char chown_usage[] = "chown [OPTION]... OWNER[.[GROUP] FILE...\n\n"
45 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
46 "\nOptions:\n\t-R\tchange files and directories recursively\n";
47 static const char chmod_usage[] = "chmod [-R] MODE[,MODE]... FILE...\n\n"
48 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
49 "one or more of the letters rwxst.\n\n"
50 "\nOptions:\n\t-R\tchange files and directories recursively.\n";
53 static int fileAction(const char *fileName, struct stat* statbuf)
58 if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid,
59 (gid==-1)? statbuf->st_gid : gid) == 0) {
64 /* Parse the specified modes */
65 if ( parse_mode(theMode, &(statbuf->st_mode)) == FALSE ) {
66 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, theMode);
69 if (chmod(fileName, statbuf->st_mode) == 0)
77 int chmod_chown_chgrp_main(int argc, char **argv)
79 int recursiveFlag=FALSE;
83 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP;
85 appUsage = (whichApp==CHOWN_APP)? chown_usage : (whichApp==CHMOD_APP)? chmod_usage : chgrp_usage;
94 while (**argv == '-') {
95 while (*++(*argv)) switch (**argv) {
100 fprintf(stderr, "Unknown option: %c\n", **argv);
107 if ( whichApp == CHMOD_APP ) {
111 /* Find the selected group */
112 if ( whichApp==CHGRP_APP ) {
114 gid = my_getgrnam(groupName);
118 groupName = strchr(*argv, '.');
121 gid = my_getgrnam(groupName);
129 /* Find the selected user (if appropriate) */
130 if (whichApp==CHOWN_APP) {
131 uid = my_getpwnam(*argv);
133 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
139 /* Ok, ready to do the deed now */
141 fprintf(stderr, "%s: too few arguments", invocationName);
145 if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE)
151 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);