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

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

Added include files for global functions and variables.

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