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: ftell.c,v 1.4 2016-03-06 21:36:52 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 
ftell(FILE * fp)23 fpos_t ftell(FILE *fp)
24 {
25 #ifdef Z80
26 #asm
27 	pop	bc
28 	pop	hl
29 	push	hl
30 	push	bc
31 IF !__CPU_INTEL__ && !__CPU_GBZ80__
32         push    ix      ;save callers ix
33 ENDIF
34 	ld	e,(hl)	;gets the underlying fp
35 	inc	hl
36 	ld	d,(hl)
37 	inc	hl
38 	ld	a,(hl)	; flags
39 	and	_IOUSE
40 	jr	z,ftell_abort
41 	ld	a,(hl)
42 	and	_IOSYSTEM
43 	jr	nz,ftell_abort
44 IF !__CPU_INTEL__ && !__CPU_GBZ80__
45 	ld	a,(hl)
46 	and	_IOEXTRA
47 	jr	nz,ftell_trampoline
48 ENDIF
49 	push	de
50 	call	fdtell
51 	pop	bc
52 IF !__CPU_INTEL__ && !__CPU_GBZ80__
53         pop     ix
54 ENDIF
55 	ret
56 .ftell_abort
57 	ld	de,65535	;-1
58 	ld	l,e
59 	ld	h,d
60 IF !__CPU_INTEL__ && !__CPU_GBZ80__
61         pop     ix
62 	ret
63 
64 .ftell_trampoline
65 	; Call the seek function via the trampoline
66 	dec	hl
67 	dec	hl
68   IF __CPU_R2K__ | __CPU_R3K__
69 	ld	ix,hl
70   ELSE
71 	push	hl
72 	pop	ix	;ix = fp
73   ENDIF
74 	ld	de,0	;posn
75 	ld	bc,0
76 	ld	a,SEEK_CUR
77 	ex	af,af
78   IF __CPU_R2K__ | __CPU_R3K__
79 	ld	hl,(ix+fp_extra)
80   ELSE
81 	ld	l,(ix+fp_extra)
82 	ld	h,(ix+fp_extra+1)
83   ENDIF
84 	ld	a,__STDIO_MSG_SEEK
85 	call	l_jphl
86 	pop	ix
87 ENDIF
88 #endasm
89 #else
90 	if ( fp->flags&_IOUSE && fchkstd(fp)== 0 ) {
91 		return (fdtell(fp->fd));
92 	}
93 	return -1L;
94 #endif
95 }
96 
97 
98