From 653f672ab61346fea2ec10d3706f0e3e9e8f51de Mon Sep 17 00:00:00 2001 From: Samanta Navarro Date: Wed, 4 Nov 2020 11:38:00 +0000 Subject: [PATCH] whereis: filter bin, man and src differently Consider "s." prefixes for source code files only (even though I do not know which VCS does that), compression suffixes for manual pages and strict matching for executables. Calling "whereis python3" is kind of okay to return python3.8 next to python3, but python3.8-config is not the same tool as python3. Signed-off-by: Samanta Navarro --- misc-utils/whereis.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/misc-utils/whereis.c b/misc-utils/whereis.c index d94e25a58d..db092ce609 100644 --- a/misc-utils/whereis.c +++ b/misc-utils/whereis.c @@ -392,29 +392,32 @@ static void free_dirlist(struct wh_dirlist **ls0, int type) } -static int filename_equal(const char *cp, const char *dp) +static int filename_equal(const char *cp, const char *dp, int type) { int i = strlen(dp); DBG(SEARCH, ul_debug("compare '%s' and '%s'", cp, dp)); - if (dp[0] == 's' && dp[1] == '.' && filename_equal(cp, dp + 2)) + if (type & SRC_DIR && + dp[0] == 's' && dp[1] == '.' && filename_equal(cp, dp + 2, type)) return 1; - if (i > 1 && !strcmp(dp + i - 2, ".Z")) - i -= 2; - else if (i > 2 && !strcmp(dp + i - 3, ".gz")) - i -= 3; - else if (i > 2 && !strcmp(dp + i - 3, ".xz")) - i -= 3; - else if (i > 3 && !strcmp(dp + i - 4, ".bz2")) - i -= 4; - else if (i > 3 && !strcmp(dp + i - 4, ".zst")) - i -= 4; + if (type & MAN_DIR) { + if (i > 1 && !strcmp(dp + i - 2, ".Z")) + i -= 2; + else if (i > 2 && !strcmp(dp + i - 3, ".gz")) + i -= 3; + else if (i > 2 && !strcmp(dp + i - 3, ".xz")) + i -= 3; + else if (i > 3 && !strcmp(dp + i - 4, ".bz2")) + i -= 4; + else if (i > 3 && !strcmp(dp + i - 4, ".zst")) + i -= 4; + } while (*cp && *dp && *cp == *dp) cp++, dp++, i--; if (*cp == 0 && *dp == 0) return 1; - if (*cp == 0 && *dp++ == '.') { + if (!(type & BIN_DIR) && *cp == 0 && *dp++ == '.') { --i; while (i > 0 && *dp) if (--i, *dp++ == '.') @@ -424,7 +427,8 @@ static int filename_equal(const char *cp, const char *dp) return 0; } -static void findin(const char *dir, const char *pattern, int *count, char **wait) +static void findin(const char *dir, const char *pattern, int *count, + char **wait, int type) { DIR *dirp; struct dirent *dp; @@ -436,7 +440,7 @@ static void findin(const char *dir, const char *pattern, int *count, char **wait DBG(SEARCH, ul_debug("find '%s' in '%s'", pattern, dir)); while ((dp = readdir(dirp)) != NULL) { - if (!filename_equal(pattern, dp->d_name)) + if (!filename_equal(pattern, dp->d_name, type)) continue; if (uflag && *count == 0) @@ -476,7 +480,7 @@ static void lookup(const char *pattern, struct wh_dirlist *ls, int want) for (; ls; ls = ls->next) { if ((ls->type & want) && ls->path) - findin(ls->path, patbuf, &count, &wait); + findin(ls->path, patbuf, &count, &wait, ls->type); } free(wait); -- 2.47.3