summaryrefslogtreecommitdiff
path: root/chmod_chown_chgrp.c
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
committerErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
commite49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /chmod_chown_chgrp.c
parentc0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff)
downloadbusybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'chmod_chown_chgrp.c')
-rw-r--r--chmod_chown_chgrp.c238
1 files changed, 128 insertions, 110 deletions
diff --git a/chmod_chown_chgrp.c b/chmod_chown_chgrp.c
index b4c5da936..d5e67b599 100644
--- a/chmod_chown_chgrp.c
+++ b/chmod_chown_chgrp.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini chown/chmod/chgrp implementation for busybox 3 * Mini chown/chmod/chgrp implementation for busybox
3 * 4 *
@@ -34,8 +35,8 @@
34static uid_t uid = -1; 35static uid_t uid = -1;
35static gid_t gid = -1; 36static gid_t gid = -1;
36static int whichApp; 37static int whichApp;
37static char* invocationName=NULL; 38static char *invocationName = NULL;
38static char* theMode=NULL; 39static char *theMode = NULL;
39 40
40 41
41#define CHGRP_APP 1 42#define CHGRP_APP 1
@@ -43,130 +44,147 @@ static char* theMode=NULL;
43#define CHMOD_APP 3 44#define CHMOD_APP 3
44 45
45static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n" 46static const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n\n"
46 "Change the group membership of each FILE to GROUP.\n" 47 "Change the group membership of each FILE to GROUP.\n"
47 "\nOptions:\n\t-R\tchange files and directories recursively\n"; 48
48static const char chown_usage[] = "chown [OPTION]... OWNER[.[GROUP] FILE...\n\n" 49 "\nOptions:\n\t-R\tchange files and directories recursively\n";
49 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n" 50static const char chown_usage[] =
50 "\nOptions:\n\t-R\tchange files and directories recursively\n"; 51 "chown [OPTION]... OWNER[.[GROUP] FILE...\n\n"
51static const char chmod_usage[] = "chmod [-R] MODE[,MODE]... FILE...\n\n" 52 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
52"Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n" 53
53"one or more of the letters rwxst.\n\n" 54 "\nOptions:\n\t-R\tchange files and directories recursively\n";
54 "\nOptions:\n\t-R\tchange files and directories recursively.\n"; 55static const char chmod_usage[] =
55 56 "chmod [-R] MODE[,MODE]... FILE...\n\n"
56 57 "Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n"
57static int fileAction(const char *fileName, struct stat* statbuf) 58
59 "one or more of the letters rwxst.\n\n"
60 "\nOptions:\n\t-R\tchange files and directories recursively.\n";
61
62
63static int fileAction(const char *fileName, struct stat *statbuf)
58{ 64{
59 switch (whichApp) { 65 switch (whichApp) {
60 case CHGRP_APP: 66 case CHGRP_APP:
61 case CHOWN_APP: 67 case CHOWN_APP:
62#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) 68#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
63 if (lchown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, 69 if (lchown
64 (gid==-1)? statbuf->st_gid : gid) == 0) 70 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
71 (gid == -1) ? statbuf->st_gid : gid) == 0)
65#else 72#else
66 if (chown(fileName, (whichApp==CHOWN_APP)? uid : statbuf->st_uid, 73 if (chown
67 (gid==-1)? statbuf->st_gid : gid) == 0) 74 (fileName, (whichApp == CHOWN_APP) ? uid : statbuf->st_uid,
75 (gid == -1) ? statbuf->st_gid : gid) == 0)
68#endif 76#endif
69 { 77 {
70 return( TRUE); 78 return (TRUE);
71 } 79 }
72 break; 80 break;
73 case CHMOD_APP: 81 case CHMOD_APP:
74 /* Parse the specified modes */ 82 /* Parse the specified modes */
75 if ( parse_mode(theMode, &(statbuf->st_mode)) == FALSE ) { 83 if (parse_mode(theMode, &(statbuf->st_mode)) == FALSE) {
76 fprintf(stderr, "%s: unknown mode: %s\n", invocationName, theMode); 84 fprintf(stderr, "%s: unknown mode: %s\n", invocationName,
77 exit( FALSE); 85 theMode);
78 } 86 exit(FALSE);
79 if (chmod(fileName, statbuf->st_mode) == 0) 87 }
80 return( TRUE); 88 if (chmod(fileName, statbuf->st_mode) == 0)
81 break; 89 return (TRUE);
82 } 90 break;
83 perror(fileName); 91 }
84 return( FALSE); 92 perror(fileName);
93 return (FALSE);
85} 94}
86 95
87int chmod_chown_chgrp_main(int argc, char **argv) 96int chmod_chown_chgrp_main(int argc, char **argv)
88{ 97{
89 int recursiveFlag=FALSE; 98 int recursiveFlag = FALSE;
90 char *groupName; 99 char *groupName;
91 char *p; 100 char *p;
92 const char *appUsage; 101 const char *appUsage;
93 102
94 whichApp = (strcmp(*argv, "chown")==0)? CHOWN_APP : (strcmp(*argv, "chmod")==0)? CHMOD_APP : CHGRP_APP; 103 whichApp =
95 104 (strcmp(*argv, "chown") ==
96 appUsage = (whichApp==CHOWN_APP)? chown_usage : (whichApp==CHMOD_APP)? chmod_usage : chgrp_usage; 105 0) ? CHOWN_APP : (strcmp(*argv,
97 106 "chmod") == 0) ? CHMOD_APP : CHGRP_APP;
98 if (argc < 2) 107
99 usage( appUsage); 108 appUsage =
100 invocationName = *argv; 109 (whichApp == CHOWN_APP) ? chown_usage : (whichApp ==
101 argc--; 110 CHMOD_APP) ? chmod_usage :
102 argv++; 111 chgrp_usage;
103 112
104 /* Parse options */ 113 if (argc < 2)
105 while (**argv == '-') { 114 usage(appUsage);
106 while (*++(*argv)) switch (**argv) { 115 invocationName = *argv;
107 case 'R':
108 recursiveFlag = TRUE;
109 break;
110 default:
111 fprintf(stderr, invalid_option, invocationName, **argv);
112 usage( appUsage);
113 }
114 argc--; 116 argc--;
115 argv++; 117 argv++;
116 } 118
117 119 /* Parse options */
118 if ( whichApp == CHMOD_APP ) { 120 while (**argv == '-') {
119 theMode = *argv; 121 while (*++(*argv))
120 } else { 122 switch (**argv) {
121 123 case 'R':
122 /* Find the selected group */ 124 recursiveFlag = TRUE;
123 if ( whichApp==CHGRP_APP ) { 125 break;
124 groupName = *argv; 126 default:
125 gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */ 127 fprintf(stderr, invalid_option, invocationName, **argv);
126 if (groupName == p) 128 usage(appUsage);
127 gid = my_getgrnam(groupName); 129 }
128 if (gid == -1) 130 argc--;
129 goto bad_group; 131 argv++;
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 } 132 }
142 133
134 if (whichApp == CHMOD_APP) {
135 theMode = *argv;
136 } else {
143 137
144 /* Find the selected user (if appropriate) */ 138 /* Find the selected group */
145 if (whichApp==CHOWN_APP) { 139 if (whichApp == CHGRP_APP) {
146 uid = strtoul(*argv, &p, 10); /* if numeric ...*/ 140 groupName = *argv;
147 if (*argv == p) 141 gid = strtoul(groupName, &p, 10); /* maybe it's already numeric */
148 uid = my_getpwnam(*argv); 142 if (groupName == p)
149 if (uid == -1) { 143 gid = my_getgrnam(groupName);
150 fprintf(stderr, "%s: unknown user name: %s\n", invocationName, *argv); 144 if (gid == -1)
151 exit( FALSE); 145 goto bad_group;
152 } 146 } else {
147 groupName = strchr(*argv, '.');
148 if (groupName) {
149 *groupName++ = '\0';
150 gid = strtoul(groupName, &p, 10);
151 if (groupName == p)
152 gid = my_getgrnam(groupName);
153 if (gid == -1)
154 goto bad_group;
155 } else
156 gid = -1;
157 }
158
159
160 /* Find the selected user (if appropriate) */
161 if (whichApp == CHOWN_APP) {
162 uid = strtoul(*argv, &p, 10); /* if numeric ... */
163 if (*argv == p)
164 uid = my_getpwnam(*argv);
165 if (uid == -1) {
166 fprintf(stderr, "%s: unknown user name: %s\n",
167 invocationName, *argv);
168 exit(FALSE);
169 }
170 }
153 } 171 }
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
167bad_group:
168 fprintf(stderr, "%s: unknown group name: %s\n", invocationName, groupName);
169 exit( FALSE);
170}
171 172
173 /* Ok, ready to do the deed now */
174 if (argc <= 1) {
175 fprintf(stderr, "%s: too few arguments\n", invocationName);
176 exit(FALSE);
177 }
178 while (argc-- > 1) {
179 if (recursiveAction
180 (*(++argv), recursiveFlag, TRUE, FALSE, fileAction,
181 fileAction) == FALSE)
182 exit(FALSE);
183 }
184 exit(TRUE);
172 185
186 bad_group:
187 fprintf(stderr, "%s: unknown group name: %s\n", invocationName,
188 groupName);
189 exit(FALSE);
190}