libc: Implement fsetpos() and fgetpos()

Not much to do, since these are kind of equivalent to fseek() and ftell().
This commit is contained in:
apio 2022-10-14 19:12:40 +02:00
parent e0aa552fae
commit 3b83d7ccaf
2 changed files with 24 additions and 0 deletions

View File

@ -13,6 +13,11 @@ typedef struct
int f_err; int f_err;
} FILE; } FILE;
typedef struct
{
long f_offset;
} fpos_t;
extern FILE* __stderr; // The standard error stream. extern FILE* __stderr; // The standard error stream.
extern FILE* __stdout; // The standard output stream. extern FILE* __stdout; // The standard output stream.
#define stderr __stderr #define stderr __stderr
@ -44,9 +49,15 @@ extern "C"
/* Moves stream's read/write offset by offset, depending on whence. */ /* Moves stream's read/write offset by offset, depending on whence. */
int fseek(FILE* stream, long offset, int 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. */ /* Returns the current offset for stream. */
long ftell(FILE* 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. */ /* Rewinds stream's offset to start of file. */
void rewind(FILE* stream); void rewind(FILE* stream);

View File

@ -87,6 +87,11 @@ extern "C"
return 0; return 0;
} }
int fsetpos(FILE* stream, const fpos_t* pos)
{
return fseek(stream, pos->f_offset, SEEK_SET);
}
long ftell(FILE* stream) long ftell(FILE* stream)
{ {
return lseek(stream->f_fd, 0, 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... // 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) void rewind(FILE* stream)
{ {
lseek(stream->f_fd, 0, SEEK_SET); lseek(stream->f_fd, 0, SEEK_SET);