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.4 (Berkeley) 03/09/87"; 13 #endif 14 15 #ifndef vms 16 #include <sys/file.h> 17 #else 18 #include <file.h> 19 #endif 20 #include "ex.h" 21 22 static long offset = -1; 23 static long block = -1; 24 static int bcnt = 0; 25 static int b_size = MAXBSIZE; 26 static char *ibuf; 27 28 topen(file, buf) 29 char *file, *buf; 30 { 31 int fd; 32 struct stat statb; 33 34 offset = -1; 35 block = -1; 36 if ((fd = open(file, O_RDONLY, 0)) < 0) 37 return(-1); 38 if (fstat(fd, &statb) < 0) { 39 (void)close(fd); 40 return(-1); 41 } 42 ibuf = buf; 43 b_size = statb.st_blksize; 44 return(fd); 45 } 46 47 tseek(fd, off) 48 int fd; 49 long off; 50 { 51 int nblock; 52 53 nblock = off / b_size * b_size; 54 offset = off % b_size; 55 if (nblock == block) 56 return(0); 57 block = nblock; 58 if (lseek(fd, nblock, L_SET) < 0) 59 return(-1); 60 if ((bcnt = read(fd, ibuf, b_size)) < 0) 61 return(-1); 62 return(0); 63 } 64 65 tgets(buf, cnt, fd) 66 register char *buf; 67 int cnt; 68 int fd; 69 { 70 register char *cp; 71 register cc; 72 73 cc = offset; 74 if (cc == -1) { 75 if ((bcnt = read(fd, ibuf, b_size)) <= 0) 76 return (NULL); 77 cc = 0; 78 block = 0; 79 } 80 if (bcnt == 0) /* EOF */ 81 return(NULL); 82 cp = ibuf + cc; 83 while (--cnt > 0) { 84 if (++cc > bcnt) { 85 block += b_size; 86 if ((bcnt = read(fd, ibuf, b_size)) <= 0) { 87 offset = cc; 88 return (NULL); 89 } 90 cp = ibuf; 91 cc = 1; 92 } 93 if ((*buf++ = *cp++) == '\n') 94 break; 95 } 96 *--buf = NULL; 97 offset = cc; 98 return(1); 99 } 100 101 tclose(fd) 102 int fd; 103 { 104 (void)close(fd); 105 offset = -1; 106 block = -1; 107 bcnt = 0; 108 } 109 #endif 110