One more day of hacking.
[unix-history] / usr / src / usr.bin / ex / ex_tagio.c
CommitLineData
b2e575b2
JB
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
fa104d3b 12static char *sccsid = "@(#)ex_tagio.c 7.3 (Berkeley) %G%";
b2e575b2
JB
13#endif
14
15#include <sys/file.h>
16#include "ex.h"
17
18static long offset = -1;
19static long block = -1;
20static int bcnt = 0;
21static int b_size = MAXBSIZE;
22static char *ibuf;
23
24topen(file, buf)
25char *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
43tseek(fd, off)
44int fd;
45long 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
61tgets(buf, cnt, fd)
62register char *buf;
63int cnt;
64int 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) {
fa104d3b 81 block += b_size;
b2e575b2
JB
82 if ((bcnt = read(fd, ibuf, b_size)) <= 0) {
83 offset = cc;
84 return (NULL);
85 }
b2e575b2
JB
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
97tclose(fd)
98int fd;
99{
100 (void)close(fd);
101 offset = -1;
102 block = -1;
103 bcnt = 0;
104}
105#endif