From: millert Date: Tue, 8 Apr 1997 02:44:05 +0000 (+0000) Subject: which(1) and whereis(1) are now the same program (hard linked). X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=3d908e8eb36e33a95b56ea39e32b4d3291d6d547;p=openbsd which(1) and whereis(1) are now the same program (hard linked). --- diff --git a/usr.bin/which/Makefile b/usr.bin/which/Makefile index c6433a1986b..da32657eb19 100644 --- a/usr.bin/which/Makefile +++ b/usr.bin/which/Makefile @@ -1,5 +1,7 @@ -# $OpenBSD: Makefile,v 1.4 1997/02/21 18:34:57 millert Exp $ +# $OpenBSD: Makefile,v 1.5 1997/04/08 02:44:05 millert Exp $ PROG= which +MAN= which.1 whereis.1 +LINKS= ${BINDIR}/which ${BINDIR}/whereis .include diff --git a/usr.bin/which/whereis.1 b/usr.bin/which/whereis.1 new file mode 100644 index 00000000000..f5f7a501b2d --- /dev/null +++ b/usr.bin/which/whereis.1 @@ -0,0 +1,82 @@ +.\" $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. diff --git a/usr.bin/which/which.1 b/usr.bin/which/which.1 index 0091b272df2..4b96652e3ae 100644 --- a/usr.bin/which/which.1 +++ b/usr.bin/which/which.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: which.1,v 1.4 1997/02/21 18:34:59 millert Exp $ +.\" $OpenBSD: which.1,v 1.5 1997/04/08 02:44:06 millert Exp $ .\" Copyright (c) 1980, 1991 Regents of the University of California. .\" All rights reserved. .\" @@ -93,6 +93,7 @@ with other shells like .Sh SEE ALSO .Xr csh 1 , .Xr sh 1 , +.Xr whereis 1 , .Xr environ 7 .Sh HISTORY A diff --git a/usr.bin/which/which.c b/usr.bin/which/which.c index 0f9c9265d2f..84a78cbe529 100644 --- a/usr.bin/which/which.c +++ b/usr.bin/which/which.c @@ -1,4 +1,4 @@ -/* $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 @@ -31,11 +31,12 @@ */ #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 #include +#include #include #include @@ -45,13 +46,17 @@ static char rcsid[] = "$OpenBSD: which.c,v 1.1 1997/02/21 18:35:00 millert Exp $ #include #include +#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 @@ -65,15 +70,44 @@ main(argc, argv) 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())) @@ -82,16 +116,17 @@ main(argc, argv) 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; @@ -110,7 +145,7 @@ which(prog, path) } 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) { @@ -137,13 +172,15 @@ which(prog, path) } (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); }