xref: /titanic_52/usr/src/cmd/refer/hunt8.c (revision b6c3f7863936abeae522e48a13887dddeb691a45)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include <locale.h>
18 #include <stdio.h>
19 #include <assert.h>
20 #define	unopen(fil) {if (fil != NULL) {fclose(fil); fil = NULL; }}
21 
22 extern void err();
23 extern long indexdate, gdate();
24 extern FILE *iopen();
25 
26 int ckexist(char *, char *);
27 
28 static void
29 runbib(char *s)
30 {
31 	/* make a file suitable for fgrep */
32 	char tmp[200];
33 	sprintf(tmp, "/usr/lib/refer/mkey '%s' > '%s.ig'", s, s);
34 	system(tmp);
35 }
36 
37 int
38 makefgrep(char *indexname)
39 {
40 	FILE *fa, *fb;
41 	if (ckexist(indexname, ".ig")) {
42 		/* existing gfrep -type index */
43 #if D1
44 		fprintf(stderr, "found fgrep\n");
45 #endif
46 		fa = iopen(indexname, ".ig");
47 		fb = iopen(indexname, "");
48 		if (gdate(fb) > gdate(fa)) {
49 			if (fa != NULL)
50 				fclose(fa);
51 			runbib(indexname);
52 			fa = iopen(indexname, ".ig");
53 		}
54 		indexdate = gdate(fa);
55 		unopen(fa);
56 		unopen(fb);
57 	} else
58 		if (ckexist(indexname, "")) {
59 			/* make fgrep */
60 #if D1
61 			fprintf(stderr, "make fgrep\n");
62 #endif
63 			runbib(indexname);
64 			time(&indexdate);
65 		} else /* failure */
66 			return (0);
67 	return (1); /* success */
68 }
69 
70 int
71 ckexist(char *s, char *t)
72 {
73 	char fnam[100];
74 	strcpy(fnam, s);
75 	strcat(fnam, t);
76 	return (access(fnam, 04) != -1);
77 }
78 
79 FILE *
80 iopen(char *s, char *t)
81 {
82 	char fnam[100];
83 	FILE *f;
84 	strcpy(fnam, s);
85 	strcat(fnam, t);
86 	f = fopen(fnam, "r");
87 	if (f == NULL) {
88 		err(gettext("Missing expected file %s"), fnam);
89 		exit(1);
90 	}
91 	return (f);
92 }
93