773f4b0c0a87b7f267f0dee6be99d7c6c2210c7d
[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 char* theMode=NULL;
35
36
37 #define CHGRP_APP   1
38 #define CHOWN_APP   2
39 #define CHMOD_APP   3
40
41 static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n"
42     "Change the group membership of each FILE to GROUP.\n"
43     "\nOptions:\n\t-R\tchange files and directories recursively\n";
44 static const char chown_usage[] = "chown [OPTION]...  OWNER[.[GROUP] FILE...\n\n"
45     "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
46     "\nOptions:\n\t-R\tchange files and directories recursively\n";
47 static const char chmod_usage[] = "chmod [-R] MODE[,MODE]... FILE...\n\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  "\nOptions:\n\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 (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
59             if (lchown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, 
60                         (gid==-1)? statbuf->st_gid : gid) == 0) 
61 #else
62             if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, 
63                         (gid==-1)? statbuf->st_gid : gid) == 0) 
64 #endif
65             {
66                 return( TRUE);
67             }
68             break;
69         case CHMOD_APP:
70             /* Parse the specified modes */
71             if ( parse_mode(theMode, &(statbuf->st_mode)) == FALSE ) {
72                 fprintf(stderr, "%s: Unknown mode: %s\n", invocationName, theMode);
73                 exit( FALSE);
74             }
75             if (chmod(fileName, statbuf->st_mode) == 0)
76                 return( TRUE);
77             break;
78     }
79     perror(fileName);
80     return( FALSE);
81 }
82
83 int chmod_chown_chgrp_main(int argc, char **argv)
84 {
85     int recursiveFlag=FALSE;
86     char *groupName;
87     const char *appUsage;
88
89     whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP; 
90
91     appUsage = (whichApp==CHOWN_APP)? chown_usage : (whichApp==CHMOD_APP)? chmod_usage : chgrp_usage;
92
93     if (argc < 2) 
94         usage( appUsage);
95     invocationName = *argv;
96     argc--;
97     argv++;
98
99     /* Parse options */
100     while (**argv == '-') {
101         while (*++(*argv)) switch (**argv) {
102             case 'R':
103                 recursiveFlag = TRUE;
104                 break;
105             default:
106                 fprintf(stderr, "Unknown option: %c\n", **argv);
107                 usage( appUsage);
108         }
109         argc--;
110         argv++;
111     }
112     
113     if ( whichApp == CHMOD_APP ) {
114         theMode = *argv;
115     } else {
116
117         /* Find the selected group */
118         if ( whichApp==CHGRP_APP ) {
119             groupName = *argv;
120             gid = my_getgrnam(groupName);
121             if (gid == -1)
122                 goto bad_group;
123         } else {
124             groupName = strchr(*argv, '.');
125             if (groupName) {
126                 *groupName++ = '\0';
127                 gid = my_getgrnam(groupName);
128                 if (gid == -1)
129                     goto bad_group;
130             } else
131                 gid = -1;
132         }
133
134
135         /* Find the selected user (if appropriate)  */
136         if (whichApp==CHOWN_APP) {
137             uid = my_getpwnam(*argv);
138             if (uid == -1) {
139                 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
140                 exit( FALSE);
141             }
142         }
143     }
144     
145     /* Ok, ready to do the deed now */
146     if (argc <= 1) {
147         fprintf(stderr, "%s: too few arguments", invocationName);
148         exit( FALSE);
149     }
150     while (argc-- > 1) {
151         if (recursiveAction( *(++argv), recursiveFlag, TRUE, FALSE, fileAction, fileAction)==FALSE)
152             exit( FALSE);
153     }
154     exit(TRUE);
155
156 bad_group:
157     fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
158     exit( FALSE);
159 }
160
161