source: buchla-68k/libcio/read.c@ e225e77

Last change on this file since e225e77 was e225e77, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Added missing includes and declarations.

  • Property mode set to 100644
File size: 4.3 KB
Line 
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 "biosdefs.h"
39#include "io.h"
40#include "errno.h"
41#include "fcntl.h"
42#include "stddefs.h"
43
44#if DEBUGIT
45extern short fsdebug;
46#endif
47
48extern int16_t _badfd(void);
49extern int16_t _conin(int8_t *buff, int16_t len);
50extern int16_t _seek(struct fcb *fcp);
51
52extern void *memcpy(void *vp1, void *vp2, int16_t n);
53
54int16_t _filerd(struct fcb *fp, int8_t *buffer, uint16_t len);
55
56extern int16_t ReadRN(struct fcb *fcp, int8_t *buf);
57extern int16_t blkrd(struct fcb *fcp, int8_t *buf, int16_t ns);
58
59static int16_t (*t_read[])() = {
60
61 _badfd, /* 0 - invalid type */
62 _filerd, /* 1 - disk file read */
63 _conin /* 2 - console read */
64};
65
66/*
67
68*/
69
70/*
71 =============================================================================
72 read(fd, buff, len) -- Reads 'len' bytes from file 'fd' into 'buff'.
73 Returns FAILURE (-1) for errors, number of bytes read if successful.
74 =============================================================================
75*/
76
77int16_t read(int16_t fd, int8_t *buff, uint16_t len)
78{
79 register struct channel *chp;
80
81 if (fd < 0 OR fd > MAXCHAN) { /* check fd range */
82
83 errno = EBADF; /* bad fd */
84 return(FAILURE);
85 }
86
87 chp = &chantab[fd]; /* point at the channel table */
88 return((*t_read[chp->c_read])(chp->c_arg, buff, len)); /* do the read */
89}
90
91/*
92
93*/
94
95/*
96 =============================================================================
97 _getsec(fp, buf, len) -- Gets 'len' bytes into 'buf' from file 'fp'.
98 Returns FAILURE (-1) for errors, SUCCESS (0) if successful.
99 =============================================================================
100*/
101
102int16_t _getsec(struct fcb *fp, int8_t *buf, uint16_t len)
103{
104 if ((errno = ReadRN(fp, Wrkbuf)) NE 0) /* get current sector */
105 return(FAILURE);
106
107 memcpy(buf, Wrkbuf + fp->offset, len); /* move what we need */
108
109 if ((fp->offset = (fp->offset + len) & (BPSEC - 1)) EQ 0) {
110
111 ++fp->curlsn; /* advance the sector number */
112
113 if (_seek(fp) < 0) /* seek to the next sector */
114 return(FAILURE);
115 }
116
117 return(SUCCESS); /* return: all bytes read */
118}
119
120/*
121
122*/
123
124/*
125 =============================================================================
126 _filerd(fp, buffer, len) -- Reads 'len' bytes into 'buffer' from 'fp'.
127 Returns FAILURE (-1) for errors, number of bytes read if successful.
128 =============================================================================
129*/
130
131int16_t _filerd(struct fcb *fp, int8_t *buffer, uint16_t len)
132{
133 register uint16_t l;
134 register uint16_t j, k;
135 register int32_t curpos, newpos;
136
137 l = 0;
138 curpos = fp->offset + (fp->curlsn << FILESHFT);
139 newpos = curpos + len;
140
141#if DEBUGIT
142 if (fsdebug)
143 printf("_filerd(): len=%u, curpos=%ld, newpos=%ld, curlen=%ld\n",
144 len, curpos, newpos, fp->curlen);
145#endif
146
147 if (newpos GT fp->curlen) {
148
149 len = fp->curlen - curpos;
150
151#if DEBUGIT
152 if (fsdebug)
153 printf("_filerd(): len adjusted to %u\n", len);
154#endif
155 }
156
157 if (fp->offset) { /* see if we start in the middle of a sector */
158
159 if ((l = BPSEC - fp->offset) > len) /* see what we need */
160 l = len;
161
162 if (_getsec(fp, buffer, l)) /* read what we can */
163 return(len); /* return if ERROR */
164 }
165
166 if (k = (len - l) / BPSEC) /* see what we still need */
167 if ((j = blkrd(fp, buffer + l, k)) NE 0)
168 return((k - j) * BPSEC + l); /* return bytes read */
169
170 l += k * BPSEC; /* adjust l by what we just read */
171
172 if (l < len) /* see if we still need a partial sector */
173 if (_getsec(fp, buffer + l, len - l)) /* read partial sector */
174 return(l); /* return if ERROR or EOF */
175
176 return(len); /* return - got the whole thing */
177}
Note: See TracBrowser for help on using the repository browser.