A few minor updates. ;-)
[oweals/busybox.git] / chmod_chown_chgrp.c
1 /*
2  * Mini chown/chmod/chgrp implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #define BB_DECLARE_EXTERN
26 #define bb_need_invalid_option
27 #include "messages.c"
28
29 #include <stdio.h>
30 #include <grp.h>
31 #include <pwd.h>
32
33
34 static uid_t uid = -1;
35 static gid_t gid = -1;
36 static int whichApp;
37 static char* invocationName=NULL;
38 static char* theMode=NULL;
39
40
41 #define CHGRP_APP   1
42 #define CHOWN_APP   2
43 #define CHMOD_APP   3
44
45 static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n"
46     "Change the group membership of each FILE to GROUP.\n"
47     "\nOptions:\n\t-R\tchange files and directories recursively\n";
48 static const char chown_usage[] = "chown [OPTION]...  OWNER[.[GROUP] FILE...\n\n"
49     "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
50     "\nOptions:\n\t-R\tchange files and directories recursively\n";
51 static const char chmod_usage[] = "chmod [-R] MODE[,MODE]... FILE...\n\n"
52 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
53 "one or more of the letters rwxst.\n\n"
54  "\nOptions:\n\t-R\tchange files and directories recursively.\n";
55
56
57 static int fileAction(const char *fileName, struct stat* statbuf)
58 {
59     switch (whichApp) {
60         case CHGRP_APP:
61         case CHOWN_APP:
62 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
63             if (lchown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, 
64                         (gid==-1)? statbuf->st_gid : gid) == 0) 
65 #else
66             if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, 
67                         (gid==-1)? statbuf->st_gid : gid) == 0) 
68 #endif
69             {
70                 return( TRUE);
71             }
72             break;
73         case CHMOD_APP:
74             /* Parse the specified modes */
75             if ( parse_mode(theMode, &(statbuf->st_mode)) == FALSE ) {
76                 fprintf(stderr, "%s: unknown mode: %s\n", invocationName, theMode);
77                 exit( FALSE);
78             }
79             if (chmod(fileName, statbuf->st_mode) == 0)
80                 return( TRUE);
81             break;
82     }
83     perror(fileName);
84     return( FALSE);
85 }
86
87 int chmod_chown_chgrp_main(int argc, char **argv)
88 {
89     int recursiveFlag=FALSE;
90     char *groupName;
91     char *p;
92     const char *appUsage;
93
94     whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP; 
95
96     appUsage = (whichApp==CHOWN_APP)? chown_usage : (whichApp==CHMOD_APP)? chmod_usage : chgrp_usage;
97
98     if (argc < 2) 
99         usage( appUsage);
100     invocationName = *argv;
101     argc--;
102     argv++;
103
104     /* Parse options */
105     while (**argv == '-') {
106         while (*++(*argv)) switch (**argv) {
107             case 'R':
108                 recursiveFlag = TRUE;
109                 break;
110             default:
111                 fprintf(stderr, invalid_option, invocationName, **argv);
112                 usage( appUsage);
113         }
114         argc--;
115         argv++;
116     }
117     
118     if ( whichApp == CHMOD_APP ) {
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) {
133                 *groupName++ = '\0';
134                 gid = strtoul(groupName, &p, 10);
135                 if (groupName == p)
136                     gid = my_getgrnam(groupName);
137                 if (gid == -1)
138                     goto bad_group;
139             } else
140                 gid = -1;
141         }
142
143
144         /* Find the selected user (if appropriate)  */
145         if (whichApp==CHOWN_APP) {
146             uid = strtoul(*argv, &p, 10); /* if numeric ...*/
147             if (*argv == p)
148                 uid = my_getpwnam(*argv);
149             if (uid == -1) {
150                 fprintf(stderr, "%s: unknown user name: %s\n", invocationName, *argv);
151                 exit( FALSE);
152             }
153         }
154     }
155     
156     /* Ok, ready to do the deed now */
157     if (argc <= 1) {
158         fprintf(stderr, "%s: too few arguments\n", invocationName);
159         exit( FALSE);
160     }
161     while (argc-- > 1) {
162         if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE)
163             exit( FALSE);
164     }
165     exit(TRUE);
166
167 bad_group:
168     fprintf(stderr, "%s: unknown group name: %s\n", invocationName, groupName);
169     exit( FALSE);
170 }
171
172