Always report the applet name when doing error reporting.
[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,2000 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 #define bb_need_too_few_args
29 #include "messages.c"
30
31 #include <stdio.h>
32 #include <grp.h>
33 #include <pwd.h>
34
35
36 static unsigned long uid = -1;
37 static unsigned long gid = -1;
38 static int whichApp;
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"
47 #ifndef BB_FEATURE_TRIVIAL_HELP
48         "\nChange the group membership of each FILE to GROUP.\n"
49         "\nOptions:\n\t-R\tChanges files and directories recursively.\n"
50 #endif
51         ;
52 static const char chown_usage[] =
53         "chown [OPTION]...  OWNER[<.|:>[GROUP] FILE...\n"
54 #ifndef BB_FEATURE_TRIVIAL_HELP
55         "\nChange the owner and/or group of each FILE to OWNER and/or GROUP.\n"
56         "\nOptions:\n\t-R\tChanges files and directories recursively.\n"
57 #endif
58         ;
59 static const char chmod_usage[] =
60         "chmod [-R] MODE[,MODE]... FILE...\n"
61 #ifndef BB_FEATURE_TRIVIAL_HELP
62         "\nEach MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
63         "one or more of the letters rwxst.\n\n"
64         "\nOptions:\n\t-R\tChanges files and directories recursively.\n"
65 #endif
66         ;
67
68
69 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
70 {
71         switch (whichApp) {
72         case CHGRP_APP:
73         case CHOWN_APP:
74 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
75                 if (lchown
76                         (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
77                          (gid == -1) ? statbuf->st_gid : gid) == 0)
78 #else
79                 if (chown
80                         (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
81                          (gid == -1) ? statbuf->st_gid : gid) == 0)
82 #endif
83                 {
84                         return (TRUE);
85                 }
86                 break;
87         case CHMOD_APP:
88                 /* Parse the specified modes */
89                 if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) {
90                         fatalError( "unknown mode: %s\n", theMode);
91                 }
92                 if (chmod(fileName, statbuf->st_mode) == 0)
93                         return (TRUE);
94                 break;
95         }
96         perror(fileName);
97         return (FALSE);
98 }
99
100 int chmod_chown_chgrp_main(int argc, char **argv)
101 {
102         int recursiveFlag = FALSE;
103         char *groupName=NULL;
104         char *p=NULL;
105         const char *appUsage;
106
107         whichApp = (strcmp(applet_name, "chown") == 0)? 
108                         CHOWN_APP : (strcmp(applet_name, "chmod") == 0)? 
109                                 CHMOD_APP : CHGRP_APP;
110
111         appUsage = (whichApp == CHOWN_APP)? 
112                         chown_usage : (whichApp == CHMOD_APP) ? chmod_usage : chgrp_usage;
113
114         if (argc < 2)
115                 usage(appUsage);
116         argv++;
117
118         /* Parse options */
119         while (--argc >= 0 && *argv && (**argv == '-')) {
120                 while (*++(*argv)) {
121                         switch (**argv) {
122                                 case 'R':
123                                         recursiveFlag = TRUE;
124                                         break;
125                                 default:
126                                         fprintf(stderr, invalid_option, applet_name, **argv);
127                                         usage(appUsage);
128                         }
129                 }
130                 argv++;
131         }
132
133         if (argc == 0 || *argv == NULL) {
134                 fprintf(stderr, too_few_args, applet_name);
135                 usage(appUsage);
136         }
137
138         if (whichApp == CHMOD_APP) {
139                 theMode = *argv;
140         } else {
141
142                 /* Find the selected group */
143                 if (whichApp == CHGRP_APP) {
144                         groupName = *argv;
145                         gid = strtoul(groupName, &p, 10);       /* maybe it's already numeric */
146                         if (groupName == p)
147                                 gid = my_getgrnam(groupName);
148                         if (gid == -1)
149                                 goto bad_group;
150                 } else {
151                         groupName = strchr(*argv, '.');
152                         if (groupName == NULL)
153                                 groupName = strchr(*argv, ':');
154                         if (groupName) {
155                                 *groupName++ = '\0';
156                                 gid = strtoul(groupName, &p, 10);
157                                 if (groupName == p)
158                                         gid = my_getgrnam(groupName);
159                                 if (gid == -1)
160                                         goto bad_group;
161                         } else
162                                 gid = -1;
163                 }
164
165
166                 /* Find the selected user (if appropriate)  */
167                 if (whichApp == CHOWN_APP) {
168                         uid = strtoul(*argv, &p, 10);   /* if numeric ... */
169                         if (*argv == p)
170                                 uid = my_getpwnam(*argv);
171                         if (uid == -1) {
172                                 fatalError( "unknown user name: %s\n", *argv);
173                         }
174                 }
175         }
176
177         /* Ok, ready to do the deed now */
178         if (argc <= 1) {
179                 fatalError( "too few arguments\n");
180         }
181         while (argc-- > 1) {
182                 if (recursiveAction (*(++argv), recursiveFlag, FALSE, FALSE, 
183                                         fileAction, fileAction, NULL) == FALSE)
184                         exit(FALSE);
185         }
186         exit(TRUE);
187
188   bad_group:
189         fatalError( "unknown group name: %s\n", groupName);
190 }
191
192 /*
193 Local Variables:
194 c-file-style: "linux"
195 c-basic-offset: 4
196 tab-width: 4
197 End:
198 */