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