Change whereis(1) over from a perl script to a C program.
authorDavid Rhodus <drhodus@dragonflybsd.org>
Thu, 25 Mar 2004 16:39:40 +0000 (16:39 +0000)
committerDavid Rhodus <drhodus@dragonflybsd.org>
Thu, 25 Mar 2004 16:39:40 +0000 (16:39 +0000)
Patch sent in by: Dheeraj Reddy <dheerajs@comcast.net>

usr.bin/whereis/Makefile
usr.bin/whereis/whereis.1
usr.bin/whereis/whereis.c [new file with mode: 0644]
usr.bin/whereis/whereis.pl [deleted file]

index f42ea8f..167a97a 100644 (file)
@@ -1,7 +1,8 @@
-# $FreeBSD: src/usr.bin/whereis/Makefile,v 1.2.8.2 2001/04/25 14:04:39 ru Exp $
-# $DragonFly: src/usr.bin/whereis/Makefile,v 1.2 2003/06/17 04:29:33 dillon Exp $
+# $FreeBSD: src/usr.bin/whereis/Makefile,v 1.7 2002/12/30 09:48:40 obrien Exp $
+# $DragonFly: src/usr.bin/whereis/Makefile,v 1.3 2004/03/25 16:39:40 drhodus Exp $
 
-SCRIPTS=whereis.pl
+PROG=  whereis
 MAN=   whereis.1
+WARNS?=        5
 
 .include <bsd.prog.mk>
index 73895c2..e5db1b3 100644 (file)
@@ -1,6 +1,8 @@
 .\" Copyright (c) 1993
 .\"    The Regents of the University of California.  All rights reserved.
 .\"
+.\" Copyright 2002     Joerg Wunsch
+.\"
 .\" Redistribution and use in source and binary forms, with or without
 .\" modification, are permitted provided that the following conditions
 .\" are met:
 .\"
 .\"    @(#)whereis.1   8.2 (Berkeley) 12/30/93
 .\"
-.\" $FreeBSD: src/usr.bin/whereis/whereis.1,v 1.8.2.3 2001/08/16 13:17:14 ru Exp $
-.\" $DragonFly: src/usr.bin/whereis/whereis.1,v 1.2 2003/06/17 04:29:33 dillon Exp $
+.\" $FreeBSD: src/usr.bin/whereis/whereis.1,v 1.21 2002/11/26 17:33:36 ru Exp $
+.\" $DragonFly: src/usr.bin/whereis/whereis.1,v 1.3 2004/03/25 16:39:40 drhodus Exp $
 .\"
-.Dd June 15, 1996
+.Dd August 22, 2002
 .Dt WHEREIS 1
 .Os
 .Sh NAME
 .Nd locate programs
 .Sh SYNOPSIS
 .Nm
-.Op Fl bms
-.Op Fl u
-.Op Fl BMS dir ... Fl f
+.Op Fl abmqsux
+.Op Fl BMS Ar dir ... Fl f
 .Ar program ...
 .Sh DESCRIPTION
 The
 .Nm
 utility checks the standard binary, manual page, and source
 directories for the specified programs, printing out the paths of any
-it finds.  The supplied names are first stripped of leading path name
-components, any single trailing extension added by
-.Xr gzip 1
-or
+it finds.  The supplied program names are first stripped of leading
+path name components, any single trailing extension added by
+.Xr gzip 1 ,
 .Xr compress 1 ,
+or
+.Xr bzip2 1 ,
 and the leading
 .Ql s.\&
 or trailing
@@ -67,7 +69,8 @@ The default path searched is the string returned by the
 utility for the
 .Dq user.cs_path
 string, with
-.Pa /usr/libexec
+.Pa /usr/libexec ,
+.Pa /usr/games
 and the current user's
 .Ev $PATH
 appended.  Manual pages are searched by default along the
@@ -92,6 +95,8 @@ option.
 Specify directories to search for program sources.  Requires the
 .Fl f
 option.
+.It Fl a
+Report all matches instead of only the first of each requested type.
 .It Fl b
 Search for binaries.
 .It Fl f
@@ -101,42 +106,80 @@ Delimits the list of directories after the
 or
 .Fl S
 options, and indicates the beginning of the
-.Ar name
+.Ar program
 list.
 .It Fl m
 Search for manual pages.
