summaryrefslogtreecommitdiff
path: root/loginutils
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-06-20 09:01:58 +0000
committerEric Andersen <andersen@codepoet.org>2003-06-20 09:01:58 +0000
commit8876fb2f59a0b515b3121d5894933eef88ce566a (patch)
treef67de9320202043aca8ded20fb80d668c3b0c2d8 /loginutils
parentdfce3536ace2bcd38bdd3731841998ce344d786e (diff)
downloadbusybox-w32-8876fb2f59a0b515b3121d5894933eef88ce566a.tar.gz
busybox-w32-8876fb2f59a0b515b3121d5894933eef88ce566a.tar.bz2
busybox-w32-8876fb2f59a0b515b3121d5894933eef88ce566a.zip
last_patch89 from vodz:
Manuel, I rewrite bb_getopt_ulflags() function for more universal usage. My version support now: - options with arguments (optional arg as GNU extension also) - complementaly and/or incomplementaly and/or incongruously and/or list options - long_opt (all applets may have long option, add supporting is trivial) This realisation full compatibile from your version. Code size grow 480 bytes, but only coreutils/* over compensate this size after using new function. Last patch reduced over 800 bytes and not full applied to all. "mkdir" and "mv" applets have long_opt now for demonstrate trivial addition support long_opt with usage new bb_getopt_ulflags(). Complementaly and/or incomplementaly and/or incongruously and/or list options logic is not trivial, but new "cut" and "grep" applets using this logic for examples with full demostrating. New "grep" applet reduced over 300 bytes. Mark, Also. I removed bug from "grep" applet. $ echo a b | busybox grep -e a b a b a b But right is printing one only. --w vodz
Diffstat (limited to 'loginutils')
-rw-r--r--loginutils/addgroup.c31
-rw-r--r--loginutils/adduser.c40
2 files changed, 26 insertions, 45 deletions
diff --git a/loginutils/addgroup.c b/loginutils/addgroup.c
index 2e8188dec..af1cd7a83 100644
--- a/loginutils/addgroup.c
+++ b/loginutils/addgroup.c
@@ -122,6 +122,17 @@ static int addgroup(const char *filename, char *group, gid_t gid, const char *us
122 return 0; 122 return 0;
123} 123}
124 124
125#ifndef CONFIG_ADDUSER
126static inline void if_i_am_not_root(void)
127{
128 if (geteuid()) {
129 bb_error_msg_and_die( "Only root may add a user or group to the system.");
130 }
131}
132#else
133extern void if_i_am_not_root(void);
134#endif
135
125/* 136/*
126 * addgroup will take a login_name as its first parameter. 137 * addgroup will take a login_name as its first parameter.
127 * 138 *
@@ -131,21 +142,13 @@ static int addgroup(const char *filename, char *group, gid_t gid, const char *us
131 * ________________________________________________________________________ */ 142 * ________________________________________________________________________ */
132int addgroup_main(int argc, char **argv) 143int addgroup_main(int argc, char **argv)
133{ 144{
134 int opt;
135 char *group; 145 char *group;
136 char *user; 146 char *user;
137 gid_t gid = 0; 147 gid_t gid = 0;
138 148
139 /* get remaining args */ 149 /* get remaining args */
140 while ((opt = getopt (argc, argv, "g:")) != -1) { 150 if(bb_getopt_ulflags(argc, argv, "g:", &group)) {
141 switch (opt) { 151 gid = strtol(group, NULL, 10);
142 case 'g':
143 gid = strtol(optarg, NULL, 10);
144 break;
145 default:
146 bb_show_usage();
147 break;
148 }
149 } 152 }
150 153
151 if (optind < argc) { 154 if (optind < argc) {
@@ -161,14 +164,8 @@ int addgroup_main(int argc, char **argv)
161 } else { 164 } else {
162 user = ""; 165 user = "";
163 } 166 }
164 167 if_i_am_not_root();
165 if (geteuid() != 0) {
166 bb_error_msg_and_die
167 ("Only root may add a group to the system.");
168 }
169 168
170 /* werk */ 169 /* werk */
171 return addgroup(bb_path_group_file, group, gid, user); 170 return addgroup(bb_path_group_file, group, gid, user);
172} 171}
173
174/* $Id: addgroup.c,v 1.10 2003/03/19 09:12:20 mjn3 Exp $ */
diff --git a/loginutils/adduser.c b/loginutils/adduser.c
index cfaf860e0..6784d32cc 100644
--- a/loginutils/adduser.c
+++ b/loginutils/adduser.c
@@ -208,9 +208,15 @@ static int adduser(const char *filename, struct passwd *p)
208 208
209 209
210/* return current uid (root is always uid == 0, right?) */ 210/* return current uid (root is always uid == 0, right?) */
211static inline uid_t i_am_not_root(void) 211#ifndef CONFIG_ADDGROUP
212static inline void if_i_am_not_root(void)
213#else
214void if_i_am_not_root(void)
215#endif
212{ 216{
213 return geteuid(); 217 if (geteuid()) {
218 bb_error_msg_and_die( "Only root may add a user or group to the system.");
219 }
214} 220}
215 221
216/* 222/*
@@ -224,11 +230,10 @@ static inline uid_t i_am_not_root(void)
224 * ________________________________________________________________________ */ 230 * ________________________________________________________________________ */
225int adduser_main(int argc, char **argv) 231int adduser_main(int argc, char **argv)
226{ 232{
227 int opt;
228 const char *login; 233 const char *login;
229 const char *gecos; 234 const char *gecos = default_gecos;
230 const char *home = NULL; 235 const char *home = NULL;
231 const char *shell; 236 const char *shell = default_shell;
232 237
233 struct passwd pw; 238 struct passwd pw;
234 239
@@ -236,30 +241,11 @@ int adduser_main(int argc, char **argv)
236 if (argc < 2) { 241 if (argc < 2) {
237 bb_show_usage(); 242 bb_show_usage();
238 } 243 }
239 gecos = default_gecos;
240 shell = default_shell;
241
242 /* get args */ 244 /* get args */
243 while ((opt = getopt (argc, argv, "h:g:s:")) != -1) 245 bb_getopt_ulflags(argc, argv, "h:g:s:", &home, &gecos, &shell);
244 switch (opt) {
245 case 'h':
246 home = optarg;
247 break;
248 case 'g':
249 gecos = optarg;
250 break;
251 case 's':
252 shell = optarg;
253 break;
254 default:
255 bb_show_usage();
256 break;
257 }
258 246
259 /* got root? */ 247 /* got root? */
260 if (i_am_not_root()) { 248 if_i_am_not_root();
261 bb_error_msg_and_die( "Only root may add a user or group to the system.");
262 }
263 249
264 /* get login */ 250 /* get login */
265 if (optind >= argc) { 251 if (optind >= argc) {
@@ -288,5 +274,3 @@ int adduser_main(int argc, char **argv)
288 /* grand finale */ 274 /* grand finale */
289 return adduser(bb_path_passwd_file, &pw); 275 return adduser(bb_path_passwd_file, &pw);
290} 276}
291
292/* $Id: adduser.c,v 1.5 2003/03/19 09:12:20 mjn3 Exp $ */