source: buchla-68k/libcio/lseek.c@ 9519422

Last change on this file since 9519422 was 7258c6a, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Use standard integer types.

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