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