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