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 <stdio.h> 18 #include <locale.h> 19 #include <assert.h> 20 #define SAME 0 21 #define FGCT 10 22 #define FGSIZE 150 23 24 int keepold = 1; /* keep old things for fgrep search */ 25 char fgspace[FGSIZE]; 26 char *fgp = fgspace; 27 char *fgnames[FGCT]; 28 char **fgnamp = fgnames; 29 30 extern char *mindex(); 31 32 long 33 findline(char *in, char **out, int outlen, long indexdate) 34 { 35 static char name[100] = ""; 36 char *p, **ftp; 37 extern long gdate(); 38 static FILE *fa = NULL; 39 long lp, llen; 40 int len, k, nofil; 41 42 #if D1 43 fprintf(stderr, "findline: %s\n", in); 44 #endif 45 if (mindex(in, '!')) 46 return (0); 47 48 nofil = in[0] == 0; 49 for (p = in; *p && *p != ':' && *p != ';'; p++) 50 ; 51 if (*p) *p++ = 0; 52 else p = in; 53 k = sscanf(p, "%ld,%ld", &lp, &llen); 54 #ifdef D1 55 fprintf(stderr, "p %s k %d lp %ld llen %ld\n", p, k, lp, llen); 56 #endif 57 if (k < 2) { 58 lp = 0; 59 llen = outlen; 60 } 61 #ifdef D1 62 fprintf(stderr, "lp %ld llen %ld\n", lp, llen); 63 #endif 64 #ifdef D1 65 fprintf(stderr, "fa now %o, p %o in %o %s\n", fa, p, in, in); 66 #endif 67 if (nofil) { 68 #if D1 69 fprintf(stderr, "set fa to stdin\n"); 70 #endif 71 fa = stdin; 72 } else 73 if (strcmp(name, in) != 0 || 1) { 74 #if D1 75 fprintf(stderr, "old: %s new %s not equal\n", name, in); 76 #endif 77 if (fa != NULL) 78 fa = freopen(in, "r", fa); 79 else 80 fa = fopen(in, "r"); 81 #if D1 82 if (fa == NULL) 83 fprintf(stderr, "failed to (re)open *%s*\n", 84 in); 85 #endif 86 if (fa == NULL) 87 return (0); 88 /* err("Can't open %s", in); */ 89 strcpy(name, in); 90 if (gdate(fa) > indexdate && indexdate != 0) { 91 if (keepold) { 92 for (ftp = fgnames; ftp < fgnamp; ftp++) 93 if (strcmp(*ftp, name) == SAME) 94 return (0); 95 strcpy(*fgnamp++ = fgp, name); 96 assert(fgnamp < fgnames+FGCT); 97 while (*fgp && *fgp != ':') 98 fgp++; 99 *fgp++ = 0; 100 assert(fgp < fgspace+FGSIZE); 101 return (0); 102 } 103 fprintf(stderr, gettext( 104 "Warning: index predates file '%s'\n"), 105 name); 106 } 107 } 108 #if D1 109 else 110 fprintf(stderr, "old %s new %s same fa %o\n", 111 name, in, fa); 112 #endif 113 if (fa != NULL) { 114 fseek(fa, lp, 0); 115 *out = (char *)malloc(llen + 1); 116 if (*out == NULL) { 117 return (0); 118 } 119 len = fread(*out, 1, llen, fa); 120 *(*out + llen) = 0; 121 #ifdef D1 122 fprintf(stderr, "length as read is %d\n", len); 123 #endif 124 } 125 return (llen); 126 } 127