1: /* Copyright (c) 1985 Regents of the University of California */
   2: 
   3: /*
   4:  * These routines are for faster tag lookup.  They support the binary
   5:  * search used in tagfind() instead of the linear search.  The speedup
   6:  * is quite noticable looking for a tag at the end of a long tags
   7:  * file.  Define FASTTAG in the Makefile to use these routines.
   8:  */
   9: 
  10: #ifdef FASTTAG
  11: #ifndef lint
  12: static char *sccsid = "@(#)ex_tagio.c	7.3 (Berkeley) 1/31/86";
  13: #endif
  14: 
  15: #include <sys/file.h>
  16: #include "ex.h"
  17: 
  18: static long offset = -1;
  19: static long block = -1;
  20: static int bcnt = 0;
  21: static int b_size = MAXBSIZE;
  22: static char *ibuf;
  23: 
  24: topen(file, buf)
  25: char *file, *buf;
  26: {
  27:     int fd;
  28:     struct stat statb;
  29: 
  30:     offset = -1;
  31:     block = -1;
  32:     if ((fd = open(file, O_RDONLY, 0)) < 0)
  33:         return(-1);
  34:     if (fstat(fd, &statb) < 0) {
  35:         (void)close(fd);
  36:         return(-1);
  37:     }
  38:     ibuf = buf;
  39:     b_size = statb.st_blksize;
  40:     return(fd);
  41: }
  42: 
  43: tseek(fd, off)
  44: int fd;
  45: long off;
  46: {
  47:     int nblock;
  48: 
  49:     nblock = off / b_size * b_size;
  50:     offset = off % b_size;
  51:     if (nblock == block)
  52:         return(0);
  53:     block = nblock;
  54:     if (lseek(fd, nblock, L_SET) < 0)
  55:         return(-1);
  56:     if ((bcnt = read(fd, ibuf, b_size)) < 0)
  57:         return(-1);
  58:     return(0);
  59: }
  60: 
  61: tgets(buf, cnt, fd)
  62: register char *buf;
  63: int cnt;
  64: int fd;
  65: {
  66:     register char *cp;
  67:     register cc;
  68: 
  69:     cc = offset;
  70:     if (cc == -1) {
  71:         if ((bcnt = read(fd, ibuf, b_size)) <= 0)
  72:             return (NULL);
  73:         cc = 0;
  74:         block = 0;
  75:     }
  76:     if (bcnt == 0)      /* EOF */
  77:         return(NULL);
  78:     cp = ibuf + cc;
  79:     while (--cnt > 0) {
  80:         if (++cc > bcnt) {
  81:             block += b_size;
  82:             if ((bcnt = read(fd, ibuf, b_size)) <= 0) {
  83:                 offset = cc;
  84:                 return (NULL);
  85:             }
  86:             cp = ibuf;
  87:             cc = 1;
  88:         }
  89:         if ((*buf++ = *cp++) == '\n')
  90:             break;
  91:     }
  92:     *--buf = NULL;
  93:     offset = cc;
  94:     return(1);
  95: }
  96: 
  97: tclose(fd)
  98: int fd;
  99: {
 100:     (void)close(fd);
 101:     offset = -1;
 102:     block = -1;
 103:     bcnt = 0;
 104: }
 105: #endif

Defined functions

tclose defined in line 97; used 2 times
tgets defined in line 61; used 2 times
topen defined in line 24; used 1 times
tseek defined in line 43; used 1 times

Defined variables

b_size defined in line 21; used 8 times
bcnt defined in line 20; used 6 times
block defined in line 19; used 6 times
ibuf defined in line 22; used 6 times
offset defined in line 18; used 6 times
sccsid defined in line 12; never used
Last modified: 1986-02-01
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1084
Valid CSS Valid XHTML 1.0 Strict