1: /* 2: * Copyright (c) 1980 Regents of the University of California. 3: * All rights reserved. The Berkeley software License Agreement 4: * specifies the terms and conditions for redistribution. 5: */ 6: 7: #if !defined(lint) && defined(DOSCCS) 8: char copyright[] = 9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10: All rights reserved.\n"; 11: 12: static char sccsid[] = "@(#)whereis.c 5.1.1 (2.11BSD) 1996/10/23"; 13: #endif 14: 15: #include <sys/param.h> 16: #include <sys/dir.h> 17: #include <stdio.h> 18: #include <ctype.h> 19: 20: static char *bindirs[] = { 21: "/etc", 22: "/bin", 23: "/sbin", 24: "/usr/bin", 25: "/usr/sbin", 26: "/usr/games", 27: "/usr/ucb", 28: "/usr/libexec", 29: "/usr/local", 30: "/usr/local/bin", 31: "/usr/new", 32: 0 33: }; 34: static char *mandirs[] = { 35: "/usr/man/man1", 36: "/usr/man/man2", 37: "/usr/man/man3", 38: "/usr/man/man4", 39: "/usr/man/man5", 40: "/usr/man/man6", 41: "/usr/man/man7", 42: "/usr/man/man8", 43: "/usr/man/manl", 44: "/usr/man/mann", 45: "/usr/man/mano", 46: 0 47: }; 48: static char *srcdirs[] = { 49: "/usr/src/bin", 50: "/usr/src/sbin", 51: "/usr/src/usr.bin", 52: "/usr/src/usr.sbin", 53: "/usr/src/etc", 54: "/usr/src/ucb", 55: "/usr/src/games", 56: "/usr/src/usr.lib", 57: "/usr/src/lib", 58: "/usr/src/libexec", 59: "/usr/src/local", 60: "/usr/src/new", 61: "/usr/src/include", 62: "/usr/src/lib/libc/gen", 63: "/usr/src/lib/libc/stdio", 64: "/usr/src/lib/libc/sys", 65: "/usr/src/lib/libc/net/inet", 66: "/usr/src/lib/libc/net/misc", 67: "/usr/src/ucb/pascal", 68: "/usr/src/ucb/pascal/utilities", 69: 0 70: }; 71: 72: char sflag = 1; 73: char bflag = 1; 74: char mflag = 1; 75: char **Sflag; 76: int Scnt; 77: char **Bflag; 78: int Bcnt; 79: char **Mflag; 80: int Mcnt; 81: char uflag; 82: /* 83: * whereis name 84: * look for source, documentation and binaries 85: */ 86: main(argc, argv) 87: int argc; 88: char *argv[]; 89: { 90: 91: argc--, argv++; 92: if (argc == 0) { 93: usage: 94: fprintf(stderr, "whereis [ -sbmu ] [ -SBM dir ... -f ] name...\n"); 95: exit(1); 96: } 97: do 98: if (argv[0][0] == '-') { 99: register char *cp = argv[0] + 1; 100: while (*cp) switch (*cp++) { 101: 102: case 'f': 103: break; 104: 105: case 'S': 106: getlist(&argc, &argv, &Sflag, &Scnt); 107: break; 108: 109: case 'B': 110: getlist(&argc, &argv, &Bflag, &Bcnt); 111: break; 112: 113: case 'M': 114: getlist(&argc, &argv, &Mflag, &Mcnt); 115: break; 116: 117: case 's': 118: zerof(); 119: sflag++; 120: continue; 121: 122: case 'u': 123: uflag++; 124: continue; 125: 126: case 'b': 127: zerof(); 128: bflag++; 129: continue; 130: 131: case 'm': 132: zerof(); 133: mflag++; 134: continue; 135: 136: default: 137: goto usage; 138: } 139: argv++; 140: } else 141: lookup(*argv++); 142: while (--argc > 0); 143: } 144: 145: getlist(argcp, argvp, flagp, cntp) 146: char ***argvp; 147: int *argcp; 148: char ***flagp; 149: int *cntp; 150: { 151: 152: (*argvp)++; 153: *flagp = *argvp; 154: *cntp = 0; 155: for ((*argcp)--; *argcp > 0 && (*argvp)[0][0] != '-'; (*argcp)--) 156: (*cntp)++, (*argvp)++; 157: (*argcp)++; 158: (*argvp)--; 159: } 160: 161: 162: zerof() 163: { 164: 165: if (sflag && bflag && mflag) 166: sflag = bflag = mflag = 0; 167: } 168: int count; 169: int print; 170: 171: 172: lookup(cp) 173: register char *cp; 174: { 175: register char *dp; 176: 177: for (dp = cp; *dp; dp++) 178: continue; 179: for (; dp > cp; dp--) { 180: if (*dp == '.') { 181: *dp = 0; 182: break; 183: } 184: } 185: for (dp = cp; *dp; dp++) 186: if (*dp == '/') 187: cp = dp + 1; 188: if (uflag) { 189: print = 0; 190: count = 0; 191: } else 192: print = 1; 193: again: 194: if (print) 195: printf("%s:", cp); 196: if (sflag) { 197: looksrc(cp); 198: if (uflag && print == 0 && count != 1) { 199: print = 1; 200: goto again; 201: } 202: } 203: count = 0; 204: if (bflag) { 205: lookbin(cp); 206: if (uflag && print == 0 && count != 1) { 207: print = 1; 208: goto again; 209: } 210: } 211: count = 0; 212: if (mflag) { 213: lookman(cp); 214: if (uflag && print == 0 && count != 1) { 215: print = 1; 216: goto again; 217: } 218: } 219: if (print) 220: printf("\n"); 221: } 222: 223: looksrc(cp) 224: char *cp; 225: { 226: if (Sflag == 0) { 227: find(srcdirs, cp); 228: } else 229: findv(Sflag, Scnt, cp); 230: } 231: 232: lookbin(cp) 233: char *cp; 234: { 235: if (Bflag == 0) 236: find(bindirs, cp); 237: else 238: findv(Bflag, Bcnt, cp); 239: } 240: 241: lookman(cp) 242: char *cp; 243: { 244: if (Mflag == 0) { 245: find(mandirs, cp); 246: } else 247: findv(Mflag, Mcnt, cp); 248: } 249: 250: findv(dirv, dirc, cp) 251: char **dirv; 252: int dirc; 253: char *cp; 254: { 255: 256: while (dirc > 0) 257: findin(*dirv++, cp), dirc--; 258: } 259: 260: find(dirs, cp) 261: char **dirs; 262: char *cp; 263: { 264: 265: while (*dirs) 266: findin(*dirs++, cp); 267: } 268: 269: findin(dir, cp) 270: char *dir, *cp; 271: { 272: DIR *dirp; 273: struct direct *dp; 274: 275: dirp = opendir(dir); 276: if (dirp == NULL) 277: return; 278: while ((dp = readdir(dirp)) != NULL) { 279: if (itsit(cp, dp->d_name)) { 280: count++; 281: if (print) 282: printf(" %s/%s", dir, dp->d_name); 283: } 284: } 285: closedir(dirp); 286: } 287: 288: itsit(cp, dp) 289: register char *cp, *dp; 290: { 291: register int i = strlen(dp); 292: 293: if (dp[0] == 's' && dp[1] == '.' && itsit(cp, dp+2)) 294: return (1); 295: while (*cp && *dp && *cp == *dp) 296: cp++, dp++, i--; 297: if (*cp == 0 && *dp == 0) 298: return (1); 299: while (isdigit(*dp)) 300: dp++; 301: if (*cp == 0 && *dp++ == '.') { 302: --i; 303: while (i > 0 && *dp) 304: if (--i, *dp++ == '.') 305: return (*dp++ == 'C' && *dp++ == 0); 306: return (1); 307: } 308: return (0); 309: }