1 /*
   2  * Copyright 2017 Gary Mills
   3  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
   4  * Use is subject to license terms.
   5  */
   6 
   7 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T     */
   8 /*        All Rights Reserved   */
   9 
  10 /*
  11  * Copyright (c) 1980 Regents of the University of California.
  12  * All rights reserved. The Berkeley software License Agreement
  13  * specifies the terms and conditions for redistribution.
  14  */
  15 
  16 #include <stdio.h>
  17 #include <locale.h>
  18 #include <assert.h>
  19 #define SAME 0
  20 #define FGCT 10
  21 #define FGSIZE 150
  22 
  23 int keepold = 1;        /* keep old things for fgrep search */
  24 char fgspace[FGSIZE];
  25 char *fgp = fgspace;
  26 char *fgnames[FGCT];
  27 char **fgnamp = fgnames;
  28 
  29 extern char *mindex();
  30 
  31 long
  32 findline(char *in, char **out, int outlen, long indexdate)
  33 {
  34         static char name[100] = "";
  35         char *p, **ftp;
  36         extern long gdate();
  37         static FILE *fa = NULL;
  38         long lp, llen;
  39         int k, nofil;
  40 
  41 #if D1
  42         fprintf(stderr, "findline: %s\n", in);
  43 #endif
  44         if (mindex(in, '!'))
  45                 return (0);
  46 
  47         nofil = in[0] == 0;
  48         for (p = in; *p && *p != ':' && *p != ';'; p++)
  49                 ;
  50         if (*p) *p++ = 0;
  51         else p = in;
  52         k = sscanf(p, "%ld,%ld", &lp, &llen);
  53 #ifdef D1
  54         fprintf(stderr, "p %s k %d lp %ld llen %ld\n", p, k, lp, llen);
  55 #endif
  56         if (k < 2) {
  57                 lp = 0;
  58                 llen = outlen;
  59         }
  60 #ifdef D1
  61         fprintf(stderr, "lp %ld llen %ld\n", lp, llen);
  62 #endif
  63 #ifdef D1
  64         fprintf(stderr, "fa now %o, p %o in %o %s\n", fa, p, in, in);
  65 #endif
  66         if (nofil) {
  67 #if D1
  68                 fprintf(stderr, "set fa to stdin\n");
  69 #endif
  70                 fa = stdin;
  71         } else
  72                 if (strcmp(name, in) != 0 || 1) {
  73 #if D1
  74                         fprintf(stderr, "old: %s new %s not equal\n", name, in);
  75 #endif
  76                         if (fa != NULL)
  77                                 fa = freopen(in, "r", fa);
  78                         else
  79                                 fa = fopen(in, "r");
  80 #if D1
  81                         if (fa == NULL)
  82                                 fprintf(stderr, "failed to (re)open *%s*\n",
  83                                     in);
  84 #endif
  85                         if (fa == NULL)
  86                                 return (0);
  87                         /* err("Can't open %s", in); */
  88                         strcpy(name, in);
  89                         if (gdate(fa) > indexdate && indexdate != 0) {
  90                                 if (keepold) {
  91                                         for (ftp = fgnames; ftp < fgnamp; ftp++)
  92                                                 if (strcmp(*ftp, name) == SAME)
  93                                                         return (0);
  94                                         strcpy(*fgnamp++ = fgp, name);
  95                                         assert(fgnamp < fgnames+FGCT);
  96                                         while (*fgp && *fgp != ':')
  97                                                 fgp++;
  98                                         *fgp++ = 0;
  99                                         assert(fgp < fgspace+FGSIZE);
 100                                         return (0);
 101                                 }
 102                                 fprintf(stderr, gettext(
 103                                     "Warning: index predates file '%s'\n"),
 104                                     name);
 105                         }
 106                 }
 107 #if D1
 108                 else
 109                         fprintf(stderr, "old %s new %s same fa %o\n",
 110                             name, in, fa);
 111 #endif
 112         if (fa != NULL) {
 113                 fseek(fa, lp, 0);
 114                 *out = (char *)malloc(llen + 1);
 115                 if (*out == NULL) {
 116                         return (0);
 117                 }
 118                 (void) fread(*out, 1, llen, fa);
 119                 *(*out + llen) = 0;
 120 #ifdef D1
 121                 fprintf(stderr, "length as read is %d\n", len);
 122 #endif
 123         }
 124         return (llen);
 125 }