xref: /titanic_41/usr/src/cmd/look/look.c (revision 0d8b53344490faab75b127936edb53a2f1e0dc01)
1*0d8b5334Sceastha /*
2*0d8b5334Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*0d8b5334Sceastha  * Use is subject to license terms.
4*0d8b5334Sceastha  */
5*0d8b5334Sceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
15*0d8b5334Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate #include <ctype.h>
197c478bd9Sstevel@tonic-gate #include <string.h>
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate FILE *dfile;
227c478bd9Sstevel@tonic-gate char *filenam  = "/usr/share/lib/dict/words";
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate int fold;
257c478bd9Sstevel@tonic-gate int dict;
267c478bd9Sstevel@tonic-gate int tab;
277c478bd9Sstevel@tonic-gate #define WORDSIZE 257
287c478bd9Sstevel@tonic-gate char entry[WORDSIZE];
297c478bd9Sstevel@tonic-gate char word[WORDSIZE];
307c478bd9Sstevel@tonic-gate char key[WORDSIZE];
317c478bd9Sstevel@tonic-gate 
32*0d8b5334Sceastha int	compare(char *, char *);
33*0d8b5334Sceastha void	canon(char *, char *);
34*0d8b5334Sceastha int	getword(char *);
35*0d8b5334Sceastha 
36*0d8b5334Sceastha int
main(int argc,char ** argv)37*0d8b5334Sceastha main(int argc, char **argv)
387c478bd9Sstevel@tonic-gate {
39*0d8b5334Sceastha 	int c;
407c478bd9Sstevel@tonic-gate 	long top,bot,mid;
417c478bd9Sstevel@tonic-gate 	char *wstring, *ptr;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 	while(argc>=2 && *argv[1]=='-') {
447c478bd9Sstevel@tonic-gate 		for(;;) {
457c478bd9Sstevel@tonic-gate 			switch(*++argv[1]) {
467c478bd9Sstevel@tonic-gate 			case 'd':
477c478bd9Sstevel@tonic-gate 				dict++;
487c478bd9Sstevel@tonic-gate 				continue;
497c478bd9Sstevel@tonic-gate 			case 'f':
507c478bd9Sstevel@tonic-gate 				fold++;
517c478bd9Sstevel@tonic-gate 				continue;
527c478bd9Sstevel@tonic-gate 			case 't':
537c478bd9Sstevel@tonic-gate 				tab = argv[1][1];
547c478bd9Sstevel@tonic-gate 				if(tab)
557c478bd9Sstevel@tonic-gate 					++argv[1];
567c478bd9Sstevel@tonic-gate 				continue;
577c478bd9Sstevel@tonic-gate 			case 0:
587c478bd9Sstevel@tonic-gate 				break;
597c478bd9Sstevel@tonic-gate 			default:
607c478bd9Sstevel@tonic-gate 				continue;
617c478bd9Sstevel@tonic-gate 			}
627c478bd9Sstevel@tonic-gate 			break;
637c478bd9Sstevel@tonic-gate 		}
647c478bd9Sstevel@tonic-gate 		argc --;
657c478bd9Sstevel@tonic-gate 		argv++;
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	if(argc<=1)
68*0d8b5334Sceastha 		return (1);
697c478bd9Sstevel@tonic-gate 	if(argc==2) {
707c478bd9Sstevel@tonic-gate 		fold++;
717c478bd9Sstevel@tonic-gate 		dict++;
727c478bd9Sstevel@tonic-gate 	} else
737c478bd9Sstevel@tonic-gate 		filenam = argv[2];
747c478bd9Sstevel@tonic-gate 	dfile = fopen(filenam,"r");
757c478bd9Sstevel@tonic-gate 	if(dfile==NULL) {
767c478bd9Sstevel@tonic-gate 		fprintf(stderr,"look: can't open %s\n",filenam);
777c478bd9Sstevel@tonic-gate 		exit(2);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	wstring = strdup(argv[1]);
807c478bd9Sstevel@tonic-gate 	if (tab != NULL) {
817c478bd9Sstevel@tonic-gate 		if ((ptr = strchr(wstring, tab)) != NULL) {
827c478bd9Sstevel@tonic-gate 			*++ptr = '\0';
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	canon(wstring,key);
867c478bd9Sstevel@tonic-gate 	bot = 0;
877c478bd9Sstevel@tonic-gate 	fseek(dfile,0L,2);
887c478bd9Sstevel@tonic-gate 	top = ftell(dfile);
897c478bd9Sstevel@tonic-gate 	for(;;) {
907c478bd9Sstevel@tonic-gate 		mid = (top+bot)/2;
917c478bd9Sstevel@tonic-gate 		fseek(dfile,mid,0);
927c478bd9Sstevel@tonic-gate 		do {
937c478bd9Sstevel@tonic-gate 			c = getc(dfile);
947c478bd9Sstevel@tonic-gate 			mid++;
957c478bd9Sstevel@tonic-gate 		} while(c!=EOF && c!='\n');
967c478bd9Sstevel@tonic-gate 		if(!getword(entry))
977c478bd9Sstevel@tonic-gate 			break;
987c478bd9Sstevel@tonic-gate 		canon(entry,word);
997c478bd9Sstevel@tonic-gate 		switch(compare(key,word)) {
1007c478bd9Sstevel@tonic-gate 		case -2:
1017c478bd9Sstevel@tonic-gate 		case -1:
1027c478bd9Sstevel@tonic-gate 		case 0:
1037c478bd9Sstevel@tonic-gate 			if(top<=mid)
1047c478bd9Sstevel@tonic-gate 				break;
1057c478bd9Sstevel@tonic-gate 			top = mid;
1067c478bd9Sstevel@tonic-gate 			continue;
1077c478bd9Sstevel@tonic-gate 		case 1:
1087c478bd9Sstevel@tonic-gate 		case 2:
1097c478bd9Sstevel@tonic-gate 			bot = mid;
1107c478bd9Sstevel@tonic-gate 			continue;
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 		break;
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	fseek(dfile,bot,0);
1157c478bd9Sstevel@tonic-gate 	while(ftell(dfile)<top) {
1167c478bd9Sstevel@tonic-gate 		if(!getword(entry))
117*0d8b5334Sceastha 			return (0);
1187c478bd9Sstevel@tonic-gate 		canon(entry,word);
1197c478bd9Sstevel@tonic-gate 		switch(compare(key,word)) {
1207c478bd9Sstevel@tonic-gate 		case -2:
121*0d8b5334Sceastha 			return (0);
1227c478bd9Sstevel@tonic-gate 		case -1:
1237c478bd9Sstevel@tonic-gate 		case 0:
1247c478bd9Sstevel@tonic-gate 			puts(entry);
1257c478bd9Sstevel@tonic-gate 			break;
1267c478bd9Sstevel@tonic-gate 		case 1:
1277c478bd9Sstevel@tonic-gate 		case 2:
1287c478bd9Sstevel@tonic-gate 			continue;
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		break;
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	while(getword(entry)) {
1337c478bd9Sstevel@tonic-gate 		canon(entry,word);
1347c478bd9Sstevel@tonic-gate 		switch(compare(key,word)) {
1357c478bd9Sstevel@tonic-gate 		case -1:
1367c478bd9Sstevel@tonic-gate 		case 0:
1377c478bd9Sstevel@tonic-gate 			puts(entry);
1387c478bd9Sstevel@tonic-gate 			continue;
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 		break;
1417c478bd9Sstevel@tonic-gate 	}
142*0d8b5334Sceastha 	return (0);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
145*0d8b5334Sceastha int
compare(char * s,char * t)146*0d8b5334Sceastha compare(char *s, char *t)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	for(;*s==*t;s++,t++)
1497c478bd9Sstevel@tonic-gate 		if(*s==0)
1507c478bd9Sstevel@tonic-gate 			return(0);
1517c478bd9Sstevel@tonic-gate 	return(*s==0? -1:
1527c478bd9Sstevel@tonic-gate 		*t==0? 1:
1537c478bd9Sstevel@tonic-gate 		*s<*t? -2:
1547c478bd9Sstevel@tonic-gate 		2);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
157*0d8b5334Sceastha int
getword(char * w)158*0d8b5334Sceastha getword(char *w)
1597c478bd9Sstevel@tonic-gate {
160*0d8b5334Sceastha 	int c;
161*0d8b5334Sceastha 	int avail = WORDSIZE - 1;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	while(avail--) {
1647c478bd9Sstevel@tonic-gate 		c = getc(dfile);
1657c478bd9Sstevel@tonic-gate 		if(c==EOF)
1667c478bd9Sstevel@tonic-gate 			return(0);
1677c478bd9Sstevel@tonic-gate 		if(c=='\n')
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 		*w++ = c;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	while (c != '\n')
1727c478bd9Sstevel@tonic-gate 		c = getc(dfile);
1737c478bd9Sstevel@tonic-gate 	*w = 0;
1747c478bd9Sstevel@tonic-gate 	return(1);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
177*0d8b5334Sceastha void
canon(char * old,char * new)178*0d8b5334Sceastha canon(char *old, char *new)
1797c478bd9Sstevel@tonic-gate {
180*0d8b5334Sceastha 	int c;
181*0d8b5334Sceastha 	int avail = WORDSIZE - 1;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	for(;;) {
1847c478bd9Sstevel@tonic-gate 		*new = c = *old++;
1857c478bd9Sstevel@tonic-gate 		if(c==0) {
1867c478bd9Sstevel@tonic-gate 			*new = 0;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		if(dict) {
1907c478bd9Sstevel@tonic-gate 			if(!isalnum(c))
1917c478bd9Sstevel@tonic-gate 				continue;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 		if(fold) {
1947c478bd9Sstevel@tonic-gate 			if(isupper(c))
1957c478bd9Sstevel@tonic-gate 				*new += 'a' - 'A';
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 		new++;
1987c478bd9Sstevel@tonic-gate 		avail--;
1997c478bd9Sstevel@tonic-gate 		if (avail <= 0) {
2007c478bd9Sstevel@tonic-gate 			*new = 0;
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate }
205