]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
whereis: add glob(7) support (new option -g)
authorKarel Zak <kzak@redhat.com>
Wed, 25 Jan 2023 13:13:05 +0000 (14:13 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 25 Jan 2023 13:30:36 +0000 (14:30 +0100)
$ whereis -b -g 'find???'
find???: /usr/bin/findmnt /usr/bin/findscu

Fixes: https://github.com/util-linux/util-linux/issues/2021
Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/whereis
configure.ac
meson.build
misc-utils/whereis.1.adoc
misc-utils/whereis.c

index 013f68c72d7306679abcae42d261c6f813fb16e8..7db67a398c719b1a71d6796cd3941a60ad7a7672 100644 (file)
@@ -17,7 +17,7 @@ _whereis_module()
        esac
        case $cur in
                -*)
-                       OPTS="-b -B -m -M -s -S -f -u -l"
+                       OPTS="-b -B -m -M -s -S -f -u -l -g"
                        COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
                        return 0
                        ;;
index 06843d2ab85c8b7a4c228d11d34f2b9fa961f0d1..675098d37fcd844002172d022f86298e41a7bf1b 100644 (file)
@@ -545,6 +545,7 @@ AC_CHECK_FUNCS([ \
        __fpending \
        __fpurge \
        fpurge \
+       fnmatch \
        fsconfig \
        fsmount \
        fsopen \
index 50648935af41705beee766036ea74dbf6e66f91c..d263cb6d20b268da1eb844e706791a94f9d62a7a 100644 (file)
@@ -485,6 +485,7 @@ funcs = '''
         errx
         explicit_bzero
         fmemopen
+        fnmatch
         fseeko
         fsconfig
         fsmount
index 2b07a967d8a51e081e0a2970c3a5e3c3de4d88de..e5b5457ed3126bf21d1c1087ec69abb32cf3ee58 100644 (file)
@@ -50,7 +50,7 @@ whereis - locate the binary, source, and manual page files for a command
 
 == DESCRIPTION
 
-*whereis* locates the binary, source and manual files for the specified command names. The supplied names are first stripped of leading pathname components. Prefixes of *s.* resulting from use of source code control are also dealt with. *whereis* then attempts to locate the desired program in the standard Linux places, and in the places specified by *$PATH* and *$MANPATH*.
+*whereis* locates the binary, source and manual files for the specified command names. The supplied names are *first stripped of leading pathname components*. Prefixes of *s.* resulting from use of source code control are also dealt with. *whereis* then attempts to locate the desired program in the standard Linux places, and in the places specified by *$PATH* and *$MANPATH*.
 
 The search restrictions (options *-b*, *-m* and *-s*) are cumulative and apply to the subsequent _name_ patterns on the command line. Any new search restriction resets the search mask. For example,
 
@@ -97,6 +97,12 @@ Terminates the directory list and signals the start of filenames. It _must_ be u
 *-l*::
 Output the list of effective lookup paths that *whereis* is using. When none of *-B*, *-M*, or *-S* is specified, the option will output the hard-coded paths that the command was able to find on the system.
 
+*-g*::
+Interpret the next names as a *glob(7)* patterns. *whereis* always compares only filenames (aka basename) and never complete path. Using directory names in the pattern has no effect. Don’t forget that the shell interprets the pattern when specified on the command line without quotes. It’s necessary to use quotes for the _name_, for example:
+____
+  whereis -g 'find*'
+____
+
 include::man-common/help-version.adoc[]
 
 == FILE SEARCH PATHS
index db092ce609cbad3603f2c7b8bc5fbcfe3b7f403e..d21b434762c8bb3cf997a6bd561c6098d3806d3d 100644 (file)
@@ -48,6 +48,9 @@
 #include <string.h>
 #include <ctype.h>
 #include <assert.h>
+#ifdef HAVE_FNMATCH
+# include <fnmatch.h>
+#endif
 
 #include "xalloc.h"
 #include "nls.h"
@@ -75,7 +78,8 @@ UL_DEBUG_DEFINE_MASKNAMES(whereis) = UL_DEBUG_EMPTY_MASKNAMES;
 #define UL_DEBUG_CURRENT_MASK  UL_DEBUG_MASK(whereis)
 #include "debugobj.h"
 
-static char uflag = 0;
+static char uflag;
+static char use_glob;
 
 /* supported types */
 enum {
@@ -212,6 +216,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -S <dirs>  define sources lookup path\n"), out);
        fputs(_(" -f         terminate <dirs> argument list\n"), out);
        fputs(_(" -u         search for unusual entries\n"), out);
+       fputs(_(" -g         interpret name as glob (pathnames pattern)\n"), out);
        fputs(_(" -l         output effective lookup paths\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
@@ -394,13 +399,20 @@ static void free_dirlist(struct wh_dirlist **ls0, int type)
 
 static int filename_equal(const char *cp, const char *dp, int type)
 {
-       int i = strlen(dp);
+       size_t i;
 
        DBG(SEARCH, ul_debug("compare '%s' and '%s'", cp, dp));
 
+#ifdef HAVE_FNMATCH
+       if (use_glob)
+               return fnmatch(cp, dp, 0) == 0;
+#endif
        if (type & SRC_DIR &&
            dp[0] == 's' && dp[1] == '.' && filename_equal(cp, dp + 2, type))
                return 1;
+
+       i = strlen(dp);
+
        if (type & MAN_DIR) {
                if (i > 1 && !strcmp(dp + i - 2, ".Z"))
                        i -= 2;
@@ -635,6 +647,9 @@ int main(int argc, char **argv)
                        case 'l':
                                list_dirlist(ls);
                                break;
+                       case 'g':
+                               use_glob = 1;
+                               break;
 
                        case 'V':
                                print_version(EXIT_SUCCESS);