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