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