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

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

Added include files for global functions and variables.

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