Commit | Line | Data |
---|---|---|
045d3852 HK |
1 | #include <pagsiz.h> |
2 | #include "bio.h" | |
3 | ||
4 | /* | |
5 | * NAMES: bread(), brseek(), blseek() | |
6 | * | |
7 | * DESCRIPTION: | |
8 | * This is a buffered read package. | |
9 | * | |
10 | * Bread may be called with a negative nbytes which causes it to | |
11 | * read backwards. In this case, buffer should point to the first | |
12 | * byte following the buffer. If only a partial read is possible | |
13 | * (due to beginning of file), only the last bytes of the buffer | |
14 | * will be filled. | |
15 | */ | |
16 | ||
17 | ||
18 | bread(brs, buff, nbytes) | |
19 | struct brbuf *brs; char *buff; { | |
20 | register int k, nb; | |
21 | ||
22 | if (nbytes > 0) { | |
23 | for (nb=nbytes; nb>0; nb--) { | |
24 | if (brs->nr == 0) { | |
25 | brs->nr = read(brs->fd, brs->next=brs->b, BSIZE); | |
26 | brs->nl = 0; | |
27 | if (brs->nr < 0) return(-1); | |
28 | if (brs->nr == 0) return(nbytes-nb); | |
29 | } | |
30 | *buff++ = *brs->next++; | |
31 | brs->nr--; | |
32 | brs->nl++; | |
33 | } | |
34 | } | |
35 | else { | |
36 | nbytes = -nbytes; | |
37 | for (nb=nbytes; nb>0; nb--) { | |
38 | if (brs->nl == 0) { | |
39 | if ((k=tell(brs->fd)) >= BSIZE + brs->nr) { | |
40 | lseek(brs->fd, (long) -(BSIZE + brs->nr), 1); | |
41 | brs->nl = read(brs->fd, brs->b, BSIZE); | |
42 | } else { | |
43 | lseek(brs->fd, 0L, 0); | |
44 | k = k - brs->nr; | |
45 | if (k < 0) k = 0; | |
46 | brs->nl = read(brs->fd, brs->b, k); | |
47 | } | |
48 | if (brs->nl == 0) return(nbytes-nb); | |
49 | brs->next = brs->b + brs->nl; | |
50 | brs->nr = 0; | |
51 | } | |
52 | *--buff = *--brs->next; | |
53 | brs->nr++; | |
54 | brs->nl--; | |
55 | } | |
56 | } | |
57 | return(nbytes); | |
58 | } | |
59 | ||
60 | blseek(brs, offset, flag) | |
61 | struct brbuf *brs; long offset; { | |
62 | brs->nl = 0; | |
63 | brs->nr = 0; | |
64 | return(lseek(brs->fd,offset,flag)); | |
65 | } | |
66 | ||
67 | binit(brs) | |
68 | struct brbuf *brs; { | |
69 | brs->nl = brs->nr = 0; | |
70 | } | |
71 | ||
72 | long | |
73 | tell(fildes) { | |
74 | return(lseek(fildes, 0L, 1)); | |
75 | } |