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

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

Removed form-feed comments.

  • Property mode set to 100644
File size: 3.8 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, int8_t *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, int8_t *buff, uint16_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, int8_t *buf, uint16_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(struct fcb *fp, int8_t *buffer, uint16_t len)
101{
102 register uint16_t l;
103 register uint16_t j, k;
104 register int32_t curpos, newpos;
105
106 l = 0;
107 curpos = fp->offset + (fp->curlsn << FILESHFT);
108 newpos = curpos + len;
109
110#if DEBUGIT
111 if (fsdebug)
112 printf("_filerd(): len=%u, curpos=%ld, newpos=%ld, curlen=%ld\n",
113 len, curpos, newpos, fp->curlen);
114#endif
115
116 if (newpos GT fp->curlen) {
117
118 len = fp->curlen - curpos;
119
120#if DEBUGIT
121 if (fsdebug)
122 printf("_filerd(): len adjusted to %u\n", len);
123#endif
124 }
125
126 if (fp->offset) { /* see if we start in the middle of a sector */
127
128 if ((l = BPSEC - fp->offset) > len) /* see what we need */
129 l = len;
130
131 if (_getsec(fp, buffer, l)) /* read what we can */
132 return(len); /* return if ERROR */
133 }
134
135 if (k = (len - l) / BPSEC) /* see what we still need */
136 if ((j = blkrd(fp, buffer + l, k)) NE 0)
137 return((k - j) * BPSEC + l); /* return bytes read */
138
139 l += k * BPSEC; /* adjust l by what we just read */
140
141 if (l < len) /* see if we still need a partial sector */
142 if (_getsec(fp, buffer + l, len - l)) /* read partial sector */
143 return(l); /* return if ERROR or EOF */
144
145 return(len); /* return - got the whole thing */
146}
147
Note: See TracBrowser for help on using the repository browser.