From 23d859e793cc169974d66ea5ef991910f56bb4b6 Mon Sep 17 00:00:00 2001 From: bumbread Date: Fri, 29 Jul 2022 01:16:23 +1100 Subject: [PATCH] Stream positioning funcs --- inc/stdio.h | 2 +- src/_win/stdio.c | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/inc/stdio.h b/inc/stdio.h index 4560745..852ae2e 100644 --- a/inc/stdio.h +++ b/inc/stdio.h @@ -43,9 +43,9 @@ typedef struct { #define L_tmpnam FILENAME_MAX +#define SEEK_SET 0 #define SEEK_CUR 1 #define SEEK_END 2 -#define SEEK_SET 0 #ifdef _os_win #define TMP_MAX SHORT_MAX diff --git a/src/_win/stdio.c b/src/_win/stdio.c index 4739233..347648d 100644 --- a/src/_win/stdio.c +++ b/src/_win/stdio.c @@ -426,6 +426,13 @@ int fgetpos(FILE *restrict stream, fpos_t *restrict pos) { } int fseek(FILE *stream, long int offset, int whence) { + // Note(bumbread): the SEEK_SET, SEEK_CUR and SEEK_END are defined to match + // Windows' constants, so no conversion is requierd between them. + LONG pos_hi = 0; + DWORD pos_lo = SetFilePointer(stream->handle, offset, &pos_hi, whence); + if(pos_lo == INVALID_SET_FILE_POINTER) { + return -1L; + } return 0; } @@ -441,11 +448,17 @@ int fsetpos(FILE *stream, const fpos_t *pos) { } long int ftell(FILE *stream) { - return 0; + LONG pos_hi = 0; + DWORD pos_lo = SetFilePointer(stream->handle, 0, &pos_hi, FILE_CURRENT); + if(pos_lo == INVALID_SET_FILE_POINTER) { + return -1L; + } + int64_t offset = ((int64_t)pos_hi << 32) | (int64_t)pos_lo; + return offset; } void rewind(FILE *stream) { - + fseek(stream, 0, SEEK_SET); } int getchar(void) {