tools/llvm: Do not build with symbols
[minix3.git] / minix / commands / look / look.c
blob746b247cf724529b7b7d0b1a0801eea65171aae9
1 /* look 1.3 - Find lines in a sorted list. Author: Kees J. Bot
2 */
3 #define nil 0
4 #include <sys/types.h>
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <string.h>
10 char DEFAULT[] = "/usr/lib/dict/words";
12 char *string, *wordlist= DEFAULT;
14 #define MAXLEN 1024 /* Maximum word length. */
16 int dflg= 0, fflg= 0;
18 void nonascii(char *what)
20 fprintf(stderr, "look: %s contains non-ASCII characters.\n", what);
21 exit(1);
24 int compare(char *prefix, char *word)
26 char *p= prefix, *w= word;
27 int cp, cw;
29 do {
30 do {
31 if ((cp= *p++) == 0) return 0;
32 if (!isascii(cp)) nonascii("prefix string");
33 } while (dflg && !isspace(cp) && !isalnum(cp));
35 if (dflg) {
36 if (isspace(cp)) {
37 while (isspace(*p)) p++;
38 cp= ' ';
41 if (fflg && isupper(cp)) cp= tolower(cp);
43 do {
44 if ((cw= *w++) == 0) return 1;
45 if (!isascii(cw)) nonascii(wordlist);
46 } while (dflg && !isspace(cw) && !isalnum(cw));
48 if (dflg) {
49 if (isspace(cw)) {
50 while (isspace(*w)) w++;
51 cw= ' ';
54 if (fflg && isupper(cw)) cw= tolower(cw);
55 } while (cp == cw);
57 return cp - cw;
60 char *readword(FILE *f)
62 static char word[MAXLEN + 2];
63 int n;
65 if (fgets(word, sizeof(word), f) == nil) {
66 if (ferror(f)) {
67 fprintf(stderr, "look: read error on %s",
68 wordlist);
69 exit(1);
71 return nil;
74 n= strlen(word);
76 if (word[n-1] != '\n') {
77 fprintf(stderr, "look: word from %s is too long\n", wordlist);
78 exit(1);
80 word[n-1] = 0;
82 return word;
85 void look(void)
87 off_t low, mid, high;
88 FILE *f;
89 char *word;
90 int c;
92 if ((f= fopen(wordlist, "r")) == nil) {
93 fprintf(stderr, "look: Can't open %s\n", wordlist);
94 exit(1);
97 low= 0;
99 fseek(f, (off_t) 0, 2);
101 high= ftell(f);
103 while (low <= high) {
104 mid= (low + high) / 2;
106 fseek(f, mid, 0);
108 if (mid != 0) readword(f);
110 if ((word= readword(f)) == nil)
111 c= -1;
112 else
113 c= compare(string, word);
115 if (c <= 0) high= mid - 1; else low= mid + 1;
117 fseek(f, low, 0);
118 if (low != 0) readword(f);
120 c=0;
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;
135 while (*p != 0) {
136 switch (*p++) {
137 case 'd': dflg= 1; break;
138 case 'f': fflg= 1; break;
139 default:
140 fprintf(stderr, "look: Bad flag: %c\n", p[-1]);
141 exit(1);
144 argc--;
145 argv++;
147 if (argc == 3)
148 wordlist= argv[2];
149 else
150 if (argc != 2) {
151 fprintf(stderr, "Usage: look [-df] string [file]\n");
152 exit(1);
154 string= argv[1];
155 look();
156 exit(0);
158 /* Kees J. Bot 24-5-89. */