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 <stdio.h>
187c478bd9Sstevel@tonic-gate #include <locale.h>
197c478bd9Sstevel@tonic-gate #include <assert.h>
207c478bd9Sstevel@tonic-gate #define SAME 0
217c478bd9Sstevel@tonic-gate #define FGCT 10
227c478bd9Sstevel@tonic-gate #define FGSIZE 150
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate int keepold = 1; /* keep old things for fgrep search */
257c478bd9Sstevel@tonic-gate char fgspace[FGSIZE];
267c478bd9Sstevel@tonic-gate char *fgp = fgspace;
277c478bd9Sstevel@tonic-gate char *fgnames[FGCT];
287c478bd9Sstevel@tonic-gate char **fgnamp = fgnames;
297c478bd9Sstevel@tonic-gate
30*11a8fa6cSceastha extern char *mindex();
31*11a8fa6cSceastha
32*11a8fa6cSceastha long
findline(char * in,char ** out,int outlen,long indexdate)33*11a8fa6cSceastha findline(char *in, char **out, int outlen, long indexdate)
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate static char name[100] = "";
367c478bd9Sstevel@tonic-gate char *p, **ftp;
377c478bd9Sstevel@tonic-gate extern long gdate();
387c478bd9Sstevel@tonic-gate static FILE *fa = NULL;
397c478bd9Sstevel@tonic-gate long lp, llen;
407c478bd9Sstevel@tonic-gate int len, k, nofil;
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #if D1
437c478bd9Sstevel@tonic-gate fprintf(stderr, "findline: %s\n", in);
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate if (mindex(in, '!'))
467c478bd9Sstevel@tonic-gate return (0);
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate nofil = in[0] == 0;
497c478bd9Sstevel@tonic-gate for (p = in; *p && *p != ':' && *p != ';'; p++)
507c478bd9Sstevel@tonic-gate ;
517c478bd9Sstevel@tonic-gate if (*p) *p++ = 0;
527c478bd9Sstevel@tonic-gate else p = in;
537c478bd9Sstevel@tonic-gate k = sscanf(p, "%ld,%ld", &lp, &llen);
547c478bd9Sstevel@tonic-gate #ifdef D1
557c478bd9Sstevel@tonic-gate fprintf(stderr, "p %s k %d lp %ld llen %ld\n", p, k, lp, llen);
567c478bd9Sstevel@tonic-gate #endif
57*11a8fa6cSceastha if (k < 2) {
587c478bd9Sstevel@tonic-gate lp = 0;
597c478bd9Sstevel@tonic-gate llen = outlen;
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate #ifdef D1
627c478bd9Sstevel@tonic-gate fprintf(stderr, "lp %ld llen %ld\n", lp, llen);
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate #ifdef D1
657c478bd9Sstevel@tonic-gate fprintf(stderr, "fa now %o, p %o in %o %s\n", fa, p, in, in);
667c478bd9Sstevel@tonic-gate #endif
67*11a8fa6cSceastha if (nofil) {
687c478bd9Sstevel@tonic-gate #if D1
697c478bd9Sstevel@tonic-gate fprintf(stderr, "set fa to stdin\n");
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate fa = stdin;
72*11a8fa6cSceastha } else
73*11a8fa6cSceastha if (strcmp(name, in) != 0 || 1) {
747c478bd9Sstevel@tonic-gate #if D1
757c478bd9Sstevel@tonic-gate fprintf(stderr, "old: %s new %s not equal\n", name, in);
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate if (fa != NULL)
787c478bd9Sstevel@tonic-gate fa = freopen(in, "r", fa);
797c478bd9Sstevel@tonic-gate else
807c478bd9Sstevel@tonic-gate fa = fopen(in, "r");
817c478bd9Sstevel@tonic-gate #if D1
827c478bd9Sstevel@tonic-gate if (fa == NULL)
83*11a8fa6cSceastha fprintf(stderr, "failed to (re)open *%s*\n",
84*11a8fa6cSceastha in);
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate if (fa == NULL)
877c478bd9Sstevel@tonic-gate return (0);
887c478bd9Sstevel@tonic-gate /* err("Can't open %s", in); */
897c478bd9Sstevel@tonic-gate strcpy(name, in);
90*11a8fa6cSceastha if (gdate(fa) > indexdate && indexdate != 0) {
91*11a8fa6cSceastha if (keepold) {
927c478bd9Sstevel@tonic-gate for (ftp = fgnames; ftp < fgnamp; ftp++)
937c478bd9Sstevel@tonic-gate if (strcmp(*ftp, name) == SAME)
947c478bd9Sstevel@tonic-gate return (0);
957c478bd9Sstevel@tonic-gate strcpy(*fgnamp++ = fgp, name);
967c478bd9Sstevel@tonic-gate assert(fgnamp < fgnames+FGCT);
977c478bd9Sstevel@tonic-gate while (*fgp && *fgp != ':')
987c478bd9Sstevel@tonic-gate fgp++;
997c478bd9Sstevel@tonic-gate *fgp++ = 0;
1007c478bd9Sstevel@tonic-gate assert(fgp < fgspace+FGSIZE);
1017c478bd9Sstevel@tonic-gate return (0);
1027c478bd9Sstevel@tonic-gate }
103*11a8fa6cSceastha fprintf(stderr, gettext(
104*11a8fa6cSceastha "Warning: index predates file '%s'\n"),
105*11a8fa6cSceastha name);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate #if D1
1097c478bd9Sstevel@tonic-gate else
110*11a8fa6cSceastha fprintf(stderr, "old %s new %s same fa %o\n",
111*11a8fa6cSceastha name, in, fa);
1127c478bd9Sstevel@tonic-gate #endif
113*11a8fa6cSceastha if (fa != NULL) {
1147c478bd9Sstevel@tonic-gate fseek(fa, lp, 0);
1157c478bd9Sstevel@tonic-gate *out = (char *)malloc(llen + 1);
1167c478bd9Sstevel@tonic-gate if (*out == NULL) {
1177c478bd9Sstevel@tonic-gate return (0);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate len = fread(*out, 1, llen, fa);
1207c478bd9Sstevel@tonic-gate *(*out + llen) = 0;
1217c478bd9Sstevel@tonic-gate #ifdef D1
1227c478bd9Sstevel@tonic-gate fprintf(stderr, "length as read is %d\n", len);
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate return (llen);
1267c478bd9Sstevel@tonic-gate }
127