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

Last change on this file since c59409e 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
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
[b28a12e]38#include "ram.h"
[e225e77]39
[8973acd]40static int16_t (*t_read[])(io_arg arg, void *buff, int16_t len) = {
[f40a309]41
[4a17aeb]42 _badfio, /* 0 - invalid type */
[f40a309]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
[8973acd]54int16_t read(int16_t fd, void *buff, int16_t len)
[f40a309]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
[8973acd]75int16_t _getsec(struct fcb *fp, void *buf, int16_t len)
[f40a309]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
[8973acd]100int16_t _filerd(io_arg arg, void *buffer, int16_t len)
[f40a309]101{
[8973acd]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;
[f40a309]110
111 l = 0;
[8973acd]112 cpos = fp->offset + ((int32_t)fp->curlsn << FILESHFT);
113 npos = cpos + len;
[f40a309]114
115#if DEBUGIT
116 if (fsdebug)
117 printf("_filerd(): len=%u, curpos=%ld, newpos=%ld, curlen=%ld\n",
[8973acd]118 len, cpos, npos, fp->curlen);
[f40a309]119#endif
120
[8973acd]121 if (npos GT fp->curlen) {
[f40a309]122
[8973acd]123 len = (int16_t)(fp->curlen - cpos);
[f40a309]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
[8973acd]136 if (_getsec(fp, buffer8, l)) /* read what we can */
[f40a309]137 return(len); /* return if ERROR */
138 }
139
[8973acd]140 if ((k = (len - l) / BPSEC)) { /* see what we still need */
141 if ((j = blkrd(fp, buffer8 + l, k)) NE 0)
[f40a309]142 return((k - j) * BPSEC + l); /* return bytes read */
[8973acd]143 }
[f40a309]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 */
[8973acd]148 if (_getsec(fp, buffer8 + l, len - l)) /* read partial sector */
[f40a309]149 return(l); /* return if ERROR or EOF */
150
151 return(len); /* return - got the whole thing */
152}
[6262b5c]153
Note: See TracBrowser for help on using the repository browser.