From 6ca24b75c75b9c6f22e1e69693d326b8e3330841 Mon Sep 17 00:00:00 2001 From: Owen Shepherd Date: Mon, 12 Nov 2012 04:09:02 +0000 Subject: [PATCH] PDCLIB-16: Add _unlocked variations of all I/O routines; move work into these versions PDCLIB-15: Make most stdio routines lock the stream and then call through to the _unlocked version --- functions/stdio/_PDCLIB_fdopen.c | 1 + functions/stdio/_PDCLIB_ftell64.c | 10 +++++++++- functions/stdio/clearerr.c | 9 ++++++++- functions/stdio/fclose.c | 1 + functions/stdio/feof.c | 10 +++++++++- functions/stdio/ferror.c | 10 +++++++++- functions/stdio/fflush.c | 10 +++++++++- functions/stdio/fgetc.c | 10 +++++++++- functions/stdio/fgetpos.c | 10 +++++++++- functions/stdio/fgets.c | 11 ++++++++++- functions/stdio/fprintf.c | 16 +++++++++++++++- functions/stdio/fputc.c | 10 +++++++++- functions/stdio/fputs.c | 12 +++++++++++- functions/stdio/fread.c | 14 +++++++++++++- functions/stdio/freopen.c | 13 ++++++++++++- functions/stdio/fscanf.c | 14 +++++++++++++- functions/stdio/fseek.c | 10 +++++++++- functions/stdio/fsetpos.c | 12 +++++++++++- functions/stdio/ftell.c | 12 ++++++++++-- functions/stdio/fwrite.c | 14 +++++++++++++- functions/stdio/getc.c | 5 +++++ functions/stdio/getchar.c | 6 ++++++ functions/stdio/printf.c | 10 ++++++++++ functions/stdio/putc.c | 6 ++++++ functions/stdio/putchar.c | 5 +++++ functions/stdio/puts.c | 10 +++++++++- functions/stdio/scanf.c | 7 +++++++ functions/stdio/ungetc.c | 10 +++++++++- functions/stdio/vfprintf.c | 14 +++++++++++++- functions/stdio/vfscanf.c | 14 +++++++++++++- functions/stdio/vprintf.c | 6 ++++++ functions/stdio/vscanf.c | 5 +++++ functions/stdio/vsnprintf.c | 5 ++++- functions/stdio/vsprintf.c | 4 +++- functions/stdio/vsscanf.c | 4 +++- includes/stdio.h | 26 +++++++++++++++++++++++++- 36 files changed, 319 insertions(+), 27 deletions(-) diff --git a/functions/stdio/_PDCLIB_fdopen.c b/functions/stdio/_PDCLIB_fdopen.c index 7b53952..c628ebc 100644 --- a/functions/stdio/_PDCLIB_fdopen.c +++ b/functions/stdio/_PDCLIB_fdopen.c @@ -12,6 +12,7 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> #include +#include extern struct _PDCLIB_file_t * _PDCLIB_filelist; diff --git a/functions/stdio/_PDCLIB_ftell64.c b/functions/stdio/_PDCLIB_ftell64.c index 650eda5..e0ea368 100644 --- a/functions/stdio/_PDCLIB_ftell64.c +++ b/functions/stdio/_PDCLIB_ftell64.c @@ -12,7 +12,7 @@ #ifndef REGTEST -uint_fast64_t _PDCLIB_ftell64( struct _PDCLIB_file_t * stream ) +uint_fast64_t _PDCLIB_ftell64_unlocked( struct _PDCLIB_file_t * stream ) { /* ftell() must take into account: - the actual *physical* offset of the file, i.e. the offset as recognized @@ -38,6 +38,14 @@ uint_fast64_t _PDCLIB_ftell64( struct _PDCLIB_file_t * stream ) return ( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + (int)stream->ungetidx ) ); } +uint_fast64_t _PDCLIB_ftell64( struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + uint_fast64_t pos = _PDCLIB_ftell64_unlocked( stream ); + funlockfile( stream ); + return pos; +} + #endif #ifdef TEST diff --git a/functions/stdio/clearerr.c b/functions/stdio/clearerr.c index af50f14..2b032bd 100644 --- a/functions/stdio/clearerr.c +++ b/functions/stdio/clearerr.c @@ -10,11 +10,18 @@ #ifndef REGTEST -void clearerr( struct _PDCLIB_file_t * stream ) +void clearerr_unlocked( struct _PDCLIB_file_t * stream ) { stream->status &= ~( _PDCLIB_ERRORFLAG | _PDCLIB_EOFFLAG ); } +void clearerr( struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + clearerr_unlocked( stream ); + funlockfile( stream ); +} + #endif #ifdef TEST diff --git a/functions/stdio/fclose.c b/functions/stdio/fclose.c index dbd7f6e..c4f5d8a 100644 --- a/functions/stdio/fclose.c +++ b/functions/stdio/fclose.c @@ -12,6 +12,7 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> +#include extern struct _PDCLIB_file_t * _PDCLIB_filelist; diff --git a/functions/stdio/feof.c b/functions/stdio/feof.c index d5cf188..ca43c75 100644 --- a/functions/stdio/feof.c +++ b/functions/stdio/feof.c @@ -10,11 +10,19 @@ #ifndef REGTEST -int feof( struct _PDCLIB_file_t * stream ) +int feof_unlocked( struct _PDCLIB_file_t * stream ) { return stream->status & _PDCLIB_EOFFLAG; } +int feof( struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + int eof = feof_unlocked( stream ); + funlockfile( stream ); + return eof; +} + #endif #ifdef TEST diff --git a/functions/stdio/ferror.c b/functions/stdio/ferror.c index 7ca531e..394dcda 100644 --- a/functions/stdio/ferror.c +++ b/functions/stdio/ferror.c @@ -10,11 +10,19 @@ #ifndef REGTEST -int ferror( struct _PDCLIB_file_t * stream ) +int ferror_unlocked( struct _PDCLIB_file_t * stream ) { return stream->status & _PDCLIB_ERRORFLAG; } +int ferror( struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + int error = ferror_unlocked( stream ); + funlockfile( stream ); + return error; +} + #endif #ifdef TEST diff --git a/functions/stdio/fflush.c b/functions/stdio/fflush.c index b8d32ec..df45880 100644 --- a/functions/stdio/fflush.c +++ b/functions/stdio/fflush.c @@ -13,7 +13,7 @@ extern struct _PDCLIB_file_t * _PDCLIB_filelist; -int fflush( struct _PDCLIB_file_t * stream ) +int fflush_unlocked( struct _PDCLIB_file_t * stream ) { if ( stream == NULL ) { @@ -38,6 +38,14 @@ int fflush( struct _PDCLIB_file_t * stream ) return _PDCLIB_flushbuffer( stream ); } } + +int fflush( struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + int res = fflush_unlocked(stream); + funlockfile( stream ); + return res; +} #endif diff --git a/functions/stdio/fgetc.c b/functions/stdio/fgetc.c index 29522c1..ae7a835 100644 --- a/functions/stdio/fgetc.c +++ b/functions/stdio/fgetc.c @@ -12,7 +12,7 @@ #include <_PDCLIB_glue.h> -int fgetc( struct _PDCLIB_file_t * stream ) +int fgetc_unlocked( struct _PDCLIB_file_t * stream ) { if ( _PDCLIB_prepread( stream ) == EOF ) { @@ -25,6 +25,14 @@ int fgetc( struct _PDCLIB_file_t * stream ) return (unsigned char)stream->buffer[stream->bufidx++]; } +int fgetc( struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + int c = fgetc_unlocked( stream ); + funlockfile( stream ); + return c; +} + #endif #ifdef TEST diff --git a/functions/stdio/fgetpos.c b/functions/stdio/fgetpos.c index 3e41c40..28352c8 100644 --- a/functions/stdio/fgetpos.c +++ b/functions/stdio/fgetpos.c @@ -10,7 +10,7 @@ #ifndef REGTEST -int fgetpos( struct _PDCLIB_file_t * _PDCLIB_restrict stream, struct _PDCLIB_fpos_t * _PDCLIB_restrict pos ) +int fgetpos_unlocked( struct _PDCLIB_file_t * _PDCLIB_restrict stream, struct _PDCLIB_fpos_t * _PDCLIB_restrict pos ) { pos->offset = stream->pos.offset + stream->bufidx - stream->ungetidx; pos->status = stream->pos.status; @@ -18,6 +18,14 @@ int fgetpos( struct _PDCLIB_file_t * _PDCLIB_restrict stream, struct _PDCLIB_fpo return 0; } +int fgetpos( struct _PDCLIB_file_t * _PDCLIB_restrict stream, struct _PDCLIB_fpos_t * _PDCLIB_restrict pos ) +{ + flockfile( stream ); + int res = fgetpos_unlocked( stream, pos ); + funlockfile( stream ); + return res; +} + #endif #ifdef TEST diff --git a/functions/stdio/fgets.c b/functions/stdio/fgets.c index 44518a3..0451058 100644 --- a/functions/stdio/fgets.c +++ b/functions/stdio/fgets.c @@ -12,7 +12,7 @@ #include <_PDCLIB_glue.h> -char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +char * fgets_unlocked( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { if ( size == 0 ) { @@ -46,6 +46,15 @@ char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCL return ( dest == s ) ? NULL : s; } +char * fgets( char * _PDCLIB_restrict s, int size, + struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +{ + flockfile( stream ); + char* r = fgets_unlocked( s, size, stream ); + funlockfile( stream ); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/fprintf.c b/functions/stdio/fprintf.c index cb922ba..421ca52 100644 --- a/functions/stdio/fprintf.c +++ b/functions/stdio/fprintf.c @@ -11,7 +11,8 @@ #ifndef REGTEST -int fprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) +int fprintf_unlocked( struct _PDCLIB_file_t * _PDCLIB_restrict stream, + const char * _PDCLIB_restrict format, ... ) { int rc; va_list ap; @@ -21,6 +22,19 @@ int fprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDCL return rc; } +int fprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, + const char * _PDCLIB_restrict format, ... ) +{ + int rc; + va_list ap; + va_start( ap, format ); + flockfile( stream ); + rc = vfprintf_unlocked( stream, format, ap ); + funlockfile( stream ); + va_end( ap ); + return rc; +} + #endif #ifdef TEST diff --git a/functions/stdio/fputc.c b/functions/stdio/fputc.c index 818d45f..ae998ea 100644 --- a/functions/stdio/fputc.c +++ b/functions/stdio/fputc.c @@ -16,7 +16,7 @@ Returns c if successful, EOF otherwise. If a write error occurs, the error indicator of the stream is set. */ -int fputc( int c, struct _PDCLIB_file_t * stream ) +int fputc_unlocked( int c, struct _PDCLIB_file_t * stream ) { if ( _PDCLIB_prepwrite( stream ) == EOF ) { @@ -34,6 +34,14 @@ int fputc( int c, struct _PDCLIB_file_t * stream ) return c; } +int fputc( int c, struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + int r = fputc_unlocked( c, stream ); + funlockfile( stream ); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/fputs.c b/functions/stdio/fputs.c index 2c5e1fa..0b1e325 100644 --- a/functions/stdio/fputs.c +++ b/functions/stdio/fputs.c @@ -11,7 +11,8 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> -int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +int fputs_unlocked( const char * _PDCLIB_restrict s, + struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { if ( _PDCLIB_prepwrite( stream ) == EOF ) { @@ -45,6 +46,15 @@ int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_rest return 0; } +int fputs( const char * _PDCLIB_restrict s, + struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +{ + flockfile( stream ); + int r = fputs_unlocked( s, stream ); + funlockfile( stream ); + return r; +} + #endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/functions/stdio/fread.c b/functions/stdio/fread.c index 9f7d3fa..1fdc753 100644 --- a/functions/stdio/fread.c +++ b/functions/stdio/fread.c @@ -15,7 +15,9 @@ #include #include -size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +size_t fread_unlocked( void * _PDCLIB_restrict ptr, + size_t size, size_t nmemb, + struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { if ( _PDCLIB_prepread( stream ) == EOF ) { @@ -41,6 +43,16 @@ size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PD return nmemb_i; } +size_t fread( void * _PDCLIB_restrict ptr, + size_t size, size_t nmemb, + struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +{ + flockfile( stream ); + size_t r = fread_unlocked( ptr, size, nmemb, stream ); + funlockfile( stream ); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/freopen.c b/functions/stdio/freopen.c index 682e3c6..7868c95 100644 --- a/functions/stdio/freopen.c +++ b/functions/stdio/freopen.c @@ -14,8 +14,13 @@ #include #include -struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +struct _PDCLIB_file_t * freopen( + const char * _PDCLIB_restrict filename, + const char * _PDCLIB_restrict mode, + struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { + flockfile( stream ); + unsigned int status = stream->status & ( _IONBF | _IOLBF | _IOFBF | _PDCLIB_FREEBUFFER | _PDCLIB_DELONCLOSE ); /* TODO: This function can change wide orientation of a stream */ if ( stream->status & _PDCLIB_FWRITE ) @@ -25,6 +30,7 @@ struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const c if ( ( filename == NULL ) && ( stream->filename == NULL ) ) { /* TODO: Special handling for mode changes on std-streams */ + funlockfile( stream ); return NULL; } _PDCLIB_close( stream->handle ); @@ -49,16 +55,19 @@ struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const c /* Allocate new buffer */ if ( ( stream->filename = (char *)malloc( strlen( filename ) ) ) == NULL ) { + funlockfile( stream ); return NULL; } strcpy( stream->filename, filename ); } if ( ( mode == NULL ) || ( filename[0] == '\0' ) ) { + funlockfile( stream ); return NULL; } if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 ) { + funlockfile( stream ); return NULL; } /* Re-add the flags we saved above */ @@ -69,8 +78,10 @@ struct _PDCLIB_file_t * freopen( const char * _PDCLIB_restrict filename, const c /* TODO: Setting mbstate */ if ( ( stream->handle = _PDCLIB_open( filename, stream->status ) ) == _PDCLIB_NOHANDLE ) { + funlockfile( stream ); return NULL; } + funlockfile( stream ); return stream; } diff --git a/functions/stdio/fscanf.c b/functions/stdio/fscanf.c index 6c0c70b..fbe280b 100644 --- a/functions/stdio/fscanf.c +++ b/functions/stdio/fscanf.c @@ -11,7 +11,19 @@ #ifndef REGTEST -int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) +int fscanf_unlocked( FILE * _PDCLIB_restrict stream, + const char * _PDCLIB_restrict format, ... ) +{ + int rc; + va_list ap; + va_start( ap, format ); + rc = vfscanf_unlocked( stream, format, ap ); + va_end( ap ); + return rc; +} + +int fscanf( FILE * _PDCLIB_restrict stream, + const char * _PDCLIB_restrict format, ... ) { int rc; va_list ap; diff --git a/functions/stdio/fseek.c b/functions/stdio/fseek.c index 8e10c2b..c897325 100644 --- a/functions/stdio/fseek.c +++ b/functions/stdio/fseek.c @@ -12,7 +12,7 @@ #include <_PDCLIB_glue.h> -int fseek( struct _PDCLIB_file_t * stream, long loffset, int whence ) +int fseek_unlocked( struct _PDCLIB_file_t * stream, long loffset, int whence ) { _PDCLIB_int64_t offset = loffset; if ( stream->status & _PDCLIB_FWRITE ) @@ -37,6 +37,14 @@ int fseek( struct _PDCLIB_file_t * stream, long loffset, int whence ) return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF; } +int fseek( struct _PDCLIB_file_t * stream, long loffset, int whence ) +{ + flockfile( stream ); + int r = fseek_unlocked( stream, loffset, whence ); + funlockfile( stream ); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/fsetpos.c b/functions/stdio/fsetpos.c index 2082b48..aa2f8e6 100644 --- a/functions/stdio/fsetpos.c +++ b/functions/stdio/fsetpos.c @@ -11,7 +11,8 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> -int fsetpos( struct _PDCLIB_file_t * stream, const struct _PDCLIB_fpos_t * pos ) +int fsetpos_unlocked( struct _PDCLIB_file_t * stream, + const struct _PDCLIB_fpos_t * pos ) { if ( stream->status & _PDCLIB_FWRITE ) { @@ -29,6 +30,15 @@ int fsetpos( struct _PDCLIB_file_t * stream, const struct _PDCLIB_fpos_t * pos ) return 0; } +int fsetpos( struct _PDCLIB_file_t * stream, + const struct _PDCLIB_fpos_t * pos ) +{ + flockfile( stream ); + int res = fsetpos_unlocked( stream, pos ); + funlockfile( stream ); + return res; +} + #endif #ifdef TEST diff --git a/functions/stdio/ftell.c b/functions/stdio/ftell.c index d39297f..74bb902 100644 --- a/functions/stdio/ftell.c +++ b/functions/stdio/ftell.c @@ -13,9 +13,9 @@ #ifndef REGTEST -long int ftell( struct _PDCLIB_file_t * stream ) +long int ftell_unlocked( struct _PDCLIB_file_t * stream ) { - uint_fast64_t off64 = _PDCLIB_ftell64( stream ); + uint_fast64_t off64 = _PDCLIB_ftell64_unlocked( stream ); if ( off64 > LONG_MAX ) { @@ -26,6 +26,14 @@ long int ftell( struct _PDCLIB_file_t * stream ) return off64; } +long int ftell( struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + long int off = ftell_unlocked( stream ); + funlockfile( stream ); + return off; +} + #endif #ifdef TEST diff --git a/functions/stdio/fwrite.c b/functions/stdio/fwrite.c index 90c8f44..9ab8a0a 100644 --- a/functions/stdio/fwrite.c +++ b/functions/stdio/fwrite.c @@ -17,7 +17,9 @@ //TODO OS(2012-08-01): Ascertain purpose of lineend & potentially remove -size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +size_t fwrite_unlocked( const void * _PDCLIB_restrict ptr, + size_t size, size_t nmemb, + struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { if ( _PDCLIB_prepwrite( stream ) == EOF ) { @@ -88,6 +90,16 @@ size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, str return nmemb_i; } +size_t fwrite( const void * _PDCLIB_restrict ptr, + size_t size, size_t nmemb, + struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +{ + flockfile( stream ); + size_t r = fwrite_unlocked( ptr, size, nmemb, stream ); + funlockfile( stream ); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/getc.c b/functions/stdio/getc.c index a459c22..9338baf 100644 --- a/functions/stdio/getc.c +++ b/functions/stdio/getc.c @@ -10,6 +10,11 @@ #ifndef REGTEST +int getc_unlocked( struct _PDCLIB_file_t * stream ) +{ + return fgetc_unlocked( stream ); +} + int getc( struct _PDCLIB_file_t * stream ) { return fgetc( stream ); diff --git a/functions/stdio/getchar.c b/functions/stdio/getchar.c index 1603728..406abe4 100644 --- a/functions/stdio/getchar.c +++ b/functions/stdio/getchar.c @@ -10,6 +10,12 @@ #ifndef REGTEST +int getchar_unlocked( void ) +{ + return fgetc_unlocked( stdin ); +} + + int getchar( void ) { return fgetc( stdin ); diff --git a/functions/stdio/printf.c b/functions/stdio/printf.c index 122f8df..a4efd6a 100644 --- a/functions/stdio/printf.c +++ b/functions/stdio/printf.c @@ -21,6 +21,16 @@ int printf( const char * _PDCLIB_restrict format, ... ) return rc; } +int printf_unlocked( const char * _PDCLIB_restrict format, ... ) +{ + int rc; + va_list ap; + va_start( ap, format ); + rc = vfprintf_unlocked( stdout, format, ap ); + va_end( ap ); + return rc; +} + #endif #ifdef TEST diff --git a/functions/stdio/putc.c b/functions/stdio/putc.c index 60a4dba..c1f3fc2 100644 --- a/functions/stdio/putc.c +++ b/functions/stdio/putc.c @@ -10,6 +10,12 @@ #ifndef REGTEST +int putc_unlocked( int c, struct _PDCLIB_file_t * stream ) +{ + return fputc_unlocked( c, stream ); +} + + int putc( int c, struct _PDCLIB_file_t * stream ) { return fputc( c, stream ); diff --git a/functions/stdio/putchar.c b/functions/stdio/putchar.c index 4e2d6ae..52063a5 100644 --- a/functions/stdio/putchar.c +++ b/functions/stdio/putchar.c @@ -10,6 +10,11 @@ #ifndef REGTEST +int putchar_unlocked( int c ) +{ + return fputc_unlocked( c, stdout ); +} + int putchar( int c ) { return fputc( c, stdout ); diff --git a/functions/stdio/puts.c b/functions/stdio/puts.c index ec67478..4e55226 100644 --- a/functions/stdio/puts.c +++ b/functions/stdio/puts.c @@ -13,7 +13,7 @@ extern char * _PDCLIB_eol; -int puts( const char * s ) +int puts_unlocked( const char * s ) { if ( _PDCLIB_prepwrite( stdout ) == EOF ) { @@ -42,6 +42,14 @@ int puts( const char * s ) } } +int puts( const char * s ) +{ + flockfile( stdout ); + int r = puts_unlocked( s ); + funlockfile( stdout ); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/scanf.c b/functions/stdio/scanf.c index 41501bc..e11ccbd 100644 --- a/functions/stdio/scanf.c +++ b/functions/stdio/scanf.c @@ -11,6 +11,13 @@ #ifndef REGTEST +int scanf_unlocked( const char * _PDCLIB_restrict format, ... ) +{ + va_list ap; + va_start( ap, format ); + return vfscanf_unlocked( stdin, format, ap ); +} + int scanf( const char * _PDCLIB_restrict format, ... ) { va_list ap; diff --git a/functions/stdio/ungetc.c b/functions/stdio/ungetc.c index 001735b..45b8a92 100644 --- a/functions/stdio/ungetc.c +++ b/functions/stdio/ungetc.c @@ -10,7 +10,7 @@ #ifndef REGTEST -int ungetc( int c, struct _PDCLIB_file_t * stream ) +int ungetc_unlocked( int c, struct _PDCLIB_file_t * stream ) { if ( c == EOF || stream->ungetidx == _PDCLIB_UNGETCBUFSIZE ) { @@ -19,6 +19,14 @@ int ungetc( int c, struct _PDCLIB_file_t * stream ) return stream->ungetbuf[stream->ungetidx++] = (unsigned char) c; } +int ungetc( int c, struct _PDCLIB_file_t * stream ) +{ + flockfile( stream ); + int r = ungetc_unlocked( c, stream ); + funlockfile( stream); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/vfprintf.c b/functions/stdio/vfprintf.c index cc6e7a6..ffb4bbe 100644 --- a/functions/stdio/vfprintf.c +++ b/functions/stdio/vfprintf.c @@ -12,7 +12,9 @@ #ifndef REGTEST -int vfprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, va_list arg ) +int vfprintf_unlocked( struct _PDCLIB_file_t * _PDCLIB_restrict stream, + const char * _PDCLIB_restrict format, + va_list arg ) { /* TODO: This function should interpret format as multibyte characters. */ struct _PDCLIB_status_t status; @@ -46,6 +48,16 @@ int vfprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDC return status.i; } +int vfprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, + const char * _PDCLIB_restrict format, + va_list arg ) +{ + flockfile( stream ); + int r = vfprintf_unlocked( stream, format, arg ); + funlockfile( stream ); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/vfscanf.c b/functions/stdio/vfscanf.c index b9a6896..da365bd 100644 --- a/functions/stdio/vfscanf.c +++ b/functions/stdio/vfscanf.c @@ -12,7 +12,9 @@ #ifndef REGTEST -int vfscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, va_list arg ) +int vfscanf_unlocked( FILE * _PDCLIB_restrict stream, + const char * _PDCLIB_restrict format, + va_list arg ) { /* TODO: This function should interpret format as multibyte characters. */ struct _PDCLIB_status_t status; @@ -85,6 +87,16 @@ int vfscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict forma return status.n; } +int vfscanf( FILE * _PDCLIB_restrict stream, + const char * _PDCLIB_restrict format, + va_list arg ) +{ + flockfile( stream ); + int r = vfscanf_unlocked( stream, format, arg ); + funlockfile( stream ); + return r; +} + #endif #ifdef TEST diff --git a/functions/stdio/vprintf.c b/functions/stdio/vprintf.c index 48ad557..6eba229 100644 --- a/functions/stdio/vprintf.c +++ b/functions/stdio/vprintf.c @@ -11,6 +11,12 @@ #ifndef REGTEST +int vprintf_unlocked( const char * _PDCLIB_restrict format, + _PDCLIB_va_list arg ) +{ + return vfprintf_unlocked( stdout, format, arg ); +} + int vprintf( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) { return vfprintf( stdout, format, arg ); diff --git a/functions/stdio/vscanf.c b/functions/stdio/vscanf.c index 0b9189d..dc3a236 100644 --- a/functions/stdio/vscanf.c +++ b/functions/stdio/vscanf.c @@ -11,6 +11,11 @@ #ifndef REGTEST +int vscanf_unlocked( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) +{ + return vfscanf_unlocked( stdin, format, arg ); +} + int vscanf( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) { return vfscanf( stdin, format, arg ); diff --git a/functions/stdio/vsnprintf.c b/functions/stdio/vsnprintf.c index 06536d1..64a0651 100644 --- a/functions/stdio/vsnprintf.c +++ b/functions/stdio/vsnprintf.c @@ -11,7 +11,10 @@ #ifndef REGTEST -int vsnprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) +int vsnprintf( char * _PDCLIB_restrict s, + size_t n, + const char * _PDCLIB_restrict format, + _PDCLIB_va_list arg ) { /* TODO: This function should interpret format as multibyte characters. */ struct _PDCLIB_status_t status; diff --git a/functions/stdio/vsprintf.c b/functions/stdio/vsprintf.c index 3dfe3c2..5f08688 100644 --- a/functions/stdio/vsprintf.c +++ b/functions/stdio/vsprintf.c @@ -12,7 +12,9 @@ #ifndef REGTEST -int vsprintf( char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, va_list arg ) +int vsprintf( char * _PDCLIB_restrict s, + const char * _PDCLIB_restrict format, + va_list arg ) { return vsnprintf( s, SIZE_MAX, format, arg ); /* TODO: Replace with a non-checking call */ } diff --git a/functions/stdio/vsscanf.c b/functions/stdio/vsscanf.c index 4300a3e..99b8bad 100644 --- a/functions/stdio/vsscanf.c +++ b/functions/stdio/vsscanf.c @@ -12,7 +12,9 @@ #ifndef REGTEST #include -int vsscanf( const char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, va_list arg ) +int vsscanf( const char * _PDCLIB_restrict s, + const char * _PDCLIB_restrict format, + va_list arg ) { /* TODO: This function should interpret format as multibyte characters. */ struct _PDCLIB_status_t status; diff --git a/includes/stdio.h b/includes/stdio.h index acc0012..79a54bd 100644 --- a/includes/stdio.h +++ b/includes/stdio.h @@ -788,7 +788,6 @@ int fsetpos( FILE * stream, const fpos_t * pos ) _PDCLIB_nothrow; TODO: Implementation-defined errno setting for ftell(). */ long int ftell( FILE * stream ) _PDCLIB_nothrow; -_PDCLIB_uint_fast64_t _PDCLIB_ftell64( FILE * stream ) _PDCLIB_nothrow; /* Equivalent to (void)fseek( stream, 0L, SEEK_SET ), except that the error indicator for the stream is also cleared. @@ -861,5 +860,30 @@ char *fgets_unlocked(char *s, int n, FILE *stream); int fputs_unlocked(const char *s, FILE *stream); #endif +#if defined(_PDCLIB_EXTENSIONS) +int fgetpos_unlocked( FILE * _PDCLIB_restrict stream, fpos_t * _PDCLIB_restrict pos ) _PDCLIB_nothrow; +int fsetpos_unlocked( FILE * stream, const fpos_t * pos ) _PDCLIB_nothrow; +long int ftell_unlocked( FILE * stream ) _PDCLIB_nothrow; +int fseek_unlocked( FILE * stream, long int offset, int whence ) _PDCLIB_nothrow; + +int puts_unlocked( const char * s ) _PDCLIB_nothrow; +int ungetc_unlocked( int c, FILE * stream ) _PDCLIB_nothrow; + + +int printf_unlocked( const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow; +int vprintf_unlocked( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow; +int fprintf_unlocked( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow; +int vfprintf_unlocked( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow; +int scanf_unlocked( const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow; +int vscanf_unlocked( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow; +int fscanf_unlocked( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) _PDCLIB_nothrow; +int vfscanf_unlocked( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) _PDCLIB_nothrow; + + +// Todo: remove prefix? +_PDCLIB_uint_fast64_t _PDCLIB_ftell64( FILE * stream ) _PDCLIB_nothrow; +_PDCLIB_uint_fast64_t _PDCLIB_ftell64_unlocked( FILE * stream ) _PDCLIB_nothrow; +#endif + _PDCLIB_END_EXTERN_C #endif -- 2.40.0