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

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

No more warnings in libcio.

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