libc fseek issues fixed.

This commit is contained in:
Scott Duensing 2026-07-23 18:50:40 -05:00
parent dc152e575f
commit e9db34ffd3

View file

@ -57,6 +57,17 @@ typedef struct {
u16 refNum; u16 refNum;
unsigned long position; unsigned long position;
} __GsosMarkRecGS; } __GsosMarkRecGS;
// SetMark ($2016) / SetEOF ($2018) take a positioning base word followed
// by a displacement long -- NOT the bare-position layout that GetMark
// ($2017) / GetEOF ($2019) return. These calls require pCount=3; the
// Get side keeps the 3-field records above. base=0 means "displacement
// measured from the start of the file".
typedef struct {
u16 pCount;
u16 refNum;
u16 base;
unsigned long displacement;
} __GsosSetPosRecGS;
typedef struct { typedef struct {
u16 pCount; u16 pCount;
void *pathname; void *pathname;
@ -140,8 +151,8 @@ extern u16 gsosRead (__GsosIORecGS *p) __attribute__((weak, retain, used))
extern u16 gsosWrite (__GsosIORecGS *p) __attribute__((weak, retain, used)); extern u16 gsosWrite (__GsosIORecGS *p) __attribute__((weak, retain, used));
extern u16 gsosClose (__GsosRefNumRecGS *p) __attribute__((weak, retain, used)); extern u16 gsosClose (__GsosRefNumRecGS *p) __attribute__((weak, retain, used));
extern u16 gsosGetEOF (__GsosEOFRecGS *p) __attribute__((weak, retain, used)); extern u16 gsosGetEOF (__GsosEOFRecGS *p) __attribute__((weak, retain, used));
extern u16 gsosSetEOF (__GsosEOFRecGS *p) __attribute__((weak, retain, used)); extern u16 gsosSetEOF (__GsosSetPosRecGS *p) __attribute__((weak, retain, used));
extern u16 gsosSetMark(__GsosMarkRecGS *p) __attribute__((weak, retain, used)); extern u16 gsosSetMark(__GsosSetPosRecGS *p) __attribute__((weak, retain, used));
extern u16 gsosGetMark(__GsosMarkRecGS *p) __attribute__((weak, retain, used)); extern u16 gsosGetMark(__GsosMarkRecGS *p) __attribute__((weak, retain, used));
extern u16 gsosCreate (__GsosCreateParm *p) __attribute__((weak, retain, used)); extern u16 gsosCreate (__GsosCreateParm *p) __attribute__((weak, retain, used));
extern u16 gsosDestroy (__GsosDestroyParm *p) __attribute__((weak, retain, used)); extern u16 gsosDestroy (__GsosDestroyParm *p) __attribute__((weak, retain, used));
@ -1573,14 +1584,14 @@ FILE *fopen(const char *path, const char *mode) {
f->refNum = op.refNum; f->refNum = op.refNum;
if (truncate) { if (truncate) {
// "w" / "w+" — truncate to zero length. // "w" / "w+" — truncate to zero length.
__GsosEOFRecGS e = { 2, op.refNum, 0 }; __GsosSetPosRecGS e = { 3, op.refNum, 0, 0 };
if (gsosSetEOF(&e) != 0) f->err = 1; if (gsosSetEOF(&e) != 0) f->err = 1;
} }
if (append) { if (append) {
// "a" / "a+" — position at end-of-file. // "a" / "a+" — position at end-of-file.
__GsosEOFRecGS e = { 2, op.refNum, 0 }; __GsosEOFRecGS e = { 2, op.refNum, 0 };
if (gsosGetEOF(&e) == 0) { if (gsosGetEOF(&e) == 0) {
__GsosMarkRecGS m = { 2, op.refNum, e.eof }; __GsosSetPosRecGS m = { 3, op.refNum, 0, e.eof };
gsosSetMark(&m); gsosSetMark(&m);
} }
} }
@ -1709,7 +1720,7 @@ int fseek(FILE *stream, long offset, int whence) {
} }
long target = base + offset; long target = base + offset;
if (target < 0) return -1; if (target < 0) return -1;
__GsosMarkRecGS m = { 2, stream->refNum, (unsigned long)target }; __GsosSetPosRecGS m = { 3, stream->refNum, 0, (unsigned long)target };
if (gsosSetMark(&m) != 0) return -1; if (gsosSetMark(&m) != 0) return -1;
stream->eof = 0; stream->eof = 0;
stream->unget = -1; stream->unget = -1;
@ -1956,7 +1967,7 @@ static unsigned char __renameCopyBuf[RENAME_COPY_BUF_SZ];
// use stack-resident parm blocks (smaller scope, less BSS). // use stack-resident parm blocks (smaller scope, less BSS).
static __GsosIORecGS __rcIORec; static __GsosIORecGS __rcIORec;
static __GsosRefNumRecGS __rcRefRec; static __GsosRefNumRecGS __rcRefRec;
static __GsosEOFRecGS __rcEofRec; static __GsosSetPosRecGS __rcEofRec;
static __GsosDestroyParm __rcDestroy; static __GsosDestroyParm __rcDestroy;
// Tear-down on any mid-flow failure. Closes both refs (best-effort) // Tear-down on any mid-flow failure. Closes both refs (best-effort)
@ -1990,7 +2001,7 @@ static int __renameCopyDelete(const char *src, const char *dst) {
return -1; return -1;
} }
u16 dstRef = dstOpen.refNum; u16 dstRef = dstOpen.refNum;
__rcEofRec.pCount = 2; __rcEofRec.refNum = dstRef; __rcEofRec.eof = 0; __rcEofRec.pCount = 3; __rcEofRec.refNum = dstRef; __rcEofRec.base = 0; __rcEofRec.displacement = 0;
if (gsosSetEOF(&__rcEofRec) != 0) { if (gsosSetEOF(&__rcEofRec) != 0) {
__renameCleanup(srcRef, dstRef, dst); __renameCleanup(srcRef, dstRef, dst);
errno = 5; errno = 5;