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