# 2.11BSD#431 - /usr/src/usr.bin/look.c

##### Functions

    canon             X
    compare           X
    getword           X
    main              X

##### Variables

    dict              X
    entry             X
    filenam           X
    fold              X
    key               X
    tab               X
    word              X

       1: static char *sccsid = "@(#)look.c	4.2 (Berkeley) 7/2/81";
       2: #include <stdio.h>
       3: #include <ctype.h>
       4:
       5: FILE *dfile;
       6: char *filenam  = "/usr/dict/words";
       7:
       8: int fold;
       9: int dict;
      10: int tab;
      11: char entry[250];
      12: char word[250];
      13: char key[50];
      14:
      15: main(argc,argv)
      16: char **argv;
      17: {
      18:     register c;
      19:     long top,bot,mid;
      20:     long ftell();
      21:
      22:     while(argc>=2 && *argv[1]=='-') {
      23:         for(;;) {
      24:             switch(*++argv[1]) {
      25:             case 'd':
      26:                 dict++;
      27:                 continue;
      28:             case 'f':
      29:                 fold++;
      30:                 continue;
      31:             case 't':
      32:                 tab = argv[1][1];
      33:                 if(tab)
      34:                     ++argv[1];
      35:                 continue;
      36:             case 0:
      37:                 break;
      38:             default:
      39:                 continue;
      40:             }
      41:             break;
      42:         }
      43:         argc --;
      44:         argv++;
      45:     }
      46:     if(argc<=1)
      47:         return;
      48:     if(argc==2) {
      49:         fold++;
      50:         dict++;
      51:     } else
      52:         filenam = argv[2];
      53:     dfile = fopen(filenam,"r");
      54:     if(dfile==NULL) {
      55:         fprintf(stderr,"look: can't open %s\n",filenam);
      56:         exit(2);
      57:     }
      58:     canon(argv[1],key);
      59:     bot = 0;
      60:     fseek(dfile,0L,2);
      61:     top = ftell(dfile);
      62:     for(;;) {
      63:         mid = (top+bot)/2;
      64:         fseek(dfile,mid,0);
      65:         do {
      66:             c = getc(dfile);
      67:             mid++;
      68:         } while(c!=EOF && c!='\n');
      69:         if(!getword(entry))
      70:             break;
      71:         canon(entry,word);
      72:         switch(compare(key,word)) {
      73:         case -2:
      74:         case -1:
      75:         case 0:
      76:             if(top<=mid)
      77:                 break;
      78:             top = mid;
      79:             continue;
      80:         case 1:
      81:         case 2:
      82:             bot = mid;
      83:             continue;
      84:         }
      85:         break;
      86:     }
      87:     fseek(dfile,bot,0);
      88:     while(ftell(dfile)<top) {
      89:         if(!getword(entry))
      90:             return;
      91:         canon(entry,word);
      92:         switch(compare(key,word)) {
      93:         case -2:
      94:             return;
      95:         case -1:
      96:         case 0:
      97:             puts(entry,stdout);
      98:             break;
      99:         case 1:
     100:         case 2:
     101:             continue;
     102:         }
     103:         break;
     104:     }
     105:     while(getword(entry)) {
     106:         canon(entry,word);
     107:         switch(compare(key,word)) {
     108:         case -1:
     109:         case 0:
     110:             puts(entry,stdout);
     111:             continue;
     112:         }
     113:         break;
     114:     }
     115:     exit(0);
     116: }
     117:
     118: compare(s,t)
     119: register char *s,*t;
     120: {
     121:     for(;*s==*t;s++,t++)
     122:         if(*s==0)
     123:             return(0);
     124:     return(*s==0? -1:
     125:         *t==0? 1:
     126:         *s<*t? -2:
     127:         2);
     128: }
     129:
     130: getword(w)
     131: char *w;
     132: {
     133:     register c;
     134:     for(;;) {
     135:         c = getc(dfile);
     136:         if(c==EOF)
     137:             return(0);
     138:         if(c=='\n')
     139:             break;
     140:         *w++ = c;
     141:     }
     142:     *w = 0;
     143:     return(1);
     144: }
     145:
     146: canon(old,new)
     147: char *old,*new;
     148: {
     149:     register c;
     150:     for(;;) {
     151:         *new = c = *old++;
     152:         if(c==0||c==tab) {
     153:             *new = 0;
     154:             break;
     155:         }
     156:         if(dict) {
     157:             if(!isalnum(c))
     158:                 continue;
     159:         }
     160:         if(fold) {
     161:             if(isupper(c))
     162:                 *new += 'a' - 'A';
     163:         }
     164:         new++;
     165:     }
     166: }

#### Defined functions

canon defined in line 146; used 4 times

- in line 58, 71, 91, 106

compare defined in line 118; used 3 times

- in line 72, 92, 107

getword defined in line 130; used 3 times

- in line 69, 89, 105

main defined in line 15; never used

#### Defined variables

dict defined in line 9; used 3 times

- in line 26, 50, 156

entry defined in line 11; used 8 times

- in line 69-71(2), 89-91(2), 97, 105-110(3)

filenam defined in line 6; used 3 times

- in line 52-55(3)

fold defined in line 8; used 3 times

- in line 29, 49, 160

key defined in line 13; used 4 times

- in line 58, 72, 92, 107

sccsid defined in line 1; never used
tab defined in line 10; used 3 times

- in line 32-33(2), 152

word defined in line 12; used 6 times

- in line 71-72(2), 91-92(2), 106-107(2)

Last modified: 1987-02-17 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1168 |