From 8876fb2f59a0b515b3121d5894933eef88ce566a Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Fri, 20 Jun 2003 09:01:58 +0000 Subject: 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 --- loginutils/addgroup.c | 31 ++++++++++++++----------------- loginutils/adduser.c | 40 ++++++++++++---------------------------- 2 files changed, 26 insertions(+), 45 deletions(-) (limited to 'loginutils') 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 return 0; } +#ifndef CONFIG_ADDUSER +static inline void if_i_am_not_root(void) +{ + if (geteuid()) { + bb_error_msg_and_die( "Only root may add a user or group to the system."); + } +} +#else +extern void if_i_am_not_root(void); +#endif + /* * addgroup will take a login_name as its first parameter. * @@ -131,21 +142,13 @@ static int addgroup(const char *filename, char *group, gid_t gid, const char *us * ________________________________________________________________________ */ int addgroup_main(int argc, char **argv) { - int opt; char *group; char *user; gid_t gid = 0; /* get remaining args */ - while ((opt = getopt (argc, argv, "g:")) != -1) { - switch (opt) { - case 'g': - gid = strtol(optarg, NULL, 10); - break; - default: - bb_show_usage(); - break; - } + if(bb_getopt_ulflags(argc, argv, "g:", &group)) { + gid = strtol(group, NULL, 10); } if (optind < argc) { @@ -161,14 +164,8 @@ int addgroup_main(int argc, char **argv) } else { user = ""; } - - if (geteuid() != 0) { - bb_error_msg_and_die - ("Only root may add a group to the system."); - } + if_i_am_not_root(); /* werk */ return addgroup(bb_path_group_file, group, gid, user); } - -/* $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) /* return current uid (root is always uid == 0, right?) */ -static inline uid_t i_am_not_root(void) +#ifndef CONFIG_ADDGROUP +static inline void if_i_am_not_root(void) +#else +void if_i_am_not_root(void) +#endif { - return geteuid(); + if (geteuid()) { + bb_error_msg_and_die( "Only root may add a user or group to the system."); + } } /* @@ -224,11 +230,10 @@ static inline uid_t i_am_not_root(void) * ________________________________________________________________________ */ int adduser_main(int argc, char **argv) { - int opt; const char *login; - const char *gecos; + const char *gecos = default_gecos; const char *home = NULL; - const char *shell; + const char *shell = default_shell; struct passwd pw; @@ -236,30 +241,11 @@ int adduser_main(int argc, char **argv) if (argc < 2) { bb_show_usage(); } - gecos = default_gecos; - shell = default_shell; - /* get args */ - while ((opt = getopt (argc, argv, "h:g:s:")) != -1) - switch (opt) { - case 'h': - home = optarg; - break; - case 'g': - gecos = optarg; - break; - case 's': - shell = optarg; - break; - default: - bb_show_usage(); - break; - } + bb_getopt_ulflags(argc, argv, "h:g:s:", &home, &gecos, &shell); /* got root? */ - if (i_am_not_root()) { - bb_error_msg_and_die( "Only root may add a user or group to the system."); - } + if_i_am_not_root(); /* get login */ if (optind >= argc) { @@ -288,5 +274,3 @@ int adduser_main(int argc, char **argv) /* grand finale */ return adduser(bb_path_passwd_file, &pw); } - -/* $Id: adduser.c,v 1.5 2003/03/19 09:12:20 mjn3 Exp $ */ -- cgit v1.2.3