2 * Mini chown/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;
34 static const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n"
35 "Change the group membership of each FILE to GROUP.\n"
36 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
37 static const char chown_usage[] = "[OPTION]... OWNER[.[GROUP] FILE...\n"
38 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
39 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
43 static int fileAction(const char *fileName)
46 if ((stat(fileName, &statBuf) < 0) ||
48 ((chownApp==TRUE)? uid: statBuf.st_uid),
56 int chown_main(int argc, char **argv)
60 int recursiveFlag=FALSE;
64 chownApp = (strcmp(*argv, "chown")==0)? TRUE : FALSE;
67 fprintf(stderr, "Usage: %s %s", *argv,
68 (chownApp==TRUE)? chown_usage : chgrp_usage);
76 while (**argv == '-') {
77 while (*++(*argv)) switch (**argv) {
82 fprintf(stderr, "Unknown option: %c\n", **argv);
89 /* Find the selected group */
90 groupName = strchr(*argv, '.');
91 if ( chownApp==TRUE && groupName )
95 grp = getgrnam(groupName);
97 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
102 /* Find the selected user (if appropriate) */
103 if (chownApp==TRUE) {
104 pwd = getpwnam(*argv);
106 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
112 /* Ok, ready to do the deed now */
114 fprintf(stderr, "%s: too few arguments", invocationName);
119 recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction);