diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-16 10:59:20 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-16 11:00:20 +0100 |
commit | 1d180cd7493f0a88fa39229cddcb30a0e44ade4c (patch) | |
tree | 4793a097e6ac1aefc0cce7399d06db9150ba09d8 | |
parent | eaced1ec85315b9e11226f9a4ab935066e6946a0 (diff) | |
download | busybox-w32-1d180cd7493f0a88fa39229cddcb30a0e44ade4c.tar.gz busybox-w32-1d180cd7493f0a88fa39229cddcb30a0e44ade4c.tar.bz2 busybox-w32-1d180cd7493f0a88fa39229cddcb30a0e44ade4c.zip |
lineedit: use strncmp instead of is_prefixed_with (we know the length)
Also: add comments, rename some variables
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/lineedit.c | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 1a3f29656..0c48e8179 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -736,9 +736,9 @@ enum { | |||
736 | FIND_FILE_ONLY = 2, | 736 | FIND_FILE_ONLY = 2, |
737 | }; | 737 | }; |
738 | 738 | ||
739 | static int path_parse(char ***p) | 739 | static unsigned path_parse(char ***p) |
740 | { | 740 | { |
741 | int npth; | 741 | unsigned npth; |
742 | const char *pth; | 742 | const char *pth; |
743 | char *tmp; | 743 | char *tmp; |
744 | char **res; | 744 | char **res; |
@@ -755,7 +755,7 @@ static int path_parse(char ***p) | |||
755 | return 1; | 755 | return 1; |
756 | 756 | ||
757 | tmp = (char*)pth; | 757 | tmp = (char*)pth; |
758 | npth = 2; /* path component count */ | 758 | npth = 1; /* path component count */ |
759 | while (1) { | 759 | while (1) { |
760 | tmp = strchr(tmp, ':'); | 760 | tmp = strchr(tmp, ':'); |
761 | if (!tmp) | 761 | if (!tmp) |
@@ -766,7 +766,7 @@ static int path_parse(char ***p) | |||
766 | npth++; | 766 | npth++; |
767 | } | 767 | } |
768 | 768 | ||
769 | *p = res = xmalloc(npth * sizeof(res[0])); | 769 | *p = res = xzalloc((npth + 1) * sizeof(res[0])); |
770 | res[0] = tmp = xstrdup(pth); | 770 | res[0] = tmp = xstrdup(pth); |
771 | npth = 1; | 771 | npth = 1; |
772 | while (1) { | 772 | while (1) { |
@@ -778,8 +778,8 @@ static int path_parse(char ***p) | |||
778 | break; /* :<empty> */ | 778 | break; /* :<empty> */ |
779 | res[npth++] = tmp; | 779 | res[npth++] = tmp; |
780 | } | 780 | } |
781 | /* special case: match subdirectories of the current directory */ | 781 | /* special case: "match subdirectories of the current directory" */ |
782 | res[npth++] = NULL; | 782 | /*res[npth++] = NULL; - filled by xzalloc() */ |
783 | return npth; | 783 | return npth; |
784 | } | 784 | } |
785 | 785 | ||
@@ -790,38 +790,38 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) | |||
790 | { | 790 | { |
791 | char *path1[1]; | 791 | char *path1[1]; |
792 | char **paths = path1; | 792 | char **paths = path1; |
793 | int npaths; | 793 | unsigned npaths; |
794 | int i; | 794 | unsigned i; |
795 | unsigned pf_len; | 795 | unsigned baselen; |
796 | const char *pfind; | 796 | const char *basecmd; |
797 | char *dirbuf = NULL; | 797 | char *dirbuf = NULL; |
798 | 798 | ||
799 | npaths = 1; | 799 | npaths = 1; |
800 | path1[0] = (char*)"."; | 800 | path1[0] = (char*)"."; |
801 | 801 | ||
802 | pfind = strrchr(command, '/'); | 802 | basecmd = strrchr(command, '/'); |
803 | if (!pfind) { | 803 | if (!basecmd) { |
804 | if (type == FIND_EXE_ONLY) | 804 | if (type == FIND_EXE_ONLY) |
805 | npaths = path_parse(&paths); | 805 | npaths = path_parse(&paths); |
806 | pfind = command; | 806 | basecmd = command; |
807 | } else { | 807 | } else { |
808 | /* point to 'l' in "..../last_component" */ | 808 | /* point to 'l' in "..../last_component" */ |
809 | pfind++; | 809 | basecmd++; |
810 | /* dirbuf = ".../.../.../" */ | 810 | /* dirbuf = ".../.../.../" */ |
811 | dirbuf = xstrndup(command, pfind - command); | 811 | dirbuf = xstrndup(command, basecmd - command); |
812 | # if ENABLE_FEATURE_USERNAME_COMPLETION | 812 | # if ENABLE_FEATURE_USERNAME_COMPLETION |
813 | if (dirbuf[0] == '~') /* ~/... or ~user/... */ | 813 | if (dirbuf[0] == '~') /* ~/... or ~user/... */ |
814 | dirbuf = username_path_completion(dirbuf); | 814 | dirbuf = username_path_completion(dirbuf); |
815 | # endif | 815 | # endif |
816 | path1[0] = dirbuf; | 816 | path1[0] = dirbuf; |
817 | } | 817 | } |
818 | pf_len = strlen(pfind); | 818 | baselen = strlen(basecmd); |
819 | 819 | ||
820 | if (type == FIND_EXE_ONLY && !dirbuf) { | 820 | if (type == FIND_EXE_ONLY && !dirbuf) { |
821 | # if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1 | 821 | # if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1 |
822 | const char *p = applet_names; | 822 | const char *p = applet_names; |
823 | while (*p) { | 823 | while (*p) { |
824 | if (strncmp(pfind, p, pf_len) == 0) | 824 | if (strncmp(basecmd, p, baselen) == 0) |
825 | add_match(xstrdup(p)); | 825 | add_match(xstrdup(p)); |
826 | while (*p++ != '\0') | 826 | while (*p++ != '\0') |
827 | continue; | 827 | continue; |
@@ -834,7 +834,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) | |||
834 | const char *b = state->get_exe_name(i++); | 834 | const char *b = state->get_exe_name(i++); |
835 | if (!b) | 835 | if (!b) |
836 | break; | 836 | break; |
837 | if (strncmp(pfind, b, pf_len) == 0) | 837 | if (strncmp(basecmd, b, baselen) == 0) |
838 | add_match(xstrdup(b)); | 838 | add_match(xstrdup(b)); |
839 | } | 839 | } |
840 | } | 840 | } |
@@ -847,7 +847,10 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) | |||
847 | struct stat st; | 847 | struct stat st; |
848 | char *found; | 848 | char *found; |
849 | 849 | ||
850 | if (paths[i] == NULL) { | 850 | if (paths[i] == NULL) { /* path_parse()'s last component? */ |
851 | /* in PATH completion, current dir's subdir names | ||
852 | * can be completions (but only subdirs, not files). | ||
853 | */ | ||
851 | type = FIND_DIR_ONLY; | 854 | type = FIND_DIR_ONLY; |
852 | paths[i] = (char *)"."; | 855 | paths[i] = (char *)"."; |
853 | } | 856 | } |
@@ -861,10 +864,10 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) | |||
861 | const char *name_found = next->d_name; | 864 | const char *name_found = next->d_name; |
862 | 865 | ||
863 | /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */ | 866 | /* .../<tab>: bash 3.2.0 shows dotfiles, but not . and .. */ |
864 | if (!pfind[0] && DOT_OR_DOTDOT(name_found)) | 867 | if (!basecmd[0] && DOT_OR_DOTDOT(name_found)) |
865 | continue; | 868 | continue; |
866 | /* match? */ | 869 | /* match? */ |
867 | if (!is_prefixed_with(name_found, pfind)) | 870 | if (strncmp(basecmd, name_found, baselen) != 0) |
868 | continue; /* no */ | 871 | continue; /* no */ |
869 | 872 | ||
870 | found = concat_path_file(paths[i], name_found); | 873 | found = concat_path_file(paths[i], name_found); |
@@ -906,7 +909,7 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) | |||
906 | } | 909 | } |
907 | free(dirbuf); | 910 | free(dirbuf); |
908 | 911 | ||
909 | return pf_len; | 912 | return baselen; |
910 | } | 913 | } |
911 | 914 | ||
912 | /* build_match_prefix: | 915 | /* build_match_prefix: |