1 /* $MirOS: src/usr.bin/oldroff/hunt/hunt7.c,v 1.2 2008/11/08 23:04:31 tg Exp $ */
2 
3 /*-
4  * Copyright (c) 1979, 1980, 1981, 1986, 1988, 1990, 1991, 1992
5  *     The Regents of the University of California.
6  * Copyright (C) Caldera International Inc.  2001-2002.
7  * Copyright (c) 2003, 2004
8  *	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms,
12  * with or without modification, are permitted provided
13  * that the following conditions are met:
14  *
15  * Redistributions of source code and documentation must retain
16  * the above copyright notice, this list of conditions and the
17  * following disclaimer.  Redistributions in binary form must
18  * reproduce the above copyright notice, this list of conditions
19  * and the following disclaimer in the documentation and/or other
20  * materials provided with the distribution.
21  *
22  * All advertising materials mentioning features or use of this
23  * software must display the following acknowledgement:
24  *   This product includes software developed or owned by
25  *   Caldera International, Inc.
26  *
27  * Neither the name of Caldera International, Inc. nor the names
28  * of other contributors may be used to endorse or promote products
29  * derived from this software without specific prior written permission.
30  *
31  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
32  * INTERNATIONAL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
33  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
36  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
41  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  */
44 
45 #ifndef lint
46 static char sccsid[] = "@(#)hunt7.c	4.2 (Berkeley) 4/18/91";
47 #endif /* not lint */
48 
49 #include <stdio.h>
50 #include <assert.h>
51 #define SAME 0
52 #define FGCT 10
53 #define FGSIZE 150
54 
55 int keepold = 1;	/* keep old things for fgrep search */
56 char fgspace[FGSIZE];
57 char *fgp = fgspace;
58 char *fgnames[FGCT];
59 char **fgnamp = fgnames;
60 
findline(in,out,outlen,indexdate)61 findline(in, out, outlen, indexdate)
62 long indexdate;
63 char *in, *out;
64 {
65 	static char name[100] = "";
66 	char *p, **ftp;
67 	extern long gdate();
68 	static FILE *fa = NULL;
69 	long lp, llen;
70 	int len, k, nofil;
71 
72 # if D1
73 	fprintf(stderr, "findline: %s\n", in);
74 # endif
75 	if (mindex(in, '!'))
76 		return(remote(in, out));
77 	nofil = in[0]==0;
78 	for(p=in; *p && *p != ':' && *p != ';'; p++)
79 		;
80 	if (*p) *p++=0;
81 	else p=in;
82 	k = sscanf(p, "%ld,%ld", &lp, &llen);
83 # ifdef D1
84 	fprintf(stderr, "p %s k %d lp %ld llen %ld\n",p,k,lp,llen);
85 # endif
86 	if (k<2)
87 	{
88 		lp = 0;
89 		llen=outlen;
90 	}
91 # ifdef D1
92 	fprintf(stderr, "lp %ld llen %ld\n",lp, llen);
93 # endif
94 # ifdef D1
95 	fprintf(stderr, "fa now %o, p %o in %o %s\n",fa, p,in,in);
96 # endif
97 	if (nofil)
98 	{
99 # if D1
100 		fprintf(stderr, "set fa to stdin\n");
101 # endif
102 		fa = stdin;
103 	}
104 	else
105 		if (strcmp (name, in) != 0 || 1)
106 		{
107 # if D1
108 			fprintf(stderr, "old: %s new %s not equal\n",name,in);
109 # endif
110 			if (fa != NULL)
111 				fa = freopen(in, "r", fa);
112 			else
113 				fa = fopen(in, "r");
114 # if D1
115 			if (fa==NULL)
116 				fprintf(stderr, "failed to (re)open *%s*\n",in);
117 # endif
118 			if (fa == NULL)
119 				return(0);
120 			/* err("Can't open %s", in); */
121 			strcpy(name, in);
122 			if (gdate(fa) > indexdate && indexdate != 0)
123 			{
124 				if (keepold)
125 				{
126 					for(ftp=fgnames; ftp<fgnamp; ftp++)
127 						if (strcmp(*ftp, name)==SAME)
128 							return(0);
129 					strcpy (*fgnamp++ = fgp, name);
130 					assert(fgnamp<fgnames+FGCT);
131 					while (*fgp && *fgp!=':')
132 						fgp++;
133 					*fgp++ = 0;
134 					assert (fgp<fgspace+FGSIZE);
135 					return(0);
136 				}
137 				fprintf(stderr, "Warning: index predates file '%s'\n", name);
138 			}
139 		}
140 # if D1
141 		else
142 			fprintf(stderr, "old %s new %s same fa %o\n", name,in,fa);
143 # endif
144 	if (fa != NULL)
145 	{
146 		fseek (fa, lp, 0);
147 		len = (llen >= outlen) ? outlen-1 : llen;
148 		len = fread (out, 1, len, fa);
149 		out[len] = 0;
150 # ifdef D1
151 		fprintf(stderr, "length as read is %d\n",len);
152 # endif
153 	}
154 	return(len);
155 }
156