From 219271fd548949abce8bd75c34dd42e519418fc4 Mon Sep 17 00:00:00 2001 From: Owen Shepherd Date: Mon, 13 Aug 2012 21:11:29 +0100 Subject: [PATCH] * Test cleanups: surround the code for all functions by #ifndef REGTEST * Don't REGTEST internal functions (it doesn't make sense) * : add _PDCLIB_fdopen, which opens a file based upon its _PDCLIB_fd_t file descriptor TODO: Should this be public or not? POSIX fdopen demonstrates utility TODO: Can we stop storing the file name? Is that useful? * fopen: rewrite based upon _PDCLIB_fdopen to consolidate code * strndup: don't REGTEST - mingw doesn't implement. * : conditional definition of wchar_t for * : Add 4 simple functions for dependencies * <_PDCLIB_aux.h>: rename _PDCLIB_ALL to _PDCLIB_EXTENSIONS * platform/example/functions/stdio/tmpfile.c: return NULL, not 1 * Build system: SubInclude the platform directory to enable the building of support routines (e.g. app startup files) * Test system: If $(CRT0) is set, link the contents of it into the tests. This enables the platform code to provide startup files. --- Jamfile | 7 +- Jamrules | 4 +- functions/_PDCLIB/atomax.c | 4 + functions/_PDCLIB/closeall.c | 2 + functions/_PDCLIB/digits.c | 4 + functions/_PDCLIB/errno.c | 3 +- functions/_PDCLIB/filemode.c | 4 + functions/_PDCLIB/prepread.c | 2 + functions/_PDCLIB/prepwrite.c | 2 + functions/_PDCLIB/print.c | 8 ++ functions/_PDCLIB/scan.c | 8 +- functions/_PDCLIB/strtox_main.c | 6 +- functions/_PDCLIB/strtox_prelim.c | 5 +- functions/stdio/_PDCLIB_fdopen.c | 92 ++++++++++++++++++++++ functions/stdio/fopen.c | 64 +++------------ functions/string/strndup.c | 5 +- functions/wchar/wcschr.c | 32 ++++++++ functions/wchar/wcscpy.c | 33 ++++++++ functions/wchar/wcslen.c | 29 +++++++ functions/wchar/wcsrchr.c | 35 ++++++++ includes/stddef.h | 3 + includes/stdio.h | 31 +++++++- includes/wchar.h | 35 ++++++++ internals/_PDCLIB_aux.h | 2 +- opt/nothread/call_once.c | 8 +- opt/nothread/cnd_init.c | 2 + opt/nothread/cnd_signal.c | 2 + opt/nothread/cnd_wait.c | 2 + opt/nothread/mtx_destroy.c | 2 + opt/nothread/mtx_init.c | 2 + opt/nothread/mtx_lock.c | 3 +- opt/nothread/mtx_timedlock.c | 2 + opt/nothread/mtx_trylock.c | 2 + opt/nothread/mtx_unlock.c | 2 + opt/nothread/thrd_yield.c | 2 + opt/nothread/tss_create.c | 2 + opt/nothread/tss_delete.c | 2 + opt/nothread/tss_get.c | 6 ++ opt/nothread/tss_set.c | 2 + platform/example/functions/stdio/tmpfile.c | 2 +- 40 files changed, 395 insertions(+), 68 deletions(-) create mode 100644 functions/stdio/_PDCLIB_fdopen.c create mode 100644 functions/wchar/wcschr.c create mode 100644 functions/wchar/wcscpy.c create mode 100644 functions/wchar/wcslen.c create mode 100644 functions/wchar/wcsrchr.c create mode 100644 includes/wchar.h diff --git a/Jamfile b/Jamfile index 971e21d..99441d9 100644 --- a/Jamfile +++ b/Jamfile @@ -1,4 +1,8 @@ SubDir PDCLIB_TOP ; +if $(PDCLIB_PLATFORM) { + SubInclude PDCLIB_TOP platform $(PDCLIB_PLATFORM) ; + SubDir PDCLIB_TOP ; +} PDCLibConfig ; PDCLIB_SOURCES = [ RecursiveGlob $(PDCLIB_TOP) : [ FDirName functions ] : *.c ] ; @@ -25,11 +29,12 @@ if ! $(PDCLIB_NO_TEST) { Object $(testfile).o : $(file) ; Object $(regtestfile).o : $(file) ; - MainFromObjects $(testfile) : $(testfile).o ; + MainFromObjects $(testfile) : $(testfile).o $(CRT0) ; MainFromObjects $(regtestfile) : $(regtestfile).o ; CCFLAGS on $(testfile).o += -DTEST $(PDCLIB_TEST_CCFLAGS) ; CCFLAGS on $(regtestfile).o += -DTEST -DREGTEST $(PDCLIB_REGTEST_CCFLAGS) ; + CCHDRS on $(regtestfile).o = [ FIncludes [ FDirName testing ] ] ; LINKFLAGS on $(testfile)$(SUFEXE) += $(PDCLIB_TEST_LINKFLAGS) ; LINKFLAGS on $(regtestfile)$(SUFEXE) += $(PDCLIB_REGTEST_LINKFLAGS) ; diff --git a/Jamrules b/Jamrules index 8657cc9..dd0e406 100644 --- a/Jamrules +++ b/Jamrules @@ -32,14 +32,14 @@ if ! $(PDCLIB_HAVE_PLATFORM) && ! $(PDCLIB_PLATFORM) { -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes ; PDCLIB_CCFLAGS = -ffreestanding - -nostdinc + #-nostdinc -std=c11 -g -D_PDCLIB_BUILD $(PDCLIB_WARNINGS) ; PDCLIB_C++FLAGS = -ffreestanding - -nostdinc + #-nostdinc -std=c++11 -g -D_PDCLIB_BUILD diff --git a/functions/_PDCLIB/atomax.c b/functions/_PDCLIB/atomax.c index 45095e7..58a8e58 100644 --- a/functions/_PDCLIB/atomax.c +++ b/functions/_PDCLIB/atomax.c @@ -6,6 +6,7 @@ Permission is granted to use, modify, and / or redistribute at will. */ +#ifndef REGTEST #include <_PDCLIB_int.h> #include #include @@ -26,16 +27,19 @@ _PDCLIB_intmax_t _PDCLIB_atomax( const char * s ) } return ( sign == '+' ) ? rc : -rc; } +#endif #ifdef TEST #include <_PDCLIB_test.h> int main( void ) { +#ifndef REGTEST /* basic functionality */ TESTCASE( _PDCLIB_atomax( "123" ) == 123 ); /* testing skipping of leading whitespace and trailing garbage */ TESTCASE( _PDCLIB_atomax( " \n\v\t\f123xyz" ) == 123 ); +#endif return TEST_RESULTS; } diff --git a/functions/_PDCLIB/closeall.c b/functions/_PDCLIB/closeall.c index 71f6007..5086d0d 100644 --- a/functions/_PDCLIB/closeall.c +++ b/functions/_PDCLIB/closeall.c @@ -8,6 +8,7 @@ #include +#ifndef REGTEST extern struct _PDCLIB_file_t * _PDCLIB_filelist; void _PDCLIB_closeall( void ) @@ -21,6 +22,7 @@ void _PDCLIB_closeall( void ) stream = next; } } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/functions/_PDCLIB/digits.c b/functions/_PDCLIB/digits.c index ad41e5d..52a7ae3 100644 --- a/functions/_PDCLIB/digits.c +++ b/functions/_PDCLIB/digits.c @@ -6,7 +6,9 @@ Permission is granted to use, modify, and / or redistribute at will. */ +#ifndef REGTEST #include <_PDCLIB_int.h> +#endif char _PDCLIB_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; @@ -20,8 +22,10 @@ char _PDCLIB_Xdigits[] = "0123456789ABCDEF"; int main( void ) { +#ifndef REGTEST TESTCASE( strcmp( _PDCLIB_digits, "0123456789abcdefghijklmnopqrstuvwxyz" ) == 0 ); TESTCASE( strcmp( _PDCLIB_Xdigits, "0123456789ABCDEF" ) == 0 ); +#endif return TEST_RESULTS; } diff --git a/functions/_PDCLIB/errno.c b/functions/_PDCLIB/errno.c index 7a69080..4facee5 100644 --- a/functions/_PDCLIB/errno.c +++ b/functions/_PDCLIB/errno.c @@ -6,9 +6,8 @@ Permission is granted to use, modify, and / or redistribute at will. */ -#include <_PDCLIB_int.h> - #ifndef REGTEST +#include <_PDCLIB_int.h> int _PDCLIB_errno = 0; diff --git a/functions/_PDCLIB/filemode.c b/functions/_PDCLIB/filemode.c index 0e293ab..ded5408 100644 --- a/functions/_PDCLIB/filemode.c +++ b/functions/_PDCLIB/filemode.c @@ -8,6 +8,7 @@ #include +#ifndef REGTEST /* Helper function that parses the C-style mode string passed to fopen() into the PDCLib flags FREAD, FWRITE, FAPPEND, FRW (read-write) and FBIN (binary mode). @@ -53,12 +54,14 @@ unsigned int _PDCLIB_filemode( char const * const mode ) /* Longer than three chars - invalid. */ return 0; } +#endif #ifdef TEST #include <_PDCLIB_test.h> int main( void ) { +#ifndef REGTEST TESTCASE( _PDCLIB_filemode( "r" ) == _PDCLIB_FREAD ); TESTCASE( _PDCLIB_filemode( "w" ) == _PDCLIB_FWRITE ); TESTCASE( _PDCLIB_filemode( "a" ) == ( _PDCLIB_FAPPEND | _PDCLIB_FWRITE ) ); @@ -78,6 +81,7 @@ int main( void ) TESTCASE( _PDCLIB_filemode( "r++" ) == 0 ); TESTCASE( _PDCLIB_filemode( "wbb" ) == 0 ); TESTCASE( _PDCLIB_filemode( "a+bx" ) == 0 ); +#endif return TEST_RESULTS; } diff --git a/functions/_PDCLIB/prepread.c b/functions/_PDCLIB/prepread.c index 6ce38d9..cf1c2df 100644 --- a/functions/_PDCLIB/prepread.c +++ b/functions/_PDCLIB/prepread.c @@ -9,6 +9,7 @@ #include #include +#ifndef REGTEST #include <_PDCLIB_glue.h> int _PDCLIB_prepread( struct _PDCLIB_file_t * stream ) @@ -35,6 +36,7 @@ int _PDCLIB_prepread( struct _PDCLIB_file_t * stream ) return 0; } } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/functions/_PDCLIB/prepwrite.c b/functions/_PDCLIB/prepwrite.c index 0b31529..1b1c4b5 100644 --- a/functions/_PDCLIB/prepwrite.c +++ b/functions/_PDCLIB/prepwrite.c @@ -9,6 +9,7 @@ #include #include +#ifndef REGTEST int _PDCLIB_prepwrite( struct _PDCLIB_file_t * stream ) { if ( ( stream->bufidx < stream->bufend ) || ( stream->ungetidx > 0 ) || @@ -26,6 +27,7 @@ int _PDCLIB_prepwrite( struct _PDCLIB_file_t * stream ) stream->status |= _PDCLIB_FWRITE | _PDCLIB_BYTESTREAM; return 0; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/functions/_PDCLIB/print.c b/functions/_PDCLIB/print.c index 72db66b..124496c 100644 --- a/functions/_PDCLIB/print.c +++ b/functions/_PDCLIB/print.c @@ -12,6 +12,8 @@ #include #include +#ifndef REGTEST + /* Using an integer's bits as flags for both the conversion flags and length modifiers. */ @@ -506,12 +508,15 @@ const char * _PDCLIB_print( const char * spec, struct _PDCLIB_status_t * status return ++spec; } +#endif + #ifdef TEST #define _PDCLIB_FILEID "_PDCLIB/print.c" #define _PDCLIB_STRINGIO #include <_PDCLIB_test.h> +#ifndef REGTEST static int testprintf( char * buffer, const char * format, ... ) { /* Members: base, flags, n, i, current, s, width, prec, stream, arg */ @@ -535,13 +540,16 @@ static int testprintf( char * buffer, const char * format, ... ) va_end( status.arg ); return status.i; } +#endif #define TEST_CONVERSION_ONLY int main( void ) { +#ifndef REGTEST char target[100]; #include "printf_testcases.h" +#endif return TEST_RESULTS; } diff --git a/functions/_PDCLIB/scan.c b/functions/_PDCLIB/scan.c index 6910aef..57f9831 100644 --- a/functions/_PDCLIB/scan.c +++ b/functions/_PDCLIB/scan.c @@ -16,6 +16,8 @@ #include #include +#ifndef REGTEST + /* Using an integer's bits as flags for both the conversion flags and length modifiers. */ @@ -588,7 +590,7 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status ) /* TODO: Floats. */ return NULL; } - +#endif #ifdef TEST #define _PDCLIB_FILEID "_PDCLIB/scan.c" @@ -596,6 +598,7 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status ) #include <_PDCLIB_test.h> +#ifndef REGTEST static int testscanf( char const * s, char const * format, ... ) { struct _PDCLIB_status_t status; @@ -612,13 +615,16 @@ static int testscanf( char const * s, char const * format, ... ) va_end( status.arg ); return status.n; } +#endif #define TEST_CONVERSION_ONLY int main( void ) { +#ifndef REGTEST char source[100]; #include "scanf_testcases.h" +#endif return TEST_RESULTS; } diff --git a/functions/_PDCLIB/strtox_main.c b/functions/_PDCLIB/strtox_main.c index 1556ec4..86b6d42 100644 --- a/functions/_PDCLIB/strtox_main.c +++ b/functions/_PDCLIB/strtox_main.c @@ -6,12 +6,13 @@ Permission is granted to use, modify, and / or redistribute at will. */ -#include <_PDCLIB_int.h> #include #include #include #include +#ifndef REGTEST +#include <_PDCLIB_int.h> _PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, unsigned int base, uintmax_t error, uintmax_t limval, int limdigit, char * sign ) { _PDCLIB_uintmax_t rc = 0; @@ -43,6 +44,7 @@ _PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, unsigned int base, uintm } return rc; } +#endif #ifdef TEST #include <_PDCLIB_test.h> @@ -50,6 +52,7 @@ _PDCLIB_uintmax_t _PDCLIB_strtox_main( const char ** p, unsigned int base, uintm int main( void ) { +#ifndef REGTEST const char * p; char test[] = "123_"; char fail[] = "xxx"; @@ -77,6 +80,7 @@ int main( void ) sign = '-'; TESTCASE( _PDCLIB_strtox_main( &p, 10u, (uintmax_t)999, (uintmax_t)99, 8, &sign ) == 0 ); TESTCASE( p == NULL ); +#endif return TEST_RESULTS; } diff --git a/functions/_PDCLIB/strtox_prelim.c b/functions/_PDCLIB/strtox_prelim.c index 29b7919..7be4030 100644 --- a/functions/_PDCLIB/strtox_prelim.c +++ b/functions/_PDCLIB/strtox_prelim.c @@ -9,7 +9,7 @@ #include #include #include - +#ifndef REGTEST const char * _PDCLIB_strtox_prelim( const char * p, char * sign, int * base ) { /* skipping leading whitespace */ @@ -50,12 +50,14 @@ const char * _PDCLIB_strtox_prelim( const char * p, char * sign, int * base ) } return ( ( *base >= 2 ) && ( *base <= 36 ) ) ? p : NULL; } +#endif #ifdef TEST #include <_PDCLIB_test.h> int main( void ) { +#ifndef REGTEST int base = 0; char sign = '\0'; char test1[] = " 123"; @@ -83,6 +85,7 @@ int main( void ) TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL ); base = 37; TESTCASE( _PDCLIB_strtox_prelim( test3, &sign, &base ) == NULL ); +#endif return TEST_RESULTS; } diff --git a/functions/stdio/_PDCLIB_fdopen.c b/functions/stdio/_PDCLIB_fdopen.c new file mode 100644 index 0000000..8556327 --- /dev/null +++ b/functions/stdio/_PDCLIB_fdopen.c @@ -0,0 +1,92 @@ +/* $Id$ */ + +/* _PDCLIB_fdopen( _PDCLIB_fd_t fd, const char * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#include + +#ifndef REGTEST +#include <_PDCLIB_glue.h> +#include + +extern struct _PDCLIB_file_t * _PDCLIB_filelist; + +struct _PDCLIB_file_t * _PDCLIB_fdopen( _PDCLIB_fd_t fd, + int mode, + const char * _PDCLIB_restrict filename ) +{ + size_t filename_len; + struct _PDCLIB_file_t * rc; + if ( mode == NULL ) + { + /* Mode invalid */ + return NULL; + } + /* To reduce the number of malloc calls, all data fields are concatenated: + * the FILE structure itself, + * ungetc buffer, + * filename buffer, + * data buffer. + Data buffer comes last because it might change in size ( setvbuf() ). + */ + filename_len = filename ? strlen( filename ) + 1 : 1; + if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) + _PDCLIB_UNGETCBUFSIZE + filename_len + BUFSIZ ) ) == NULL ) + { + /* no memory */ + return NULL; + } + rc->status = mode; + rc->handle = fd; + /* Setting pointers into the memory block allocated above */ + rc->ungetbuf = (unsigned char *)rc + sizeof( struct _PDCLIB_file_t ); + rc->filename = (char *)rc->ungetbuf + _PDCLIB_UNGETCBUFSIZE; + rc->buffer = rc->filename + filename_len; + /* Copying filename to FILE structure */ + if(filename) strcpy( rc->filename, filename ); + /* Initializing the rest of the structure */ + rc->bufsize = BUFSIZ; + rc->bufidx = 0; + rc->ungetidx = 0; + /* Setting buffer to _IOLBF because "when opened, a stream is fully + buffered if and only if it can be determined not to refer to an + interactive device." + */ + rc->status |= _IOLBF; + /* TODO: Setting mbstate */ + /* Adding to list of open files */ + rc->next = _PDCLIB_filelist; + _PDCLIB_filelist = rc; + return rc; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + /* Some of the tests are not executed for regression tests, as the libc on + my system is at once less forgiving (segfaults on mode NULL) and more + forgiving (accepts undefined modes). + */ + FILE * fh; + remove( testfile ); + TESTCASE_NOREG( fopen( NULL, NULL ) == NULL ); + TESTCASE( fopen( NULL, "w" ) == NULL ); + TESTCASE_NOREG( fopen( "", NULL ) == NULL ); + TESTCASE( fopen( "", "w" ) == NULL ); + TESTCASE( fopen( "foo", "" ) == NULL ); + TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */ + TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */ + TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL ); + TESTCASE( fclose( fh ) == 0 ); + TESTCASE( remove( testfile ) == 0 ); + return TEST_RESULTS; +} + +#endif diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index 7c0b81a..18b7b3a 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -12,64 +12,26 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> #include +#include extern struct _PDCLIB_file_t * _PDCLIB_filelist; -struct _PDCLIB_file_t * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode ) +FILE * fopen( const char * _PDCLIB_restrict filename, + const char * _PDCLIB_restrict mode ) { - struct _PDCLIB_file_t * rc; - size_t filename_len; - if ( mode == NULL || filename == NULL || filename[0] == '\0' ) - { - /* Mode or filename invalid */ + int imode = _PDCLIB_filemode( mode ); + _PDCLIB_fd_t fd = _PDCLIB_open( filename, imode ); + if(fd == _PDCLIB_NOHANDLE) { return NULL; } - /* To reduce the number of malloc calls, all data fields are concatenated: - * the FILE structure itself, - * ungetc buffer, - * filename buffer, - * data buffer. - Data buffer comes last because it might change in size ( setvbuf() ). - */ - filename_len = strlen( filename ) + 1; - if ( ( rc = calloc( 1, sizeof( struct _PDCLIB_file_t ) + _PDCLIB_UNGETCBUFSIZE + filename_len + BUFSIZ ) ) == NULL ) - { - /* no memory */ - return NULL; - } - if ( ( rc->status = _PDCLIB_filemode( mode ) ) == 0 ) - { - /* invalid mode */ - free( rc ); - return NULL; - } - rc->handle = _PDCLIB_open( filename, rc->status ); - if ( rc->handle == _PDCLIB_NOHANDLE ) - { - /* OS open() failed */ - free( rc ); - return NULL; + + FILE * f = _PDCLIB_fdopen( fd, imode, filename ); + if(!f) { + int saveErrno = errno; + _PDCLIB_close( fd ); + errno = saveErrno; } - /* Setting pointers into the memory block allocated above */ - rc->ungetbuf = (unsigned char *)rc + sizeof( struct _PDCLIB_file_t ); - rc->filename = (char *)rc->ungetbuf + _PDCLIB_UNGETCBUFSIZE; - rc->buffer = rc->filename + filename_len; - /* Copying filename to FILE structure */ - strcpy( rc->filename, filename ); - /* Initializing the rest of the structure */ - rc->bufsize = BUFSIZ; - rc->bufidx = 0; - rc->ungetidx = 0; - /* Setting buffer to _IOLBF because "when opened, a stream is fully - buffered if and only if it can be determined not to refer to an - interactive device." - */ - rc->status |= _IOLBF; - /* TODO: Setting mbstate */ - /* Adding to list of open files */ - rc->next = _PDCLIB_filelist; - _PDCLIB_filelist = rc; - return rc; + return f; } #endif diff --git a/functions/string/strndup.c b/functions/string/strndup.c index e25c7ed..e50f419 100644 --- a/functions/string/strndup.c +++ b/functions/string/strndup.c @@ -31,6 +31,8 @@ char *strndup( const char * s, size_t len ) int main( void ) { +#ifndef REGTEST + /* Missing on Windows. Maybe use conditionals? */ const char *teststr = "Hello, world"; const char *teststr2 = "\xFE\x8C\n"; char *testres, *testres2; @@ -49,7 +51,8 @@ int main( void ) TESTCASE(strcmp(testres2, teststr2) == 0); free(testres); free(testres2); - +#endif + return TEST_RESULTS; } diff --git a/functions/wchar/wcschr.c b/functions/wchar/wcschr.c new file mode 100644 index 0000000..51d20a4 --- /dev/null +++ b/functions/wchar/wcschr.c @@ -0,0 +1,32 @@ +/* wcschr( const wchar_t *, wchar_t ); + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#include + +#ifndef REGTEST + +wchar_t *wcschr(const wchar_t * haystack, wchar_t needle) +{ + while(*haystack) { + if(*haystack == needle) return (wchar_t*) haystack; + haystack++; + } + return NULL; +} + + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + return TEST_RESULTS; +} + +#endif diff --git a/functions/wchar/wcscpy.c b/functions/wchar/wcscpy.c new file mode 100644 index 0000000..02394ee --- /dev/null +++ b/functions/wchar/wcscpy.c @@ -0,0 +1,33 @@ +/* wchar_t * wcscpy( wchar_t restrict *, const wchar_t restrict * ); + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +#ifndef REGTEST + +wchar_t *wcscpy( wchar_t * _PDCLIB_restrict dest, + const wchar_t * _PDCLIB_restrict src) +{ + wchar_t * rv = dest; + while(*src) { + *(dest++) = *(src++); + } + + return rv; +} + + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + return TEST_RESULTS; +} + +#endif diff --git a/functions/wchar/wcslen.c b/functions/wchar/wcslen.c new file mode 100644 index 0000000..d5d878d --- /dev/null +++ b/functions/wchar/wcslen.c @@ -0,0 +1,29 @@ +/* wcslen( const wchar_t * ); + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +#ifndef REGTEST + +size_t wcslen( const wchar_t * str ) +{ + size_t n = 0; + while(*(str++)) n++; + return n; +} + + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + return TEST_RESULTS; +} + +#endif diff --git a/functions/wchar/wcsrchr.c b/functions/wchar/wcsrchr.c new file mode 100644 index 0000000..396da4f --- /dev/null +++ b/functions/wchar/wcsrchr.c @@ -0,0 +1,35 @@ +/* $Id$ */ + +/* wcsrchr( const wchar_t *, wchar_t ); + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#include + +#ifndef REGTEST + +wchar_t *wcsrchr(const wchar_t * haystack, wchar_t needle) +{ + wchar_t *found = NULL; + while(*haystack) { + if(*haystack == needle) found = haystack; + haystack++; + } + return found; +} + + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + return TEST_RESULTS; +} + +#endif diff --git a/includes/stddef.h b/includes/stddef.h index 6d8c03a..1d5e3af 100644 --- a/includes/stddef.h +++ b/includes/stddef.h @@ -20,8 +20,11 @@ typedef _PDCLIB_size_t size_t; #endif #ifndef __cplusplus +#ifndef _PDCLIB_WCHAR_T_DEFINED +#define _PDCLIB_WCHAR_T_DEFINED _PDCLIB_WCHAR_T_DEFINED typedef _PDCLIB_wchar_t wchar_t; #endif +#endif #ifndef _PDCLIB_NULL_DEFINED #define _PDCLIB_NULL_DEFINED _PDCLIB_NULL_DEFINED diff --git a/includes/stdio.h b/includes/stdio.h index 1d7c0bf..12b7f0a 100644 --- a/includes/stdio.h +++ b/includes/stdio.h @@ -36,10 +36,28 @@ typedef struct _PDCLIB_file_t FILE; #define L_tmpnam _PDCLIB_L_tmpnam #define TMP_MAX _PDCLIB_TMP_MAX -/* See fseek(), third argument */ -#define SEEK_CUR _PDCLIB_SEEK_CUR -#define SEEK_END _PDCLIB_SEEK_END -#define SEEK_SET _PDCLIB_SEEK_SET +/* See fseek(), third argument + * + * Some system headers (e.g. windows) also define the SEEK_* values. Check for + * this and validate that they're the same value + */ +#if !defined(SEEK_CUR) + #define SEEK_CUR _PDCLIB_SEEK_CUR +#elif SEEK_CUR != _PDCLIB_SEEK_CUR + #error SEEK_CUR != _PDCLIB_SEEK_CUR +#endif + +#if !defined(SEEK_END) + #define SEEK_END _PDCLIB_SEEK_END +#elif SEEK_END != _PDCLIB_SEEK_END + #error SEEK_END != _PDCLIB_SEEK_END +#endif + +#if !defined(SEEK_SET) + #define SEEK_SET _PDCLIB_SEEK_SET +#elif SEEK_SET != _PDCLIB_SEEK_SET + #error SEEK_SET != _PDCLIB_SEEK_SET +#endif extern FILE * stdin; extern FILE * stdout; @@ -155,6 +173,11 @@ int fflush( FILE * stream ); */ FILE * fopen( const char * _PDCLIB_restrict filename, const char * _PDCLIB_restrict mode ); +/* Creates a stream connected to the file descriptor \p fd with mode \p mode. + Mode must match the mode with which the file descriptor was opened. +*/ +FILE * _PDCLIB_fdopen( _PDCLIB_fd_t fd, int mode, const char* filename ); + /* Close any file currently associated with the given stream. Open the file identified by the given filename with the given mode (equivalent to fopen()), and associate it with the given stream. If filename is a NULL pointer, diff --git a/includes/wchar.h b/includes/wchar.h new file mode 100644 index 0000000..eab2984 --- /dev/null +++ b/includes/wchar.h @@ -0,0 +1,35 @@ +/* 7. + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + + +#ifndef _PDCLIB_WCHAR_H +#define _PDCLIB_WCHAR_H +#include <_PDCLIB_int.h> +_PDCLIB_BEGIN_EXTERN_C +/* This is VASTLY incomplete. Functions being implemented as required by other + portions of the library + */ + +#ifndef _PDCLIB_SIZE_T_DEFINED +#define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED +typedef _PDCLIB_size_t size_t; +#endif + +#ifndef __cplusplus +#ifndef _PDCLIB_WCHAR_T_DEFINED +#define _PDCLIB_WCHAR_T_DEFINED _PDCLIB_WCHAR_T_DEFINED +typedef _PDCLIB_wchar_t wchar_t; +#endif +#endif + +wchar_t *wcschr( const wchar_t * haystack, wchar_t needle ); +wchar_t *wcsrchr( const wchar_t * haystack, wchar_t needle ); +size_t wcslen( const wchar_t * string); +wchar_t *wcscpy( wchar_t * _PDCLIB_restrict dest, + const wchar_t * _PDCLIB_restrict src); + +_PDCLIB_END_EXTERN_C +#endif diff --git a/internals/_PDCLIB_aux.h b/internals/_PDCLIB_aux.h index 8990ed8..e7e908a 100644 --- a/internals/_PDCLIB_aux.h +++ b/internals/_PDCLIB_aux.h @@ -194,7 +194,7 @@ #define _PDCLIB_CXX_MAX(max) _PDCLIB_CXX_MINMAX(0, max) #define _PDCLIB_XOPEN_MAX(max) _PDCLIB_XOPEN_MINMAX(0, max) #define _PDCLIB_POSIX_MAX(max) _PDCLIB_POSIX_MINMAX(0, max) -#if defined(_PDCLIB_ALL) || defined(_PDCLIB_BUILD) +#if defined(_PDCLIB_EXTENSIONS) || defined(_PDCLIB_BUILD) #define _PDCLIB_C_MINMAX(min, max) 1 #define _PDCLIB_CXX_MINMAX(min, max) 1 #define _PDCLIB_POSIX_MINMAX(min, max) 1 diff --git a/opt/nothread/call_once.c b/opt/nothread/call_once.c index be1ed2e..8320170 100644 --- a/opt/nothread/call_once.c +++ b/opt/nothread/call_once.c @@ -1,3 +1,4 @@ +#ifndef REGTEST #include void _PDCLIB_call_once(_PDCLIB_once_flag *flag, void (*func)(void)) @@ -7,20 +8,24 @@ void _PDCLIB_call_once(_PDCLIB_once_flag *flag, void (*func)(void)) *flag = _PDCLIB_ONCE_FLAG_DONE; } } +#endif #ifdef TEST #include <_PDCLIB_test.h> +#ifndef REGTEST static int count = 0; -once_flag once = ONCE_FLAG_INIT; +static once_flag once = ONCE_FLAG_INIT; static void do_once(void) { count++; } +#endif int main( void ) { +#ifndef REGTEST TESTCASE(count == 0); call_once(&once, do_once); TESTCASE(count == 1); @@ -28,6 +33,7 @@ int main( void ) TESTCASE(count == 1); do_once(); TESTCASE(count == 2); +#endif return TEST_RESULTS; } diff --git a/opt/nothread/cnd_init.c b/opt/nothread/cnd_init.c index 379f28f..2d3fc7e 100644 --- a/opt/nothread/cnd_init.c +++ b/opt/nothread/cnd_init.c @@ -1,3 +1,4 @@ +#ifndef REGTEST #include int cnd_init(cnd_t *cond) @@ -5,6 +6,7 @@ int cnd_init(cnd_t *cond) /* does nothing */ return thrd_success; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/cnd_signal.c b/opt/nothread/cnd_signal.c index 05cbcf6..363806e 100644 --- a/opt/nothread/cnd_signal.c +++ b/opt/nothread/cnd_signal.c @@ -1,9 +1,11 @@ +#ifndef REGTEST #include int cnd_signal(cnd_t *cond) { return thrd_success; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/cnd_wait.c b/opt/nothread/cnd_wait.c index 4c632cc..be2fcbb 100644 --- a/opt/nothread/cnd_wait.c +++ b/opt/nothread/cnd_wait.c @@ -1,9 +1,11 @@ +#ifndef REGTEST #include int cnd_wait(cnd_t *cond, mtx_t *mtx) { return thrd_error; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/mtx_destroy.c b/opt/nothread/mtx_destroy.c index 75baec2..5227274 100644 --- a/opt/nothread/mtx_destroy.c +++ b/opt/nothread/mtx_destroy.c @@ -1,7 +1,9 @@ +#ifndef REGTEST #include void mtx_destroy(mtx_t *mtx) {} +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/mtx_init.c b/opt/nothread/mtx_init.c index 922c942..76b3eeb 100644 --- a/opt/nothread/mtx_init.c +++ b/opt/nothread/mtx_init.c @@ -1,3 +1,4 @@ +#ifndef REGTEST #include int mtx_init(mtx_t *mtx, int type) @@ -5,6 +6,7 @@ int mtx_init(mtx_t *mtx, int type) *mtx = 0; return thrd_success; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/mtx_lock.c b/opt/nothread/mtx_lock.c index 121444e..d086986 100644 --- a/opt/nothread/mtx_lock.c +++ b/opt/nothread/mtx_lock.c @@ -1,5 +1,5 @@ +#ifndef REGTEST #include -#include int mtx_lock(mtx_t *mtx) { @@ -8,6 +8,7 @@ int mtx_lock(mtx_t *mtx) return thrd_success; } else return thrd_error; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/mtx_timedlock.c b/opt/nothread/mtx_timedlock.c index e3cd4fb..6ab7dfb 100644 --- a/opt/nothread/mtx_timedlock.c +++ b/opt/nothread/mtx_timedlock.c @@ -1,9 +1,11 @@ +#ifndef REGTEST #include int mtx_timedlock(mtx_t *restrict mtx, const struct timespec *restrict ts) { return mtx_lock(mtx); } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/mtx_trylock.c b/opt/nothread/mtx_trylock.c index 3333a86..bffc8b8 100644 --- a/opt/nothread/mtx_trylock.c +++ b/opt/nothread/mtx_trylock.c @@ -1,3 +1,4 @@ +#ifndef REGTEST #include int mtx_trylock(mtx_t *mtx) @@ -9,6 +10,7 @@ int mtx_trylock(mtx_t *mtx) return thrd_success; } } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/mtx_unlock.c b/opt/nothread/mtx_unlock.c index 1c11401..214a063 100644 --- a/opt/nothread/mtx_unlock.c +++ b/opt/nothread/mtx_unlock.c @@ -1,3 +1,4 @@ +#ifndef REGTEST #include int mtx_unlock(mtx_t *mtx) @@ -7,6 +8,7 @@ int mtx_unlock(mtx_t *mtx) return thrd_success; } else return thrd_error; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/thrd_yield.c b/opt/nothread/thrd_yield.c index 840157b..c92c495 100644 --- a/opt/nothread/thrd_yield.c +++ b/opt/nothread/thrd_yield.c @@ -1,9 +1,11 @@ +#ifndef REGTEST #include void thrd_yield(void) { /* does nothing */ } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/tss_create.c b/opt/nothread/tss_create.c index 2a28fca..e7ad53a 100644 --- a/opt/nothread/tss_create.c +++ b/opt/nothread/tss_create.c @@ -1,3 +1,4 @@ +#ifndef REGTEST #include int tss_create(tss_t *key, tss_dtor_t dtor) @@ -6,6 +7,7 @@ int tss_create(tss_t *key, tss_dtor_t dtor) key->value = NULL; return thrd_success; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/tss_delete.c b/opt/nothread/tss_delete.c index 5d251f4..758209f 100644 --- a/opt/nothread/tss_delete.c +++ b/opt/nothread/tss_delete.c @@ -1,9 +1,11 @@ +#ifndef REGTEST #include void tss_delete(tss_t key) { key.self->self = NULL; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/opt/nothread/tss_get.c b/opt/nothread/tss_get.c index 3e9917b..c302fff 100644 --- a/opt/nothread/tss_get.c +++ b/opt/nothread/tss_get.c @@ -1,23 +1,29 @@ +#ifndef REGTEST #include void *tss_get(tss_t key) { return key.value; } +#endif #ifdef TEST #include <_PDCLIB_test.h> +#ifndef REGTEST static tss_t key; static char v; +#endif int main( void ) { +#ifndef REGTEST TESTCASE(tss_create(&key, NULL) == thrd_success); TESTCASE(tss_get(key) == NULL); TESTCASE(tss_set(key, &v) == thrd_success); TESTCASE(tss_get(key) == &v); tss_delete(key); +#endif return TEST_RESULTS; } diff --git a/opt/nothread/tss_set.c b/opt/nothread/tss_set.c index 7de3ec5..d8ae84b 100644 --- a/opt/nothread/tss_set.c +++ b/opt/nothread/tss_set.c @@ -1,3 +1,4 @@ +#ifndef REGTEST #include int tss_set(tss_t key, void *val) @@ -5,6 +6,7 @@ int tss_set(tss_t key, void *val) key.self->value = val; return thrd_success; } +#endif #ifdef TEST #include <_PDCLIB_test.h> diff --git a/platform/example/functions/stdio/tmpfile.c b/platform/example/functions/stdio/tmpfile.c index 2d52f60..3f384e7 100644 --- a/platform/example/functions/stdio/tmpfile.c +++ b/platform/example/functions/stdio/tmpfile.c @@ -20,7 +20,7 @@ extern struct _PDCLIB_file_t * _PDCLIB_filelist; struct _PDCLIB_file_t * tmpfile( void ) { errno = ENOTSUP; - return 1; + return NULL; } #endif -- 2.40.0