rationalized handling of child processes, cleaned up mail1 some more
[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
5a6c967e 12static char *sccsid = "@(#)ex_tagio.c 7.4 (Berkeley) %G%";
b2e575b2
JB
13#endif
14
5a6c967e 15#ifndef vms
b2e575b2 16#include <sys/file.h>
5a6c967e
CH
17#else
18#include <file.h>
19#endif
b2e575b2
JB
20#include "ex.h"
21
22static long offset = -1;
23static long block = -1;
24static int bcnt = 0;
25static int b_size = MAXBSIZE;
26static char *ibuf;
27
28topen(file, buf)
29char *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
47tseek(fd, off)
48int fd;
49long 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
65tgets(buf, cnt, fd)
66register char *buf;
67int cnt;
68int 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) {
fa104d3b 85 block += b_size;
b2e575b2
JB
86 if ((bcnt = read(fd, ibuf, b_size)) <= 0) {
87 offset = cc;
88 return (NULL);
89 }
b2e575b2
JB
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
101tclose(fd)
102int fd;
103{
104 (void)close(fd);
105 offset = -1;
106 block = -1;
107 bcnt = 0;
108}
109#endif