[f40a309] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | lseek.c -- position a file to a specified byte location
|
---|
| 4 | Version 7 -- 1987-10-28 -- D.N. Lynx Crowe
|
---|
| 5 | =============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #define DEBUGIT 0
|
---|
| 9 |
|
---|
[b28a12e] | 10 | #include "ram.h"
|
---|
[f40a309] | 11 |
|
---|
[7258c6a] | 12 | int32_t lseek(int16_t fd, int32_t pos, int16_t how)
|
---|
[f40a309] | 13 | {
|
---|
| 14 | register struct fcb *fp;
|
---|
| 15 |
|
---|
| 16 | if ((fd < 0) OR (fd > MAXCHAN)) {
|
---|
| 17 |
|
---|
| 18 | #if DEBUGIT
|
---|
| 19 | if (fsdebug)
|
---|
| 20 | printf("lseek(%d): bad file index\n", fd);
|
---|
| 21 | #endif
|
---|
| 22 |
|
---|
| 23 | errno = EBADF; /* file number bad */
|
---|
| 24 | return(-1L);
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | if (chantab[fd].c_seek EQ 0) {
|
---|
| 28 |
|
---|
| 29 | #if DEBUGIT
|
---|
| 30 | if (fsdebug)
|
---|
| 31 | printf("lseek(%d): device not seekable\n", fd);
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 | errno = EINVAL; /* device can't seek */
|
---|
| 35 | return(-1L);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | fp = chantab[fd].c_arg; /* get fcb pointer from channel table */
|
---|
| 39 |
|
---|
| 40 | switch (how) { /* dispatch off of seek type */
|
---|
| 41 |
|
---|
| 42 | case 2: /* relative to end of file */
|
---|
| 43 | pos += fp->curlen;
|
---|
| 44 | break;
|
---|
| 45 |
|
---|
| 46 | case 1: /* relative to current position */
|
---|
| 47 | pos += fp->offset + (fp->curlsn << FILESHFT);
|
---|
| 48 |
|
---|
| 49 | case 0: /* relative to start of file */
|
---|
| 50 | break;
|
---|
| 51 |
|
---|
| 52 | default:
|
---|
| 53 | errno = EINVAL; /* invalid seek type */
|
---|
| 54 | return(-1L);
|
---|
| 55 | }
|
---|
| 56 | /* |
---|
| 57 |
|
---|
| 58 | */
|
---|
| 59 | if (pos < 0) { /* trap seeks before BOF */
|
---|
| 60 |
|
---|
| 61 | fp->offset = 0;
|
---|
| 62 | fp->curlsn = 0;
|
---|
| 63 | fp->modefl |= FC_ERR;
|
---|
| 64 |
|
---|
| 65 | #if DEBUGIT
|
---|
| 66 | if (fsdebug)
|
---|
| 67 | printf("lseek(%d): seek (%ld) is before BOF\n", fd, pos);
|
---|
| 68 | #endif
|
---|
| 69 |
|
---|
| 70 | errno = EINVAL;
|
---|
| 71 | return(-1L);
|
---|
| 72 |
|
---|
| 73 | } else if (pos > fp->curlen) { /* trap seeks past EOF */
|
---|
| 74 |
|
---|
| 75 | fp->offset = fp->curlen & (BPSEC -1);
|
---|
| 76 | fp->curlsn = fp->curlen >> FILESHFT;
|
---|
| 77 | fp->modefl |= FC_ERR;
|
---|
| 78 |
|
---|
| 79 | #if DEBUGIT
|
---|
| 80 | if (fsdebug)
|
---|
| 81 | printf("lseek(%d): seek (%ld) is after EOF (%ld)\n",
|
---|
| 82 | fd, pos, fp->curlen);
|
---|
| 83 | #endif
|
---|
| 84 |
|
---|
| 85 | errno = EINVAL;
|
---|
| 86 | return(-1L);
|
---|
| 87 | }
|
---|
[7258c6a] | 88 |
|
---|
[f40a309] | 89 | fp->offset = pos & ((int32_t)BPSEC - 1); /* calculate sector offset */
|
---|
| 90 | fp->curlsn = pos >> FILESHFT; /* calculate logical sector */
|
---|
| 91 |
|
---|
| 92 | if (_seek(fp) < 0) { /* position to the physical sector */
|
---|
| 93 |
|
---|
| 94 | fp->modefl |= FC_ERR; /* couldn't seek */
|
---|
| 95 | errno = EIO;
|
---|
| 96 | return(-1L);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | if (pos EQ fp->curlen)
|
---|
| 100 | fp->modefl |= FC_EOF;
|
---|
| 101 |
|
---|
| 102 | #if DEBUGIT
|
---|
| 103 | if (fsdebug)
|
---|
| 104 | printf("lseek(): pos=%d, curlsn=%ld, curdsn=%ld, offset=%u\n",
|
---|
| 105 | pos, fp->curlsn, fp->curdsn, fp->offset);
|
---|
| 106 | #endif
|
---|
| 107 |
|
---|
| 108 | return(pos); /* return current position */
|
---|
| 109 | }
|
---|
[6262b5c] | 110 |
|
---|
| 111 |
|
---|