From 3b83d7ccaf9540a059b577246463baa523cfee00 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 14 Oct 2022 19:12:40 +0200 Subject: [PATCH] libc: Implement fsetpos() and fgetpos() Not much to do, since these are kind of equivalent to fseek() and ftell(). --- libs/libc/include/stdio.h | 11 +++++++++++ libs/libc/src/file.cpp | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/libs/libc/include/stdio.h b/libs/libc/include/stdio.h index b0e0aab9..2145336a 100644 --- a/libs/libc/include/stdio.h +++ b/libs/libc/include/stdio.h @@ -13,6 +13,11 @@ typedef struct int f_err; } FILE; +typedef struct +{ + long f_offset; +} fpos_t; + extern FILE* __stderr; // The standard error stream. extern FILE* __stdout; // The standard output stream. #define stderr __stderr @@ -44,9 +49,15 @@ extern "C" /* Moves stream's read/write offset by offset, depending on whence. */ int fseek(FILE* stream, long offset, int whence); + /* Moves stream's read/write offset to the offset stored in the pos structure. */ + int fsetpos(FILE* stream, const fpos_t* pos); + /* Returns the current offset for stream. */ long ftell(FILE* stream); + /* Stores the current offset for stream in the pos structure. */ + int fgetpos(FILE* stream, fpos_t* pos); + /* Rewinds stream's offset to start of file. */ void rewind(FILE* stream); diff --git a/libs/libc/src/file.cpp b/libs/libc/src/file.cpp index e0525a22..e603f121 100644 --- a/libs/libc/src/file.cpp +++ b/libs/libc/src/file.cpp @@ -87,6 +87,11 @@ extern "C" return 0; } + int fsetpos(FILE* stream, const fpos_t* pos) + { + return fseek(stream, pos->f_offset, SEEK_SET); + } + long ftell(FILE* stream) { return lseek(stream->f_fd, 0, @@ -94,6 +99,14 @@ extern "C" // maybe? We'd have to update this value in fread() and fwrite() as well... } + int fgetpos(FILE* stream, fpos_t* pos) + { + long result = ftell(stream); + if (result < 0) { return -1; } + pos->f_offset = result; + return 0; + } + void rewind(FILE* stream) { lseek(stream->f_fd, 0, SEEK_SET);