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