xref: /illumos-gate/usr/src/cmd/look/look.c (revision d4d37f1b)
10d8b5334Sceastha /*
20d8b5334Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
30d8b5334Sceastha  * Use is subject to license terms.
40d8b5334Sceastha  */
50d8b5334Sceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7*d4d37f1bSToomas Soome /*	  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 
157c478bd9Sstevel@tonic-gate #include <stdio.h>
167c478bd9Sstevel@tonic-gate #include <ctype.h>
177c478bd9Sstevel@tonic-gate #include <string.h>
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate FILE *dfile;
207c478bd9Sstevel@tonic-gate char *filenam  = "/usr/share/lib/dict/words";
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate int fold;
237c478bd9Sstevel@tonic-gate int dict;
247c478bd9Sstevel@tonic-gate int tab;
257c478bd9Sstevel@tonic-gate #define WORDSIZE 257
267c478bd9Sstevel@tonic-gate char entry[WORDSIZE];
277c478bd9Sstevel@tonic-gate char word[WORDSIZE];
287c478bd9Sstevel@tonic-gate char key[WORDSIZE];
297c478bd9Sstevel@tonic-gate 
300d8b5334Sceastha int	compare(char *, char *);
310d8b5334Sceastha void	canon(char *, char *);
320d8b5334Sceastha int	getword(char *);
330d8b5334Sceastha 
340d8b5334Sceastha int
main(int argc,char ** argv)350d8b5334Sceastha main(int argc, char **argv)
367c478bd9Sstevel@tonic-gate {
370d8b5334Sceastha 	int c;
387c478bd9Sstevel@tonic-gate 	long top,bot,mid;
397c478bd9Sstevel@tonic-gate 	char *wstring, *ptr;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	while(argc>=2 && *argv[1]=='-') {
427c478bd9Sstevel@tonic-gate 		for(;;) {
437c478bd9Sstevel@tonic-gate 			switch(*++argv[1]) {
447c478bd9Sstevel@tonic-gate 			case 'd':
457c478bd9Sstevel@tonic-gate 				dict++;
467c478bd9Sstevel@tonic-gate 				continue;
477c478bd9Sstevel@tonic-gate 			case 'f':
487c478bd9Sstevel@tonic-gate 				fold++;
497c478bd9Sstevel@tonic-gate 				continue;
507c478bd9Sstevel@tonic-gate 			case 't':
517c478bd9Sstevel@tonic-gate 				tab = argv[1][1];
527c478bd9Sstevel@tonic-gate 				if(tab)
537c478bd9Sstevel@tonic-gate 					++argv[1];
547c478bd9Sstevel@tonic-gate 				continue;
557c478bd9Sstevel@tonic-gate 			case 0:
567c478bd9Sstevel@tonic-gate 				break;
577c478bd9Sstevel@tonic-gate 			default:
587c478bd9Sstevel@tonic-gate 				continue;
597c478bd9Sstevel@tonic-gate 			}
607c478bd9Sstevel@tonic-gate 			break;
617c478bd9Sstevel@tonic-gate 		}
627c478bd9Sstevel@tonic-gate 		argc --;
637c478bd9Sstevel@tonic-gate 		argv++;
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 	if(argc<=1)
660d8b5334Sceastha 		return (1);
677c478bd9Sstevel@tonic-gate 	if(argc==2) {
687c478bd9Sstevel@tonic-gate 		fold++;
697c478bd9Sstevel@tonic-gate 		dict++;
707c478bd9Sstevel@tonic-gate 	} else
717c478bd9Sstevel@tonic-gate 		filenam = argv[2];
727c478bd9Sstevel@tonic-gate 	dfile = fopen(filenam,"r");
737c478bd9Sstevel@tonic-gate 	if(dfile==NULL) {
747c478bd9Sstevel@tonic-gate 		fprintf(stderr,"look: can't open %s\n",filenam);
757c478bd9Sstevel@tonic-gate 		exit(2);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	wstring = strdup(argv[1]);
78*d4d37f1bSToomas Soome 	if (tab != 0) {
797c478bd9Sstevel@tonic-gate 		if ((ptr = strchr(wstring, tab)) != NULL) {
807c478bd9Sstevel@tonic-gate 			*++ptr = '\0';
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	canon(wstring,key);
847c478bd9Sstevel@tonic-gate 	bot = 0;
857c478bd9Sstevel@tonic-gate 	fseek(dfile,0L,2);
867c478bd9Sstevel@tonic-gate 	top = ftell(dfile);
877c478bd9Sstevel@tonic-gate 	for(;;) {
887c478bd9Sstevel@tonic-gate 		mid = (top+bot)/2;
897c478bd9Sstevel@tonic-gate 		fseek(dfile,mid,0);
907c478bd9Sstevel@tonic-gate 		do {
917c478bd9Sstevel@tonic-gate 			c = getc(dfile);
927c478bd9Sstevel@tonic-gate 			mid++;
937c478bd9Sstevel@tonic-gate 		} while(c!=EOF && c!='\n');
947c478bd9Sstevel@tonic-gate 		if(!getword(entry))
957c478bd9Sstevel@tonic-gate 			break;
967c478bd9Sstevel@tonic-gate 		canon(entry,word);
977c478bd9Sstevel@tonic-gate 		switch(compare(key,word)) {
987c478bd9Sstevel@tonic-gate 		case -2:
997c478bd9Sstevel@tonic-gate 		case -1:
1007c478bd9Sstevel@tonic-gate 		case 0:
1017c478bd9Sstevel@tonic-gate 			if(top<=mid)
1027c478bd9Sstevel@tonic-gate 				break;
1037c478bd9Sstevel@tonic-gate 			top = mid;
1047c478bd9Sstevel@tonic-gate 			continue;
1057c478bd9Sstevel@tonic-gate 		case 1:
1067c478bd9Sstevel@tonic-gate 		case 2:
1077c478bd9Sstevel@tonic-gate 			bot = mid;
1087c478bd9Sstevel@tonic-gate 			continue;
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		break;
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	fseek(dfile,bot,0);
1137c478bd9Sstevel@tonic-gate 	while(ftell(dfile)<top) {
1147c478bd9Sstevel@tonic-gate 		if(!getword(entry))
1150d8b5334Sceastha 			return (0);
1167c478bd9Sstevel@tonic-gate 		canon(entry,word);
1177c478bd9Sstevel@tonic-gate 		switch(compare(key,word)) {
1187c478bd9Sstevel@tonic-gate 		case -2:
1190d8b5334Sceastha 			return (0);
1207c478bd9Sstevel@tonic-gate 		case -1:
1217c478bd9Sstevel@tonic-gate 		case 0:
1227c478bd9Sstevel@tonic-gate 			puts(entry);
1237c478bd9Sstevel@tonic-gate 			break;
1247c478bd9Sstevel@tonic-gate 		case 1:
1257c478bd9Sstevel@tonic-gate 		case 2:
1267c478bd9Sstevel@tonic-gate 			continue;
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 		break;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	while(getword(entry)) {
1317c478bd9Sstevel@tonic-gate 		canon(entry,word);
1327c478bd9Sstevel@tonic-gate 		switch(compare(key,word)) {
1337c478bd9Sstevel@tonic-gate 		case -1:
1347c478bd9Sstevel@tonic-gate 		case 0:
1357c478bd9Sstevel@tonic-gate 			puts(entry);
1367c478bd9Sstevel@tonic-gate 			continue;
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 		break;
1397c478bd9Sstevel@tonic-gate 	}
1400d8b5334Sceastha 	return (0);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1430d8b5334Sceastha int
compare(char * s,char * t)1440d8b5334Sceastha compare(char *s, char *t)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	for(;*s==*t;s++,t++)
1477c478bd9Sstevel@tonic-gate 		if(*s==0)
1487c478bd9Sstevel@tonic-gate 			return(0);
1497c478bd9Sstevel@tonic-gate 	return(*s==0? -1:
1507c478bd9Sstevel@tonic-gate 		*t==0? 1:
1517c478bd9Sstevel@tonic-gate 		*s<*t? -2:
1527c478bd9Sstevel@tonic-gate 		2);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1550d8b5334Sceastha int
getword(char * w)1560d8b5334Sceastha getword(char *w)
1577c478bd9Sstevel@tonic-gate {
1580d8b5334Sceastha 	int c;
1590d8b5334Sceastha 	int avail = WORDSIZE - 1;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	while(avail--) {
1627c478bd9Sstevel@tonic-gate 		c = getc(dfile);
1637c478bd9Sstevel@tonic-gate 		if(c==EOF)
1647c478bd9Sstevel@tonic-gate 			return(0);
1657c478bd9Sstevel@tonic-gate 		if(c=='\n')
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		*w++ = c;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	while (c != '\n')
1707c478bd9Sstevel@tonic-gate 		c = getc(dfile);
1717c478bd9Sstevel@tonic-gate 	*w = 0;
1727c478bd9Sstevel@tonic-gate 	return(1);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1750d8b5334Sceastha void
canon(char * old,char * new)1760d8b5334Sceastha canon(char *old, char *new)
1777c478bd9Sstevel@tonic-gate {
1780d8b5334Sceastha 	int c;
1790d8b5334Sceastha 	int avail = WORDSIZE - 1;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	for(;;) {
1827c478bd9Sstevel@tonic-gate 		*new = c = *old++;
1837c478bd9Sstevel@tonic-gate 		if(c==0) {
1847c478bd9Sstevel@tonic-gate 			*new = 0;
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		if(dict) {
1887c478bd9Sstevel@tonic-gate 			if(!isalnum(c))
1897c478bd9Sstevel@tonic-gate 				continue;
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		if(fold) {
1927c478bd9Sstevel@tonic-gate 			if(isupper(c))
1937c478bd9Sstevel@tonic-gate 				*new += 'a' - 'A';
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		new++;
1967c478bd9Sstevel@tonic-gate 		avail--;
1977c478bd9Sstevel@tonic-gate 		if (avail <= 0) {
1987c478bd9Sstevel@tonic-gate 			*new = 0;
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate }
203