]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
whereis: filter bin, man and src differently
authorSamanta Navarro <ferivoz@riseup.net>
Wed, 4 Nov 2020 11:38:00 +0000 (11:38 +0000)
committerSamanta Navarro <ferivoz@riseup.net>
Wed, 4 Nov 2020 11:43:09 +0000 (11:43 +0000)
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 <ferivoz@riseup.net>
misc-utils/whereis.c

index d94e25a58d8cb6df27ddf493f37c86df358da9da..db092ce609cbad3603f2c7b8bc5fbcfe3b7f403e 100644 (file)
@@ -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);