/*- * Copyright (c) 1993 * The Regents of the University of California. All rights reserved. * * %sccs.include.redist.c% */ #ifndef lint static char copyright[] = "@(#) Copyright (c) 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)whereis.c 8.1 (Berkeley) 06/06/93"; #endif /* not lint */ #include #include #include #include #include #include #include #include void usage __P((void)); int main(argc, argv) int argc; char *argv[]; { struct stat sb; size_t len; int ch, sverrno, mib[2]; char *p, *t, *std, path[MAXPATHLEN]; while ((ch = getopt(argc, argv, "")) != EOF) switch (ch) { case '?': default: usage(); } argc -= optind; argv += optind; /* Retrieve the standard path. */ mib[0] = CTL_USER; mib[1] = USER_CS_PATH; if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) return (-1); if (len == 0) err(1, "user_cs_path: sysctl: zero length\n"); if ((std = malloc(len)) == NULL) err(1, NULL); if (sysctl(mib, 2, std, &len, NULL, 0) == -1) { sverrno = errno; free(std); errno = sverrno; err(1, "sysctl: user_cs_path"); } /* For each path, for each program... */ for (; *argv; ++argv) for (p = std;; *p++ = ':') { t = p; if ((p = strchr(p, ':')) != NULL) { *p = '\0'; if (t == p) t = "."; } else if (strlen(t) == 0) t = "."; (void)snprintf(path, sizeof(path), "%s/%s", t, *argv); if (!stat(path, &sb)) (void)printf("%s\n", path); if (p == NULL) break; } } void usage() { (void)fprintf(stderr, "whereis: program ...\n"); exit (1); }