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