Some formatting updates (ran the code through indent)
[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                         fprintf(stderr, "%s: unknown mode: %s\n", invocationName,
85                                         theMode);
86                         exit(FALSE);
87                 }
88                 if (chmod(fileName, statbuf->st_mode) == 0)
89                         return (TRUE);
90                 break;
91         }
92         perror(fileName);
93         return (FALSE);
94 }
95
96 int chmod_chown_chgrp_main(int argc, char **argv)
97 {
98         int recursiveFlag = FALSE;
99         char *groupName;
100         char *p;
101         const char *appUsage;
102
103         whichApp =
104                 (strcmp(*argv, "chown") ==
105                  0) ? CHOWN_APP : (strcmp(*argv,
106                                                                   "chmod") == 0) ? CHMOD_APP : CHGRP_APP;
107
108         appUsage =
109                 (whichApp == CHOWN_APP) ? chown_usage : (whichApp ==
110                                                                                                  CHMOD_APP) ? chmod_usage :
111                 chgrp_usage;
112
113         if (argc < 2)
114                 usage(appUsage);
115         invocationName = *argv;
116         argc--;
117         argv++;
118
119         /* Parse options */
120         while (**argv == '-') {
121                 while (*++(*argv))
122                         switch (**argv) {
123                         case 'R':
124                                 recursiveFlag = TRUE;
125                                 break;
126                         default:
127                                 fprintf(stderr, invalid_option, invocationName, **argv);
128                                 usage(appUsage);
129                         }
130                 argc--;
131                 argv++;
132         }
133
134         if (whichApp == CHMOD_APP) {
135                 theMode = *argv;
136         } else {
137
138                 /* Find the selected group */
139                 if (whichApp == CHGRP_APP) {
140                         groupName = *argv;
141                         gid = strtoul(groupName, &p, 10);       /* maybe it's already numeric */
142                         if (groupName == p)
143                                 gid = my_getgrnam(groupName);
144                         if (gid == -1)
145                                 goto bad_group;
146                 } else {
147                         groupName = strchr(*argv, '.');
148                         if (groupName) {
149                                 *groupName++ = '\0';
150                                 gid = strtoul(groupName, &p, 10);
151                                 if (groupName == p)
152                                         gid = my_getgrnam(groupName);
153                                 if (gid == -1)
154                                         goto bad_group;
155                         } else
156                                 gid = -1;
157                 }
158
159
160                 /* Find the selected user (if appropriate)  */
161                 if (whichApp == CHOWN_APP) {
162                         uid = strtoul(*argv, &p, 10);   /* if numeric ... */
163                         if (*argv == p)
164                                 uid = my_getpwnam(*argv);
165                         if (uid == -1) {
166                                 fprintf(stderr, "%s: unknown user name: %s\n",
167                                                 invocationName, *argv);
168                                 exit(FALSE);
169                         }
170                 }
171         }
172
173         /* Ok, ready to do the deed now */
174         if (argc <= 1) {
175                 fprintf(stderr, "%s: too few arguments\n", invocationName);
176                 exit(FALSE);
177         }
178         while (argc-- > 1) {
179                 if (recursiveAction
180                         (*(++argv), recursiveFlag, TRUE, FALSE, fileAction,
181                          fileAction) == FALSE)
182                         exit(FALSE);
183         }
184         exit(TRUE);
185
186   bad_group:
187         fprintf(stderr, "%s: unknown group name: %s\n", invocationName,
188                         groupName);
189         exit(FALSE);
190 }