Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / look / look.c
bloba988fd85d42a8911842bc6722f85536abfccb6e6
1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
9 /*
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley software License Agreement
12 * specifies the terms and conditions for redistribution.
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <ctype.h>
18 #include <string.h>
20 FILE *dfile;
21 char *filenam = "/usr/share/lib/dict/words";
23 int fold;
24 int dict;
25 int tab;
26 #define WORDSIZE 257
27 char entry[WORDSIZE];
28 char word[WORDSIZE];
29 char key[WORDSIZE];
31 int compare(char *, char *);
32 void canon(char *, char *);
33 int getword(char *);
35 int
36 main(int argc, char **argv)
38 int c;
39 long top, bot, mid;
40 char *wstring, *ptr;
42 while (argc >= 2 && *argv[1] == '-') {
43 for (;;) {
44 switch (*++argv[1]) {
45 case 'd':
46 dict++;
47 continue;
48 case 'f':
49 fold++;
50 continue;
51 case 't':
52 tab = argv[1][1];
53 if (tab)
54 ++argv[1];
55 continue;
56 case 0:
57 break;
58 default:
59 continue;
61 break;
63 argc --;
64 argv++;
66 if (argc <= 1)
67 return (1);
68 if (argc == 2) {
69 fold++;
70 dict++;
71 } else
72 filenam = argv[2];
73 dfile = fopen(filenam, "r");
74 if (dfile == NULL) {
75 (void) fprintf(stderr, "look: can't open %s\n", filenam);
76 exit(2);
78 wstring = strdup(argv[1]);
79 if (tab != 0) {
80 if ((ptr = strchr(wstring, tab)) != NULL) {
81 *++ptr = '\0';
84 canon(wstring, key);
85 bot = 0;
86 (void) fseek(dfile, 0L, 2);
87 top = ftell(dfile);
88 for (;;) {
89 mid = (top+bot)/2;
90 (void) fseek(dfile, mid, 0);
91 do {
92 c = getc(dfile);
93 mid++;
94 } while (c != EOF && c != '\n');
95 if (!getword(entry))
96 break;
97 canon(entry, word);
98 switch (compare(key, word)) {
99 case -2:
100 case -1:
101 case 0:
102 if (top <= mid)
103 break;
104 top = mid;
105 continue;
106 case 1:
107 case 2:
108 bot = mid;
109 continue;
111 break;
113 (void) fseek(dfile, bot, 0);
114 while (ftell(dfile) < top) {
115 if (!getword(entry))
116 return (0);
117 canon(entry, word);
118 switch (compare(key, word)) {
119 case -2:
120 return (0);
121 case -1:
122 case 0:
123 (void) puts(entry);
124 break;
125 case 1:
126 case 2:
127 continue;
129 break;
131 while (getword(entry)) {
132 canon(entry, word);
133 switch (compare(key, word)) {
134 case -1:
135 case 0:
136 (void) puts(entry);
137 continue;
139 break;
141 return (0);
145 compare(char *s, char *t)
147 for (; *s == *t; s++, t++)
148 if (*s == 0)
149 return (0);
150 return (*s == 0? -1:
151 *t == 0? 1:
152 *s < *t? -2: 2);
156 getword(char *w)
158 int c;
159 int avail = WORDSIZE - 1;
161 while (avail--) {
162 c = getc(dfile);
163 if (c == EOF)
164 return (0);
165 if (c == '\n')
166 break;
167 *w++ = c;
169 while (c != '\n')
170 c = getc(dfile);
171 *w = 0;
172 return (1);
175 void
176 canon(char *old, char *new)
178 int c;
179 int avail = WORDSIZE - 1;
181 for (;;) {
182 *new = c = *old++;
183 if (c == 0) {
184 *new = 0;
185 break;
187 if (dict) {
188 if (!isalnum(c))
189 continue;
191 if (fold) {
192 if (isupper(c))
193 *new += 'a' - 'A';
195 new++;
196 avail--;
197 if (avail <= 0) {
198 *new = 0;
199 break;