+.It Fl q
+.Pq Dq quiet .
+Suppress the output of the utility name in front of the normal
+output line.
+This can become handy for use in a backquote substitution of a
+shell command line, see
+.Sx EXAMPLES .
 .It Fl s
 Search for source directories.
 .It Fl u
 Search for
 .Dq unusual
-entries.  A file is said to be unusual if it does not have one entry
-of each requested type.
+entries.  A file is said to be unusual if it does not have at least
+one entry of each requested type.
+Only the name of the unusual entry is printed.
+.It Fl x
+Do not use
+.Dq expensive
+tools when searching for source directories.
+Normally, after unsuccessfully searching all the first-level
+subdirectories of the source directory list,
+.Nm
+will ask
+.Xr locate 1
+to find the entry on its behalf.
+Since this can take much longer, it can be turned off with
+.Fl x .
 .El
 .Sh EXAMPLES
 The following finds all utilities under
 .Pa /usr/bin
 that do not have documentation:
+.Pp
 .Dl whereis -m -u /usr/bin/*
+.Pp
+Change to the source code directory of
+.Xr ls 1 :
+.Pp
+.Dl cd `whereis -sq ls`
 .Sh SEE ALSO
 .Xr find 1 ,
 .Xr locate 1 ,
 .Xr man 1 ,
 .Xr which 1 ,
 .Xr sysctl 8
-.Sh BUGS
-The search for sources is implemented as a quick search as the
-first-level subdirectory of each element of the list of source
-directories first.  If this didn't succeed, the utility
-.Xr locate 1
-is requested to do the search in deeper nested subdirectories.  This
-might take some time, and will only succeed if the locate database is
-up-to-date.
 .Sh HISTORY
 The
 .Nm
-command appeared in
+utility appeared in
 .Bx 3.0 .
 This version re-implements the historical
 functionality that was lost in
 .Bx 4.4 .
+.Sh AUTHORS
+This implementation of the
+.Nm
+command was written by
+.An J\(:org Wunsch .
+.Sh BUGS
+This re-implementation of the
+.Nm
+utility is not bug-for-bug compatible with historical versions.
+It is believed to be compatible with the version that was shipping with
+.Fx 2.2
+through
+.Fx 4.5
+though.
+.Pp
+The
+.Nm
+utility can report some unrelated source entries when the
+.Fl a
+option is specified.
diff --git a/usr.bin/whereis/whereis.c b/usr.bin/whereis/whereis.c
new file mode 100644 (file)
index 0000000..71864db
--- /dev/null
@@ -0,0 +1,671 @@
+/*
+ * Copyright © 2002, Jörg Wunsch
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ * $FreeBSD: src/usr.bin/whereis/whereis.c,v 1.12 2002/08/22 01:50:51 johan Exp $
+ * $DragonFly: src/usr.bin/whereis/whereis.c,v 1.1 2004/03/25 16:39:40 drhodus Exp $
+ */
+
+/*
+ * 4.3BSD UI-compatible whereis(1) utility.  Rewritten from scratch
+ * since the original 4.3BSD version suffers legal problems that
+ * prevent it from being redistributed, and since the 4.4BSD version
+ * was pretty inferior in functionality.
+ */
+
+#include <sys/types.h>
+
+
+#include <sys/stat.h>
+#include <sys/sysctl.h>
+
+#include <dirent.h>
+#include <err.h>
+#include <errno.h>
+#include <regex.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#include "pathnames.h"
+
+#define        NO_BIN_FOUND    1
+#define        NO_MAN_FOUND    2
+#define        NO_SRC_FOUND    4
+
+typedef const char *ccharp;
+
+int opt_a, opt_b, opt_m, opt_q, opt_s, opt_u, opt_x;
+ccharp *bindirs, *mandirs, *sourcedirs;
+char **query;
+
+const char *sourcepath = PATH_SOURCES;
+
+char   *colonify(ccharp *);
+int     contains(ccharp *, const char *);
+void    decolonify(char *, ccharp **, int *);
+void    defaults(void);
+void    scanopts(int, char **);
+void    usage(void);
+
+/*
+ * Throughout this program, a number of strings are dynamically
+ * allocated but never freed.  Their memory is written to when
+ * splitting the strings into string lists which will later be
+ * processed.  Since it's important that those string lists remain
+ * valid even after the functions allocating the memory returned,
+ * those functions cannot free them.  They could be freed only at end
+ * of main(), which is pretty pointless anyway.
+ *
+ * The overall amount of memory to be allocated for processing the
+ * strings is not expected to exceed a few kilobytes.  For that
+ * reason, allocation can usually always be assumed to succeed (within
+ * a virtual memory environment), thus we simply bail out using
+ * abort(3) in case of an allocation failure.
+ */
+
+void
+usage(void)
+{
+       errx(EX_USAGE,
+            "usage: whereis [-abmqsux] [-BMS dir... -f] name ...");
+}
+
+/*
+ * Scan options passed to program.
+ *
+ * Note that the -B/-M/-S options expect a list of directory
+ * names that must be terminated with -f.
+ */
+void
+scanopts(int argc, char **argv)
+{
+       int c, i, opt_f;
+       ccharp **dirlist;
+
+       opt_f = 0;
+       while ((c = getopt(argc, argv, "BMSabfmqsux")) != -1)
+               switch (c) {
+               case 'B':
+                       dirlist = &bindirs;
+                       goto dolist;
+
+               case 'M':
+                       dirlist = &mandirs;
+                       goto dolist;
+
+               case 'S':
+                       dirlist = &sourcedirs;
+                 dolist:
+                       i = 0;
+                       *dirlist = realloc(*dirlist, (i + 1) * sizeof(char *));
+                       (*dirlist)[i] = NULL;
+                       while (optind < argc &&
+                              strcmp(argv[optind], "-f") != 0 &&
+                              strcmp(argv[optind], "-B") != 0 &&
+                              strcmp(argv[optind], "-M") != 0 &&
+                              strcmp(argv[optind], "-S") != 0) {
+                               decolonify(argv[optind], dirlist, &i);
+                               optind++;
+                       }
+                       break;
+
+               case 'a':
+                       opt_a = 1;
+                       break;
+
+               case 'b':
+                       opt_b = 1;
+                       break;
+
+               case 'f':
+                       goto breakout;
+
+               case 'm':
+                       opt_m = 1;
+                       break;
+
+               case 'q':
+                       opt_q = 1;
+                       break;
+
+               case 's':
+                       opt_s = 1;
+                       break;
+
+               case 'u':
+                       opt_u = 1;
+                       break;
+
+               case 'x':
+                       opt_x = 1;
+                       break;
+
+               default:
+                       usage();
+               }
+  breakout:
+       if (optind == argc)
+               usage();
+       query = argv + optind;
+}
+
+/*
+ * Find out whether string `s' is contained in list `cpp'.
+ */
+int
+contains(ccharp *cpp, const char *s)
+{
+       ccharp cp;
+
+       if (cpp == NULL)
+               return (0);
+
+       while ((cp = *cpp) != NULL) {
+               if (strcmp(cp, s) == 0)
+                       return (1);
+               cpp++;
+       }
+       return (0);
+}
+
+/*
+ * Split string `s' at colons, and pass it to the string list pointed
+ * to by `cppp' (which has `*ip' elements).  Note that the original
+ * string is modified by replacing the colon with a NUL byte.  The
+ * partial string is only added if it has a length greater than 0, and
+ * if it's not already contained in the string list.
+ */
+void
+decolonify(char *s, ccharp **cppp, int *ip)
+{
+       char *cp;
+
+       while ((cp = strchr(s, ':')), *s != '\0') {
+               if (cp)
+                       *cp = '\0';
+               if (strlen(s) && !contains(*cppp, s)) {
+                       *cppp = realloc(*cppp, (*ip + 2) * sizeof(char *));
+                       if (cppp == NULL)
+                               abort();
+                       (*cppp)[*ip] = s;
+                       (*cppp)[*ip + 1] = NULL;
+                       (*ip)++;
+               }
+               if (cp)
+                       s = cp + 1;
+               else
+                       break;
+       }
+}
+
+/*
+ * Join string list `cpp' into a colon-separated string.
+ */
+char *
+colonify(ccharp *cpp)
+{
+       size_t s;
+       char *cp;
+       int i;
+
+       if (cpp == NULL)
+               return (0);
+
+       for (s = 0, i = 0; cpp[i] != NULL; i++)
+               s += strlen(cpp[i]) + 1;
+       if ((cp = malloc(s + 1)) == NULL)
+               abort();
+       for (i = 0, *cp = '\0'; cpp[i] != NULL; i++) {
+               strcat(cp, cpp[i]);
+               strcat(cp, ":");
+       }
+       cp[s - 1] = '\0';               /* eliminate last colon */
+
+       return (cp);
+}
+
+/*
+ * Provide defaults for all options and directory lists.
+ */
+void
+defaults(void)
+{
+       size_t s;
+       char *b, buf[BUFSIZ], *cp;
+       int nele;
+       FILE *p;
+       DIR *dir;
+       struct stat sb;
+       struct dirent *dirp;
+
+       /* default to -bms if none has been specified */
+       if (!opt_b && !opt_m && !opt_s)
+               opt_b = opt_m = opt_s = 1;
+
+       /* -b defaults to default path + /usr/libexec +
+        * /usr/games + user's path */
+       if (!bindirs) {
+               if (sysctlbyname("user.cs_path", (void *)NULL, &s,
+                                (void *)NULL, 0) == -1)
+                       err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
+               if ((b = malloc(s + 1)) == NULL)
+                       abort();
+               if (sysctlbyname("user.cs_path", b, &s, (void *)NULL, 0) == -1)
+                       err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
+               nele = 0;
+               decolonify(b, &bindirs, &nele);
+               bindirs = realloc(bindirs, (nele + 3) * sizeof(char *));
+               if (bindirs == NULL)
+                       abort();
+               bindirs[nele++] = PATH_LIBEXEC;
+               bindirs[nele++] = PATH_GAMES;
+               bindirs[nele] = NULL;
+               if ((cp = getenv("PATH")) != NULL) {
+                       /* don't destroy the original environment... */
+                       if ((b = malloc(strlen(cp) + 1)) == NULL)
+                               abort();
+                       strcpy(b, cp);
+                       decolonify(b, &bindirs, &nele);
+               }
+       }
+
+       /* -m defaults to $(manpath) */
+       if (!mandirs) {
+               if ((p = popen(MANPATHCMD, "r")) == NULL)
+                       err(EX_OSERR, "cannot execute manpath command");
+               if (fgets(buf, BUFSIZ - 1, p) == NULL ||
+                   pclose(p))
+                       err(EX_OSERR, "error processing manpath results");
+               if ((b = strchr(buf, '\n')) != NULL)
+                       *b = '\0';
+               if ((b = malloc(strlen(buf) + 1)) == NULL)
+                       abort();
+               strcpy(b, buf);
+               nele = 0;
+               decolonify(b, &mandirs, &nele);
+       }
+
+       /* -s defaults to precompiled list, plus subdirs of /usr/ports */
+       if (!sourcedirs) {
+               if ((b = malloc(strlen(sourcepath) + 1)) == NULL)
+                       abort();
+               strcpy(b, sourcepath);
+               nele = 0;
+               decolonify(b, &sourcedirs, &nele);
+
+               if (stat(PATH_PORTS, &sb) == -1) {
+                       if (errno == ENOENT)
+                               /* no /usr/ports, we are done */
+                               return;
+                       err(EX_OSERR, "stat(" PATH_PORTS ")");
+               }
+               if ((sb.st_mode & S_IFMT) != S_IFDIR)
+                       /* /usr/ports is not a directory, ignore */
+                       return;
+               if (access(PATH_PORTS, R_OK | X_OK) != 0)
+                       return;
+               if ((dir = opendir(PATH_PORTS)) == NULL)
+                       err(EX_OSERR, "opendir" PATH_PORTS ")");
+               while ((dirp = readdir(dir)) != NULL) {
+                       if (dirp->d_name[0] == '.' ||
+                           strcmp(dirp->d_name, "CVS") == 0)
+                               /* ignore dot entries and CVS subdir */
+                               continue;
+                       if ((b = malloc(sizeof PATH_PORTS + 1 + dirp->d_namlen))
+                           == NULL)
+                               abort();
+                       strcpy(b, PATH_PORTS);
+                       strcat(b, "/");
+                       strcat(b, dirp->d_name);
+                       if (stat(b, &sb) == -1 ||
+                           (sb.st_mode & S_IFMT) != S_IFDIR ||
+                           access(b, R_OK | X_OK) != 0) {
+                               free(b);
+                               continue;
+                       }
+                       sourcedirs = realloc(sourcedirs,
+                                            (nele + 2) * sizeof(char *));
+                       if (sourcedirs == NULL)
+                               abort();
+                       sourcedirs[nele++] = b;
+                       sourcedirs[nele] = NULL;
+               }
+               closedir(dir);
+       }
+}
+
+int
+main(int argc, char **argv)
+{
+       int unusual, i, printed;
+       char *bin, buf[BUFSIZ], *cp, *cp2, *man, *name, *src;
+       ccharp *dp;
+       size_t nlen, olen, s;
+       struct stat sb;
+       regex_t re, re2;
+       regmatch_t matches[2];
+       regoff_t rlen;
+       FILE *p;
+
+       scanopts(argc, argv);
+       defaults();
+
+       if (mandirs == NULL)
+               opt_m = 0;
+       if (bindirs == NULL)
+               opt_b = 0;
+       if (sourcedirs == NULL)
+               opt_s = 0;
+       if (opt_m + opt_b + opt_s == 0)
+               errx(EX_DATAERR, "no directories to search");
+
+       if (opt_m) {
+               setenv("MANPATH", colonify(mandirs), 1);
+               if ((i = regcomp(&re, MANWHEREISMATCH, REG_EXTENDED)) != 0) {
+                       regerror(i, &re, buf, BUFSIZ - 1);
+                       errx(EX_UNAVAILABLE, "regcomp(%s) failed: %s",
+                            MANWHEREISMATCH, buf);
+               }
+       }
+
+       for (; (name = *query) != NULL; query++) {
+               /* strip leading path name component */
+               if ((cp = strrchr(name, '/')) != NULL)
+                       name = cp + 1;
+               /* strip SCCS or RCS suffix/prefix */
+               if (strlen(name) > 2 && strncmp(name, "s.", 2) == 0)
+                       name += 2;
+               if ((s = strlen(name)) > 2 && strcmp(name + s - 2, ",v") == 0)
+                       name[s - 2] = '\0';
+               /* compression suffix */
+               s = strlen(name);
+               if (s > 2 &&
+                   (strcmp(name + s - 2, ".z") == 0 ||
+                    strcmp(name + s - 2, ".Z") == 0))
+                       name[s - 2] = '\0';
+               else if (s > 3 &&
+                        strcmp(name + s - 3, ".gz") == 0)
+                       name[s - 3] = '\0';
+               else if (s > 4 &&
+                        strcmp(name + s - 4, ".bz2") == 0)
+                       name[s - 4] = '\0';
+
+               unusual = 0;
+               bin = man = src = NULL;
+               s = strlen(name);
+
+               if (opt_b) {
+                       /*
+                        * Binaries have to match exactly, and must be regular
+                        * executable files.
+                        */
+                       unusual = unusual | NO_BIN_FOUND;
+                       for (dp = bindirs; *dp != NULL; dp++) {
+                               cp = malloc(strlen(*dp) + 1 + s + 1);
+                               if (cp == NULL)
+                                       abort();
+                               strcpy(cp, *dp);
+                               strcat(cp, "/");
+                               strcat(cp, name);
+                               if (stat(cp, &sb) == 0 &&
+                                   (sb.st_mode & S_IFMT) == S_IFREG &&
+                                   (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
+                                   != 0) {
+                                       unusual = unusual & ~NO_BIN_FOUND;
+                                       if (bin == NULL) {
+                                               bin = strdup(cp);
+                                       } else {
+                                               olen = strlen(bin);
+                                               nlen = strlen(cp);
+                                               bin = realloc(bin, 
+                                                             olen + nlen + 2);
+                                               if (bin == 0)
+                                                       abort();
+                                               strcat(bin, " ");
+                                               strcat(bin, cp);
+                                       }
+                                       if (!opt_a) {
+                                               free(cp);
+                                               break;
+                                       }
+                               }
+                               free(cp);
+                       }
+               }
+
+               if (opt_m) {
+                       /*
+                        * Ask the man command to perform the search for us.
+                        */
+                       unusual = unusual | NO_MAN_FOUND;
+                       if (opt_a)
+                               cp = malloc(sizeof MANWHEREISALLCMD - 2 + s);
+                       else
+                               cp = malloc(sizeof MANWHEREISCMD - 2 + s);
+
+                       if (cp == NULL)
+                               abort();
+
+                       if (opt_a)
+                               sprintf(cp, MANWHEREISALLCMD, name);
+                       else
+                               sprintf(cp, MANWHEREISCMD, name);
+
+                       if ((p = popen(cp, "r")) != NULL) {
+                           
+                               while (fgets(buf, BUFSIZ - 1, p) != NULL) {
+                                       unusual = unusual & ~NO_MAN_FOUND;
+                               
+                                       if ((cp2 = strchr(buf, '\n')) != NULL)
+                                               *cp2 = '\0';
+                                       if (regexec(&re, buf, 2, 
+                                                   matches, 0) == 0 &&
+                                           (rlen = matches[1].rm_eo - 
+                                            matches[1].rm_so) > 0) {
+                                               /*
+                                                * man -w found formated
+                                                * page, need to pick up
+                                                * source page name.
+                                                */
+                                               cp2 = malloc(rlen + 1);
+                                               if (cp2 == NULL)
+                                                       abort();
+                                               memcpy(cp2, 
+                                                      buf + matches[1].rm_so,
+                                                      rlen);
+                                               cp2[rlen] = '\0';
+                                       } else {
+                                               /*
+                                                * man -w found plain source
+                                                * page, use it.
+                                                */
+                                               s = strlen(buf);
+                                               cp2 = malloc(s + 1);
+                                               if (cp2 == NULL)
+                                                       abort();
+                                               strcpy(cp2, buf);
+                                       }
+
+                                       if (man == NULL) {
+                                               man = strdup(cp2);
+                                       } else {
+                                               olen = strlen(man);
+                                               nlen = strlen(cp2);
+                                               man = realloc(man, 
+                                                             olen + nlen + 2);
+                                               if (man == 0)
+                                                       abort();
+                                               strcat(man, " ");
+                                               strcat(man, cp2);
+                                       }
+
+                                       free(cp2);
+                                       
+                                       if (!opt_a)
+                                               break;
+                               }
+                               pclose(p);
+                               free(cp);
+                       }
+               }
+
+               if (opt_s) {
+                       /*
+                        * Sources match if a subdir with the exact
+                        * name is found.
+                        */
+                       unusual = unusual | NO_SRC_FOUND;
+                       for (dp = sourcedirs; *dp != NULL; dp++) {
+                               cp = malloc(strlen(*dp) + 1 + s + 1);
+                               if (cp == NULL)
+                                       abort();
+                               strcpy(cp, *dp);
+                               strcat(cp, "/");
+                               strcat(cp, name);
+                               if (stat(cp, &sb) == 0 &&
+                                   (sb.st_mode & S_IFMT) == S_IFDIR) {
+                                       unusual = unusual & ~NO_SRC_FOUND;
+                                       if (src == NULL) {
+                                               src = strdup(cp);
+                                       } else {
+                                               olen = strlen(src);
+                                               nlen = strlen(cp);
+                                               src = realloc(src, 
+                                                             olen + nlen + 2);
+                                               if (src == 0)
+                                                       abort();
+                                               strcat(src, " ");
+                                               strcat(src, cp);
+                                       }
+                                       if (!opt_a) {
+                                               free(cp);
+                                               break;
+                                       }
+                               }
+                               free(cp);
+                       }
+                       /*
+                        * If still not found, ask locate to search it
+                        * for us.  This will find sources for things
+                        * like lpr that are well hidden in the
+                        * /usr/src tree, but takes a lot longer.
+                        * Thus, option -x (`expensive') prevents this
+                        * search.
+                        *
+                        * Do only match locate output that starts
+                        * with one of our source directories, and at
+                        * least one further level of subdirectories.
+                        */
+                       if (opt_x || (src && !opt_a))
+                               goto done_sources;
+
+                       cp = malloc(sizeof LOCATECMD - 2 + s);
+                       if (cp == NULL)
+                               abort();
+                       sprintf(cp, LOCATECMD, name);
+                       if ((p = popen(cp, "r")) == NULL)
+                               goto done_sources;
+                       while ((src == NULL || opt_a) &&
+                              (fgets(buf, BUFSIZ - 1, p)) != NULL) {
+                               if ((cp2 = strchr(buf, '\n')) != NULL)
+                                       *cp2 = '\0';
+                               for (dp = sourcedirs;
+                                    (src == NULL || opt_a) && *dp != NULL;
+                                    dp++) {
+                                       cp2 = malloc(strlen(*dp) + 9);
+                                       if (cp2 == NULL)
+                                               abort();
+                                       strcpy(cp2, "^");
+                                       strcat(cp2, *dp);
+                                       strcat(cp2, "/[^/]+/");
+                                       if ((i = regcomp(&re2, cp2,
+                                                        REG_EXTENDED|REG_NOSUB))
+                                           != 0) {
+                                               regerror(i, &re, buf,
+                                                        BUFSIZ - 1);
+                                               errx(EX_UNAVAILABLE,
+                                                    "regcomp(%s) failed: %s",
+                                                    cp2, buf);
+                                       }
+                                       free(cp2);
+                                       if (regexec(&re2, buf, 0,
+                                                   (regmatch_t *)NULL, 0)
+                                           == 0) {
+                                               unusual = unusual & 
+                                                         ~NO_SRC_FOUND;
+                                               if (src == NULL) {
+                                                       src = strdup(buf);
+                                               } else {
+                                                       olen = strlen(src);
+                                                       nlen = strlen(buf);
+                                                       src = realloc(src, 
+                                                                     olen + 
+                                                                     nlen + 2);
+                                                       if (src == 0)
+                                                               abort();
+                                                       strcat(src, " ");
+                                                       strcat(src, buf);
+                                               }
+                                       }
+                                       regfree(&re2);
+                               }
+                       }
+                       pclose(p);
+                       free(cp);
+               }
+         done_sources:
+
+               if (opt_u && !unusual)
+                       continue;
+
+               printed = 0;
+               if (!opt_q) {
+                       printf("%s:", name);
+                       printed++;
+               }
+               if (bin) {
+                       if (printed++)
+                               putchar(' ');
+                       fputs(bin, stdout);
+               }
+               if (man) {
+                       if (printed++)
+                               putchar(' ');
+                       fputs(man, stdout);
+               }
+               if (src) {
+                       if (printed++)
+                               putchar(' ');
+                       fputs(src, stdout);
+               }
+               if (printed)
+                       putchar('\n');
+       }
+
+       if (opt_m)
+               regfree(&re);
+
+       return (0);
+}
diff --git a/usr.bin/whereis/whereis.pl b/usr.bin/whereis/whereis.pl
deleted file mode 100644 (file)
index 1f43246..0000000
+++ /dev/null
@@ -1,243 +0,0 @@
-#!/usr/bin/perl
-#
-# Copyright © 1995, 1996 Jörg Wunsch
-#
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-# 1. Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-#    notice, this list of conditions and the following disclaimer in the
-#    documentation and/or other materials provided with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
-# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-# IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-# whereis -- search for binaries, man pages and source directories.
-#
-# Rewritten from scratch for FreeBSD after the 4.3BSD manual page.
-#
-# $FreeBSD: src/usr.bin/whereis/whereis.pl,v 1.8 1999/08/28 01:07:37 peter Exp $
-# $DragonFly: src/usr.bin/whereis/Attic/whereis.pl,v 1.2 2003/06/17 04:29:33 dillon Exp $
-#
-
-sub usage
-{
-    print STDERR "usage: $0 [-bms] [-u] [-BMS dir... -f] name ...\n";
-    exit 1;
-}
-
-sub scanopts
-{
-    local($i, $j);
-  arg:
-    while ($ARGV[$i] =~ /^-/) {
-      opt:
-       for ($j = 1; $j < length($ARGV[$i]); $j++) {
-           local($_) = substr($ARGV[$i], $j, 1);
-           local($what, @list);
-           $opt_b++, next opt if /b/;
-           $opt_m++, next opt if /m/;
-           $opt_s++, next opt if /s/;
-           $opt_u++, next opt if /u/;
-           &usage unless /[BMS]/;
-
-           # directory list processing
-           $what = $_; @list = ();
-           push(@list, substr($ARGV[$i], $j+1)) if $j+1 < length($ARGV[$i]);
-           $i++;
-           while ($i <= $#ARGV && $ARGV[$i] !~ /^-/) {
-               push(@list, $ARGV[$i++]);
-           }
-           if ($what eq "B") {@binaries = @list;}
-           elsif ($what eq "M") {@manuals = @list;}
-           elsif ($what eq "S") {@sources = @list;}
-
-           $i++, last arg if $ARGV[$i] =~ /^-f$/;
-           next arg;
-       }
-       $i++;
-    }
-    &usage if $i > $#ARGV;
-
-    while ($ARGV[$i]) {
-       push(@names, $ARGV[$i++]);
-    }
-}
-
-
-sub decolonify
-{
-    local($list) = @_;
-    local($_, @rv);
-    foreach(split(/:/, $list)) {
-       push(@rv, $_);
-    }
-    return @rv;
-}
-
-
-&scanopts;
-
-# default to all if no type requested
-if ($opt_b + $opt_m + $opt_s == 0) {$opt_b = $opt_m = $opt_s = 1;}
-
-if (!defined(@binaries)) {
-    #
-    # first, use default path, then append /usr/libexec and the user's path
-    #
-    local($cs_path) = `/sbin/sysctl -n user.cs_path`;
-    local(@list, %path);
-
-    chop($cs_path);
-
-    @list = &decolonify($cs_path);
-    push(@list, "/usr/libexec");
-    push(@list, &decolonify($ENV{'PATH'}));
-
-    # resolve ~, remove duplicates
-    foreach (@list) {
-       s/^~/$ENV{'HOME'}/ if /^~/;
-       push(@binaries, $_) if !$path{$_};
-       $path{$_}++;
-    }
-}
-
-if (!defined(@manuals)) {
-    #
-    # first, use default manpath, then append user's $MANPATH
-    #
-    local($usermanpath) = $ENV{'MANPATH'};
-    delete $ENV{'MANPATH'};
-    local($manpath) = `/usr/bin/manpath`;
-    local(@list, %path, $i);
-
-    chop($manpath);
-
-    @list = &decolonify($manpath);
-    push(@list, &decolonify($usermanpath));
-
-    # remove duplicates
-    foreach (@list) {
-       push(@manuals, $_) if !$path{$_};
-       $path{$_}++;
-    }
-}
-
-if (!defined(@sources)) {
-    #
-    # default command sources
-    #
-    local($_);
-
-    @sources = ("/usr/src/bin", "/usr/src/usr.bin", "/usr/src/sbin",
-               "/usr/src/usr.sbin", "/usr/src/libexec",
-               "/usr/src/gnu/bin", "/usr/src/gnu/usr.bin",
-               "/usr/src/gnu/sbin", "/usr/src/gnu/usr.sbin",
-               "/usr/src/gnu/libexec", "/usr/src/contrib");
-
-    #
-    # if /usr/ports exists, look in all its subdirs, too
-    #
-    if (-d "/usr/ports" && opendir(PORTS, "/usr/ports")) {
-       while ($_ = readdir(PORTS)) {
-           next if /^\.\.?$/;
-           next if /^distfiles$/; # magic
-           next if ! -d "/usr/ports/$_";
-           push(@sources, "/usr/ports/$_");
-       }
-       closedir(PORTS);
-    }
-}
-
-if ($opt_m) {
-    # construct a new MANPATH
-    foreach (@manuals) {
-       next if ! -d $_;
-       if ($manpath) { $manpath .= ":$_"; }
-       else { $manpath = $_; }
-    }
-}
-
-#
-# main loop
-#
-foreach $name (@names) {
-    $name =~ s|^.*/||;         # strip leading path name component
-    $name =~ s/,v$//; $name =~ s/^s\.//; # RCS or SCCS suffix/prefix
-    $name =~ s/\.(Z|z|gz)$//;  # compression suffix
-
-    $line = "";
-    $unusual = 0;
-
-    if ($opt_b) {
-       #
-       # Binaries have to match exactly, and must be regular executable
-       # files.
-       #
-       $unusual++;
-       foreach (@binaries) {
-           $line .= " $_/$name", $unusual--, last if -f "$_/$name" && -x _;
-       }
-    }
-
-    if ($opt_m) {
-       #
-       # Ask the man command to do the search for us.
-       #
-       $unusual++;
-       chop($result = `man -S 1:8 -M $manpath -w $name 2> /dev/null`);
-       if ($result ne '') {
-           $unusual--;
-           ($cat, $junk, $src) = split(/[() \t\n]+/, $result);
-           if ($src ne '') { $line .= " $src"; }
-           else { $line .= " $cat"; }
-       }
-    }
-
-    if ($opt_s) {
-       #
-       # Sources match if a subdir with the exact name is found.
-       #
-       $found = 0;
-       $unusual++;
-       foreach (@sources) {
-               $line .= " $_/$name", $unusual--, $found++ if -d "$_/$name";
-       }
-       #
-       # If not yet found, ask locate(1) to do the search for us.
-       # This will find sources for things like lpr, but take longer.
-       # Do only match locate output that starts with one of our
-       # source directories, and at least one further level of
-       # subdirectories.
-       #
-       if (!$found && open(LOCATE, "locate */$name 2>/dev/null |")) {
-         locate_item:
-           while (chop($loc = <LOCATE>)) {
-               foreach (@sources) {
-                   $line .= " $loc", $unusual--, last locate_item
-                       if $loc =~ m|^$_/[^/]+/|;
-               }
-           }
-           close(LOCATE);
-       }
-    }
-
-    if ($opt_u) {
-       print "$name:\n" if $unusual;
-    } else {
-       print "$name:$line\n";
-    }
-}
-