From e9db34ffd3fdfdf637533d4899b81fa130e86684 Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Thu, 23 Jul 2026 18:50:40 -0500 Subject: [PATCH] libc fseek issues fixed. --- runtime/src/libc.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/runtime/src/libc.c b/runtime/src/libc.c index 467f8aa..198e6dd 100644 --- a/runtime/src/libc.c +++ b/runtime/src/libc.c @@ -57,6 +57,17 @@ typedef struct { u16 refNum; unsigned long position; } __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 { u16 pCount; 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 gsosClose (__GsosRefNumRecGS *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 gsosSetMark(__GsosMarkRecGS *p) __attribute__((weak, retain, used)); +extern u16 gsosSetEOF (__GsosSetPosRecGS *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 gsosCreate (__GsosCreateParm *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; if (truncate) { // "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 (append) { // "a" / "a+" — position at end-of-file. __GsosEOFRecGS e = { 2, op.refNum, 0 }; if (gsosGetEOF(&e) == 0) { - __GsosMarkRecGS m = { 2, op.refNum, e.eof }; + __GsosSetPosRecGS m = { 3, op.refNum, 0, e.eof }; gsosSetMark(&m); } } @@ -1709,7 +1720,7 @@ int fseek(FILE *stream, long offset, int whence) { } long target = base + offset; 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; stream->eof = 0; stream->unget = -1; @@ -1956,7 +1967,7 @@ static unsigned char __renameCopyBuf[RENAME_COPY_BUF_SZ]; // use stack-resident parm blocks (smaller scope, less BSS). static __GsosIORecGS __rcIORec; static __GsosRefNumRecGS __rcRefRec; -static __GsosEOFRecGS __rcEofRec; +static __GsosSetPosRecGS __rcEofRec; static __GsosDestroyParm __rcDestroy; // 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; } 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) { __renameCleanup(srcRef, dstRef, dst); errno = 5;