1 /* vi: set sw=4 ts=4: */
3 * Mini chown/chmod/chgrp implementation for busybox
6 * Copyright (C) 1999,2000,2001 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
30 #define BB_DECLARE_EXTERN
31 #define bb_need_invalid_option
32 #define bb_need_too_few_args
40 static char *theMode = NULL;
47 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
52 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
54 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
55 (gid == -1) ? statbuf->st_gid : gid) == 0)
58 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
59 (gid == -1) ? statbuf->st_gid : gid) == 0)
66 /* Parse the specified modes */
67 if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) {
68 error_msg_and_die( "unknown mode: %s", theMode);
70 if (chmod(fileName, statbuf->st_mode) == 0)
78 int chmod_chown_chgrp_main(int argc, char **argv)
81 int recursiveFlag = FALSE;
85 whichApp = (applet_name[2]=='o')? /* chown */
86 CHOWN_APP : (applet_name[2]=='m')? /* chmod */
87 CHMOD_APP : CHGRP_APP;
94 while (--argc >= 0 && *argv && (**argv == '-')) {
95 while (stopIt==FALSE && *++(*argv)) {
110 if (argc == 0 || *argv == NULL) {
111 error_msg(too_few_args);
114 if (whichApp == CHMOD_APP) {
119 /* Find the selected group */
120 if (whichApp == CHGRP_APP) {
122 gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */
124 gid = my_getgrnam(groupName);
126 groupName = strchr(*argv, '.');
127 if (groupName == NULL)
128 groupName = strchr(*argv, ':');
131 gid = strtoul(groupName, &p, 10);
133 gid = my_getgrnam(groupName);
139 /* Find the selected user (if appropriate) */
140 if (whichApp == CHOWN_APP) {
141 uid = strtoul(*argv, &p, 10); /* if numeric ... */
143 uid = my_getpwnam(*argv);
147 /* Ok, ready to do the deed now */
149 error_msg_and_die(too_few_args);
152 if (recursive_action (*(++argv), recursiveFlag, FALSE, FALSE,
153 fileAction, fileAction, NULL) == FALSE)
162 c-file-style: "linux"