diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-05-16 12:52:15 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-05-16 12:52:15 +0000 |
commit | 3e7b0e61200b118fd83f56fc3ebaeedba455600e (patch) | |
tree | 67db0a35e381b2c81b197e7b360d10131927e906 /shell/hush.c | |
parent | 1f4cf517f5803b0e300906c487d365a331122091 (diff) | |
download | busybox-w32-3e7b0e61200b118fd83f56fc3ebaeedba455600e.tar.gz busybox-w32-3e7b0e61200b118fd83f56fc3ebaeedba455600e.tar.bz2 busybox-w32-3e7b0e61200b118fd83f56fc3ebaeedba455600e.zip |
hush: simplify read builtin; fix set_local_var (misplaced 'goto skip;')
Diffstat (limited to 'shell/hush.c')
-rw-r--r-- | shell/hush.c | 60 |
1 files changed, 18 insertions, 42 deletions
diff --git a/shell/hush.c b/shell/hush.c index e3dd6663e..2e78e4a04 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -854,32 +854,24 @@ static int builtin_pwd(char **argv ATTRIBUTE_UNUSED) | |||
854 | /* built-in 'read VAR' handler */ | 854 | /* built-in 'read VAR' handler */ |
855 | static int builtin_read(char **argv) | 855 | static int builtin_read(char **argv) |
856 | { | 856 | { |
857 | int res; | 857 | char string[BUFSIZ]; |
858 | char *p; | ||
859 | const char *name = argv[1] ? argv[1] : "REPLY"; | ||
860 | int name_len = strlen(name); | ||
858 | 861 | ||
859 | if (argv[1]) { | 862 | if (name_len >= sizeof(string) - 2) |
860 | char string[BUFSIZ]; | 863 | return EXIT_FAILURE; |
861 | char *var = NULL; | 864 | strcpy(string, name); |
862 | 865 | p = string + name_len; | |
863 | string[0] = '\0'; /* In case stdin has only EOF */ | 866 | *p++ = '='; |
864 | /* read string */ | 867 | *p = '\0'; /* In case stdin has only EOF */ |
865 | fgets(string, sizeof(string), stdin); | 868 | /* read string. name_len+1 chars are already used by 'name=' */ |
866 | chomp(string); | 869 | fgets(p, sizeof(string) - 1 - name_len, stdin); |
867 | var = malloc(strlen(argv[1]) + strlen(string) + 2); | 870 | chomp(p); |
868 | if (var) { | 871 | return set_local_var(string, 0); |
869 | sprintf(var, "%s=%s", argv[1], string); | ||
870 | res = set_local_var(var, 0); | ||
871 | } else | ||
872 | res = -1; | ||
873 | if (res) | ||
874 | bb_perror_msg("read"); | ||
875 | free(var); /* So not move up to avoid breaking errno */ | ||
876 | return res; | ||
877 | } | ||
878 | do res = getchar(); while (res != '\n' && res != EOF); | ||
879 | return 0; | ||
880 | } | 872 | } |
881 | 873 | ||
882 | /* built-in 'set VAR=value' handler */ | 874 | /* built-in 'set [VAR=value]' handler */ |
883 | static int builtin_set(char **argv) | 875 | static int builtin_set(char **argv) |
884 | { | 876 | { |
885 | char *temp = argv[1]; | 877 | char *temp = argv[1]; |
@@ -959,7 +951,7 @@ static int builtin_umask(char **argv) | |||
959 | /* built-in 'unset VAR' handler */ | 951 | /* built-in 'unset VAR' handler */ |
960 | static int builtin_unset(char **argv) | 952 | static int builtin_unset(char **argv) |
961 | { | 953 | { |
962 | /* bash returned already true */ | 954 | /* bash always returns true */ |
963 | unset_local_var(argv[1]); | 955 | unset_local_var(argv[1]); |
964 | return EXIT_SUCCESS; | 956 | return EXIT_SUCCESS; |
965 | } | 957 | } |
@@ -2418,8 +2410,6 @@ static int expand_on_ifs(char **list, int n, char **posp, const char *str) | |||
2418 | * to be filled). This routine is extremely tricky: has to deal with | 2410 | * to be filled). This routine is extremely tricky: has to deal with |
2419 | * variables/parameters with whitespace, $* and $@, and constructs like | 2411 | * variables/parameters with whitespace, $* and $@, and constructs like |
2420 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ | 2412 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
2421 | /* NB: support for double-quoted expansion is not used yet (we never set | ||
2422 | * magic bit 0x80 elsewhere...) */ | ||
2423 | /* NB: another bug is that we cannot detect empty strings yet: | 2413 | /* NB: another bug is that we cannot detect empty strings yet: |
2424 | * "" or $empty"" expands to zero words, has to expand to empty word */ | 2414 | * "" or $empty"" expands to zero words, has to expand to empty word */ |
2425 | static int expand_vars_to_list(char **list, int n, char **posp, char *arg) | 2415 | static int expand_vars_to_list(char **list, int n, char **posp, char *arg) |
@@ -2663,20 +2653,6 @@ static char *insert_var_value(char *inp) | |||
2663 | } | 2653 | } |
2664 | 2654 | ||
2665 | 2655 | ||
2666 | |||
2667 | |||
2668 | |||
2669 | |||
2670 | |||
2671 | |||
2672 | |||
2673 | |||
2674 | |||
2675 | |||
2676 | |||
2677 | |||
2678 | |||
2679 | |||
2680 | /* This is used to get/check local shell variables */ | 2656 | /* This is used to get/check local shell variables */ |
2681 | static const char *get_local_var(const char *s) | 2657 | static const char *get_local_var(const char *s) |
2682 | { | 2658 | { |
@@ -2717,7 +2693,7 @@ static int set_local_var(const char *s, int flg_export) | |||
2717 | for (cur = top_vars; cur; cur = cur->next) { | 2693 | for (cur = top_vars; cur; cur = cur->next) { |
2718 | if (strcmp(cur->name, name) == 0) { | 2694 | if (strcmp(cur->name, name) == 0) { |
2719 | if (strcmp(cur->value, value) == 0) { | 2695 | if (strcmp(cur->value, value) == 0) { |
2720 | if (flg_export > 0 && cur->flg_export == 0) | 2696 | if (flg_export && !cur->flg_export) |
2721 | cur->flg_export = flg_export; | 2697 | cur->flg_export = flg_export; |
2722 | else | 2698 | else |
2723 | result++; | 2699 | result++; |
@@ -2730,8 +2706,8 @@ static int set_local_var(const char *s, int flg_export) | |||
2730 | free((char*)cur->value); | 2706 | free((char*)cur->value); |
2731 | cur->value = strdup(value); | 2707 | cur->value = strdup(value); |
2732 | } | 2708 | } |
2709 | goto skip; | ||
2733 | } | 2710 | } |
2734 | goto skip; | ||
2735 | } | 2711 | } |
2736 | 2712 | ||
2737 | // TODO: need simpler/generic rollback on malloc failure - see ash | 2713 | // TODO: need simpler/generic rollback on malloc failure - see ash |