From 12947b0bdfdc87d6ea82500b6e9c026689692769 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Mon, 22 Apr 2013 00:29:58 +0200 Subject: [PATCH] Add fgetpos(3) and fsetpos(3). --- libc/Makefile | 2 ++ libc/fgetpos.cpp | 32 ++++++++++++++++++++++++++++++++ libc/fsetpos.cpp | 32 ++++++++++++++++++++++++++++++++ libc/include/stdio.h | 7 +++---- 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 libc/fgetpos.cpp create mode 100644 libc/fsetpos.cpp diff --git a/libc/Makefile b/libc/Makefile index 445ab2d8..d17eef32 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -203,10 +203,12 @@ fcloseall.o \ fcntl.o \ fddir-sortix.o \ fdio.o \ +fgetpos.o \ fileno.o \ fork.o \ fpipe.o \ freopen.o \ +fsetpos.o \ fsm_bootstraprootfd.o \ fsm_closechannel.o \ fsm_closeserver.o \ diff --git a/libc/fgetpos.cpp b/libc/fgetpos.cpp new file mode 100644 index 00000000..e53e71ae --- /dev/null +++ b/libc/fgetpos.cpp @@ -0,0 +1,32 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + fgetpos.cpp + Get current file position information. + +*******************************************************************************/ + +#include + +extern "C" int fgetpos(FILE* restrict fp, fpos_t* restrict pos) +{ + if ( (*pos = ftello(fp)) < 0 ) + return -1; + return 0; +} diff --git a/libc/fsetpos.cpp b/libc/fsetpos.cpp new file mode 100644 index 00000000..37525f66 --- /dev/null +++ b/libc/fsetpos.cpp @@ -0,0 +1,32 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + fsetpos.cpp + Set current file position. + +*******************************************************************************/ + +#include + +extern "C" int fsetpos(FILE* restrict fp, const fpos_t* restrict pos) +{ + if ( fseeko(fp, *pos, SEEK_SET) < 0 ) + return -1; + return 0; +} diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 119fc409..a31e6102 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -40,8 +40,7 @@ __BEGIN_DECLS @include(NULL.h) @include(FILE.h) -struct _fpos_t; -typedef struct _fpos_t fpos_t; +typedef off_t fpos_t; /* TODO: Implement L_ctermid */ #if __POSIX_OBSOLETE <= 200801 @@ -80,6 +79,7 @@ extern int ferror(FILE* stream); extern int fflush(FILE* stream); extern int fileno(FILE* stream); extern int fgetc(FILE* stream); +extern int fgetpos(FILE* __restrict stream, fpos_t* __restrict pos); extern char* fgets(char* __restrict s, int n, FILE* __restrict stream); extern FILE* fopen(const char* __restrict filename, const char* __restrict mode); extern int fprintf(FILE* __restrict stream, const char* __restrict format, ...); @@ -90,6 +90,7 @@ extern FILE* freopen(const char* __restrict filename, const char *__restrict mod extern int fscanf(FILE* __restrict stream, const char* __restrict format, ... ); extern int fseek(FILE* stream, long offset, int whence); extern int fseeko(FILE* stream, off_t offset, int whence); +extern int fsetpos(FILE* stream, const fpos_t* pos); extern long ftell(FILE* stream); extern off_t ftello(FILE* stream); extern size_t fwrite(const void* __restrict ptr, size_t size, size_t nitems, FILE* __restrict stream); @@ -133,8 +134,6 @@ extern char* ctermid(char* s); extern FILE *fmemopen(void* __restrict buf, size_t size, const char* __restrict mode); extern FILE* open_memstream(char** bufp, size_t* sizep); extern int dprintf(int fildes, const char* __restrict format, ...); -extern int fgetpos(FILE* __restrict stream, fpos_t* __restrict pos); -extern int fsetpos(FILE* stream, const fpos_t* pos); extern int ftrylockfile(FILE* file); extern int getchar_unlocked(void); extern int getc_unlocked(FILE* stream);