- add which(1) functionality.

- fix bug where if a program appeared in more than one place it would
  erroneously exit(2)
- fix error messages to use err and errx as appropriate.
- made sure we don't pick up directories or non executable files.
This commit is contained in:
christos 2004-04-01 20:50:32 +00:00
parent cab05c8825
commit 745a6d0f0e

View File

@ -1,4 +1,4 @@
/* $NetBSD: whereis.c,v 1.13 2003/08/07 11:17:17 agc Exp $ */ /* $NetBSD: whereis.c,v 1.14 2004/04/01 20:50:32 christos Exp $ */
/*- /*-
* Copyright (c) 1993 * Copyright (c) 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993\n\
#if 0 #if 0
static char sccsid[] = "@(#)whereis.c 8.3 (Berkeley) 5/4/95"; static char sccsid[] = "@(#)whereis.c 8.3 (Berkeley) 5/4/95";
#endif #endif
__RCSID("$NetBSD: whereis.c,v 1.13 2003/08/07 11:17:17 agc Exp $"); __RCSID("$NetBSD: whereis.c,v 1.14 2004/04/01 20:50:32 christos Exp $");
#endif /* not lint */ #endif /* not lint */
#include <sys/param.h> #include <sys/param.h>
@ -53,22 +53,23 @@ __RCSID("$NetBSD: whereis.c,v 1.13 2003/08/07 11:17:17 agc Exp $");
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
void usage __P((void)); static void usage(void) __attribute__((__noreturn__));
int main __P((int, char *[]));
int int
main(argc, argv) main(int argc, char *argv[])
int argc;
char *argv[];
{ {
struct stat sb; struct stat sb;
size_t len; size_t len;
int ch, sverrno, mib[2]; int ch, mib[2];
char *p, *t, *std, path[MAXPATHLEN]; char *p, *t, *std, path[MAXPATHLEN];
int useenvpath = 0, found = 0; int which = strcmp(getprogname(), "which") == 0;
int useenvpath = which, found = 0;
while ((ch = getopt(argc, argv, "p")) != -1) while ((ch = getopt(argc, argv, "ap")) != -1)
switch (ch) { switch (ch) {
case 'a':
which = 0;
break;
case 'p': case 'p':
useenvpath = 1; /* use environment for PATH */ useenvpath = 1; /* use environment for PATH */
break; break;
@ -85,28 +86,24 @@ main(argc, argv)
if (useenvpath) { if (useenvpath) {
if ((std = getenv("PATH")) == NULL) if ((std = getenv("PATH")) == NULL)
err(1, "getenv: PATH" ); errx(1, "PATH environment variable is not set");
} else { } else {
/* Retrieve the standard path. */ /* Retrieve the standard path. */
mib[0] = CTL_USER; mib[0] = CTL_USER;
mib[1] = USER_CS_PATH; mib[1] = USER_CS_PATH;
if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1)
return (-1); err(1, "sysctl: user.cs_path");
if (len == 0) if (len == 0)
err(1, "user_cs_path: sysctl: zero length"); errx(1, "sysctl: user.cs_path (zero length)");
if ((std = malloc(len)) == NULL) if ((std = malloc(len)) == NULL)
err(1, NULL); err(1, NULL);
if (sysctl(mib, 2, std, &len, NULL, 0) == -1) { if (sysctl(mib, 2, std, &len, NULL, 0) == -1)
sverrno = errno; err(1, "sysctl: user.cs_path");
free(std);
errno = sverrno;
err(1, "sysctl: user_cs_path");
}
} }
/* For each path, for each program... */ /* For each path, for each program... */
for (; *argv; ++argv) for (; *argv; ++argv)
for (p = std;; *p++ = ':') { for (p = std; p; p && (*p++ = ':')) {
t = p; t = p;
if ((p = strchr(p, ':')) != NULL) { if ((p = strchr(p, ':')) != NULL) {
*p = '\0'; *p = '\0';
@ -116,21 +113,25 @@ main(argc, argv)
if (strlen(t) == 0) if (strlen(t) == 0)
t = "."; t = ".";
(void)snprintf(path, sizeof(path), "%s/%s", t, *argv); (void)snprintf(path, sizeof(path), "%s/%s", t, *argv);
if (!stat(path, &sb)) { if (stat(path, &sb) == -1)
continue;
if (!S_ISREG(sb.st_mode))
continue;
if (access(path, X_OK) == -1)
continue;
(void)printf("%s\n", path); (void)printf("%s\n", path);
found++; found++;
} if (which)
if (p == NULL)
break; break;
} }
return ((found == 0) ? 3 : ((found == argc) ? 0 : 2)); return ((found == 0) ? 3 : ((found >= argc) ? 0 : 2));
} }
void static void
usage() usage(void)
{ {
(void)fprintf(stderr, "usage: whereis [-p] program [...]\n"); (void)fprintf(stderr, "Usage: %s [-ap] program [...]\n", getprogname());
exit(1); exit(1);
} }