KNF, ANSI C
[unix-history] / usr / src / usr.bin / ex / ex_tagio.c
CommitLineData
2791ff57 1/*-
eb035710
KB
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
2791ff57
KB
4 *
5 * %sccs.include.proprietary.c%
6 */
7
8#ifndef lint
eb035710 9static char sccsid[] = "@(#)ex_tagio.c 8.1 (Berkeley) %G%";
2791ff57 10#endif /* not lint */
b2e575b2
JB
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 */
b2e575b2 18#ifdef FASTTAG
b2e575b2 19
5a6c967e 20#ifndef vms
b2e575b2 21#include <sys/file.h>
5a6c967e
CH
22#else
23#include <file.h>
24#endif
b2e575b2
JB
25#include "ex.h"
26
27static long offset = -1;
28static long block = -1;
29static int bcnt = 0;
30static int b_size = MAXBSIZE;
31static char *ibuf;
32
33topen(file, buf)
34char *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
52tseek(fd, off)
53int fd;
54long 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
70tgets(buf, cnt, fd)
71register char *buf;
72int cnt;
73int 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) {
fa104d3b 90 block += b_size;
b2e575b2
JB
91 if ((bcnt = read(fd, ibuf, b_size)) <= 0) {
92 offset = cc;
93 return (NULL);
94 }
b2e575b2
JB
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
106tclose(fd)
107int fd;
108{
109 (void)close(fd);
110 offset = -1;
111 block = -1;
112 bcnt = 0;
113}
114#endif