1*11a8fa6cSceastha /*
2*11a8fa6cSceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3*11a8fa6cSceastha * Use is subject to license terms.
4*11a8fa6cSceastha */
5*11a8fa6cSceastha
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate #include <locale.h>
187c478bd9Sstevel@tonic-gate #include <stdio.h>
197c478bd9Sstevel@tonic-gate #include <assert.h>
207c478bd9Sstevel@tonic-gate #define unopen(fil) {if (fil != NULL) {fclose(fil); fil = NULL; }}
217c478bd9Sstevel@tonic-gate
22*11a8fa6cSceastha extern void err();
237c478bd9Sstevel@tonic-gate extern long indexdate, gdate();
247c478bd9Sstevel@tonic-gate extern FILE *iopen();
25*11a8fa6cSceastha
26*11a8fa6cSceastha int ckexist(char *, char *);
27*11a8fa6cSceastha
28*11a8fa6cSceastha static void
runbib(char * s)29*11a8fa6cSceastha runbib(char *s)
307c478bd9Sstevel@tonic-gate {
317c478bd9Sstevel@tonic-gate /* make a file suitable for fgrep */
327c478bd9Sstevel@tonic-gate char tmp[200];
337c478bd9Sstevel@tonic-gate sprintf(tmp, "/usr/lib/refer/mkey '%s' > '%s.ig'", s, s);
347c478bd9Sstevel@tonic-gate system(tmp);
357c478bd9Sstevel@tonic-gate }
367c478bd9Sstevel@tonic-gate
37*11a8fa6cSceastha int
makefgrep(char * indexname)38*11a8fa6cSceastha makefgrep(char *indexname)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate FILE *fa, *fb;
41*11a8fa6cSceastha if (ckexist(indexname, ".ig")) {
427c478bd9Sstevel@tonic-gate /* existing gfrep -type index */
437c478bd9Sstevel@tonic-gate #if D1
447c478bd9Sstevel@tonic-gate fprintf(stderr, "found fgrep\n");
457c478bd9Sstevel@tonic-gate #endif
467c478bd9Sstevel@tonic-gate fa = iopen(indexname, ".ig");
477c478bd9Sstevel@tonic-gate fb = iopen(indexname, "");
48*11a8fa6cSceastha if (gdate(fb) > gdate(fa)) {
497c478bd9Sstevel@tonic-gate if (fa != NULL)
507c478bd9Sstevel@tonic-gate fclose(fa);
517c478bd9Sstevel@tonic-gate runbib(indexname);
527c478bd9Sstevel@tonic-gate fa = iopen(indexname, ".ig");
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate indexdate = gdate(fa);
557c478bd9Sstevel@tonic-gate unopen(fa);
567c478bd9Sstevel@tonic-gate unopen(fb);
57*11a8fa6cSceastha } else
58*11a8fa6cSceastha if (ckexist(indexname, "")) {
597c478bd9Sstevel@tonic-gate /* make fgrep */
607c478bd9Sstevel@tonic-gate #if D1
617c478bd9Sstevel@tonic-gate fprintf(stderr, "make fgrep\n");
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate runbib(indexname);
647c478bd9Sstevel@tonic-gate time(&indexdate);
65*11a8fa6cSceastha } else /* failure */
667c478bd9Sstevel@tonic-gate return (0);
677c478bd9Sstevel@tonic-gate return (1); /* success */
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
70*11a8fa6cSceastha int
ckexist(char * s,char * t)71*11a8fa6cSceastha ckexist(char *s, char *t)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate char fnam[100];
747c478bd9Sstevel@tonic-gate strcpy(fnam, s);
757c478bd9Sstevel@tonic-gate strcat(fnam, t);
767c478bd9Sstevel@tonic-gate return (access(fnam, 04) != -1);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate FILE *
iopen(char * s,char * t)80*11a8fa6cSceastha iopen(char *s, char *t)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate char fnam[100];
837c478bd9Sstevel@tonic-gate FILE *f;
847c478bd9Sstevel@tonic-gate strcpy(fnam, s);
857c478bd9Sstevel@tonic-gate strcat(fnam, t);
867c478bd9Sstevel@tonic-gate f = fopen(fnam, "r");
87*11a8fa6cSceastha if (f == NULL) {
887c478bd9Sstevel@tonic-gate err(gettext("Missing expected file %s"), fnam);
897c478bd9Sstevel@tonic-gate exit(1);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate return (f);
927c478bd9Sstevel@tonic-gate }
93