From 28e67966f3fc0728fb2f923265a8b2275f410655 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sun, 26 Apr 2009 23:22:40 +0000 Subject: hush: make getopt32 usable in builtins. use it in unset. more uses are expected in the future. function old new delta getopt32 1356 1393 +37 builtin_export 256 266 +10 builtin_unset 418 380 -38 --- shell/hush.c | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) (limited to 'shell/hush.c') diff --git a/shell/hush.c b/shell/hush.c index 292b8b2e3..d0819f691 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -6451,7 +6451,11 @@ static int builtin_export(char **argv) } #if ENABLE_HUSH_EXPORT_N - opt_unexport = getopt32(argv, "+n"); /* "+": stop at 1st non-option */ + /* "!": do not abort on errors */ + /* "+": stop at 1st non-option */ + opt_unexport = getopt32(argv, "!+n"); + if (opt_unexport == (unsigned)-1) + return EXIT_FAILURE; argv += optind; #else opt_unexport = 0; @@ -6918,36 +6922,22 @@ static int builtin_umask(char **argv) static int builtin_unset(char **argv) { int ret; - char var; - char *arg; - - if (!*++argv) - return EXIT_SUCCESS; + unsigned opts; - var = 0; - while ((arg = *argv) != NULL && arg[0] == '-') { - arg++; - do { - switch (*arg) { - case 'v': - case 'f': - if (var == 0 || var == *arg) { - var = *arg; - break; - } - /* else: unset -vf, which is illegal. - * fall through */ - default: - bb_error_msg("unset: %s: invalid option", *argv); - return EXIT_FAILURE; - } - } while (*++arg); - argv++; + /* "!": do not abort on errors */ + /* "+": stop at 1st non-option */ + opts = getopt32(argv, "!+vf"); + if (opts == (unsigned)-1) + return EXIT_FAILURE; + if (opts == 3) { + bb_error_msg("unset: -v and -f are exclusive"); + return EXIT_FAILURE; } + argv += optind; ret = EXIT_SUCCESS; while (*argv) { - if (var != 'f') { + if (!(opts & 2)) { /* not -f */ if (unset_local_var(*argv)) { /* unset doesn't fail. * Error is when one tries to unset RO var. -- cgit v1.2.3