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