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

Defined functions

find defined in line 235; used 3 times
findin defined in line 244; used 2 times
findv defined in line 225; used 3 times
getlist defined in line 120; used 3 times
itsit defined in line 265; used 2 times
lookbin defined in line 207; used 1 times
lookman defined in line 216; used 1 times
looksrc defined in line 198; used 1 times
lookup defined in line 147; used 1 times
main defined in line 62; never used
zerof defined in line 137; used 3 times

Defined variables

Bcnt defined in line 54; used 2 times
Bflag defined in line 53; used 3 times
Mcnt defined in line 56; used 2 times
Mflag defined in line 55; used 3 times
Scnt defined in line 52; used 2 times
Sflag defined in line 51; used 3 times
bflag defined in line 49; used 4 times
bindirs defined in line 11; used 1 times
count defined in line 143; used 7 times
mandirs defined in line 22; used 1 times
mflag defined in line 50; used 4 times
print defined in line 144; used 11 times
sccsid defined in line 2; never used
sflag defined in line 48; used 4 times
srcdirs defined in line 37; used 1 times
uflag defined in line 57; used 5 times
Last modified: 1983-03-15
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 993
Valid CSS Valid XHTML 1.0 Strict