xref: /titanic_54/usr/src/cmd/refer/hunt8.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 <locale.h>
19*7c478bd9Sstevel@tonic-gate #include <stdio.h>
20*7c478bd9Sstevel@tonic-gate #include <assert.h>
21*7c478bd9Sstevel@tonic-gate #define unopen(fil) {if (fil!=NULL) {fclose(fil); fil=NULL;}}
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate extern long indexdate, gdate();
24*7c478bd9Sstevel@tonic-gate extern FILE *iopen();
25*7c478bd9Sstevel@tonic-gate runbib (s)
26*7c478bd9Sstevel@tonic-gate char *s;
27*7c478bd9Sstevel@tonic-gate {
28*7c478bd9Sstevel@tonic-gate 	/* make a file suitable for fgrep */
29*7c478bd9Sstevel@tonic-gate 	char tmp[200];
30*7c478bd9Sstevel@tonic-gate 	sprintf(tmp, "/usr/lib/refer/mkey '%s' > '%s.ig'", s,s);
31*7c478bd9Sstevel@tonic-gate 	system(tmp);
32*7c478bd9Sstevel@tonic-gate }
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate makefgrep(indexname)
35*7c478bd9Sstevel@tonic-gate char *indexname;
36*7c478bd9Sstevel@tonic-gate {
37*7c478bd9Sstevel@tonic-gate 	FILE *fa, *fb;
38*7c478bd9Sstevel@tonic-gate 	if (ckexist(indexname, ".ig"))
39*7c478bd9Sstevel@tonic-gate 	{
40*7c478bd9Sstevel@tonic-gate 		/* existing gfrep -type index */
41*7c478bd9Sstevel@tonic-gate # if D1
42*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "found fgrep\n");
43*7c478bd9Sstevel@tonic-gate # endif
44*7c478bd9Sstevel@tonic-gate 		fa = iopen(indexname, ".ig");
45*7c478bd9Sstevel@tonic-gate 		fb = iopen(indexname, "");
46*7c478bd9Sstevel@tonic-gate 		if (gdate(fb)>gdate(fa))
47*7c478bd9Sstevel@tonic-gate 		{
48*7c478bd9Sstevel@tonic-gate 			if (fa!=NULL)
49*7c478bd9Sstevel@tonic-gate 				fclose(fa);
50*7c478bd9Sstevel@tonic-gate 			runbib(indexname);
51*7c478bd9Sstevel@tonic-gate 			fa= iopen(indexname, ".ig");
52*7c478bd9Sstevel@tonic-gate 		}
53*7c478bd9Sstevel@tonic-gate 		indexdate = gdate(fa);
54*7c478bd9Sstevel@tonic-gate 		unopen(fa);
55*7c478bd9Sstevel@tonic-gate 		unopen(fb);
56*7c478bd9Sstevel@tonic-gate 	}
57*7c478bd9Sstevel@tonic-gate 	else
58*7c478bd9Sstevel@tonic-gate 		if (ckexist(indexname, ""))
59*7c478bd9Sstevel@tonic-gate 		{
60*7c478bd9Sstevel@tonic-gate 			/* make fgrep */
61*7c478bd9Sstevel@tonic-gate # if D1
62*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "make fgrep\n");
63*7c478bd9Sstevel@tonic-gate # endif
64*7c478bd9Sstevel@tonic-gate 			runbib(indexname);
65*7c478bd9Sstevel@tonic-gate 			time(&indexdate);
66*7c478bd9Sstevel@tonic-gate 		}
67*7c478bd9Sstevel@tonic-gate 		else /* failure */
68*7c478bd9Sstevel@tonic-gate 		return(0);
69*7c478bd9Sstevel@tonic-gate 	return(1); /* success */
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate ckexist(s, t)
73*7c478bd9Sstevel@tonic-gate char *s, *t;
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	char fnam[100];
76*7c478bd9Sstevel@tonic-gate 	strcpy (fnam, s);
77*7c478bd9Sstevel@tonic-gate 	strcat (fnam, t);
78*7c478bd9Sstevel@tonic-gate 	return (access(fnam, 04) != -1);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate FILE *
82*7c478bd9Sstevel@tonic-gate iopen(s, t)
83*7c478bd9Sstevel@tonic-gate char *s, *t;
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	char fnam[100];
86*7c478bd9Sstevel@tonic-gate 	FILE *f;
87*7c478bd9Sstevel@tonic-gate 	strcpy (fnam, s);
88*7c478bd9Sstevel@tonic-gate 	strcat (fnam, t);
89*7c478bd9Sstevel@tonic-gate 	f = fopen (fnam, "r");
90*7c478bd9Sstevel@tonic-gate 	if (f == NULL)
91*7c478bd9Sstevel@tonic-gate 	{
92*7c478bd9Sstevel@tonic-gate 		err(gettext("Missing expected file %s"), fnam);
93*7c478bd9Sstevel@tonic-gate 		exit(1);
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 	return(f);
96*7c478bd9Sstevel@tonic-gate }
97