--- /dev/null
+.\" $OpenBSD: whereis.1,v 1.1 1997/04/08 02:44:06 millert Exp $
+.\" $NetBSD: whereis.1,v 1.4 1995/08/31 21:54:51 jtc Exp $
+.\"
+.\" Copyright (c) 1993
+.\" The Regents of the University of California. 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.
+.\" 3. All advertising materials mentioning features or use of this software
+.\" must display the following acknowledgement:
+.\" This product includes software developed by the University of
+.\" California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.1 8.3 (Berkeley) 4/27/95
+.\"
+.Dd April 27, 1995
+.Dt WHEREIS 1
+.Os BSD 3
+.Sh NAME
+.Nm whereis
+.Nd locate programs
+.Sh SYNOPSIS
+.Nm whereis
+.Op Ar name ...
+.Sh DESCRIPTION
+The
+.Nm whereis
+utility checks the standard binary directories for the specified name,
+printing out the paths of any it finds that are executable by the
+user.
+.Pp
+The path searched is the string returned by the
+.Xr sysctl 8
+utility for the
+.Dq user.cs_path
+string.
+.Sh RETURN VALUES
+The
+.Nm
+utility exits with one of the following values:
+.Bl -tag -width 4n
+.It 0
+All names got successfully resolved.
+.It 1
+Some names got resolved but not all.
+.It 2
+No names got resolved.
+.It -1
+A system error occurred.
+.El
+.Sh SEE ALSO
+.Xr which 1 ,
+.Xr sysctl 8
+.Sh COMPATIBILITY
+The historic flags and arguments for the
+.Nm whereis
+utility are no longer available in this version.
+.Sh HISTORY
+The
+.Nm whereis
+command appeared in 3.0BSD.
-/* $OpenBSD: which.c,v 1.1 1997/02/21 18:35:00 millert Exp $ */
+/* $OpenBSD: which.c,v 1.2 1997/04/08 02:44:07 millert Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: which.c,v 1.1 1997/02/21 18:35:00 millert Exp $";
+static char rcsid[] = "$OpenBSD: which.c,v 1.2 1997/04/08 02:44:07 millert Exp $";
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
+#include <sys/sysctl.h>
#include <err.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
+#define PROG_WHICH 1
+#define PROG_WHEREIS 2
+
extern char *__progname;
-int which __P((char *, char *));
+int findprog __P((char *, char *, int));
void usage __P((void));
/*
* which(1) -- find an executable(s) in the user's path
+ * whereis(1) -- find an executable(s) in the default user path
*
* Return values:
* 0 - all executables found
char **argv;
{
char *path;
- int n, notfound = 0;
+ size_t n;
+ int ch, notfound = 0, progmode = PROG_WHICH;
(void)setlocale(LC_ALL, "");
if (argc == 1)
usage();
- if ((path = getenv("PATH")) == NULL)
- err(-1, "Can't get $PATH from environment");
+ /* Don't accept command args but check since old whereis(1) used to */
+ while ((ch = getopt(argc, argv, "")) != -1) {
+ switch (ch) {
+ default:
+ usage();
+ }
+ }
+
+ /*
+ * which(1) uses user's $PATH.
+ * whereis(1) uses user.cs_path from sysctl(3).
+ */
+ if (strcmp(__progname, "whereis") == 0) {
+ int mib[2];
+
+ progmode = PROG_WHEREIS;
+ mib[0] = CTL_USER;
+ mib[1] = USER_CS_PATH;
+ if (sysctl(mib, 2, NULL, &n, NULL, 0) == -1)
+ err(-1, "unable to get length of user.cs_path");
+ if (n == 0)
+ errx(-1, "user.cs_path was zero length!");
+ if ((path = (char *)malloc(n)) == NULL)
+ errx(-1, "can't allocate memory.");
+ if (sysctl(mib, 2, path, &n, NULL, 0) == -1)
+ err(-1, "unable to get user.cs_path");
+ } else {
+ if ((path = getenv("PATH")) == NULL)
+ err(-1, "can't get $PATH from environment");
+ }
/* To make access(2) do what we want */
if (setgid(getegid()))
err(-1, "Can't set uid to %u", geteuid());
for (n = 1; n < argc; n++)
- if (which(argv[n], path) == 0)
+ if (findprog(argv[n], path, progmode) == 0)
notfound++;
exit((notfound == 0) ? 0 : ((notfound == argc - 1) ? 2 : 1));
}
int
-which(prog, path)
+findprog(prog, path, progmode)
char *prog;
char *path;
+ int progmode;
{
char *p, filename[MAXPATHLEN];
int proglen, plen;
}
if ((path = strdup(path)) == NULL)
- errx(1, "Can't allocate memory.");
+ errx(-1, "Can't allocate memory.");
proglen = strlen(prog);
while ((p = strsep(&path, ":")) != NULL) {
}
(void)free(path);
- (void)printf("%s: Command not found.\n", prog);
+ /* whereis(1) is silent on failure. */
+ if (progmode != PROG_WHEREIS)
+ (void)printf("%s: Command not found.\n", prog);
return(0);
}
void
usage()
{
- (void) fprintf(stderr, "Usage: %s [name ...]\n", __progname);
+ (void) fprintf(stderr, "Usage: %s name [...]\n", __progname);
exit(1);
}