source: buchla-68k/orig/IOLIB/LSEEK.C@ 4b63bf3

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

Imported original source code.

  • Property mode set to 100755
File size: 2.4 KB
Line 
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
10#include "io.h"
11#include "biosdefs.h"
12#include "errno.h"
13#include "stddefs.h"
14
15extern int _seek();
16
17#if DEBUGIT
18extern short fsdebug;
19#endif
20
21long
22lseek(fd, pos, how)
23int fd;
24register long pos;
25int how;
26{
27 register struct fcb *fp;
28
29 if ((fd < 0) OR (fd > MAXCHAN)) {
30
31#if DEBUGIT
32 if (fsdebug)
33 printf("lseek(%d): bad file index\n", fd);
34#endif
35
36 errno = EBADF; /* file number bad */
37 return(-1L);
38 }
39
40 if (chantab[fd].c_seek EQ 0) {
41
42#if DEBUGIT
43 if (fsdebug)
44 printf("lseek(%d): device not seekable\n", fd);
45#endif
46
47 errno = EINVAL; /* device can't seek */
48 return(-1L);
49 }
50
51 fp = chantab[fd].c_arg; /* get fcb pointer from channel table */
52
53 switch (how) { /* dispatch off of seek type */
54
55 case 2: /* relative to end of file */
56 pos += fp->curlen;
57 break;
58
59 case 1: /* relative to current position */
60 pos += fp->offset + (fp->curlsn << FILESHFT);
61
62 case 0: /* relative to start of file */
63 break;
64
65 default:
66 errno = EINVAL; /* invalid seek type */
67 return(-1L);
68 }
69/*
70
71*/
72 if (pos < 0) { /* trap seeks before BOF */
73
74 fp->offset = 0;
75 fp->curlsn = 0;
76 fp->modefl |= FC_ERR;
77
78#if DEBUGIT
79 if (fsdebug)
80 printf("lseek(%d): seek (%ld) is before BOF\n", fd, pos);
81#endif
82
83 errno = EINVAL;
84 return(-1L);
85
86 } else if (pos > fp->curlen) { /* trap seeks past EOF */
87
88 fp->offset = fp->curlen & (BPSEC -1);
89 fp->curlsn = fp->curlen >> FILESHFT;
90 fp->modefl |= FC_ERR;
91
92#if DEBUGIT
93 if (fsdebug)
94 printf("lseek(%d): seek (%ld) is after EOF (%ld)\n",
95 fd, pos, fp->curlen);
96#endif
97
98 errno = EINVAL;
99 return(-1L);
100 }
101
102 fp->offset = pos & ((long)BPSEC - 1); /* calculate sector offset */
103 fp->curlsn = pos >> FILESHFT; /* calculate logical sector */
104
105 if (_seek(fp) < 0) { /* position to the physical sector */
106
107 fp->modefl |= FC_ERR; /* couldn't seek */
108 errno = EIO;
109 return(-1L);
110 }
111
112 if (pos EQ fp->curlen)
113 fp->modefl |= FC_EOF;
114
115#if DEBUGIT
116 if (fsdebug)
117 printf("lseek(): pos=%d, curlsn=%ld, curdsn=%ld, offset=%u\n",
118 pos, fp->curlsn, fp->curdsn, fp->offset);
119#endif
120
121 return(pos); /* return current position */
122}
123
Note: See TracBrowser for help on using the repository browser.