1 /*
2 * Get the position of a file
3 *
4 * long ftell(FILE *fp)
5 *
6 * Calls some machine dependent routine to do the dirty work
7 *
8 * djm 1/4/2000
9 *
10 * --------
11 * $Id: fseek.c,v 1.3 2016-03-06 12:12:57 dom Exp $
12 */
13
14 #define ANSI_STDIO
15
16 #ifdef Z80
17 #define STDIO_ASM
18 #endif
19
20 #include <stdio.h>
21 #include <fcntl.h>
22
23 static long fseek1(FILE *fp, fpos_t posn, int whence) __z88dk_callee;
24
fseek(FILE * fp,fpos_t posn,int whence)25 int fseek(FILE *fp, fpos_t posn, int whence) __z88dk_saveframe
26 {
27 if ( fp->flags&_IOUSE && fchkstd(fp)== 0 ) {
28 #if CPU_8080 || CPU_GBZ80
29 if (lseek(fp->desc.fd,posn,whence) != -1L ) {
30 #else
31 if (fseek1(fp,posn,whence) != -1L ) {
32 #endif
33 fp->flags &= ~_IOEOF;
34 return 0;
35 }
36 }
37 return 1;
38 }
39
40 #asm
41 IF !__CPU_INTEL__ && !__CPU_GBZ80__
42 _fseek1:
43 pop af ;return address
44 pop bc ;whence
45 pop de ;posn
46 pop hl
47 pop ix ;fp
48 push af ;return address
49
50 ld a,(ix+fp_flags)
51 and _IOEXTRA
52 jr nz, call_trampoline
53 ; Normal file descriptor, just call lseek
54 ld a,c ;save whence
55 ld c,(ix+fp_desc)
56 ld b,(ix+fp_desc+1)
57 push bc ;descriptor
58 push hl ;posn
59 push de
60 ld c,a
61 ld b,0
62 push bc ;whence
63 call lseek
64 pop bc
65 pop bc
66 pop bc
67 pop bc
68 ret
69
70 call_trampoline:
71 ; Call via the trampoline
72 ld a,c ;a = whence
73 ld c,l ;lower 16 bit of posn
74 ld b,h
75 IF __CPU_R2K__ | __CPU_R3K__
76 ld hl,(ix+fp_extra)
77 ELSE
78 ld l,(ix+fp_extra)
79 ld h,(ix+fp_extra+1)
80 ENDIF
81 ex af,af
82 ld a,__STDIO_MSG_SEEK
83 call l_jphl
84 ret
85 ENDIF
86 #endasm
87
88
89
90
91
92
93
94
95
96