1 | /*
|
---|
2 | =============================================================================
|
---|
3 | read.c -- file read functions for the C I/O Library
|
---|
4 | Version 9 -- 1987-10-28 -- D.N. Lynx Crowe
|
---|
5 |
|
---|
6 | int
|
---|
7 | read(fd, buff, len)
|
---|
8 | char *buff;
|
---|
9 |
|
---|
10 | Reads 'len' bytes from file 'fd' into 'buff'.
|
---|
11 | Returns FAILURE (-1) for errors, number of bytes read
|
---|
12 | if successful.
|
---|
13 |
|
---|
14 | int
|
---|
15 | _getsec(fp, buf, len)
|
---|
16 | register struct fcb *fp;
|
---|
17 | char *buf;
|
---|
18 | unsigned len;
|
---|
19 |
|
---|
20 | Gets 'len' bytes into 'buf' from file 'fp'.
|
---|
21 | Returns FAILURE (-1) for errors, SUCCESS (0) if successful.
|
---|
22 |
|
---|
23 | int
|
---|
24 | _filerd(fp, buffer, len)
|
---|
25 | register struct fcb *fp;
|
---|
26 | char *buffer;
|
---|
27 | unsigned len;
|
---|
28 |
|
---|
29 | Reads 'len' bytes into 'buffer' from 'fp'.
|
---|
30 | Returns FAILURE (-1) for errors, number of bytes read
|
---|
31 | if successful.
|
---|
32 |
|
---|
33 | =============================================================================
|
---|
34 | */
|
---|
35 |
|
---|
36 | #define DEBUGIT 0
|
---|
37 |
|
---|
38 | #include "ram.h"
|
---|
39 |
|
---|
40 | static int16_t (*t_read[])() = {
|
---|
41 |
|
---|
42 | _badfd, /* 0 - invalid type */
|
---|
43 | _filerd, /* 1 - disk file read */
|
---|
44 | _conin /* 2 - console read */
|
---|
45 | };
|
---|
46 |
|
---|
47 | /* |
---|
48 |
|
---|
49 | */
|
---|
50 |
|
---|
51 | /*
|
---|
52 | =============================================================================
|
---|
53 | read(fd, buff, len) -- Reads 'len' bytes from file 'fd' into 'buff'.
|
---|
54 | Returns FAILURE (-1) for errors, number of bytes read if successful.
|
---|
55 | =============================================================================
|
---|
56 | */
|
---|
57 |
|
---|
58 | int16_t read(int16_t fd, int8_t *buff, uint16_t len)
|
---|
59 | {
|
---|
60 | register struct channel *chp;
|
---|
61 |
|
---|
62 | if (fd < 0 OR fd > MAXCHAN) { /* check fd range */
|
---|
63 |
|
---|
64 | errno = EBADF; /* bad fd */
|
---|
65 | return(FAILURE);
|
---|
66 | }
|
---|
67 |
|
---|
68 | chp = &chantab[fd]; /* point at the channel table */
|
---|
69 | return((*t_read[chp->c_read])(chp->c_arg, buff, len)); /* do the read */
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* |
---|
73 |
|
---|
74 | */
|
---|
75 |
|
---|
76 | /*
|
---|
77 | =============================================================================
|
---|
78 | _getsec(fp, buf, len) -- Gets 'len' bytes into 'buf' from file 'fp'.
|
---|
79 | Returns FAILURE (-1) for errors, SUCCESS (0) if successful.
|
---|
80 | =============================================================================
|
---|
81 | */
|
---|
82 |
|
---|
83 | int16_t _getsec(struct fcb *fp, int8_t *buf, uint16_t len)
|
---|
84 | {
|
---|
85 | if ((errno = ReadRN(fp, Wrkbuf)) NE 0) /* get current sector */
|
---|
86 | return(FAILURE);
|
---|
87 |
|
---|
88 | memcpy(buf, Wrkbuf + fp->offset, len); /* move what we need */
|
---|
89 |
|
---|
90 | if ((fp->offset = (fp->offset + len) & (BPSEC - 1)) EQ 0) {
|
---|
91 |
|
---|
92 | ++fp->curlsn; /* advance the sector number */
|
---|
93 |
|
---|
94 | if (_seek(fp) < 0) /* seek to the next sector */
|
---|
95 | return(FAILURE);
|
---|
96 | }
|
---|
97 |
|
---|
98 | return(SUCCESS); /* return: all bytes read */
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* |
---|
102 |
|
---|
103 | */
|
---|
104 |
|
---|
105 | /*
|
---|
106 | =============================================================================
|
---|
107 | _filerd(fp, buffer, len) -- Reads 'len' bytes into 'buffer' from 'fp'.
|
---|
108 | Returns FAILURE (-1) for errors, number of bytes read if successful.
|
---|
109 | =============================================================================
|
---|
110 | */
|
---|
111 |
|
---|
112 | int16_t _filerd(struct fcb *fp, int8_t *buffer, uint16_t len)
|
---|
113 | {
|
---|
114 | register uint16_t l;
|
---|
115 | register uint16_t j, k;
|
---|
116 | register int32_t curpos, newpos;
|
---|
117 |
|
---|
118 | l = 0;
|
---|
119 | curpos = fp->offset + (fp->curlsn << FILESHFT);
|
---|
120 | newpos = curpos + len;
|
---|
121 |
|
---|
122 | #if DEBUGIT
|
---|
123 | if (fsdebug)
|
---|
124 | printf("_filerd(): len=%u, curpos=%ld, newpos=%ld, curlen=%ld\n",
|
---|
125 | len, curpos, newpos, fp->curlen);
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | if (newpos GT fp->curlen) {
|
---|
129 |
|
---|
130 | len = fp->curlen - curpos;
|
---|
131 |
|
---|
132 | #if DEBUGIT
|
---|
133 | if (fsdebug)
|
---|
134 | printf("_filerd(): len adjusted to %u\n", len);
|
---|
135 | #endif
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (fp->offset) { /* see if we start in the middle of a sector */
|
---|
139 |
|
---|
140 | if ((l = BPSEC - fp->offset) > len) /* see what we need */
|
---|
141 | l = len;
|
---|
142 |
|
---|
143 | if (_getsec(fp, buffer, l)) /* read what we can */
|
---|
144 | return(len); /* return if ERROR */
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (k = (len - l) / BPSEC) /* see what we still need */
|
---|
148 | if ((j = blkrd(fp, buffer + l, k)) NE 0)
|
---|
149 | return((k - j) * BPSEC + l); /* return bytes read */
|
---|
150 |
|
---|
151 | l += k * BPSEC; /* adjust l by what we just read */
|
---|
152 |
|
---|
153 | if (l < len) /* see if we still need a partial sector */
|
---|
154 | if (_getsec(fp, buffer + l, len - l)) /* read partial sector */
|
---|
155 | return(l); /* return if ERROR or EOF */
|
---|
156 |
|
---|
157 | return(len); /* return - got the whole thing */
|
---|
158 | }
|
---|
159 |
|
---|