[f40a309] | 1 | /*
|
---|
| 2 | =============================================================================
|
---|
| 3 | ftell.c -- return current file position
|
---|
| 4 | Version 7 -- 1987-10-28 -- D.N. Lynx Crowe
|
---|
| 5 | =============================================================================
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #define DEBUGIT 0
|
---|
| 9 |
|
---|
| 10 | #include "biosdefs.h"
|
---|
| 11 | #include "stdio.h"
|
---|
| 12 | #include "io.h"
|
---|
| 13 | #include "stddefs.h"
|
---|
| 14 |
|
---|
| 15 | #if DEBUGIT
|
---|
| 16 | extern short fsdebug;
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
[7258c6a] | 19 | extern int16_t _filecl(struct fcb *fp);
|
---|
[f40a309] | 20 |
|
---|
| 21 | /*
|
---|
| 22 | =============================================================================
|
---|
| 23 | ftell(fp) -- return the current position of file 'fp'.
|
---|
| 24 | =============================================================================
|
---|
| 25 | */
|
---|
| 26 |
|
---|
[7258c6a] | 27 | int32_t ftell(FILE *fp)
|
---|
[f40a309] | 28 | {
|
---|
| 29 | register struct fcb *fcp;
|
---|
| 30 | register struct channel *chp;
|
---|
[7258c6a] | 31 | register int32_t dpos, pos, diff;
|
---|
[f40a309] | 32 |
|
---|
| 33 | if (fp EQ (FILE *)0L) { /* see if we point at a FILE */
|
---|
| 34 |
|
---|
| 35 | #if DEBUGIT
|
---|
| 36 | if (fsdebug)
|
---|
| 37 | printf("ftell($%08.8lX): ERROR - null FILE pointer\n", fp);
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
| 40 | return(0L);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | if (!fp->_flags & _BUSY) { /* see if it's open */
|
---|
| 44 |
|
---|
| 45 | #if DEBUGIT
|
---|
| 46 | if (fsdebug)
|
---|
| 47 | printf("ftell($%08.8lX): ERROR - FILE not open\n", fp);
|
---|
| 48 | #endif
|
---|
| 49 |
|
---|
| 50 | return(0L);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | chp = &chantab[fp->_unit]; /* point at the channel */
|
---|
| 54 |
|
---|
| 55 | if (chp->c_close NE _filecl) { /* see if it's seekable */
|
---|
| 56 |
|
---|
| 57 | #if DEBUGIT
|
---|
| 58 | if (fsdebug)
|
---|
| 59 | printf("ftell($%08.8lX): ERROR - FILE device not seekable\n",
|
---|
| 60 | fp);
|
---|
| 61 | #endif
|
---|
| 62 |
|
---|
| 63 | return(0L);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | fcp = chp->c_arg; /* point at the FCB */
|
---|
| 67 |
|
---|
| 68 | dpos = fcp->offset + (fcp->curlsn << FILESHFT);
|
---|
| 69 |
|
---|
| 70 | if (fp->_flags & _DIRTY) /* adjust for the buffering */
|
---|
[7258c6a] | 71 | pos = dpos + (diff = ((int32_t)fp->_bp - (int32_t)fp->_buff));
|
---|
[f40a309] | 72 | else if (fp->_bp)
|
---|
[7258c6a] | 73 | pos = dpos - (diff = ((int32_t)fp->_bend - (int32_t)fp->_bp));
|
---|
[f40a309] | 74 | else
|
---|
| 75 | pos = dpos;
|
---|
| 76 |
|
---|
| 77 | #if DEBUGIT
|
---|
| 78 | if (fsdebug) {
|
---|
| 79 |
|
---|
| 80 | printf("ftell($%08.8lX): flags=$%04.4X, buff=$%08.8lX, bp=$%08.8lX, bend=$%08.8lX\n",
|
---|
| 81 | fp, fp->_flags, fp->_buff, fp->_bp, fp->_bend);
|
---|
| 82 | printf("ftell($%08.8lX): fcp=$%08.8lX, pos=%ld, dpos=%ld, diff=%ld\n",
|
---|
| 83 | fp, fcp, pos, dpos, diff);
|
---|
| 84 | printf("ftell($%08.8lX): curlsn=%ld, offset=%u\n",
|
---|
| 85 | fp, fcp->curlsn, fcp->offset);
|
---|
| 86 |
|
---|
| 87 | if ((fp->_flags & _DIRTY) AND (fp->_bp EQ NULL))
|
---|
| 88 | printf("ftell($%08.8lX): ERROR - file is DIRTY and bp is NULL\n",
|
---|
| 89 | fp);
|
---|
| 90 | }
|
---|
| 91 | #endif
|
---|
| 92 |
|
---|
| 93 | return(pos);
|
---|
| 94 | }
|
---|