1 /* vi: set sw=4 ts=4: */
3 * Mini chown/chmod/chgrp implementation for busybox
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define BB_DECLARE_EXTERN
27 #define bb_need_invalid_option
35 static uid_t uid = -1;
36 static gid_t gid = -1;
38 static char *invocationName = NULL;
39 static char *theMode = NULL;
46 static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n"
47 "Change the group membership of each FILE to GROUP.\n"
49 "\nOptions:\n\t-R\tchange files and directories recursively\n";
50 static const char chown_usage[] =
51 "chown [OPTION]... OWNER[.[GROUP] FILE...\n\n"
52 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
54 "\nOptions:\n\t-R\tchange files and directories recursively\n";
55 static const char chmod_usage[] =
56 "chmod [-R] MODE[,MODE]... FILE...\n\n"
57 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
59 "one or more of the letters rwxst.\n\n"
60 "\nOptions:\n\t-R\tchange files and directories recursively.\n";
63 static int fileAction(const char *fileName, struct stat *statbuf)
68 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
70 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
71 (gid == -1) ? statbuf->st_gid : gid) == 0)
74 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
75 (gid == -1) ? statbuf->st_gid : gid) == 0)
82 /* Parse the specified modes */
83 if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) {
84 fatalError( "%s: unknown mode: %s\n", invocationName, theMode);
86 if (chmod(fileName, statbuf->st_mode) == 0)
94 int chmod_chown_chgrp_main(int argc, char **argv)
96 int recursiveFlag = FALSE;
101 whichApp = (strcmp(*argv, "chown") == 0)?
102 CHOWN_APP : (strcmp(*argv, "chmod") == 0)?
103 CHMOD_APP : CHGRP_APP;
105 appUsage = (whichApp == CHOWN_APP)?
106 chown_usage : (whichApp == CHMOD_APP) ? chmod_usage : chgrp_usage;
110 invocationName = *argv;
115 while (**argv == '-') {
119 recursiveFlag = TRUE;
122 fprintf(stderr, invalid_option, invocationName, **argv);
129 if (whichApp == CHMOD_APP) {
133 /* Find the selected group */
134 if (whichApp == CHGRP_APP) {
136 gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */
138 gid = my_getgrnam(groupName);
142 groupName = strchr(*argv, '.');
145 gid = strtoul(groupName, &p, 10);
147 gid = my_getgrnam(groupName);
155 /* Find the selected user (if appropriate) */
156 if (whichApp == CHOWN_APP) {
157 uid = strtoul(*argv, &p, 10); /* if numeric ... */
159 uid = my_getpwnam(*argv);
161 fatalError( "%s: unknown user name: %s\n",
162 invocationName, *argv);
167 /* Ok, ready to do the deed now */
169 fatalError( "%s: too few arguments\n", invocationName);
173 (*(++argv), recursiveFlag, TRUE, FALSE, fileAction,
174 fileAction) == FALSE)
180 fatalError( "%s: unknown group name: %s\n", invocationName, groupName);
185 c-file-style: "linux"