1 /* look 1.3 - Find lines in a sorted list. Author: Kees J. Bot
10 char DEFAULT
[] = "/usr/lib/dict/words";
12 char *string
, *wordlist
= DEFAULT
;
14 #define MAXLEN 1024 /* Maximum word length. */
18 void nonascii(char *what
)
20 fprintf(stderr
, "look: %s contains non-ASCII characters.\n", what
);
24 int compare(char *prefix
, char *word
)
26 char *p
= prefix
, *w
= word
;
31 if ((cp
= *p
++) == 0) return 0;
32 if (!isascii(cp
)) nonascii("prefix string");
33 } while (dflg
&& !isspace(cp
) && !isalnum(cp
));
37 while (isspace(*p
)) p
++;
41 if (fflg
&& isupper(cp
)) cp
= tolower(cp
);
44 if ((cw
= *w
++) == 0) return 1;
45 if (!isascii(cw
)) nonascii(wordlist
);
46 } while (dflg
&& !isspace(cw
) && !isalnum(cw
));
50 while (isspace(*w
)) w
++;
54 if (fflg
&& isupper(cw
)) cw
= tolower(cw
);
60 char *readword(FILE *f
)
62 static char word
[MAXLEN
+ 2];
65 if (fgets(word
, sizeof(word
), f
) == nil
) {
67 fprintf(stderr
, "look: read error on %s",
76 if (word
[n
-1] != '\n') {
77 fprintf(stderr
, "look: word from %s is too long\n", wordlist
);
92 if ((f
= fopen(wordlist
, "r")) == nil
) {
93 fprintf(stderr
, "look: Can't open %s\n", wordlist
);
99 fseek(f
, (off_t
) 0, 2);
103 while (low
<= high
) {
104 mid
= (low
+ high
) / 2;
108 if (mid
!= 0) readword(f
);
110 if ((word
= readword(f
)) == nil
)
113 c
= compare(string
, word
);
115 if (c
<= 0) high
= mid
- 1; else low
= mid
+ 1;
118 if (low
!= 0) readword(f
);
121 while (c
>= 0 && (word
= readword(f
)) != nil
) {
122 c
= compare(string
, word
);
124 if (c
== 0) puts(word
);
128 int main(int argc
, char **argv
)
130 if (argc
== 2) dflg
= fflg
= 1;
132 while (argc
> 1 && argv
[1][0] == '-') {
133 char *p
= argv
[1] + 1;
137 case 'd': dflg
= 1; break;
138 case 'f': fflg
= 1; break;
140 fprintf(stderr
, "look: Bad flag: %c\n", p
[-1]);
151 fprintf(stderr
, "Usage: look [-df] string [file]\n");
158 /* Kees J. Bot 24-5-89. */