diff options
author | Denis Vlasenko | 2009-04-26 23:22:40 +0000 |
---|---|---|
committer | Denis Vlasenko | 2009-04-26 23:22:40 +0000 |
commit | 28e67966f3fc0728fb2f923265a8b2275f410655 (patch) | |
tree | 0c522b97ed681180f68d028094bd02b89b292b49 /shell | |
parent | 572930027d5d86a18ebd68d5f4273150a2f302e1 (diff) | |
download | busybox-28e67966f3fc0728fb2f923265a8b2275f410655.zip busybox-28e67966f3fc0728fb2f923265a8b2275f410655.tar.gz |
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
Diffstat (limited to 'shell')
-rw-r--r-- | shell/hush.c | 42 | ||||
-rw-r--r-- | shell/hush_test/hush-vars/unset.right | 5 |
2 files changed, 18 insertions, 29 deletions
diff --git a/shell/hush.c b/shell/hush.c index 292b8b2..d0819f6 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 <nonexistent_var> doesn't fail. * Error is when one tries to unset RO var. diff --git a/shell/hush_test/hush-vars/unset.right b/shell/hush_test/hush-vars/unset.right index 8dea7c4..1fbe76a 100644 --- a/shell/hush_test/hush-vars/unset.right +++ b/shell/hush_test/hush-vars/unset.right @@ -1,6 +1,5 @@ -hush: unset: -: invalid option -1 -hush: unset: -m: invalid option +0 +unset: invalid option -- m 1 0 ___ |