Stop using TRUE and FALSE for exit status.
[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 "busybox.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 long uid = -1;
37 static 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 int fileAction(const char *fileName, struct stat *statbuf, void* junk)
47 {
48         switch (whichApp) {
49         case CHGRP_APP:
50         case CHOWN_APP:
51 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
52                 if (lchown
53                         (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
54                          (gid == -1) ? statbuf->st_gid : gid) == 0)
55 #else
56                 if (chown
57                         (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
58                          (gid == -1) ? statbuf->st_gid : gid) == 0)
59 #endif
60                 {
61                         return (TRUE);
62                 }
63                 break;
64         case CHMOD_APP:
65                 /* Parse the specified modes */
66                 if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) {
67                         fatalError( "unknown mode: %s\n", theMode);
68                 }
69                 if (chmod(fileName, statbuf->st_mode) == 0)
70                         return (TRUE);
71                 break;
72         }
73         perror(fileName);
74         return (FALSE);
75 }
76
77 int chmod_chown_chgrp_main(int argc, char **argv)
78 {
79         int stopIt = FALSE;
80         int recursiveFlag = FALSE;
81         char *groupName=NULL;
82         char *p=NULL;
83         const char *appUsage;
84
85         whichApp = (strcmp(applet_name, "chown") == 0)? 
86                         CHOWN_APP : (strcmp(applet_name, "chmod") == 0)? 
87                                 CHMOD_APP : CHGRP_APP;
88
89         appUsage = (whichApp == CHOWN_APP)? 
90                         chown_usage : (whichApp == CHMOD_APP) ? chmod_usage : chgrp_usage;
91
92         if (argc < 2)
93                 usage(appUsage);
94         argv++;
95
96         /* Parse options */
97         while (--argc >= 0 && *argv && (**argv == '-')) {
98                 while (stopIt==FALSE && *++(*argv)) {
99                         switch (**argv) {
100                                 case 'R':
101                                         recursiveFlag = TRUE;
102                                         break;
103                                 default:
104                                         theMode=*argv-1;
105                                         stopIt = TRUE;
106                         }
107                 }
108                 if (stopIt==TRUE)
109                         break;
110                 argv++;
111         }
112
113         if (argc == 0 || *argv == NULL) {
114                 errorMsg(too_few_args);
115         }
116
117         if (whichApp == CHMOD_APP) {
118                 if (theMode==NULL)
119                         theMode = *argv;
120         } else {
121
122                 /* Find the selected group */
123                 if (whichApp == CHGRP_APP) {
124                         groupName = *argv;
125                         gid = strtoul(groupName, &p, 10);       /* maybe it's already numeric */
126                         if (groupName == p)
127                                 gid = my_getgrnam(groupName);
128                         if (gid == -1)
129                                 goto bad_group;
130                 } else {
131                         groupName = strchr(*argv, '.');
132                         if (groupName == NULL)
133                                 groupName = strchr(*argv, ':');
134                         if (groupName) {
135                                 *groupName++ = '\0';
136                                 gid = strtoul(groupName, &p, 10);
137                                 if (groupName == p)
138                                         gid = my_getgrnam(groupName);
139                                 if (gid == -1)
140                                         goto bad_group;
141                         } else
142                                 gid = -1;
143                 }
144
145
146                 /* Find the selected user (if appropriate)  */
147                 if (whichApp == CHOWN_APP) {
148                         uid = strtoul(*argv, &p, 10);   /* if numeric ... */
149                         if (*argv == p)
150                                 uid = my_getpwnam(*argv);
151                         if (uid == -1) {
152                                 fatalError( "unknown user name: %s\n", *argv);
153                         }
154                 }
155         }
156
157         /* Ok, ready to do the deed now */
158         if (argc < 1) {
159                 fatalError(too_few_args);
160         }
161         while (argc-- > 1) {
162                 if (recursiveAction (*(++argv), recursiveFlag, FALSE, FALSE, 
163                                         fileAction, fileAction, NULL) == FALSE)
164                         return EXIT_FAILURE;
165         }
166         return EXIT_SUCCESS;
167
168   bad_group:
169         fatalError( "unknown group name: %s\n", groupName);
170 }
171
172 /*
173 Local Variables:
174 c-file-style: "linux"
175 c-basic-offset: 4
176 tab-width: 4
177 End:
178 */