xref: /titanic_53/usr/src/cmd/refer/hunt7.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #include <stdio.h>
19*7c478bd9Sstevel@tonic-gate #include <locale.h>
20*7c478bd9Sstevel@tonic-gate #include <assert.h>
21*7c478bd9Sstevel@tonic-gate #define SAME 0
22*7c478bd9Sstevel@tonic-gate #define FGCT 10
23*7c478bd9Sstevel@tonic-gate #define FGSIZE 150
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate int keepold = 1;	/* keep old things for fgrep search */
26*7c478bd9Sstevel@tonic-gate char fgspace[FGSIZE];
27*7c478bd9Sstevel@tonic-gate char *fgp = fgspace;
28*7c478bd9Sstevel@tonic-gate char *fgnames[FGCT];
29*7c478bd9Sstevel@tonic-gate char **fgnamp = fgnames;
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate findline(in, out, outlen, indexdate)
32*7c478bd9Sstevel@tonic-gate long indexdate;
33*7c478bd9Sstevel@tonic-gate char *in, **out;
34*7c478bd9Sstevel@tonic-gate {
35*7c478bd9Sstevel@tonic-gate 	static char name[100] = "";
36*7c478bd9Sstevel@tonic-gate 	char *p, **ftp;
37*7c478bd9Sstevel@tonic-gate 	extern long gdate();
38*7c478bd9Sstevel@tonic-gate 	static FILE *fa = NULL;
39*7c478bd9Sstevel@tonic-gate 	long lp, llen;
40*7c478bd9Sstevel@tonic-gate 	int len, k, nofil;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate # if D1
43*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "findline: %s\n", in);
44*7c478bd9Sstevel@tonic-gate # endif
45*7c478bd9Sstevel@tonic-gate 	if (mindex(in, '!'))
46*7c478bd9Sstevel@tonic-gate 		/* return(remote(in, *out)); /* Does NOTHING */
47*7c478bd9Sstevel@tonic-gate 		return(0);
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	nofil = in[0]==0;
50*7c478bd9Sstevel@tonic-gate 	for(p=in; *p && *p != ':' && *p != ';'; p++)
51*7c478bd9Sstevel@tonic-gate 		;
52*7c478bd9Sstevel@tonic-gate 	if (*p) *p++=0;
53*7c478bd9Sstevel@tonic-gate 	else p=in;
54*7c478bd9Sstevel@tonic-gate 	k = sscanf(p, "%ld,%ld", &lp, &llen);
55*7c478bd9Sstevel@tonic-gate # ifdef D1
56*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "p %s k %d lp %ld llen %ld\n",p,k,lp,llen);
57*7c478bd9Sstevel@tonic-gate # endif
58*7c478bd9Sstevel@tonic-gate 	if (k<2)
59*7c478bd9Sstevel@tonic-gate 	{
60*7c478bd9Sstevel@tonic-gate 		lp = 0;
61*7c478bd9Sstevel@tonic-gate 		llen=outlen;
62*7c478bd9Sstevel@tonic-gate 	}
63*7c478bd9Sstevel@tonic-gate # ifdef D1
64*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "lp %ld llen %ld\n",lp, llen);
65*7c478bd9Sstevel@tonic-gate # endif
66*7c478bd9Sstevel@tonic-gate # ifdef D1
67*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "fa now %o, p %o in %o %s\n",fa, p,in,in);
68*7c478bd9Sstevel@tonic-gate # endif
69*7c478bd9Sstevel@tonic-gate 	if (nofil)
70*7c478bd9Sstevel@tonic-gate 	{
71*7c478bd9Sstevel@tonic-gate # if D1
72*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "set fa to stdin\n");
73*7c478bd9Sstevel@tonic-gate # endif
74*7c478bd9Sstevel@tonic-gate 		fa = stdin;
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 	else
77*7c478bd9Sstevel@tonic-gate 		if (strcmp (name, in) != 0 || 1)
78*7c478bd9Sstevel@tonic-gate 		{
79*7c478bd9Sstevel@tonic-gate # if D1
80*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "old: %s new %s not equal\n",name,in);
81*7c478bd9Sstevel@tonic-gate # endif
82*7c478bd9Sstevel@tonic-gate 			if (fa != NULL)
83*7c478bd9Sstevel@tonic-gate 				fa = freopen(in, "r", fa);
84*7c478bd9Sstevel@tonic-gate 			else
85*7c478bd9Sstevel@tonic-gate 				fa = fopen(in, "r");
86*7c478bd9Sstevel@tonic-gate # if D1
87*7c478bd9Sstevel@tonic-gate 			if (fa==NULL)
88*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "failed to (re)open *%s*\n",in);
89*7c478bd9Sstevel@tonic-gate # endif
90*7c478bd9Sstevel@tonic-gate 			if (fa == NULL)
91*7c478bd9Sstevel@tonic-gate 				return(0);
92*7c478bd9Sstevel@tonic-gate 			/* err("Can't open %s", in); */
93*7c478bd9Sstevel@tonic-gate 			strcpy(name, in);
94*7c478bd9Sstevel@tonic-gate 			if (gdate(fa) > indexdate && indexdate != 0)
95*7c478bd9Sstevel@tonic-gate 			{
96*7c478bd9Sstevel@tonic-gate 				if (keepold)
97*7c478bd9Sstevel@tonic-gate 				{
98*7c478bd9Sstevel@tonic-gate 					for(ftp=fgnames; ftp<fgnamp; ftp++)
99*7c478bd9Sstevel@tonic-gate 						if (strcmp(*ftp, name)==SAME)
100*7c478bd9Sstevel@tonic-gate 							return(0);
101*7c478bd9Sstevel@tonic-gate 					strcpy (*fgnamp++ = fgp, name);
102*7c478bd9Sstevel@tonic-gate 					assert(fgnamp<fgnames+FGCT);
103*7c478bd9Sstevel@tonic-gate 					while (*fgp && *fgp!=':')
104*7c478bd9Sstevel@tonic-gate 						fgp++;
105*7c478bd9Sstevel@tonic-gate 					*fgp++ = 0;
106*7c478bd9Sstevel@tonic-gate 					assert (fgp<fgspace+FGSIZE);
107*7c478bd9Sstevel@tonic-gate 					return(0);
108*7c478bd9Sstevel@tonic-gate 				}
109*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("Warning: index predates file '%s'\n"), name);
110*7c478bd9Sstevel@tonic-gate 			}
111*7c478bd9Sstevel@tonic-gate 		}
112*7c478bd9Sstevel@tonic-gate # if D1
113*7c478bd9Sstevel@tonic-gate 		else
114*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "old %s new %s same fa %o\n", name,in,fa);
115*7c478bd9Sstevel@tonic-gate # endif
116*7c478bd9Sstevel@tonic-gate 	if (fa != NULL)
117*7c478bd9Sstevel@tonic-gate 	{
118*7c478bd9Sstevel@tonic-gate 		fseek(fa, lp, 0);
119*7c478bd9Sstevel@tonic-gate                 *out = (char *) malloc(llen + 1);
120*7c478bd9Sstevel@tonic-gate                 if (*out == NULL) {
121*7c478bd9Sstevel@tonic-gate                 	return(0);
122*7c478bd9Sstevel@tonic-gate                 }
123*7c478bd9Sstevel@tonic-gate 		len = fread(*out, 1, llen, fa);
124*7c478bd9Sstevel@tonic-gate 		*(*out + llen) = 0;
125*7c478bd9Sstevel@tonic-gate # ifdef D1
126*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "length as read is %d\n",len);
127*7c478bd9Sstevel@tonic-gate # endif
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 	return(llen);
130*7c478bd9Sstevel@tonic-gate }
131