1 /*- 2 * Copyright (c) 1993 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1993 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)whereis.c 5.1 (Berkeley) 05/06/93"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/stat.h> 20 #include <sys/sysctl.h> 21 22 #include <err.h> 23 #include <errno.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 void usage __P((void)); 29 30 int 31 main(argc, argv) 32 int argc; 33 char *argv[]; 34 { 35 struct stat sb; 36 size_t len; 37 int ch, sverrno, mib[2]; 38 char *p, *t, *std, path[MAXPATHLEN]; 39 40 while ((ch = getopt(argc, argv, "")) != EOF) 41 switch (ch) { 42 case '?': 43 default: 44 usage(); 45 } 46 argc -= optind; 47 argv += optind; 48 49 /* Retrieve the standard path. */ 50 mib[0] = CTL_USER; 51 mib[1] = USER_CS_PATH; 52 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) 53 return (-1); 54 if (len == 0) 55 err(1, "user_cs_path: sysctl: zero length\n"); 56 if ((std = malloc(len)) == NULL) 57 err(1, NULL); 58 if (sysctl(mib, 2, std, &len, NULL, 0) == -1) { 59 sverrno = errno; 60 free(std); 61 errno = sverrno; 62 err(1, "sysctl: user_cs_path"); 63 } 64 65 /* For each path, for each program... */ 66 for (; *argv; ++argv) 67 for (p = std;; *p++ = ':') { 68 t = p; 69 if ((p = strchr(p, ':')) != NULL) { 70 *p = '\0'; 71 if (t == p) 72 t = "."; 73 } else 74 if (strlen(t) == 0) 75 t = "."; 76 (void)snprintf(path, sizeof(path), "%s/%s", t, *argv); 77 if (!stat(path, &sb)) 78 (void)printf("%s\n", path); 79 if (p == NULL) 80 break; 81 } 82 } 83 84 void 85 usage() 86 { 87 (void)fprintf(stderr, "whereis: program ...\n"); 88 exit (1); 89 } 90