a91b0a365e8e6e63569d8df63b428103e8702dd7
[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 <stdio.h>
25 #include <grp.h>
26 #include <pwd.h>
27 #include "internal.h"
28
29
30 static uid_t uid=-1;
31 static gid_t gid=-1;
32 static int whichApp;
33 static char* invocationName=NULL;
34 static mode_t mode=0644;
35
36
37 #define CHGRP_APP   1
38 #define CHOWN_APP   2
39 #define CHMOD_APP   3
40
41 static const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n"
42     "Change the group membership of each FILE to GROUP.\n"
43     "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
44 static const char chown_usage[] = "[OPTION]...  OWNER[.[GROUP] FILE...\n"
45     "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
46     "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
47 static const char chmod_usage[] = "[-R] MODE[,MODE]... FILE...\n"
48 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
49 "one or more of the letters rwxst.\n\n"
50  "\t-R\tchange files and directories recursively.\n";
51
52
53 static int fileAction(const char *fileName, struct stat* statbuf)
54 {
55     switch (whichApp) {
56         case CHGRP_APP:
57         case CHOWN_APP:
58             if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, 
59                         (gid==-1)? statbuf->st_gid : gid) == 0) {
60                 return( TRUE);
61             }
62             break;
63         case CHMOD_APP:
64             if (chmod(fileName, mode) == 0)
65                 return( TRUE);
66             break;
67     }
68     perror(fileName);
69     return( FALSE);
70 }
71
72 int chmod_chown_chgrp_main(int argc, char **argv)
73 {
74     int recursiveFlag=FALSE;
75     char *groupName;
76
77     whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP; 
78
79     if (argc < 2) {
80         fprintf(stderr, "Usage: %s %s", *argv, 
81                 (whichApp==TRUE)? chown_usage : chgrp_usage);
82         exit( FALSE);
83     }
84     invocationName=*argv;
85     argc--;
86     argv++;
87
88     /* Parse options */
89     while (**argv == '-') {
90         while (*++(*argv)) switch (**argv) {
91             case 'R':
92                 recursiveFlag = TRUE;
93                 break;
94             default:
95                 fprintf(stderr, "Unknown option: %c\n", **argv);
96                 exit( FALSE);
97         }
98         argc--;
99         argv++;
100     }
101     
102     if ( whichApp == CHMOD_APP ) {
103         /* Find the specified modes */
104         mode = ( 0 & (S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO));
105         if ( parse_mode(*argv, &mode) == FALSE ) {
106             fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, *argv);
107             exit( FALSE);
108         }
109     } else {
110
111         /* Find the selected group */
112         if ( whichApp==CHGRP_APP ) {
113             groupName = *argv;
114             gid = my_getgrnam(groupName);
115             if (gid == -1)
116                 goto bad_group;
117         } else {
118             groupName = strchr(*argv, '.');
119             if (groupName) {
120                 *groupName++ = '\0';
121                 gid = my_getgrnam(groupName);
122                 if (gid == -1)
123                     goto bad_group;
124             } else
125                 gid = -1;
126         }
127
128
129         /* Find the selected user (if appropriate)  */
130         if (whichApp==CHOWN_APP) {
131             uid = my_getpwnam(*argv);
132             if (uid == -1) {
133                 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
134                 exit( FALSE);
135             }
136         }
137     }
138     
139     /* Ok, ready to do the deed now */
140     if (argc <= 1) {
141         fprintf(stderr, "%s: too few arguments", invocationName);
142         exit( FALSE);
143     }
144     while (argc-- > 1) {
145         if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE)
146             exit( FALSE);
147     }
148     exit(TRUE);
149
150 bad_group:
151     fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
152     exit( FALSE);
153 }
154
155