A few updates (including the cp fix the Craig has been looking for)
[oweals/busybox.git] / chmod_chown_chgrp.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chown/chmod/chgrp implementation for busybox
4  *
5  *
6  * Copyright (C) 1999 by Lineo, inc.
7  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #include "internal.h"
26 #define BB_DECLARE_EXTERN
27 #define bb_need_invalid_option
28 #include "messages.c"
29
30 #include <stdio.h>
31 #include <grp.h>
32 #include <pwd.h>
33
34
35 static uid_t uid = -1;
36 static gid_t gid = -1;
37 static int whichApp;
38 static char *invocationName = NULL;
39 static char *theMode = NULL;
40
41
42 #define CHGRP_APP   1
43 #define CHOWN_APP   2
44 #define CHMOD_APP   3
45
46 static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n"
47         "Change the group membership of each FILE to GROUP.\n"
48
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"
53
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"
58
59         "one or more of the letters rwxst.\n\n"
60         "\nOptions:\n\t-R\tchange files and directories recursively.\n";
61
62
63 static int fileAction(const char *fileName, struct stat *statbuf)
64 {
65         switch (whichApp) {
66         case CHGRP_APP:
67         case CHOWN_APP:
68 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
69                 if (lchown
70                         (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
71                          (gid == -1) ? statbuf->st_gid : gid) == 0)
72 #else
73                 if (chown
74                         (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
75                          (gid == -1) ? statbuf->st_gid : gid) == 0)
76 #endif
77                 {
78                         return (TRUE);
79                 }
80                 break;
81         case CHMOD_APP:
82                 /* Parse the specified modes */
83                 if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) {
84                         fatalError( "%s: unknown mode: %s\n", invocationName, theMode);
85                 }
86                 if (chmod(fileName, statbuf->st_mode) == 0)
87                         return (TRUE);
88                 break;
89         }
90         perror(fileName);
91         return (FALSE);
92 }
93
94 int chmod_chown_chgrp_main(int argc, char **argv)
95 {
96         int recursiveFlag = FALSE;
97         char *groupName;
98         char *p;
99         const char *appUsage;
100
101         whichApp =
102                 (strcmp(*argv, "chown") == 0)? 
103                         CHOWN_APP : (strcmp(*argv, "chmod") == 0)? 
104                                 CHMOD_APP : CHGRP_APP;
105
106         appUsage =
107                 (whichApp == CHOWN_APP)? 
108                         chown_usage : (whichApp == CHMOD_APP) ? chmod_usage : chgrp_usage;
109
110         if (argc < 2)
111                 usage(appUsage);
112         invocationName = *argv;
113         argc--;
114         argv++;
115
116         /* Parse options */
117         while (**argv == '-') {
118                 while (*++(*argv))
119                         switch (**argv) {
120                         case 'R':
121                                 recursiveFlag = TRUE;
122                                 break;
123                         default:
124                                 fprintf(stderr, invalid_option, invocationName, **argv);
125                                 usage(appUsage);
126                         }
127                 argc--;
128                 argv++;
129         }
130
131         if (whichApp == CHMOD_APP) {
132                 theMode = *argv;
133         } else {
134
135                 /* Find the selected group */
136                 if (whichApp == CHGRP_APP) {
137                         groupName = *argv;
138                         gid = strtoul(groupName, &p, 10);       /* maybe it's already numeric */
139                         if (groupName == p)
140                                 gid = my_getgrnam(groupName);
141                         if (gid == -1)
142                                 goto bad_group;
143                 } else {
144                         groupName = strchr(*argv, '.');
145                         if (groupName) {
146                                 *groupName++ = '\0';
147                                 gid = strtoul(groupName, &p, 10);
148                                 if (groupName == p)
149                                         gid = my_getgrnam(groupName);
150                                 if (gid == -1)
151                                         goto bad_group;
152                         } else
153                                 gid = -1;
154                 }
155
156
157                 /* Find the selected user (if appropriate)  */
158                 if (whichApp == CHOWN_APP) {
159                         uid = strtoul(*argv, &p, 10);   /* if numeric ... */
160                         if (*argv == p)
161                                 uid = my_getpwnam(*argv);
162                         if (uid == -1) {
163                                 fatalError( "%s: unknown user name: %s\n", 
164                                                 invocationName, *argv);
165                         }
166                 }
167         }
168
169         /* Ok, ready to do the deed now */
170         if (argc <= 1) {
171                 fatalError( "%s: too few arguments\n", invocationName);
172         }
173         while (argc-- > 1) {
174                 if (recursiveAction
175                         (*(++argv), recursiveFlag, TRUE, FALSE, fileAction,
176                          fileAction) == FALSE)
177                         exit(FALSE);
178         }
179         exit(TRUE);
180
181   bad_group:
182         fatalError( "%s: unknown group name: %s\n", invocationName, groupName);
183 }
184
185 /*
186 Local Variables:
187 c-file-style: "linux"
188 c-basic-offset: 4
189 tab-width: 4
190 End:
191 */