├── .gitignore ├── CMakeLists.txt ├── COPYING.CC0 ├── Internals.txt ├── Notes.txt ├── Quickstart.txt ├── Readme.txt ├── auxiliary ├── errno │ ├── CMakeLists.txt │ └── errno_readout.c ├── pthread │ ├── CMakeLists.txt │ └── pthread_readout.c └── uctype │ ├── CMakeLists.txt │ ├── Makefile │ ├── derived_properties.c │ ├── derived_properties.h │ ├── main.c │ ├── test.h │ ├── text_utilities.c │ ├── text_utilities.h │ ├── uctype.c │ ├── uctype.h │ ├── unicode_data.c │ └── unicode_data.h ├── functions ├── _PDCLIB │ ├── Readme.txt │ ├── _PDCLIB_atomax.c │ ├── _PDCLIB_closeall.c │ ├── _PDCLIB_digits.c │ ├── _PDCLIB_filemode.c │ ├── _PDCLIB_getstream.c │ ├── _PDCLIB_is_leap.c │ ├── _PDCLIB_load_lc_collate.c │ ├── _PDCLIB_load_lc_ctype.c │ ├── _PDCLIB_load_lc_messages.c │ ├── _PDCLIB_load_lc_monetary.c │ ├── _PDCLIB_load_lc_numeric.c │ ├── _PDCLIB_load_lc_time.c │ ├── _PDCLIB_load_lines.c │ ├── _PDCLIB_prepread.c │ ├── _PDCLIB_prepwrite.c │ ├── _PDCLIB_print.c │ ├── _PDCLIB_scan.c │ ├── _PDCLIB_seed.c │ ├── _PDCLIB_setstream.c │ ├── _PDCLIB_strtok.c │ ├── _PDCLIB_strtox_main.c │ ├── _PDCLIB_strtox_prelim.c │ ├── errno.c │ └── stdarg.c ├── _dlmalloc │ ├── malloc-2.8.6.patch │ └── malloc.c ├── ctype │ ├── isalnum.c │ ├── isalpha.c │ ├── isblank.c │ ├── iscntrl.c │ ├── isdigit.c │ ├── isgraph.c │ ├── islower.c │ ├── isprint.c │ ├── ispunct.c │ ├── isspace.c │ ├── isupper.c │ ├── isxdigit.c │ ├── tolower.c │ └── toupper.c ├── inttypes │ ├── imaxabs.c │ ├── imaxdiv.c │ ├── strtoimax.c │ └── strtoumax.c ├── locale │ ├── localeconv.c │ └── setlocale.c ├── stdio │ ├── clearerr.c │ ├── fclose.c │ ├── feof.c │ ├── ferror.c │ ├── fflush.c │ ├── fgetc.c │ ├── fgetpos.c │ ├── fgets.c │ ├── fopen.c │ ├── fprintf.c │ ├── fputc.c │ ├── fputs.c │ ├── fread.c │ ├── freopen.c │ ├── fscanf.c │ ├── fseek.c │ ├── fsetpos.c │ ├── ftell.c │ ├── fwrite.c │ ├── getc.c │ ├── getchar.c │ ├── perror.c │ ├── printf.c │ ├── putc.c │ ├── putchar.c │ ├── puts.c │ ├── rename.c │ ├── rewind.c │ ├── scanf.c │ ├── setbuf.c │ ├── setvbuf.c │ ├── snprintf.c │ ├── sprintf.c │ ├── sscanf.c │ ├── testfile.txt │ ├── tmpnam.c │ ├── ungetc.c │ ├── vfprintf.c │ ├── vfscanf.c │ ├── vprintf.c │ ├── vscanf.c │ ├── vsnprintf.c │ ├── vsprintf.c │ └── vsscanf.c ├── stdlib │ ├── _Exit.c │ ├── abort.c │ ├── abort_handler_s.c │ ├── abs.c │ ├── at_quick_exit.c │ ├── atexit.c │ ├── atoi.c │ ├── atol.c │ ├── atoll.c │ ├── bsearch.c │ ├── bsearch_s.c │ ├── div.c │ ├── exit.c │ ├── ignore_handler_s.c │ ├── labs.c │ ├── ldiv.c │ ├── llabs.c │ ├── lldiv.c │ ├── qsort.c │ ├── qsort_s.c │ ├── quick_exit.c │ ├── rand.c │ ├── set_constraint_handler_s.c │ ├── srand.c │ ├── strtol.c │ ├── strtoll.c │ ├── strtoul.c │ └── strtoull.c ├── string │ ├── memchr.c │ ├── memcmp.c │ ├── memcpy.c │ ├── memcpy_s.c │ ├── memmove.c │ ├── memmove_s.c │ ├── memset.c │ ├── memset_s.c │ ├── strcat.c │ ├── strcat_s.c │ ├── strchr.c │ ├── strcmp.c │ ├── strcoll.c │ ├── strcpy.c │ ├── strcpy_s.c │ ├── strcspn.c │ ├── strerror.c │ ├── strerror_s.c │ ├── strerrorlen_s.c │ ├── strlen.c │ ├── strncat.c │ ├── strncat_s.c │ ├── strncmp.c │ ├── strncpy.c │ ├── strncpy_s.c │ ├── strpbrk.c │ ├── strrchr.c │ ├── strspn.c │ ├── strstr.c │ ├── strtok.c │ ├── strtok_s.c │ └── strxfrm.c └── time │ ├── asctime.c │ ├── ctime.c │ ├── difftime.c │ ├── gmtime.c │ ├── localtime.c │ ├── mktime.c │ └── strftime.c ├── include ├── ctype.h ├── errno.h ├── inttypes.h ├── iso646.h ├── limits.h ├── locale.h ├── pdclib │ ├── _PDCLIB_aux.h │ ├── _PDCLIB_glue.h │ ├── _PDCLIB_int.h │ └── _PDCLIB_lib_ext1.h ├── stdalign.h ├── stdarg.h ├── stdbool.h ├── stddef.h ├── stdint.h ├── stdio.h ├── stdlib.h ├── stdnoreturn.h ├── string.h ├── time.h └── wctype.h ├── platform ├── example │ ├── Readme.txt │ ├── functions │ │ ├── _PDCLIB │ │ │ ├── _PDCLIB_Exit.c │ │ │ ├── _PDCLIB_close.c │ │ │ ├── _PDCLIB_fillbuffer.c │ │ │ ├── _PDCLIB_flushbuffer.c │ │ │ ├── _PDCLIB_open.c │ │ │ ├── _PDCLIB_rename.c │ │ │ ├── _PDCLIB_seek.c │ │ │ └── _PDCLIB_stdinit.c │ │ ├── signal │ │ │ ├── raise.c │ │ │ └── signal.c │ │ ├── stdio │ │ │ ├── remove.c │ │ │ └── tmpfile.c │ │ ├── stdlib │ │ │ ├── getenv.c │ │ │ ├── getenv_s.c │ │ │ └── system.c │ │ ├── threads │ │ │ ├── call_once.c │ │ │ ├── cnd_broadcast.c │ │ │ ├── cnd_destroy.c │ │ │ ├── cnd_init.c │ │ │ ├── cnd_signal.c │ │ │ ├── cnd_timedwait.c │ │ │ ├── cnd_wait.c │ │ │ ├── mtx_destroy.c │ │ │ ├── mtx_init.c │ │ │ ├── mtx_lock.c │ │ │ ├── mtx_timedlock.c │ │ │ ├── mtx_trylock.c │ │ │ ├── mtx_unlock.c │ │ │ ├── thrd_create.c │ │ │ ├── thrd_current.c │ │ │ ├── thrd_detach.c │ │ │ ├── thrd_equal.c │ │ │ ├── thrd_exit.c │ │ │ ├── thrd_join.c │ │ │ ├── thrd_sleep.c │ │ │ ├── thrd_yield.c │ │ │ ├── tss_create.c │ │ │ ├── tss_delete.c │ │ │ ├── tss_get.c │ │ │ └── tss_set.c │ │ └── time │ │ │ ├── clock.c │ │ │ ├── time.c │ │ │ └── timespec_get.c │ └── include │ │ ├── float.h │ │ ├── pdclib │ │ └── _PDCLIB_config.h │ │ ├── signal.h │ │ └── threads.h └── xbox │ ├── Readme.txt │ ├── _tls_array.s │ ├── crt0.c │ ├── crt_initializers.c │ ├── functions │ ├── _PDCLIB │ │ ├── _PDCLIB_Exit.c │ │ ├── _PDCLIB_close.c │ │ ├── _PDCLIB_fillbuffer.c │ │ ├── _PDCLIB_flushbuffer.c │ │ ├── _PDCLIB_open.c │ │ ├── _PDCLIB_rename.c │ │ ├── _PDCLIB_seek.c │ │ ├── _PDCLIB_stdinit.c │ │ └── _PDCLIB_w32errno.c │ ├── assert │ │ └── assert.c │ ├── fenv │ │ ├── env.c │ │ ├── exceptions.c │ │ └── rounding.c │ ├── math │ │ ├── acos.c │ │ ├── acosh.c │ │ ├── asin.c │ │ ├── asinh.c │ │ ├── atan.c │ │ ├── atan2.c │ │ ├── atanh.c │ │ ├── cbrt.c │ │ ├── ceil.c │ │ ├── copysign.c │ │ ├── cos.c │ │ ├── cosh.c │ │ ├── erf.c │ │ ├── erfc.c │ │ ├── exp.c │ │ ├── exp2.c │ │ ├── expm1.c │ │ ├── fabs.c │ │ ├── fdim.c │ │ ├── floor.c │ │ ├── fma.c │ │ ├── fmax.c │ │ ├── fmin.c │ │ ├── fmod.c │ │ ├── fpclassify.c │ │ ├── frexp.c │ │ ├── hypot.c │ │ ├── ilogb.c │ │ ├── ldexp.c │ │ ├── lgamma.c │ │ ├── log.c │ │ ├── log10.c │ │ ├── log1p.c │ │ ├── log2.c │ │ ├── logb.c │ │ ├── lrint.c │ │ ├── lround.c │ │ ├── modf.c │ │ ├── nan.c │ │ ├── nearbyint.c │ │ ├── nextafter.c │ │ ├── nexttoward.c │ │ ├── pow.c │ │ ├── remainder.c │ │ ├── remquo.c │ │ ├── rint.c │ │ ├── round.c │ │ ├── scalbln.c │ │ ├── scalbn.c │ │ ├── signbit.c │ │ ├── sin.c │ │ ├── sinh.c │ │ ├── sqrt.c │ │ ├── tan.c │ │ ├── tanh.c │ │ ├── tgamma.c │ │ └── trunc.c │ ├── setjmp │ │ ├── longjmp.s │ │ └── setjmp.s │ ├── signal │ │ ├── raise.c │ │ └── signal.c │ ├── stdio │ │ ├── remove.c │ │ └── tmpfile.c │ ├── stdlib │ │ ├── getenv.c │ │ └── system.c │ ├── threads │ │ ├── _PDCLIB_xbox_tss_cleanup.c │ │ ├── call_once.c │ │ ├── cnd_broadcast.c │ │ ├── cnd_destroy.c │ │ ├── cnd_init.c │ │ ├── cnd_signal.c │ │ ├── cnd_wait_timedwait.c │ │ ├── mtx_destroy.c │ │ ├── mtx_init.c │ │ ├── mtx_lock.c │ │ ├── mtx_timedlock.c │ │ ├── mtx_trylock.c │ │ ├── mtx_unlock.c │ │ ├── thrd_create.c │ │ ├── thrd_current.c │ │ ├── thrd_detach.c │ │ ├── thrd_equal.c │ │ ├── thrd_exit.c │ │ ├── thrd_join.c │ │ ├── thrd_sleep.c │ │ ├── thrd_yield.c │ │ ├── tss_create.c │ │ ├── tss_delete.c │ │ ├── tss_get.c │ │ └── tss_set.c │ └── time │ │ ├── clock.c │ │ ├── time.c │ │ └── timespec_get.c │ ├── include │ ├── assert.h │ ├── fenv.h │ ├── float.h │ ├── math.h │ ├── pdclib │ │ ├── _PDCLIB_config.h │ │ └── _PDCLIB_xbox_tss.h │ ├── setjmp.h │ ├── signal.h │ ├── stdatomic.h │ └── threads.h │ └── tls.c └── test_support ├── _PDCLIB_iotest.h ├── _PDCLIB_test.h ├── printf_testcases.h └── scanf_testcases.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | # PDCLib test drivers 55 | *_t 56 | 57 | # PDCLib regression test drivers 58 | *_r 59 | 60 | # Auxiliary Targets 61 | get-uctypes 62 | -------------------------------------------------------------------------------- /auxiliary/errno/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.9 FATAL_ERROR ) 2 | 3 | project( errno_readout LANGUAGES C ) 4 | 5 | add_executable( errno_readout errno_readout.c ) 6 | -------------------------------------------------------------------------------- /auxiliary/pthread/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.9 FATAL_ERROR ) 2 | 3 | project( pthread_readout LANGUAGES C ) 4 | 5 | set( CMAKE_C_STANDARD 11 ) 6 | set( CMAKE_C_STANDARD_REQUIRED ON ) 7 | set( CMAKE_C_EXTENSIONS OFF ) 8 | 9 | add_executable( pthread_readout pthread_readout.c ) 10 | 11 | # For this executable specifically, make the _PDCLIB_* headers available 12 | # but *not* PDCLib's standard headers -- so we can check PDCLib config 13 | # against the host system. 14 | target_include_directories( pthread_readout BEFORE PRIVATE ${CMAKE_SOURCE_DIR}/include/pdclib ${CMAKE_SOURCE_DIR}/platform/example/include/pdclib ) 15 | 16 | # PThread linkage. 17 | set( CMAKE_THREAD_PREFER_PTHREAD 1 ) 18 | set( THREADS_PREFER_PTHREAD_FLAG 1 ) 19 | find_package( Threads ) 20 | if ( NOT CMAKE_USE_PTHREADS_INIT ) 21 | message( FATAL_ERROR "Cannot find pthread." ) 22 | endif() 23 | 24 | target_link_libraries( pthread_readout Threads::Threads ) 25 | -------------------------------------------------------------------------------- /auxiliary/uctype/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.9 FATAL_ERROR ) 2 | 3 | project( get_uctypes LANGUAGES C ) 4 | 5 | set( CMAKE_C_STANDARD 11 ) 6 | set( CMAKE_C_STANDARD_REQUIRED ON ) 7 | set( CMAKE_C_EXTENSIONS OFF ) 8 | 9 | set( uctype_SOURCES 10 | derived_properties.c 11 | text_utilities.c 12 | uctype.c 13 | unicode_data.c 14 | ) 15 | 16 | set( uctype_HEADERS 17 | derived_properties.h 18 | test.h 19 | text_utilities.h 20 | uctype.h 21 | unicode_data.h 22 | ) 23 | 24 | add_library( uctype STATIC ${uctype_SOURCES} ) 25 | target_include_directories( uctype PRIVATE ${CMAKE_SOURCE_DIR} ) 26 | 27 | add_executable( get_uctypes main.c ) 28 | target_include_directories( get_uctypes PRIVATE ${CMAKE_SOURCE_DIR} ) 29 | target_link_libraries( get_uctypes uctype ) 30 | 31 | foreach( file ${uctype_SOURCES} ) 32 | get_filename_component( basename ${file} NAME_WE ) 33 | 34 | # Test driver. 35 | add_executable( ${basename}_t ${file} ) 36 | set_property( TARGET ${basename}_t APPEND_STRING PROPERTY COMPILE_FLAGS "-DTEST" ) 37 | if ( IGNORE_NO_TESTDRIVER ) 38 | set_property( TARGET ${basename}_t APPEND_STRING PROPERTY COMPILE_FLAGS " -DNO_TESTDRIVER=1" ) 39 | endif() 40 | target_include_directories( ${basename}_t PRIVATE ${CMAKE_SOURCE_DIR} ) 41 | target_link_libraries( ${basename}_t uctype ) 42 | add_test( ${basename}_t ${basename}_t ) 43 | endforeach() 44 | -------------------------------------------------------------------------------- /auxiliary/uctype/derived_properties.h: -------------------------------------------------------------------------------- 1 | /* derived properties 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef DERIVED_PROPERTIES 8 | #define DERIVED_PROPERTIES DERIVED_PROPERTIES 9 | 10 | #include 11 | 12 | /* https://www.unicode.org/reports/tr44/#DerivedCoreProperties.txt */ 13 | 14 | struct derived_properties_t 15 | { 16 | size_t count; 17 | char * * name; 18 | size_t * begin; 19 | size_t * end; 20 | size_t * code_points; 21 | }; 22 | 23 | static const int derived_properties_fields[] = { 24 | -1, /* code point or code point range */ 25 | -1 /* property name */ 26 | }; 27 | 28 | struct derived_properties_t * read_derived_properties( const char * filename ); 29 | 30 | int lookup_property( struct derived_properties_t * dp, const char * property, size_t codepoint ); 31 | 32 | void release_derived_properties( struct derived_properties_t * dp ); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /auxiliary/uctype/test.h: -------------------------------------------------------------------------------- 1 | /* test 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef TEST_H 8 | #define TEST_H TEST_H 9 | 10 | #include 11 | 12 | #ifndef NO_TESTDRIVER 13 | #define NO_TESTDRIVER 0 14 | #endif 15 | 16 | static int TEST_RESULTS = 0; 17 | 18 | #define TESTCASE( x ) if ( x ) {} \ 19 | else { TEST_RESULTS += 1; printf( "FAILED: " __FILE__ ", line %d - %s\n", __LINE__, #x ); } 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /auxiliary/uctype/uctype.h: -------------------------------------------------------------------------------- 1 | /* uctype data 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef UCTYPE 8 | #define UCTYPE 9 | 10 | #include "derived_properties.h" 11 | #include "unicode_data.h" 12 | 13 | size_t get_towupper( size_t codepoint, struct unicode_record_t * ur ); 14 | size_t get_towlower( size_t codepoint, struct unicode_record_t * ur ); 15 | size_t get_iswupper( size_t codepoint, struct unicode_record_t * ur, struct derived_properties_t * core ); 16 | size_t get_iswlower( size_t codepoint, struct unicode_record_t * ur, struct derived_properties_t * core ); 17 | size_t get_iswalpha( size_t codepoint, struct unicode_record_t * ur, struct derived_properties_t * core ); 18 | size_t get_iswdigit( size_t codepoint ); 19 | size_t get_iswxdigit( size_t codepoint ); 20 | size_t get_iswblank( size_t codepoint, struct unicode_record_t * ur ); 21 | size_t get_iswspace( size_t codepoint, struct unicode_record_t * ur ); 22 | size_t get_iswcntrl( size_t codepoint, struct unicode_record_t * ur ); 23 | size_t get_iswgraph( size_t codepoint, struct unicode_record_t * ur ); 24 | size_t get_iswprint( size_t codepoint, struct unicode_record_t * ur ); 25 | size_t get_iswpunct( size_t codepoint, struct unicode_record_t * ur, struct derived_properties_t * core ); 26 | 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /functions/_PDCLIB/Readme.txt: -------------------------------------------------------------------------------- 1 | This directory holds various "internals" of PDCLib: 2 | 3 | - definitions of helper functions not specified by the standard (hidden in the 4 | _PDCLIB_* namespace); 5 | 6 | - definitions of data objects, both internal (like _PDCLIB_digits) and specified by 7 | the standard (_PDCLIB_errno); 8 | 9 | - test drivers for functionality that does not have its own implementation 10 | file to put the test driver in (stdarg). 11 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_atomax.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_atomax( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | _PDCLIB_intmax_t _PDCLIB_atomax( const char * s ) 13 | { 14 | _PDCLIB_intmax_t rc = 0; 15 | char sign = '+'; 16 | const char * x; 17 | /* TODO: In other than "C" locale, additional patterns may be defined */ 18 | while ( isspace( *s ) ) ++s; 19 | if ( *s == '+' ) ++s; 20 | else if ( *s == '-' ) sign = *(s++); 21 | /* TODO: Earlier version was missing tolower() but was not caught by tests */ 22 | while ( ( x = memchr( _PDCLIB_digits, tolower(*(s++)), 10 ) ) != NULL ) 23 | { 24 | rc = rc * 10 + ( x - _PDCLIB_digits ); 25 | } 26 | return ( sign == '+' ) ? rc : -rc; 27 | } 28 | 29 | #endif 30 | 31 | #ifdef TEST 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | int main( void ) 36 | { 37 | #ifndef REGTEST 38 | /* basic functionality */ 39 | TESTCASE( _PDCLIB_atomax( "123" ) == 123 ); 40 | /* testing skipping of leading whitespace and trailing garbage */ 41 | TESTCASE( _PDCLIB_atomax( " \n\v\t\f123xyz" ) == 123 ); 42 | #endif 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_closeall.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_closeall( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 12 | 13 | void _PDCLIB_closeall( void ) 14 | { 15 | struct _PDCLIB_file_t * stream = _PDCLIB_filelist; 16 | struct _PDCLIB_file_t * next; 17 | while ( stream != NULL ) 18 | { 19 | next = stream->next; 20 | fclose( stream ); 21 | stream = next; 22 | } 23 | } 24 | 25 | #endif 26 | 27 | #ifdef TEST 28 | 29 | #include "_PDCLIB_test.h" 30 | 31 | int main( void ) 32 | { 33 | /* No testdriver */ 34 | return TEST_RESULTS; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_digits.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_digits 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include "pdclib/_PDCLIB_int.h" 10 | 11 | const char _PDCLIB_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; 12 | 13 | /* For _PDCLIB/print.c only; obsolete with ctype.h */ 14 | const char _PDCLIB_Xdigits[] = "0123456789ABCDEF"; 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | #include 23 | 24 | int main( void ) 25 | { 26 | #ifndef REGTEST 27 | TESTCASE( strcmp( _PDCLIB_digits, "0123456789abcdefghijklmnopqrstuvwxyz" ) == 0 ); 28 | TESTCASE( strcmp( _PDCLIB_Xdigits, "0123456789ABCDEF" ) == 0 ); 29 | #endif 30 | return TEST_RESULTS; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_getstream.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_getstream( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "pdclib/_PDCLIB_int.h" 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | extern mtx_t _PDCLIB_filelist_mtx; 16 | #endif 17 | 18 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 19 | 20 | int _PDCLIB_getstream( struct _PDCLIB_file_t * stream ) 21 | { 22 | struct _PDCLIB_file_t * current; 23 | struct _PDCLIB_file_t * previous = NULL; 24 | 25 | _PDCLIB_LOCK( _PDCLIB_filelist_mtx ); 26 | current = _PDCLIB_filelist; 27 | 28 | while ( ( current != NULL ) && ( current != stream ) ) 29 | { 30 | previous = current; 31 | current = current->next; 32 | } 33 | 34 | if ( current == NULL ) 35 | { 36 | *_PDCLIB_errno_func() = _PDCLIB_EBADF; 37 | _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx ); 38 | return EOF; 39 | } 40 | 41 | if ( previous != NULL ) 42 | { 43 | previous->next = stream->next; 44 | } 45 | else 46 | { 47 | _PDCLIB_filelist = stream->next; 48 | } 49 | 50 | _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx ); 51 | return 0; 52 | } 53 | 54 | #endif 55 | 56 | #ifdef TEST 57 | 58 | #include "_PDCLIB_test.h" 59 | 60 | int main( void ) 61 | { 62 | /* No testdriver */ 63 | return TEST_RESULTS; 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_is_leap.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_is_leap( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include "pdclib/_PDCLIB_int.h" 10 | 11 | int _PDCLIB_is_leap( int year_offset ) 12 | { 13 | /* year given as offset from 1900, matching tm.tm_year in */ 14 | long long year = year_offset + 1900ll; 15 | return ( ( year % 4 ) == 0 && ( ( year % 25 ) != 0 || ( year % 400 ) == 0 ) ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | #ifndef REGTEST 27 | /* 1901 not leap */ 28 | TESTCASE( ! _PDCLIB_is_leap( 1 ) ); 29 | /* 1904 leap */ 30 | TESTCASE( _PDCLIB_is_leap( 4 ) ); 31 | /* 1900 not leap */ 32 | TESTCASE( ! _PDCLIB_is_leap( 0 ) ); 33 | /* 2000 leap */ 34 | TESTCASE( _PDCLIB_is_leap( 100 ) ); 35 | #endif 36 | return TEST_RESULTS; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_load_lc_collate.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_load_lc_collate( const char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "pdclib/_PDCLIB_int.h" 15 | 16 | struct _PDCLIB_lc_collate_t * _PDCLIB_load_lc_collate( const char * path, const char * locale ) 17 | { 18 | struct _PDCLIB_lc_collate_t * rc = NULL; 19 | const char * extension = "_collate.dat"; 20 | char * file = malloc( strlen( path ) + strlen( locale ) + strlen( extension ) + 1 ); 21 | 22 | if ( file ) 23 | { 24 | FILE * fh; 25 | 26 | strcpy( file, path ); 27 | strcat( file, locale ); 28 | strcat( file, extension ); 29 | 30 | if ( ( fh = fopen( file, "rb" ) ) != NULL ) 31 | { 32 | if ( ( rc = malloc( sizeof( struct _PDCLIB_lc_collate_t ) ) ) != NULL ) 33 | { 34 | /* TODO: Collation data */ 35 | 36 | rc->alloced = 1; 37 | } 38 | 39 | fclose( fh ); 40 | } 41 | 42 | free( file ); 43 | } 44 | 45 | return rc; 46 | } 47 | 48 | #endif 49 | 50 | #ifdef TEST 51 | 52 | #include "_PDCLIB_test.h" 53 | 54 | int main( void ) 55 | { 56 | #ifndef REGTEST 57 | TESTCASE( NO_TESTDRIVER ); 58 | #endif 59 | 60 | return TEST_RESULTS; 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_prepread.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_prepread( struct _PDCLIB_file_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "pdclib/_PDCLIB_glue.h" 12 | 13 | int _PDCLIB_prepread( struct _PDCLIB_file_t * stream ) 14 | { 15 | if ( ( stream->bufidx > stream->bufend ) || 16 | ( stream->status & ( _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_ERRORFLAG | _PDCLIB_WIDESTREAM | _PDCLIB_EOFFLAG ) ) || 17 | ! ( stream->status & ( _PDCLIB_FREAD | _PDCLIB_FRW ) ) ) 18 | { 19 | /* Function called on illegal (e.g. output) stream. */ 20 | *_PDCLIB_errno_func() = _PDCLIB_EBADF; 21 | stream->status |= _PDCLIB_ERRORFLAG; 22 | return EOF; 23 | } 24 | stream->status |= _PDCLIB_FREAD | _PDCLIB_BYTESTREAM; 25 | if ( ( stream->bufidx == stream->bufend ) && ( stream->ungetidx == 0 ) ) 26 | { 27 | return _PDCLIB_fillbuffer( stream ); 28 | } 29 | else 30 | { 31 | return 0; 32 | } 33 | } 34 | 35 | #endif 36 | 37 | #ifdef TEST 38 | 39 | #include "_PDCLIB_test.h" 40 | 41 | int main( void ) 42 | { 43 | /* Testing covered by ftell.c */ 44 | return TEST_RESULTS; 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_prepwrite.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_prepwrite( struct _PDCLIB_file_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int _PDCLIB_prepwrite( struct _PDCLIB_file_t * stream ) 12 | { 13 | if ( ( stream->bufidx < stream->bufend ) || ( stream->ungetidx > 0 ) || 14 | ( stream->status & ( _PDCLIB_FREAD | _PDCLIB_ERRORFLAG | _PDCLIB_WIDESTREAM | _PDCLIB_EOFFLAG ) ) || 15 | ! ( stream->status & ( _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) ) 16 | { 17 | /* Function called on illegal (e.g. input) stream. */ 18 | *_PDCLIB_errno_func() = _PDCLIB_EBADF; 19 | stream->status |= _PDCLIB_ERRORFLAG; 20 | return EOF; 21 | } 22 | stream->status |= _PDCLIB_FWRITE | _PDCLIB_BYTESTREAM; 23 | return 0; 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | /* Testing covered by ftell.c */ 35 | return TEST_RESULTS; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_seed.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_seed 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | unsigned long int _PDCLIB_seed = 1; 8 | 9 | #ifdef TEST 10 | 11 | #include "_PDCLIB_test.h" 12 | 13 | int main( void ) 14 | { 15 | /* no tests for raw data */ 16 | return TEST_RESULTS; 17 | } 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_setstream.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_setstream( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "pdclib/_PDCLIB_int.h" 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | extern mtx_t _PDCLIB_filelist_mtx; 16 | #endif 17 | 18 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 19 | 20 | void _PDCLIB_setstream( struct _PDCLIB_file_t * stream ) 21 | { 22 | _PDCLIB_LOCK( _PDCLIB_filelist_mtx ); 23 | stream->next = _PDCLIB_filelist; 24 | _PDCLIB_filelist = stream; 25 | _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx ); 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | /* No testdriver */ 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /functions/_PDCLIB/errno.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_errno 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include "pdclib/_PDCLIB_int.h" 10 | 11 | static int _PDCLIB_errno = 0; 12 | 13 | int * _PDCLIB_errno_func() 14 | { 15 | return &_PDCLIB_errno; 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | #include 25 | 26 | int main( void ) 27 | { 28 | errno = 0; 29 | TESTCASE( errno == 0 ); 30 | errno = EDOM; 31 | TESTCASE( errno == EDOM ); 32 | errno = ERANGE; 33 | TESTCASE( errno == ERANGE ); 34 | return TEST_RESULTS; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /functions/ctype/isalnum.c: -------------------------------------------------------------------------------- 1 | /* isalnum( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isalnum( int c ) 14 | { 15 | return ( isdigit( c ) || isalpha( c ) ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( isalnum( 'a' ) ); 27 | TESTCASE( isalnum( 'z' ) ); 28 | TESTCASE( isalnum( 'A' ) ); 29 | TESTCASE( isalnum( 'Z' ) ); 30 | TESTCASE( isalnum( '0' ) ); 31 | TESTCASE( isalnum( '9' ) ); 32 | TESTCASE( ! isalnum( ' ' ) ); 33 | TESTCASE( ! isalnum( '\n' ) ); 34 | TESTCASE( ! isalnum( '@' ) ); 35 | return TEST_RESULTS; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /functions/ctype/isalpha.c: -------------------------------------------------------------------------------- 1 | /* isalpha( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isalpha( int c ) 14 | { 15 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_ALPHA ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( isalpha( 'a' ) ); 27 | TESTCASE( isalpha( 'z' ) ); 28 | TESTCASE( ! isalpha( ' ' ) ); 29 | TESTCASE( ! isalpha( '1' ) ); 30 | TESTCASE( ! isalpha( '@' ) ); 31 | return TEST_RESULTS; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /functions/ctype/isblank.c: -------------------------------------------------------------------------------- 1 | /* isblank( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isblank( int c ) 14 | { 15 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_BLANK ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( isblank( ' ' ) ); 27 | TESTCASE( isblank( '\t' ) ); 28 | TESTCASE( ! isblank( '\v' ) ); 29 | TESTCASE( ! isblank( '\r' ) ); 30 | TESTCASE( ! isblank( 'x' ) ); 31 | TESTCASE( ! isblank( '@' ) ); 32 | return TEST_RESULTS; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /functions/ctype/iscntrl.c: -------------------------------------------------------------------------------- 1 | /* iscntrl( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int iscntrl( int c ) 14 | { 15 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_CNTRL ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( iscntrl( '\a' ) ); 27 | TESTCASE( iscntrl( '\b' ) ); 28 | TESTCASE( iscntrl( '\n' ) ); 29 | TESTCASE( ! iscntrl( ' ' ) ); 30 | return TEST_RESULTS; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /functions/ctype/isdigit.c: -------------------------------------------------------------------------------- 1 | /* isdigit( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isdigit( int c ) 14 | { 15 | return ( c >= _PDCLIB_lc_ctype.digits_low && c <= _PDCLIB_lc_ctype.digits_high ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( isdigit( '0' ) ); 27 | TESTCASE( isdigit( '9' ) ); 28 | TESTCASE( ! isdigit( ' ' ) ); 29 | TESTCASE( ! isdigit( 'a' ) ); 30 | TESTCASE( ! isdigit( '@' ) ); 31 | return TEST_RESULTS; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /functions/ctype/isgraph.c: -------------------------------------------------------------------------------- 1 | /* isgraph( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isgraph( int c ) 14 | { 15 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_GRAPH ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( isgraph( 'a' ) ); 27 | TESTCASE( isgraph( 'z' ) ); 28 | TESTCASE( isgraph( 'A' ) ); 29 | TESTCASE( isgraph( 'Z' ) ); 30 | TESTCASE( isgraph( '@' ) ); 31 | TESTCASE( ! isgraph( '\t' ) ); 32 | TESTCASE( ! isgraph( '\0' ) ); 33 | TESTCASE( ! isgraph( ' ' ) ); 34 | return TEST_RESULTS; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /functions/ctype/islower.c: -------------------------------------------------------------------------------- 1 | /* islower( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int islower( int c ) 14 | { 15 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_LOWER ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( islower( 'a' ) ); 27 | TESTCASE( islower( 'z' ) ); 28 | TESTCASE( ! islower( 'A' ) ); 29 | TESTCASE( ! islower( 'Z' ) ); 30 | TESTCASE( ! islower( ' ' ) ); 31 | TESTCASE( ! islower( '@' ) ); 32 | return TEST_RESULTS; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /functions/ctype/isprint.c: -------------------------------------------------------------------------------- 1 | /* isprint( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isprint( int c ) 14 | { 15 | /* FIXME: Space as of current locale charset, not source charset. */ 16 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_GRAPH ) || ( c == ' ' ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | TESTCASE( isprint( 'a' ) ); 28 | TESTCASE( isprint( 'z' ) ); 29 | TESTCASE( isprint( 'A' ) ); 30 | TESTCASE( isprint( 'Z' ) ); 31 | TESTCASE( isprint( '@' ) ); 32 | TESTCASE( ! isprint( '\t' ) ); 33 | TESTCASE( ! isprint( '\0' ) ); 34 | TESTCASE( isprint( ' ' ) ); 35 | return TEST_RESULTS; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /functions/ctype/ispunct.c: -------------------------------------------------------------------------------- 1 | /* ispunct( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int ispunct( int c ) 14 | { 15 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_PUNCT ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( ! ispunct( 'a' ) ); 27 | TESTCASE( ! ispunct( 'z' ) ); 28 | TESTCASE( ! ispunct( 'A' ) ); 29 | TESTCASE( ! ispunct( 'Z' ) ); 30 | TESTCASE( ispunct( '@' ) ); 31 | TESTCASE( ispunct( '.' ) ); 32 | TESTCASE( ! ispunct( '\t' ) ); 33 | TESTCASE( ! ispunct( '\0' ) ); 34 | TESTCASE( ! ispunct( ' ' ) ); 35 | return TEST_RESULTS; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /functions/ctype/isspace.c: -------------------------------------------------------------------------------- 1 | /* isspace( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isspace( int c ) 14 | { 15 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_SPACE ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( isspace( ' ' ) ); 27 | TESTCASE( isspace( '\f' ) ); 28 | TESTCASE( isspace( '\n' ) ); 29 | TESTCASE( isspace( '\r' ) ); 30 | TESTCASE( isspace( '\t' ) ); 31 | TESTCASE( isspace( '\v' ) ); 32 | TESTCASE( ! isspace( 'a' ) ); 33 | return TEST_RESULTS; 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /functions/ctype/isupper.c: -------------------------------------------------------------------------------- 1 | /* isupper( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isupper( int c ) 14 | { 15 | return ( _PDCLIB_lc_ctype.entry[c].flags & _PDCLIB_CTYPE_UPPER ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( isupper( 'A' ) ); 27 | TESTCASE( isupper( 'Z' ) ); 28 | TESTCASE( ! isupper( 'a' ) ); 29 | TESTCASE( ! isupper( 'z' ) ); 30 | TESTCASE( ! isupper( ' ' ) ); 31 | TESTCASE( ! isupper( '@' ) ); 32 | return TEST_RESULTS; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /functions/ctype/isxdigit.c: -------------------------------------------------------------------------------- 1 | /* isxdigit( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int isxdigit( int c ) 14 | { 15 | return ( isdigit( c ) 16 | || ( c >= _PDCLIB_lc_ctype.Xdigits_low && c <= _PDCLIB_lc_ctype.Xdigits_high ) 17 | || ( c >= _PDCLIB_lc_ctype.xdigits_low && c <= _PDCLIB_lc_ctype.xdigits_high ) ); 18 | } 19 | 20 | #endif 21 | 22 | #ifdef TEST 23 | 24 | #include "_PDCLIB_test.h" 25 | 26 | int main( void ) 27 | { 28 | TESTCASE( isxdigit( '0' ) ); 29 | TESTCASE( isxdigit( '9' ) ); 30 | TESTCASE( isxdigit( 'a' ) ); 31 | TESTCASE( isxdigit( 'f' ) ); 32 | TESTCASE( ! isxdigit( 'g' ) ); 33 | TESTCASE( isxdigit( 'A' ) ); 34 | TESTCASE( isxdigit( 'F' ) ); 35 | TESTCASE( ! isxdigit( 'G' ) ); 36 | TESTCASE( ! isxdigit( '@' ) ); 37 | TESTCASE( ! isxdigit( ' ' ) ); 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /functions/ctype/tolower.c: -------------------------------------------------------------------------------- 1 | /* tolower( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int tolower( int c ) 14 | { 15 | return _PDCLIB_lc_ctype.entry[c].lower; 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( tolower( 'A' ) == 'a' ); 27 | TESTCASE( tolower( 'Z' ) == 'z' ); 28 | TESTCASE( tolower( 'a' ) == 'a' ); 29 | TESTCASE( tolower( 'z' ) == 'z' ); 30 | TESTCASE( tolower( '@' ) == '@' ); 31 | TESTCASE( tolower( '[' ) == '[' ); 32 | return TEST_RESULTS; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /functions/ctype/toupper.c: -------------------------------------------------------------------------------- 1 | /* toupper( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int toupper( int c ) 14 | { 15 | return _PDCLIB_lc_ctype.entry[c].upper; 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( toupper( 'a' ) == 'A' ); 27 | TESTCASE( toupper( 'z' ) == 'Z' ); 28 | TESTCASE( toupper( 'A' ) == 'A' ); 29 | TESTCASE( toupper( 'Z' ) == 'Z' ); 30 | TESTCASE( toupper( '@' ) == '@' ); 31 | TESTCASE( toupper( '[' ) == '[' ); 32 | return TEST_RESULTS; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /functions/inttypes/imaxabs.c: -------------------------------------------------------------------------------- 1 | /* imaxabs( intmax_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | intmax_t imaxabs( intmax_t j ) 12 | { 13 | return ( j >= 0 ) ? j : -j; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | #include 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( imaxabs( (intmax_t)0 ) == 0 ); 27 | TESTCASE( imaxabs( INTMAX_MAX ) == INTMAX_MAX ); 28 | TESTCASE( imaxabs( INTMAX_MIN + 1 ) == -( INTMAX_MIN + 1 ) ); 29 | return TEST_RESULTS; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /functions/inttypes/imaxdiv.c: -------------------------------------------------------------------------------- 1 | /* lldiv( long long int, long long int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | imaxdiv_t imaxdiv( intmax_t numer, intmax_t denom ) 12 | { 13 | imaxdiv_t rc; 14 | rc.quot = numer / denom; 15 | rc.rem = numer % denom; 16 | return rc; 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | imaxdiv_t result; 28 | result = imaxdiv( (intmax_t)5, (intmax_t)2 ); 29 | TESTCASE( result.quot == 2 && result.rem == 1 ); 30 | result = imaxdiv( (intmax_t)-5, (intmax_t)2 ); 31 | TESTCASE( result.quot == -2 && result.rem == -1 ); 32 | result = imaxdiv( (intmax_t)5, (intmax_t)-2 ); 33 | TESTCASE( result.quot == -2 && result.rem == 1 ); 34 | TESTCASE( sizeof( result.quot ) == sizeof( intmax_t ) ); 35 | TESTCASE( sizeof( result.rem ) == sizeof( intmax_t ) ); 36 | return TEST_RESULTS; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /functions/locale/localeconv.c: -------------------------------------------------------------------------------- 1 | /* localeconv( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | struct lconv * localeconv( void ) 12 | { 13 | return _PDCLIB_lc_numeric_monetary.lconv; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | struct lconv * lconv; 25 | TESTCASE( ( lconv = localeconv() ) != NULL ); 26 | TESTCASE( strcmp( lconv->decimal_point, "." ) == 0 ); 27 | TESTCASE( strcmp( lconv->thousands_sep, "" ) == 0 ); 28 | TESTCASE( strcmp( lconv->grouping, "" ) == 0 ); 29 | return TEST_RESULTS; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /functions/stdio/clearerr.c: -------------------------------------------------------------------------------- 1 | /* clearerr( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | void clearerr( struct _PDCLIB_file_t * stream ) 12 | { 13 | stream->status &= ~( _PDCLIB_ERRORFLAG | _PDCLIB_EOFFLAG ); 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | FILE * fh; 25 | TESTCASE( ( fh = tmpfile() ) != NULL ); 26 | /* Flags should be clear */ 27 | TESTCASE( ! ferror( fh ) ); 28 | TESTCASE( ! feof( fh ) ); 29 | /* Reading from empty stream - should provoke EOF */ 30 | rewind( fh ); 31 | TESTCASE( fgetc( fh ) == EOF ); 32 | TESTCASE( ! ferror( fh ) ); 33 | TESTCASE( feof( fh ) ); 34 | /* clearerr() should clear flags */ 35 | clearerr( fh ); 36 | TESTCASE( ! ferror( fh ) ); 37 | TESTCASE( ! feof( fh ) ); 38 | /* reopen() the file write-only */ 39 | TESTCASE( ( fh = freopen( NULL, "w", fh ) ) != NULL ); 40 | /* Reading from write-only stream - should provoke error */ 41 | TESTCASE( fgetc( fh ) == EOF ); 42 | TESTCASE( ferror( fh ) ); 43 | TESTCASE( ! feof( fh ) ); 44 | /* clearerr() should clear flags */ 45 | clearerr( fh ); 46 | TESTCASE( ! ferror( fh ) ); 47 | TESTCASE( ! feof( fh ) ); 48 | TESTCASE( fclose( fh ) == 0 ); 49 | return TEST_RESULTS; 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /functions/stdio/feof.c: -------------------------------------------------------------------------------- 1 | /* feof( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int feof( struct _PDCLIB_file_t * stream ) 12 | { 13 | return stream->status & _PDCLIB_EOFFLAG; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* Testing covered by clearerr(). */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/stdio/ferror.c: -------------------------------------------------------------------------------- 1 | /* ferror( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int ferror( struct _PDCLIB_file_t * stream ) 12 | { 13 | return stream->status & _PDCLIB_ERRORFLAG; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* Testing covered by clearerr(). */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/stdio/fflush.c: -------------------------------------------------------------------------------- 1 | /* fflush( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "pdclib/_PDCLIB_glue.h" 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | #endif 16 | 17 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 18 | 19 | int fflush( struct _PDCLIB_file_t * stream ) 20 | { 21 | int rc = 0; 22 | if ( stream == NULL ) 23 | { 24 | stream = _PDCLIB_filelist; 25 | /* TODO: Check what happens when fflush( NULL ) encounters write errors, in other libs */ 26 | while ( stream != NULL ) 27 | { 28 | _PDCLIB_LOCK( stream->mtx ); 29 | 30 | if ( stream->status & _PDCLIB_FWRITE ) 31 | { 32 | if ( _PDCLIB_flushbuffer( stream ) == EOF ) 33 | { 34 | rc = EOF; 35 | } 36 | } 37 | 38 | _PDCLIB_UNLOCK( stream->mtx ); 39 | 40 | stream = stream->next; 41 | } 42 | } 43 | else 44 | { 45 | _PDCLIB_LOCK( stream->mtx ); 46 | rc = _PDCLIB_flushbuffer( stream ); 47 | _PDCLIB_UNLOCK( stream->mtx ); 48 | } 49 | 50 | return rc; 51 | } 52 | 53 | #endif 54 | 55 | #ifdef TEST 56 | 57 | #include "_PDCLIB_test.h" 58 | 59 | int main( void ) 60 | { 61 | /* Testing covered by ftell.c */ 62 | return TEST_RESULTS; 63 | } 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /functions/stdio/fgetc.c: -------------------------------------------------------------------------------- 1 | /* fgetc( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "pdclib/_PDCLIB_glue.h" 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | #endif 16 | 17 | int fgetc( struct _PDCLIB_file_t * stream ) 18 | { 19 | int rc; 20 | 21 | _PDCLIB_LOCK( stream->mtx ); 22 | 23 | if ( _PDCLIB_prepread( stream ) == EOF ) 24 | { 25 | _PDCLIB_UNLOCK( stream->mtx ); 26 | return EOF; 27 | } 28 | 29 | if ( stream->ungetidx > 0 ) 30 | { 31 | rc = (unsigned char)stream->ungetbuf[ --(stream->ungetidx) ]; 32 | } 33 | else 34 | { 35 | rc = (unsigned char)stream->buffer[stream->bufidx++]; 36 | } 37 | 38 | _PDCLIB_UNLOCK( stream->mtx ); 39 | 40 | return rc; 41 | } 42 | 43 | #endif 44 | 45 | #ifdef TEST 46 | 47 | #include "_PDCLIB_test.h" 48 | 49 | int main( void ) 50 | { 51 | /* Testing covered by ftell.c */ 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /functions/stdio/fgetpos.c: -------------------------------------------------------------------------------- 1 | /* fgetpos( FILE * , fpos_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #ifndef __STDC_NO_THREADS__ 12 | #include 13 | #endif 14 | 15 | int fgetpos( struct _PDCLIB_file_t * _PDCLIB_restrict stream, struct _PDCLIB_fpos_t * _PDCLIB_restrict pos ) 16 | { 17 | _PDCLIB_LOCK( stream->mtx ); 18 | pos->offset = stream->pos.offset + stream->bufidx - stream->ungetidx; 19 | pos->status = stream->pos.status; 20 | /* TODO: Add mbstate. */ 21 | _PDCLIB_UNLOCK( stream->mtx ); 22 | return 0; 23 | } 24 | 25 | #endif 26 | 27 | #ifdef TEST 28 | 29 | #include "_PDCLIB_test.h" 30 | 31 | #include 32 | 33 | int main( void ) 34 | { 35 | FILE * fh; 36 | fpos_t pos1, pos2; 37 | TESTCASE( ( fh = tmpfile() ) != NULL ); 38 | TESTCASE( fgetpos( fh, &pos1 ) == 0 ); 39 | TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) ); 40 | TESTCASE( fgetpos( fh, &pos2 ) == 0 ); 41 | TESTCASE( fsetpos( fh, &pos1 ) == 0 ); 42 | TESTCASE( ftell( fh ) == 0 ); 43 | TESTCASE( fsetpos( fh, &pos2 ) == 0 ); 44 | TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) ); 45 | TESTCASE( fclose( fh ) == 0 ); 46 | return TEST_RESULTS; 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /functions/stdio/fprintf.c: -------------------------------------------------------------------------------- 1 | /* fprintf( FILE *, const char *, ... ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | int fprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) 13 | { 14 | int rc; 15 | va_list ap; 16 | va_start( ap, format ); 17 | rc = vfprintf( stream, format, ap ); 18 | va_end( ap ); 19 | return rc; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | #include 26 | #include 27 | #define _PDCLIB_FILEID "stdio/fprintf.c" 28 | #define _PDCLIB_FILEIO 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | #define testprintf( stream, ... ) fprintf( stream, __VA_ARGS__ ) 33 | 34 | int main( void ) 35 | { 36 | FILE * target; 37 | TESTCASE( ( target = tmpfile() ) != NULL ); 38 | #include "printf_testcases.h" 39 | TESTCASE( fclose( target ) == 0 ); 40 | return TEST_RESULTS; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /functions/stdio/fputc.c: -------------------------------------------------------------------------------- 1 | /* fputc( int, FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "pdclib/_PDCLIB_glue.h" 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | #endif 16 | 17 | int fputc( int c, struct _PDCLIB_file_t * stream ) 18 | { 19 | _PDCLIB_LOCK( stream->mtx ); 20 | 21 | if ( _PDCLIB_prepwrite( stream ) == EOF ) 22 | { 23 | _PDCLIB_UNLOCK( stream->mtx ); 24 | return EOF; 25 | } 26 | 27 | stream->buffer[stream->bufidx++] = (char)c; 28 | if ( ( stream->bufidx == stream->bufsize ) /* _IOFBF */ 29 | || ( ( stream->status & _IOLBF ) && ( (char)c == '\n' ) ) /* _IOLBF */ 30 | || ( stream->status & _IONBF ) /* _IONBF */ 31 | ) 32 | { 33 | /* buffer filled, unbuffered stream, or end-of-line. */ 34 | c = ( _PDCLIB_flushbuffer( stream ) == 0 ) ? c : EOF; 35 | } 36 | 37 | _PDCLIB_UNLOCK( stream->mtx ); 38 | 39 | return c; 40 | } 41 | 42 | #endif 43 | 44 | #ifdef TEST 45 | 46 | #include "_PDCLIB_test.h" 47 | 48 | int main( void ) 49 | { 50 | /* Testing covered by ftell.c */ 51 | return TEST_RESULTS; 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /functions/stdio/fscanf.c: -------------------------------------------------------------------------------- 1 | /* fscanf( FILE *, const char *, ... ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... ) 13 | { 14 | int rc; 15 | va_list ap; 16 | va_start( ap, format ); 17 | rc = vfscanf( stream, format, ap ); 18 | va_end( ap ); 19 | return rc; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | #define _PDCLIB_FILEID "stdio/fscanf.c" 26 | #define _PDCLIB_FILEIO 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | #define testscanf( stream, format, ... ) fscanf( stream, format, __VA_ARGS__ ) 31 | 32 | int main( void ) 33 | { 34 | FILE * source; 35 | TESTCASE( ( source = tmpfile() ) != NULL ); 36 | #include "scanf_testcases.h" 37 | TESTCASE( fclose( source ) == 0 ); 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /functions/stdio/fsetpos.c: -------------------------------------------------------------------------------- 1 | /* fsetpos( FILE *, const fpos_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "pdclib/_PDCLIB_glue.h" 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | #endif 16 | 17 | int fsetpos( struct _PDCLIB_file_t * stream, const struct _PDCLIB_fpos_t * pos ) 18 | { 19 | _PDCLIB_LOCK( stream->mtx ); 20 | 21 | if ( stream->status & _PDCLIB_FWRITE ) 22 | { 23 | if ( _PDCLIB_flushbuffer( stream ) == EOF ) 24 | { 25 | _PDCLIB_UNLOCK( stream->mtx ); 26 | return EOF; 27 | } 28 | } 29 | if ( _PDCLIB_seek( stream, pos->offset, SEEK_SET ) == EOF ) 30 | { 31 | _PDCLIB_UNLOCK( stream->mtx ); 32 | return EOF; 33 | } 34 | stream->pos.status = pos->status; 35 | /* TODO: Add mbstate. */ 36 | 37 | _PDCLIB_UNLOCK( stream->mtx ); 38 | 39 | return 0; 40 | } 41 | 42 | #endif 43 | 44 | #ifdef TEST 45 | 46 | #include "_PDCLIB_test.h" 47 | 48 | int main( void ) 49 | { 50 | /* fsetpos() tested together with fsetpos(). */ 51 | return TEST_RESULTS; 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /functions/stdio/getc.c: -------------------------------------------------------------------------------- 1 | /* getc( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int getc( struct _PDCLIB_file_t * stream ) 12 | { 13 | return fgetc( stream ); 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* Testing covered by ftell.c */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/stdio/getchar.c: -------------------------------------------------------------------------------- 1 | /* getchar( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int getchar( void ) 12 | { 13 | return fgetc( stdin ); 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* Testing covered by ftell.c */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/stdio/printf.c: -------------------------------------------------------------------------------- 1 | /* printf( const char *, ... ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | int printf( const char * _PDCLIB_restrict format, ... ) 13 | { 14 | int rc; 15 | va_list ap; 16 | va_start( ap, format ); 17 | rc = vfprintf( stdout, format, ap ); 18 | va_end( ap ); 19 | return rc; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | #define _PDCLIB_FILEID "stdio/printf.c" 26 | #define _PDCLIB_FILEIO 27 | #include 28 | #include 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | #define testprintf( stream, ... ) printf( __VA_ARGS__ ) 33 | 34 | int main( void ) 35 | { 36 | FILE * target; 37 | TESTCASE( ( target = freopen( testfile, "wb+", stdout ) ) != NULL ); 38 | #include "printf_testcases.h" 39 | TESTCASE( fclose( target ) == 0 ); 40 | TESTCASE( remove( testfile ) == 0 ); 41 | return TEST_RESULTS; 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /functions/stdio/putc.c: -------------------------------------------------------------------------------- 1 | /* putc( int, FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int putc( int c, struct _PDCLIB_file_t * stream ) 12 | { 13 | return fputc( c, stream ); 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* Testing covered by ftell.c */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/stdio/putchar.c: -------------------------------------------------------------------------------- 1 | /* putchar( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int putchar( int c ) 12 | { 13 | return fputc( c, stdout ); 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* Testing covered by ftell.c */ 25 | putchar( 'x' ); 26 | return TEST_RESULTS; 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /functions/stdio/rewind.c: -------------------------------------------------------------------------------- 1 | /* rewind( FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | void rewind( struct _PDCLIB_file_t * stream ) 12 | { 13 | stream->status &= ~ _PDCLIB_ERRORFLAG; 14 | fseek( stream, 0L, SEEK_SET ); 15 | } 16 | 17 | #endif 18 | 19 | #ifdef TEST 20 | 21 | #include "_PDCLIB_test.h" 22 | 23 | int main( void ) 24 | { 25 | /* Testing covered by ftell.c */ 26 | return TEST_RESULTS; 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /functions/stdio/scanf.c: -------------------------------------------------------------------------------- 1 | /* scanf( const char *, ... ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | int scanf( const char * _PDCLIB_restrict format, ... ) 13 | { 14 | va_list ap; 15 | va_start( ap, format ); 16 | return vfscanf( stdin, format, ap ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | #define _PDCLIB_FILEID "stdio/scanf.c" 23 | #define _PDCLIB_FILEIO 24 | 25 | #include "_PDCLIB_test.h" 26 | 27 | #define testscanf( stream, format, ... ) scanf( format, __VA_ARGS__ ) 28 | 29 | int main( void ) 30 | { 31 | FILE * source; 32 | TESTCASE( ( source = freopen( testfile, "wb+", stdin ) ) != NULL ); 33 | #include "scanf_testcases.h" 34 | TESTCASE( fclose( source ) == 0 ); 35 | TESTCASE( remove( testfile ) == 0 ); 36 | return TEST_RESULTS; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /functions/stdio/setbuf.c: -------------------------------------------------------------------------------- 1 | /* setbuf( FILE *, char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | void setbuf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, char * _PDCLIB_restrict buf ) 12 | { 13 | if ( buf == NULL ) 14 | { 15 | setvbuf( stream, buf, _IONBF, BUFSIZ ); 16 | } 17 | else 18 | { 19 | setvbuf( stream, buf, _IOFBF, BUFSIZ ); 20 | } 21 | } 22 | 23 | #endif 24 | 25 | #ifdef TEST 26 | 27 | #include "_PDCLIB_test.h" 28 | 29 | #include 30 | 31 | int main( void ) 32 | { 33 | /* TODO: Extend testing once setvbuf() is finished. */ 34 | #ifndef REGTEST 35 | char buffer[ BUFSIZ + 1 ]; 36 | FILE * fh; 37 | /* full buffered */ 38 | TESTCASE( ( fh = tmpfile() ) != NULL ); 39 | setbuf( fh, buffer ); 40 | TESTCASE( fh->buffer == buffer ); 41 | TESTCASE( fh->bufsize == BUFSIZ ); 42 | TESTCASE( ( fh->status & ( _IOFBF | _IONBF | _IOLBF ) ) == _IOFBF ); 43 | TESTCASE( fclose( fh ) == 0 ); 44 | /* not buffered */ 45 | TESTCASE( ( fh = tmpfile() ) != NULL ); 46 | setbuf( fh, NULL ); 47 | TESTCASE( ( fh->status & ( _IOFBF | _IONBF | _IOLBF ) ) == _IONBF ); 48 | TESTCASE( fclose( fh ) == 0 ); 49 | #else 50 | puts( " NOTEST setbuf() test driver is PDCLib-specific." ); 51 | #endif 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /functions/stdio/snprintf.c: -------------------------------------------------------------------------------- 1 | /* snprintf( char *, size_t, const char *, ... ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | int snprintf( char * _PDCLIB_restrict s, size_t n, const char * _PDCLIB_restrict format, ...) 13 | { 14 | int rc; 15 | va_list ap; 16 | va_start( ap, format ); 17 | rc = vsnprintf( s, n, format, ap ); 18 | va_end( ap ); 19 | return rc; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | #define _PDCLIB_FILEID "stdio/snprintf.c" 26 | #define _PDCLIB_STRINGIO 27 | #include 28 | #include 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | #define testprintf( s, ... ) snprintf( s, 100, __VA_ARGS__ ) 33 | 34 | int main( void ) 35 | { 36 | char target[100]; 37 | #include "printf_testcases.h" 38 | TESTCASE( snprintf( NULL, 0, "foo" ) == 3 ); 39 | TESTCASE( snprintf( NULL, 0, "%d", 100 ) == 3 ); 40 | return TEST_RESULTS; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /functions/stdio/sprintf.c: -------------------------------------------------------------------------------- 1 | /* sprintf( char *, const char *, ... ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #ifndef REGTEST 12 | 13 | int sprintf( char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, ...) 14 | { 15 | int rc; 16 | va_list ap; 17 | va_start( ap, format ); 18 | rc = vsnprintf( s, SIZE_MAX, format, ap ); /* TODO: replace with non-checking call */ 19 | va_end( ap ); 20 | return rc; 21 | } 22 | 23 | #endif 24 | 25 | #ifdef TEST 26 | #define _PDCLIB_FILEID "stdio/sprintf.c" 27 | #define _PDCLIB_STRINGIO 28 | #include 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | #define testprintf( s, ... ) sprintf( s, __VA_ARGS__ ) 33 | 34 | int main( void ) 35 | { 36 | char target[100]; 37 | #include "printf_testcases.h" 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /functions/stdio/sscanf.c: -------------------------------------------------------------------------------- 1 | /* sscanf( const char *, const char *, ... ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | int sscanf( const char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, ... ) 13 | { 14 | int rc; 15 | va_list ap; 16 | va_start( ap, format ); 17 | rc = vsscanf( s, format, ap ); 18 | va_end( ap ); 19 | return rc; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | #define _PDCLIB_FILEID "stdio/sscanf.c" 26 | #define _PDCLIB_STRINGIO 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | #define testscanf( s, format, ... ) sscanf( s, format, __VA_ARGS__ ) 31 | 32 | int main( void ) 33 | { 34 | char source[100]; 35 | #include "scanf_testcases.h" 36 | return TEST_RESULTS; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /functions/stdio/testfile.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XboxDev/nxdk-pdclib/82bf35e9ebe70ae3214d4426ff7d1ef6b648376e/functions/stdio/testfile.txt -------------------------------------------------------------------------------- /functions/stdio/tmpnam.c: -------------------------------------------------------------------------------- 1 | /* tmpnam( char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "pdclib/_PDCLIB_glue.h" 12 | 13 | #include 14 | 15 | char * tmpnam( char * s ) 16 | { 17 | static char filename[ L_tmpnam ]; 18 | FILE * file = tmpfile(); 19 | if ( s == NULL ) 20 | { 21 | s = filename; 22 | } 23 | strcpy( s, file->filename ); 24 | fclose( file ); 25 | return s; 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | #include 35 | 36 | int main( void ) 37 | { 38 | TESTCASE( strlen( tmpnam( NULL ) ) < L_tmpnam ); 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /functions/stdio/ungetc.c: -------------------------------------------------------------------------------- 1 | /* ungetc( int, FILE * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #ifndef __STDC_NO_THREADS__ 12 | #include 13 | #endif 14 | 15 | int ungetc( int c, struct _PDCLIB_file_t * stream ) 16 | { 17 | int rc; 18 | 19 | _PDCLIB_LOCK( stream->mtx ); 20 | 21 | if ( c == EOF || stream->ungetidx == _PDCLIB_UNGETCBUFSIZE ) 22 | { 23 | rc = -1; 24 | } 25 | else 26 | { 27 | rc = stream->ungetbuf[stream->ungetidx++] = (unsigned char) c; 28 | } 29 | 30 | _PDCLIB_UNLOCK( stream->mtx ); 31 | 32 | return rc; 33 | } 34 | 35 | #endif 36 | 37 | #ifdef TEST 38 | 39 | #include "_PDCLIB_test.h" 40 | 41 | int main( void ) 42 | { 43 | /* Testing covered by ftell.c */ 44 | return TEST_RESULTS; 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /functions/stdio/vprintf.c: -------------------------------------------------------------------------------- 1 | /* vprintf( const char *, va_list ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | int vprintf( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) 13 | { 14 | return vfprintf( stdout, format, arg ); 15 | } 16 | 17 | #endif 18 | 19 | #ifdef TEST 20 | #define _PDCLIB_FILEID "stdio/vprintf.c" 21 | #define _PDCLIB_FILEIO 22 | #include 23 | #include 24 | #include "_PDCLIB_test.h" 25 | 26 | static int testprintf( FILE * stream, const char * format, ... ) 27 | { 28 | int i; 29 | va_list arg; 30 | va_start( arg, format ); 31 | i = vprintf( format, arg ); 32 | va_end( arg ); 33 | return i; 34 | } 35 | 36 | int main( void ) 37 | { 38 | FILE * target; 39 | TESTCASE( ( target = freopen( testfile, "wb+", stdout ) ) != NULL ); 40 | #include "printf_testcases.h" 41 | TESTCASE( fclose( target ) == 0 ); 42 | TESTCASE( remove( testfile ) == 0 ); 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /functions/stdio/vscanf.c: -------------------------------------------------------------------------------- 1 | /* vscanf( const char *, va_list ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | int vscanf( const char * _PDCLIB_restrict format, _PDCLIB_va_list arg ) 13 | { 14 | return vfscanf( stdin, format, arg ); 15 | } 16 | 17 | #endif 18 | 19 | #ifdef TEST 20 | #define _PDCLIB_FILEID "stdio/vscanf.c" 21 | #define _PDCLIB_FILEIO 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | static int testscanf( FILE * stream, const char * format, ... ) 26 | { 27 | int i; 28 | va_list arg; 29 | va_start( arg, format ); 30 | i = vscanf( format, arg ); 31 | va_end( arg ); 32 | return i; 33 | } 34 | 35 | int main( void ) 36 | { 37 | FILE * source; 38 | TESTCASE( ( source = freopen( testfile, "wb+", stdin ) ) != NULL ); 39 | #include "scanf_testcases.h" 40 | TESTCASE( fclose( source ) == 0 ); 41 | TESTCASE( remove( testfile ) == 0 ); 42 | return TEST_RESULTS; 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /functions/stdio/vsprintf.c: -------------------------------------------------------------------------------- 1 | /* vsprintf( char *, const char *, va_list ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #ifndef REGTEST 12 | 13 | int vsprintf( char * _PDCLIB_restrict s, const char * _PDCLIB_restrict format, va_list arg ) 14 | { 15 | return vsnprintf( s, SIZE_MAX, format, arg ); /* TODO: Replace with a non-checking call */ 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | #define _PDCLIB_FILEID "stdio/vsprintf.c" 22 | #define _PDCLIB_STRINGIO 23 | #include 24 | #include 25 | #include "_PDCLIB_test.h" 26 | 27 | static int testprintf( char * s, const char * format, ... ) 28 | { 29 | int i; 30 | va_list arg; 31 | va_start( arg, format ); 32 | i = vsprintf( s, format, arg ); 33 | va_end( arg ); 34 | return i; 35 | } 36 | 37 | int main( void ) 38 | { 39 | char target[100]; 40 | #include "printf_testcases.h" 41 | return TEST_RESULTS; 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /functions/stdlib/_Exit.c: -------------------------------------------------------------------------------- 1 | /* _Exit( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | #include "pdclib/_PDCLIB_glue.h" 13 | 14 | void _Exit( int status ) 15 | { 16 | _PDCLIB_closeall(); 17 | _PDCLIB_Exit( status ); 18 | } 19 | 20 | #endif 21 | 22 | #ifdef TEST 23 | 24 | #include "_PDCLIB_test.h" 25 | 26 | int main( void ) 27 | { 28 | int UNEXPECTED_RETURN = 0; 29 | _Exit( 0 ); 30 | TESTCASE( UNEXPECTED_RETURN ); 31 | return TEST_RESULTS; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /functions/stdlib/abort.c: -------------------------------------------------------------------------------- 1 | /* abort( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | void abort( void ) 13 | { 14 | raise( SIGABRT ); 15 | exit( EXIT_FAILURE ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | #include 25 | 26 | static void aborthandler( int sig ) 27 | { 28 | exit( 0 ); 29 | } 30 | 31 | int main( void ) 32 | { 33 | int UNEXPECTED_RETURN_FROM_ABORT = 0; 34 | TESTCASE( signal( SIGABRT, &aborthandler ) != SIG_ERR ); 35 | abort(); 36 | TESTCASE( UNEXPECTED_RETURN_FROM_ABORT ); 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /functions/stdlib/abort_handler_s.c: -------------------------------------------------------------------------------- 1 | /* abort_handler_s( const char *, void *, errno_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #define __STDC_WANT_LIB_EXT1__ 1 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | #include 13 | 14 | void abort_handler_s( const char * _PDCLIB_restrict msg, void * _PDCLIB_restrict ptr, errno_t errno ) 15 | { 16 | fprintf( stderr, "abort handler called:\n%s\n", msg ); 17 | abort(); 18 | } 19 | 20 | #endif 21 | 22 | #ifdef TEST 23 | 24 | #include "_PDCLIB_test.h" 25 | 26 | int main( void ) 27 | { 28 | TESTCASE( NO_TESTDRIVER ); 29 | return TEST_RESULTS; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /functions/stdlib/abs.c: -------------------------------------------------------------------------------- 1 | /* abs( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int abs( int j ) 12 | { 13 | return ( j >= 0 ) ? j : -j; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | #include 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( abs( 0 ) == 0 ); 27 | TESTCASE( abs( INT_MAX ) == INT_MAX ); 28 | TESTCASE( abs( INT_MIN + 1 ) == -( INT_MIN + 1 ) ); 29 | return TEST_RESULTS; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /functions/stdlib/at_quick_exit.c: -------------------------------------------------------------------------------- 1 | /* at_quick_exit( void (*)( void ) ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | extern void (*_PDCLIB_quickexitstack[])( void ); 12 | extern size_t _PDCLIB_quickexitptr; 13 | 14 | int at_quick_exit( void (*func)( void ) ) 15 | { 16 | if ( _PDCLIB_quickexitptr == _PDCLIB_ATEXIT_SLOTS ) 17 | { 18 | return -1; 19 | } 20 | else 21 | { 22 | _PDCLIB_quickexitstack[ _PDCLIB_quickexitptr++ ] = func; 23 | return 0; 24 | } 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | #include 34 | 35 | #if ! defined( REGTEST ) || __STDC_VERSION__ >= 201112L 36 | 37 | static int flags[ 32 ]; 38 | 39 | static void counthandler( void ) 40 | { 41 | static int count = 0; 42 | flags[ count ] = count; 43 | ++count; 44 | } 45 | 46 | static void checkhandler( void ) 47 | { 48 | int i; 49 | for ( i = 0; i < 32; ++i ) 50 | { 51 | assert( flags[ i ] == i ); 52 | } 53 | } 54 | 55 | #endif 56 | 57 | int main( void ) 58 | { 59 | #if ! defined( REGTEST ) || __STDC_VERSION__ >= 201112L 60 | int i; 61 | TESTCASE( at_quick_exit( &checkhandler ) == 0 ); 62 | for ( i = 0; i < 32; ++i ) 63 | { 64 | TESTCASE( at_quick_exit( &counthandler ) == 0 ); 65 | } 66 | #endif 67 | return TEST_RESULTS; 68 | } 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /functions/stdlib/atexit.c: -------------------------------------------------------------------------------- 1 | /* atexit( void (*)( void ) ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | extern void (*_PDCLIB_exitstack[])( void ); 12 | extern size_t _PDCLIB_exitptr; 13 | 14 | int atexit( void (*func)( void ) ) 15 | { 16 | if ( _PDCLIB_exitptr == _PDCLIB_ATEXIT_SLOTS ) 17 | { 18 | return -1; 19 | } 20 | else 21 | { 22 | _PDCLIB_exitstack[ _PDCLIB_exitptr++ ] = func; 23 | return 0; 24 | } 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | #include 34 | 35 | static int flags[ 32 ]; 36 | 37 | static void counthandler( void ) 38 | { 39 | static int count = 0; 40 | flags[ count ] = count; 41 | ++count; 42 | } 43 | 44 | static void checkhandler( void ) 45 | { 46 | int i; 47 | for ( i = 0; i < 32; ++i ) 48 | { 49 | assert( flags[ i ] == i ); 50 | } 51 | } 52 | 53 | int main( void ) 54 | { 55 | int i; 56 | TESTCASE( atexit( &checkhandler ) == 0 ); 57 | for ( i = 0; i < 32; ++i ) 58 | { 59 | TESTCASE( atexit( &counthandler ) == 0 ); 60 | } 61 | return TEST_RESULTS; 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /functions/stdlib/atoi.c: -------------------------------------------------------------------------------- 1 | /* atoi( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int atoi( const char * s ) 12 | { 13 | return (int) _PDCLIB_atomax( s ); 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* no tests for a simple wrapper */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/stdlib/atol.c: -------------------------------------------------------------------------------- 1 | /* atol( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | long int atol( const char * s ) 12 | { 13 | return (long int) _PDCLIB_atomax( s ); 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* no tests for a simple wrapper */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/stdlib/atoll.c: -------------------------------------------------------------------------------- 1 | /* atoll( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | long long int atoll( const char * s ) 12 | { 13 | return (long long int) _PDCLIB_atomax( s ); 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* no tests for a simple wrapper */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/stdlib/div.c: -------------------------------------------------------------------------------- 1 | /* div( int, int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | div_t div( int numer, int denom ) 12 | { 13 | div_t rc; 14 | rc.quot = numer / denom; 15 | rc.rem = numer % denom; 16 | /* TODO: pre-C99 compilers might require modulus corrections */ 17 | return rc; 18 | } 19 | 20 | #endif 21 | 22 | #ifdef TEST 23 | 24 | #include "_PDCLIB_test.h" 25 | 26 | int main( void ) 27 | { 28 | div_t result; 29 | result = div( 5, 2 ); 30 | TESTCASE( result.quot == 2 && result.rem == 1 ); 31 | result = div( -5, 2 ); 32 | TESTCASE( result.quot == -2 && result.rem == -1 ); 33 | result = div( 5, -2 ); 34 | TESTCASE( result.quot == -2 && result.rem == 1 ); 35 | TESTCASE( sizeof( result.quot ) == sizeof( int ) ); 36 | TESTCASE( sizeof( result.rem ) == sizeof( int ) ); 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /functions/stdlib/exit.c: -------------------------------------------------------------------------------- 1 | /* exit( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | /* TODO - "...except that a function is called after any previously registered 12 | functions that had already been called at the time it was registered." 13 | */ 14 | 15 | void (*_PDCLIB_exitstack[ _PDCLIB_ATEXIT_SLOTS ])( void ); 16 | size_t _PDCLIB_exitptr = 0; 17 | 18 | void exit( int status ) 19 | { 20 | while ( _PDCLIB_exitptr != 0 ) 21 | { 22 | _PDCLIB_exitstack[ --_PDCLIB_exitptr ](); 23 | } 24 | _Exit( status ); 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | int main( void ) 34 | { 35 | /* Unwinding of regstack tested in atexit(). */ 36 | return TEST_RESULTS; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /functions/stdlib/ignore_handler_s.c: -------------------------------------------------------------------------------- 1 | /* ignore_handler_s( const char *, void *, errno_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #define __STDC_WANT_LIB_EXT1__ 1 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | #include 13 | 14 | void ignore_handler_s( const char * _PDCLIB_restrict msg, void * _PDCLIB_restrict ptr, errno_t errno ) 15 | { 16 | return; 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | TESTCASE( NO_TESTDRIVER ); 28 | return TEST_RESULTS; 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /functions/stdlib/labs.c: -------------------------------------------------------------------------------- 1 | /* labs( long int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | long int labs( long int j ) 12 | { 13 | return ( j >= 0 ) ? j : -j; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | #include 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( labs( 0 ) == 0 ); 27 | TESTCASE( labs( LONG_MAX ) == LONG_MAX ); 28 | TESTCASE( labs( LONG_MIN + 1 ) == -( LONG_MIN + 1 ) ); 29 | return TEST_RESULTS; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /functions/stdlib/ldiv.c: -------------------------------------------------------------------------------- 1 | /* ldiv( long int, long int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | ldiv_t ldiv( long int numer, long int denom ) 12 | { 13 | ldiv_t rc; 14 | rc.quot = numer / denom; 15 | rc.rem = numer % denom; 16 | /* TODO: pre-C99 compilers might require modulus corrections */ 17 | return rc; 18 | } 19 | 20 | #endif 21 | 22 | #ifdef TEST 23 | 24 | #include "_PDCLIB_test.h" 25 | 26 | int main( void ) 27 | { 28 | ldiv_t result; 29 | result = ldiv( 5, 2 ); 30 | TESTCASE( result.quot == 2 && result.rem == 1 ); 31 | result = ldiv( -5, 2 ); 32 | TESTCASE( result.quot == -2 && result.rem == -1 ); 33 | result = ldiv( 5, -2 ); 34 | TESTCASE( result.quot == -2 && result.rem == 1 ); 35 | TESTCASE( sizeof( result.quot ) == sizeof( long ) ); 36 | TESTCASE( sizeof( result.rem ) == sizeof( long ) ); 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /functions/stdlib/llabs.c: -------------------------------------------------------------------------------- 1 | /* llabs( long int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | long long int llabs( long long int j ) 12 | { 13 | return ( j >= 0 ) ? j : -j; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | #include 23 | 24 | int main( void ) 25 | { 26 | TESTCASE( llabs( 0ll ) == 0 ); 27 | TESTCASE( llabs( LLONG_MAX ) == LLONG_MAX ); 28 | TESTCASE( llabs( LLONG_MIN + 1 ) == -( LLONG_MIN + 1 ) ); 29 | return TEST_RESULTS; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /functions/stdlib/lldiv.c: -------------------------------------------------------------------------------- 1 | /* lldiv( long long int, long long int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | lldiv_t lldiv( long long int numer, long long int denom ) 12 | { 13 | lldiv_t rc; 14 | rc.quot = numer / denom; 15 | rc.rem = numer % denom; 16 | /* TODO: pre-C99 compilers might require modulus corrections */ 17 | return rc; 18 | } 19 | 20 | #endif 21 | 22 | #ifdef TEST 23 | 24 | #include "_PDCLIB_test.h" 25 | 26 | int main( void ) 27 | { 28 | lldiv_t result; 29 | result = lldiv( 5ll, 2ll ); 30 | TESTCASE( result.quot == 2 && result.rem == 1 ); 31 | result = lldiv( -5ll, 2ll ); 32 | TESTCASE( result.quot == -2 && result.rem == -1 ); 33 | result = lldiv( 5ll, -2ll ); 34 | TESTCASE( result.quot == -2 && result.rem == 1 ); 35 | TESTCASE( sizeof( result.quot ) == sizeof( long long ) ); 36 | TESTCASE( sizeof( result.rem ) == sizeof( long long ) ); 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /functions/stdlib/quick_exit.c: -------------------------------------------------------------------------------- 1 | /* quick_exit( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | /* TODO - "...except that a function is called after any previously registered 12 | functions that had already been called at the time it was registered." 13 | */ 14 | 15 | void (*_PDCLIB_quickexitstack[ _PDCLIB_ATEXIT_SLOTS ])( void ); 16 | size_t _PDCLIB_quickexitptr = 0; 17 | 18 | void quick_exit( int status ) 19 | { 20 | while ( _PDCLIB_quickexitptr != 0 ) 21 | { 22 | _PDCLIB_quickexitstack[ --_PDCLIB_quickexitptr ](); 23 | } 24 | _Exit( status ); 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | int main( void ) 34 | { 35 | /* Unwinding of regstack tested in at_quick_exit(). */ 36 | return TEST_RESULTS; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /functions/stdlib/rand.c: -------------------------------------------------------------------------------- 1 | /* rand( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int rand( void ) 12 | { 13 | _PDCLIB_seed = _PDCLIB_seed * 1103515245 + 12345; 14 | return (int)( _PDCLIB_seed / 65536 ) % 32768; 15 | } 16 | 17 | #endif 18 | 19 | #ifdef TEST 20 | 21 | #include "_PDCLIB_test.h" 22 | 23 | int main( void ) 24 | { 25 | int rnd1, rnd2; 26 | TESTCASE( ( rnd1 = rand() ) < RAND_MAX ); 27 | TESTCASE( ( rnd2 = rand() ) < RAND_MAX ); 28 | srand( 1 ); 29 | TESTCASE( rand() == rnd1 ); 30 | TESTCASE( rand() == rnd2 ); 31 | return TEST_RESULTS; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /functions/stdlib/set_constraint_handler_s.c: -------------------------------------------------------------------------------- 1 | /* set_constraint_handler_s( constraint_handler_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #define __STDC_WANT_LIB_EXT1__ 1 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | constraint_handler_t _PDCLIB_constraint_handler = abort_handler_s; 13 | 14 | constraint_handler_t set_constraint_handler_s( constraint_handler_t handler ) 15 | { 16 | constraint_handler_t previous = _PDCLIB_constraint_handler; 17 | 18 | if ( handler == NULL ) 19 | { 20 | _PDCLIB_constraint_handler = abort_handler_s; 21 | } 22 | else 23 | { 24 | _PDCLIB_constraint_handler = handler; 25 | } 26 | 27 | return previous; 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | #ifndef REGTEST 39 | TESTCASE( _PDCLIB_constraint_handler == abort_handler_s ); 40 | TESTCASE( set_constraint_handler_s( ignore_handler_s ) == abort_handler_s ); 41 | TESTCASE( _PDCLIB_constraint_handler == ignore_handler_s ); 42 | TESTCASE( set_constraint_handler_s( NULL ) == ignore_handler_s ); 43 | TESTCASE( _PDCLIB_constraint_handler == abort_handler_s ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /functions/stdlib/srand.c: -------------------------------------------------------------------------------- 1 | /* srand( unsigned int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | void srand( unsigned int seed ) 12 | { 13 | _PDCLIB_seed = seed; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | /* tested in rand.c */ 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/string/memchr.c: -------------------------------------------------------------------------------- 1 | /* memchr( const void *, int, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | void * memchr( const void * s, int c, size_t n ) 12 | { 13 | const unsigned char * p = (const unsigned char *) s; 14 | while ( n-- ) 15 | { 16 | if ( *p == (unsigned char) c ) 17 | { 18 | return (void *) p; 19 | } 20 | ++p; 21 | } 22 | return NULL; 23 | } 24 | 25 | #endif 26 | 27 | #ifdef TEST 28 | 29 | #include "_PDCLIB_test.h" 30 | 31 | int main( void ) 32 | { 33 | TESTCASE( memchr( abcde, 'c', 5 ) == &abcde[2] ); 34 | TESTCASE( memchr( abcde, 'a', 1 ) == &abcde[0] ); 35 | TESTCASE( memchr( abcde, 'a', 0 ) == NULL ); 36 | TESTCASE( memchr( abcde, '\0', 5 ) == NULL ); 37 | TESTCASE( memchr( abcde, '\0', 6 ) == &abcde[5] ); 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /functions/string/memcmp.c: -------------------------------------------------------------------------------- 1 | /* memcmp( const void *, const void *, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int memcmp( const void * s1, const void * s2, size_t n ) 12 | { 13 | const unsigned char * p1 = (const unsigned char *) s1; 14 | const unsigned char * p2 = (const unsigned char *) s2; 15 | while ( n-- ) 16 | { 17 | if ( *p1 != *p2 ) 18 | { 19 | return *p1 - *p2; 20 | } 21 | ++p1; 22 | ++p2; 23 | } 24 | return 0; 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | int main( void ) 34 | { 35 | const char xxxxx[] = "xxxxx"; 36 | TESTCASE( memcmp( abcde, abcdx, 5 ) < 0 ); 37 | TESTCASE( memcmp( abcde, abcdx, 4 ) == 0 ); 38 | TESTCASE( memcmp( abcde, xxxxx, 0 ) == 0 ); 39 | TESTCASE( memcmp( xxxxx, abcde, 1 ) > 0 ); 40 | return 0; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /functions/string/memcpy.c: -------------------------------------------------------------------------------- 1 | /* memcpy( void *, const void *, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | void * memcpy( void * _PDCLIB_restrict s1, const void * _PDCLIB_restrict s2, size_t n ) 12 | { 13 | char * dest = (char *) s1; 14 | const char * src = (const char *) s2; 15 | while ( n-- ) 16 | { 17 | *dest++ = *src++; 18 | } 19 | return s1; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | 26 | #include "_PDCLIB_test.h" 27 | 28 | int main( void ) 29 | { 30 | char s[] = "xxxxxxxxxxx"; 31 | TESTCASE( memcpy( s, abcde, 6 ) == s ); 32 | TESTCASE( s[4] == 'e' ); 33 | TESTCASE( s[5] == '\0' ); 34 | TESTCASE( memcpy( s + 5, abcde, 5 ) == s + 5 ); 35 | TESTCASE( s[9] == 'e' ); 36 | TESTCASE( s[10] == 'x' ); 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /functions/string/memmove.c: -------------------------------------------------------------------------------- 1 | /* memmove( void *, const void *, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | void * memmove( void * s1, const void * s2, size_t n ) 12 | { 13 | char * dest = (char *) s1; 14 | const char * src = (const char *) s2; 15 | if ( dest <= src ) 16 | { 17 | while ( n-- ) 18 | { 19 | *dest++ = *src++; 20 | } 21 | } 22 | else 23 | { 24 | src += n; 25 | dest += n; 26 | while ( n-- ) 27 | { 28 | *--dest = *--src; 29 | } 30 | } 31 | return s1; 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | char s[] = "xxxxabcde"; 43 | TESTCASE( memmove( s, s + 4, 5 ) == s ); 44 | TESTCASE( s[0] == 'a' ); 45 | TESTCASE( s[4] == 'e' ); 46 | TESTCASE( s[5] == 'b' ); 47 | TESTCASE( memmove( s + 4, s, 5 ) == s + 4 ); 48 | TESTCASE( s[4] == 'a' ); 49 | return TEST_RESULTS; 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /functions/string/memset.c: -------------------------------------------------------------------------------- 1 | /* memset( void *, int, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | void * memset( void * s, int c, size_t n ) 12 | { 13 | unsigned char * p = (unsigned char *) s; 14 | while ( n-- ) 15 | { 16 | *p++ = (unsigned char) c; 17 | } 18 | return s; 19 | } 20 | 21 | #endif 22 | 23 | #ifdef TEST 24 | 25 | #include "_PDCLIB_test.h" 26 | 27 | int main( void ) 28 | { 29 | char s[] = "xxxxxxxxx"; 30 | TESTCASE( memset( s, 'o', 10 ) == s ); 31 | TESTCASE( s[9] == 'o' ); 32 | TESTCASE( memset( s, '_', (0) ) == s ); 33 | TESTCASE( s[0] == 'o' ); 34 | TESTCASE( memset( s, '_', 1 ) == s ); 35 | TESTCASE( s[0] == '_' ); 36 | TESTCASE( s[1] == 'o' ); 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /functions/string/strcat.c: -------------------------------------------------------------------------------- 1 | /* strcat( char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * strcat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 ) 12 | { 13 | char * rc = s1; 14 | if ( *s1 ) 15 | { 16 | while ( *++s1 ); 17 | } 18 | while ( (*s1++ = *s2++) ); 19 | return rc; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | 26 | #include "_PDCLIB_test.h" 27 | 28 | int main( void ) 29 | { 30 | char s[] = "xx\0xxxxxx"; 31 | TESTCASE( strcat( s, abcde ) == s ); 32 | TESTCASE( s[2] == 'a' ); 33 | TESTCASE( s[6] == 'e' ); 34 | TESTCASE( s[7] == '\0' ); 35 | TESTCASE( s[8] == 'x' ); 36 | s[0] = '\0'; 37 | TESTCASE( strcat( s, abcdx ) == s ); 38 | TESTCASE( s[4] == 'x' ); 39 | TESTCASE( s[5] == '\0' ); 40 | TESTCASE( strcat( s, "\0" ) == s ); 41 | TESTCASE( s[5] == '\0' ); 42 | TESTCASE( s[6] == 'e' ); 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /functions/string/strchr.c: -------------------------------------------------------------------------------- 1 | /* strchr( const char *, int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * strchr( const char * s, int c ) 12 | { 13 | do 14 | { 15 | if ( *s == (char) c ) 16 | { 17 | return (char *) s; 18 | } 19 | } while ( *s++ ); 20 | return NULL; 21 | } 22 | 23 | #endif 24 | 25 | #ifdef TEST 26 | 27 | #include "_PDCLIB_test.h" 28 | 29 | int main( void ) 30 | { 31 | char abccd[] = "abccd"; 32 | TESTCASE( strchr( abccd, 'x' ) == NULL ); 33 | TESTCASE( strchr( abccd, 'a' ) == &abccd[0] ); 34 | TESTCASE( strchr( abccd, 'd' ) == &abccd[4] ); 35 | TESTCASE( strchr( abccd, '\0' ) == &abccd[5] ); 36 | TESTCASE( strchr( abccd, 'c' ) == &abccd[2] ); 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /functions/string/strcmp.c: -------------------------------------------------------------------------------- 1 | /* strcmp( const char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int strcmp( const char * s1, const char * s2 ) 12 | { 13 | while ( ( *s1 ) && ( *s1 == *s2 ) ) 14 | { 15 | ++s1; 16 | ++s2; 17 | } 18 | return ( *(unsigned char *)s1 - *(unsigned char *)s2 ); 19 | } 20 | 21 | #endif 22 | 23 | #ifdef TEST 24 | 25 | #include "_PDCLIB_test.h" 26 | 27 | int main( void ) 28 | { 29 | char cmpabcde[] = "abcde"; 30 | char cmpabcd_[] = "abcd\xfc"; 31 | char empty[] = ""; 32 | TESTCASE( strcmp( abcde, cmpabcde ) == 0 ); 33 | TESTCASE( strcmp( abcde, abcdx ) < 0 ); 34 | TESTCASE( strcmp( abcdx, abcde ) > 0 ); 35 | TESTCASE( strcmp( empty, abcde ) < 0 ); 36 | TESTCASE( strcmp( abcde, empty ) > 0 ); 37 | TESTCASE( strcmp( abcde, cmpabcd_ ) < 0 ); 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /functions/string/strcoll.c: -------------------------------------------------------------------------------- 1 | /* strcoll( const char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int strcoll( const char * s1, const char * s2 ) 14 | { 15 | return strcmp( s1, s2 ); 16 | 17 | /* FIXME: This code became invalid when we started doing *real* locales... */ 18 | /* 19 | while ( ( *s1 ) && ( _PDCLIB_lc_ctype[(unsigned char)*s1].collation == _PDCLIB_lc_ctype[(unsigned char)*s2].collation ) ) 20 | { 21 | ++s1; 22 | ++s2; 23 | } 24 | return ( _PDCLIB_lc_ctype[(unsigned char)*s1].collation == _PDCLIB_lc_ctype[(unsigned char)*s2].collation ); 25 | */ 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | char cmpabcde[] = "abcde"; 37 | char empty[] = ""; 38 | TESTCASE( strcmp( abcde, cmpabcde ) == 0 ); 39 | TESTCASE( strcmp( abcde, abcdx ) < 0 ); 40 | TESTCASE( strcmp( abcdx, abcde ) > 0 ); 41 | TESTCASE( strcmp( empty, abcde ) < 0 ); 42 | TESTCASE( strcmp( abcde, empty ) > 0 ); 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /functions/string/strcpy.c: -------------------------------------------------------------------------------- 1 | /* strcpy( char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * strcpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 ) 12 | { 13 | char * rc = s1; 14 | while ( ( *s1++ = *s2++ ) ); 15 | return rc; 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | char s[] = "xxxxx"; 27 | TESTCASE( strcpy( s, "" ) == s ); 28 | TESTCASE( s[0] == '\0' ); 29 | TESTCASE( s[1] == 'x' ); 30 | TESTCASE( strcpy( s, abcde ) == s ); 31 | TESTCASE( s[0] == 'a' ); 32 | TESTCASE( s[4] == 'e' ); 33 | TESTCASE( s[5] == '\0' ); 34 | return TEST_RESULTS; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /functions/string/strcspn.c: -------------------------------------------------------------------------------- 1 | /* strcspn( const char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | size_t strcspn( const char * s1, const char * s2 ) 12 | { 13 | size_t len = 0; 14 | const char * p; 15 | while ( s1[len] ) 16 | { 17 | p = s2; 18 | while ( *p ) 19 | { 20 | if ( s1[len] == *p++ ) 21 | { 22 | return len; 23 | } 24 | } 25 | ++len; 26 | } 27 | return len; 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | TESTCASE( strcspn( abcde, "x" ) == 5 ); 39 | TESTCASE( strcspn( abcde, "xyz" ) == 5 ); 40 | TESTCASE( strcspn( abcde, "zyx" ) == 5 ); 41 | TESTCASE( strcspn( abcdx, "x" ) == 4 ); 42 | TESTCASE( strcspn( abcdx, "xyz" ) == 4 ); 43 | TESTCASE( strcspn( abcdx, "zyx" ) == 4 ); 44 | TESTCASE( strcspn( abcde, "a" ) == 0 ); 45 | TESTCASE( strcspn( abcde, "abc" ) == 0 ); 46 | TESTCASE( strcspn( abcde, "cba" ) == 0 ); 47 | return TEST_RESULTS; 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /functions/string/strerror.c: -------------------------------------------------------------------------------- 1 | /* strerror( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | /* TODO: Doing this via a static array is not the way to do it. */ 14 | char * strerror( int errnum ) 15 | { 16 | if ( errnum >= _PDCLIB_ERRNO_MAX || errnum < 0 ) 17 | { 18 | return _PDCLIB_EUNKNOWN_TEXT; 19 | } 20 | else 21 | { 22 | return _PDCLIB_lc_messages.errno_texts[errnum]; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | #include 33 | #include 34 | 35 | int main( void ) 36 | { 37 | TESTCASE( strerror( ERANGE ) != strerror( EDOM ) ); 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /functions/string/strerrorlen_s.c: -------------------------------------------------------------------------------- 1 | /* strerrorlen_s( errno_t error ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #define __STDC_WANT_LIB_EXT1__ 1 8 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | size_t strerrorlen_s( errno_t errnum ) 13 | { 14 | return strlen( strerror( errnum ) ); 15 | } 16 | 17 | #endif 18 | 19 | #ifdef TEST 20 | 21 | #include "_PDCLIB_test.h" 22 | 23 | int main( void ) 24 | { 25 | TESTCASE( NO_TESTDRIVER ); 26 | return TEST_RESULTS; 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /functions/string/strlen.c: -------------------------------------------------------------------------------- 1 | /* strlen( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | size_t strlen( const char * s ) 12 | { 13 | size_t rc = 0; 14 | while ( s[rc] ) 15 | { 16 | ++rc; 17 | } 18 | return rc; 19 | } 20 | 21 | #endif 22 | 23 | #ifdef TEST 24 | 25 | #include "_PDCLIB_test.h" 26 | 27 | int main( void ) 28 | { 29 | TESTCASE( strlen( abcde ) == 5 ); 30 | TESTCASE( strlen( "" ) == 0 ); 31 | return TEST_RESULTS; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /functions/string/strncat.c: -------------------------------------------------------------------------------- 1 | /* strncat( char *, const char *, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * strncat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n ) 12 | { 13 | char * rc = s1; 14 | while ( *s1 ) 15 | { 16 | ++s1; 17 | } 18 | while ( n && ( *s1++ = *s2++ ) ) 19 | { 20 | --n; 21 | } 22 | if ( n == 0 ) 23 | { 24 | *s1 = '\0'; 25 | } 26 | return rc; 27 | } 28 | 29 | #endif 30 | 31 | #ifdef TEST 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | int main( void ) 36 | { 37 | char s[] = "xx\0xxxxxx"; 38 | TESTCASE( strncat( s, abcde, 10 ) == s ); 39 | TESTCASE( s[2] == 'a' ); 40 | TESTCASE( s[6] == 'e' ); 41 | TESTCASE( s[7] == '\0' ); 42 | TESTCASE( s[8] == 'x' ); 43 | s[0] = '\0'; 44 | TESTCASE( strncat( s, abcdx, 10 ) == s ); 45 | TESTCASE( s[4] == 'x' ); 46 | TESTCASE( s[5] == '\0' ); 47 | TESTCASE( strncat( s, "\0", 10 ) == s ); 48 | TESTCASE( s[5] == '\0' ); 49 | TESTCASE( s[6] == 'e' ); 50 | TESTCASE( strncat( s, abcde, 0 ) == s ); 51 | TESTCASE( s[5] == '\0' ); 52 | TESTCASE( s[6] == 'e' ); 53 | TESTCASE( strncat( s, abcde, 3 ) == s ); 54 | TESTCASE( s[5] == 'a' ); 55 | TESTCASE( s[7] == 'c' ); 56 | TESTCASE( s[8] == '\0' ); 57 | return TEST_RESULTS; 58 | } 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /functions/string/strncmp.c: -------------------------------------------------------------------------------- 1 | /* strncmp( const char *, const char *, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | int strncmp( const char * s1, const char * s2, size_t n ) 12 | { 13 | while ( n && *s1 && ( *s1 == *s2 ) ) 14 | { 15 | ++s1; 16 | ++s2; 17 | --n; 18 | } 19 | if ( n == 0 ) 20 | { 21 | return 0; 22 | } 23 | else 24 | { 25 | return ( *(unsigned char *)s1 - *(unsigned char *)s2 ); 26 | } 27 | } 28 | 29 | #endif 30 | 31 | #ifdef TEST 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | int main( void ) 36 | { 37 | char cmpabcde[] = "abcde\0f"; 38 | char cmpabcd_[] = "abcde\xfc"; 39 | char empty[] = ""; 40 | char x[] = "x"; 41 | TESTCASE( strncmp( abcde, cmpabcde, 5 ) == 0 ); 42 | TESTCASE( strncmp( abcde, cmpabcde, 10 ) == 0 ); 43 | TESTCASE( strncmp( abcde, abcdx, 5 ) < 0 ); 44 | TESTCASE( strncmp( abcdx, abcde, 5 ) > 0 ); 45 | TESTCASE( strncmp( empty, abcde, 5 ) < 0 ); 46 | TESTCASE( strncmp( abcde, empty, 5 ) > 0 ); 47 | TESTCASE( strncmp( abcde, abcdx, 4 ) == 0 ); 48 | TESTCASE( strncmp( abcde, x, 0 ) == 0 ); 49 | TESTCASE( strncmp( abcde, x, 1 ) < 0 ); 50 | TESTCASE( strncmp( abcde, cmpabcd_, 10 ) < 0 ); 51 | return TEST_RESULTS; 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /functions/string/strncpy.c: -------------------------------------------------------------------------------- 1 | /* strncpy( char *, const char *, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * strncpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n ) 12 | { 13 | char * rc = s1; 14 | while ( n && ( *s1++ = *s2++ ) ) 15 | { 16 | /* Cannot do "n--" in the conditional as size_t is unsigned and we have 17 | to check it again for >0 in the next loop below, so we must not risk 18 | underflow. 19 | */ 20 | --n; 21 | } 22 | /* Checking against 1 as we missed the last --n in the loop above. */ 23 | while ( n-- > 1 ) 24 | { 25 | *s1++ = '\0'; 26 | } 27 | return rc; 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | char s[] = "xxxxxxx"; 39 | TESTCASE( strncpy( s, "", 1 ) == s ); 40 | TESTCASE( s[0] == '\0' ); 41 | TESTCASE( s[1] == 'x' ); 42 | TESTCASE( strncpy( s, abcde, 6 ) == s ); 43 | TESTCASE( s[0] == 'a' ); 44 | TESTCASE( s[4] == 'e' ); 45 | TESTCASE( s[5] == '\0' ); 46 | TESTCASE( s[6] == 'x' ); 47 | TESTCASE( strncpy( s, abcde, 7 ) == s ); 48 | TESTCASE( s[6] == '\0' ); 49 | TESTCASE( strncpy( s, "xxxx", 3 ) == s ); 50 | TESTCASE( s[0] == 'x' ); 51 | TESTCASE( s[2] == 'x' ); 52 | TESTCASE( s[3] == 'd' ); 53 | return TEST_RESULTS; 54 | } 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /functions/string/strpbrk.c: -------------------------------------------------------------------------------- 1 | /* strpbrk( const char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * strpbrk( const char * s1, const char * s2 ) 12 | { 13 | const char * p1 = s1; 14 | const char * p2; 15 | while ( *p1 ) 16 | { 17 | p2 = s2; 18 | while ( *p2 ) 19 | { 20 | if ( *p1 == *p2++ ) 21 | { 22 | return (char *) p1; 23 | } 24 | } 25 | ++p1; 26 | } 27 | return NULL; 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | TESTCASE( strpbrk( abcde, "x" ) == NULL ); 39 | TESTCASE( strpbrk( abcde, "xyz" ) == NULL ); 40 | TESTCASE( strpbrk( abcdx, "x" ) == &abcdx[4] ); 41 | TESTCASE( strpbrk( abcdx, "xyz" ) == &abcdx[4] ); 42 | TESTCASE( strpbrk( abcdx, "zyx" ) == &abcdx[4] ); 43 | TESTCASE( strpbrk( abcde, "a" ) == &abcde[0] ); 44 | TESTCASE( strpbrk( abcde, "abc" ) == &abcde[0] ); 45 | TESTCASE( strpbrk( abcde, "cba" ) == &abcde[0] ); 46 | return TEST_RESULTS; 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /functions/string/strrchr.c: -------------------------------------------------------------------------------- 1 | /* strrchr( const char *, int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * strrchr( const char * s, int c ) 12 | { 13 | size_t i = 0; 14 | while ( s[i++] ); 15 | do 16 | { 17 | if ( s[--i] == (char) c ) 18 | { 19 | return (char *) s + i; 20 | } 21 | } while ( i ); 22 | return NULL; 23 | } 24 | 25 | #endif 26 | 27 | #ifdef TEST 28 | 29 | #include "_PDCLIB_test.h" 30 | 31 | int main( void ) 32 | { 33 | char abccd[] = "abccd"; 34 | TESTCASE( strrchr( abcde, '\0' ) == &abcde[5] ); 35 | TESTCASE( strrchr( abcde, 'e' ) == &abcde[4] ); 36 | TESTCASE( strrchr( abcde, 'a' ) == &abcde[0] ); 37 | TESTCASE( strrchr( abccd, 'c' ) == &abccd[3] ); 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /functions/string/strspn.c: -------------------------------------------------------------------------------- 1 | /* strspn( const char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | size_t strspn( const char * s1, const char * s2 ) 12 | { 13 | size_t len = 0; 14 | const char * p; 15 | while ( s1[ len ] ) 16 | { 17 | p = s2; 18 | while ( *p ) 19 | { 20 | if ( s1[len] == *p ) 21 | { 22 | break; 23 | } 24 | ++p; 25 | } 26 | if ( ! *p ) 27 | { 28 | return len; 29 | } 30 | ++len; 31 | } 32 | return len; 33 | } 34 | 35 | #endif 36 | 37 | #ifdef TEST 38 | 39 | #include "_PDCLIB_test.h" 40 | 41 | int main( void ) 42 | { 43 | TESTCASE( strspn( abcde, "abc" ) == 3 ); 44 | TESTCASE( strspn( abcde, "b" ) == 0 ); 45 | TESTCASE( strspn( abcde, abcde ) == 5 ); 46 | return TEST_RESULTS; 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /functions/string/strstr.c: -------------------------------------------------------------------------------- 1 | /* strstr( const char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * strstr( const char * s1, const char * s2 ) 12 | { 13 | const char * p1 = s1; 14 | const char * p2; 15 | while ( *s1 ) 16 | { 17 | p2 = s2; 18 | while ( *p2 && ( *p1 == *p2 ) ) 19 | { 20 | ++p1; 21 | ++p2; 22 | } 23 | if ( ! *p2 ) 24 | { 25 | return (char *) s1; 26 | } 27 | ++s1; 28 | p1 = s1; 29 | } 30 | return NULL; 31 | } 32 | 33 | #endif 34 | 35 | #ifdef TEST 36 | 37 | #include "_PDCLIB_test.h" 38 | 39 | int main( void ) 40 | { 41 | char s[] = "abcabcabcdabcde"; 42 | TESTCASE( strstr( s, "x" ) == NULL ); 43 | TESTCASE( strstr( s, "xyz" ) == NULL ); 44 | TESTCASE( strstr( s, "a" ) == &s[0] ); 45 | TESTCASE( strstr( s, "abc" ) == &s[0] ); 46 | TESTCASE( strstr( s, "abcd" ) == &s[6] ); 47 | TESTCASE( strstr( s, "abcde" ) == &s[10] ); 48 | return TEST_RESULTS; 49 | } 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /functions/string/strxfrm.c: -------------------------------------------------------------------------------- 1 | /* strxfrm( char *, const char *, size_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | size_t strxfrm( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n ) 14 | { 15 | size_t len = strlen( s2 ); 16 | if ( len < n ) 17 | { 18 | /* Cannot use strncpy() here as the filling of s1 with '\0' is not part 19 | of the spec. 20 | */ 21 | /* FIXME: The code below became invalid when we started doing *real* locales... */ 22 | /*while ( n-- && ( *s1++ = _PDCLIB_lc_collate[(unsigned char)*s2++].collation ) );*/ 23 | while ( n-- && ( *s1++ = (unsigned char)*s2++ ) ); 24 | } 25 | return len; 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | char s[] = "xxxxxxxxxxx"; 37 | TESTCASE( strxfrm( NULL, "123456789012", 0 ) == 12 ); 38 | TESTCASE( strxfrm( s, "123456789012", 12 ) == 12 ); 39 | /* 40 | The following test case is true in *this* implementation, but doesn't have to. 41 | TESTCASE( s[0] == 'x' ); 42 | */ 43 | TESTCASE( strxfrm( s, "1234567890", 11 ) == 10 ); 44 | TESTCASE( s[0] == '1' ); 45 | TESTCASE( s[9] == '0' ); 46 | TESTCASE( s[10] == '\0' ); 47 | return TEST_RESULTS; 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /functions/time/asctime.c: -------------------------------------------------------------------------------- 1 | /* asctime( const struct tm * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * asctime( const struct tm * timeptr ) 12 | { 13 | return NULL; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | TESTCASE( NO_TESTDRIVER ); 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/time/ctime.c: -------------------------------------------------------------------------------- 1 | /* ctime( const time_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | char * ctime( const time_t * timer ) 12 | { 13 | return NULL; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | TESTCASE( NO_TESTDRIVER ); 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/time/localtime.c: -------------------------------------------------------------------------------- 1 | /* localtime( const time_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | struct tm * localtime( const time_t * timer ) 12 | { 13 | return NULL; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | TESTCASE( NO_TESTDRIVER ); 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /functions/time/mktime.c: -------------------------------------------------------------------------------- 1 | /* mktime( struct tm * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | time_t mktime( struct tm * timeptr ) 12 | { 13 | return -1; 14 | } 15 | 16 | #endif 17 | 18 | #ifdef TEST 19 | 20 | #include "_PDCLIB_test.h" 21 | 22 | int main( void ) 23 | { 24 | TESTCASE( NO_TESTDRIVER ); 25 | return TEST_RESULTS; 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /include/iso646.h: -------------------------------------------------------------------------------- 1 | /* Alternative spellings 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef _PDCLIB_ISO646_H 8 | #define _PDCLIB_ISO646_H _PDCLIB_ISO646_H 9 | 10 | #ifndef __cplusplus 11 | #define and && 12 | #define and_eq &= 13 | #define bitand & 14 | #define bitor | 15 | #define compl ~ 16 | #define not ! 17 | #define not_eq != 18 | #define or || 19 | #define or_eq |= 20 | #define xor ^ 21 | #define xor_eq ^= 22 | #endif 23 | 24 | /* Extension hook for downstream projects that want to have non-standard 25 | extensions to standard headers. 26 | */ 27 | #ifdef _PDCLIB_EXTEND_ISO646_H 28 | #include _PDCLIB_EXTEND_ISO646_H 29 | #endif 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /include/limits.h: -------------------------------------------------------------------------------- 1 | /* Sizes of integer types 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef _PDCLIB_LIMITS_H 8 | #define _PDCLIB_LIMITS_H _PDCLIB_LIMITS_H 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | #include "pdclib/_PDCLIB_int.h" 15 | 16 | /* TODO: Defined to 1 as multibyte characters are not supported yet. */ 17 | #define MB_LEN_MAX 1 18 | 19 | #define LLONG_MIN _PDCLIB_LLONG_MIN 20 | #define LLONG_MAX _PDCLIB_LLONG_MAX 21 | #define ULLONG_MAX _PDCLIB_ULLONG_MAX 22 | 23 | #define CHAR_BIT _PDCLIB_CHAR_BIT 24 | #define CHAR_MAX _PDCLIB_CHAR_MAX 25 | #define CHAR_MIN _PDCLIB_CHAR_MIN 26 | #define SCHAR_MAX _PDCLIB_SCHAR_MAX 27 | #define SCHAR_MIN _PDCLIB_SCHAR_MIN 28 | #define UCHAR_MAX _PDCLIB_UCHAR_MAX 29 | #define SHRT_MAX _PDCLIB_SHRT_MAX 30 | #define SHRT_MIN _PDCLIB_SHRT_MIN 31 | #define INT_MAX _PDCLIB_INT_MAX 32 | #define INT_MIN _PDCLIB_INT_MIN 33 | #define LONG_MAX _PDCLIB_LONG_MAX 34 | #define LONG_MIN _PDCLIB_LONG_MIN 35 | #define USHRT_MAX _PDCLIB_USHRT_MAX 36 | #define UINT_MAX _PDCLIB_UINT_MAX 37 | #define ULONG_MAX _PDCLIB_ULONG_MAX 38 | 39 | /* Extension hook for downstream projects that want to have non-standard 40 | extensions to standard headers. 41 | */ 42 | #ifdef _PDCLIB_EXTEND_LIMITS_H 43 | #include _PDCLIB_EXTEND_LIMITS_H 44 | #endif 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /include/pdclib/_PDCLIB_lib_ext1.h: -------------------------------------------------------------------------------- 1 | /* __STDC_WANT_LIB_EXT1__ redefinition guard 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef __STDC_WANT_LIB_EXT1__ 8 | #ifdef __STDC_WANT_LIB_EXT1_PREVIOUS__ 9 | #if __STDC_WANT_LIB_EXT1_PREVIOUS__ != -1 10 | #error __STDC_WANT_LIB_EXT1__ undefined when it was defined earlier. 11 | #endif 12 | #else 13 | #define __STDC_WANT_LIB_EXT1_PREVIOUS__ -1 14 | #endif 15 | #else 16 | #if ( __STDC_WANT_LIB_EXT1__ + 0 ) == 0 && ( 0 - __STDC_WANT_LIB_EXT1__ - 1 ) == 1 17 | #error __STDC_WANT_LIB_EXT1__ defined but empty. Should be an integer value. 18 | #endif 19 | #ifdef __STDC_WANT_LIB_EXT1_PREVIOUS__ 20 | #if ( __STDC_WANT_LIB_EXT1__ + 0 ) != __STDC_WANT_LIB_EXT1_PREVIOUS__ 21 | #error __STDC_WANT_LIB_EXT1__ redefined from previous value. 22 | #endif 23 | #else 24 | #if ( __STDC_WANT_LIB_EXT1__ + 0 ) == 0 25 | #define __STDC_WANT_LIB_EXT1_PREVIOUS__ 0 26 | #elif ( __STDC_WANT_LIB_EXT1__ + 0 ) == 1 27 | #define __STDC_WANT_LIB_EXT1_PREVIOUS__ 1 28 | #else 29 | #error __STDC_WANT_LIB_EXT1__ set to value other than 0,1 -- undefined behavior 30 | #endif 31 | #endif 32 | #endif 33 | -------------------------------------------------------------------------------- /include/stdalign.h: -------------------------------------------------------------------------------- 1 | /* Alignment 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef _PDCLIB_STDALIGN_H 8 | #define _PDCLIB_ALIGN_H _PDCLIB_ALIGN_H 9 | 10 | #ifndef __cplusplus 11 | #define alignas _Alignas 12 | #define alignof _Alignof 13 | #endif 14 | 15 | #define __alignas_is_defined 1 16 | #define __alignof_is_defined 1 17 | 18 | /* Extension hook for downstream projects that want to have non-standard 19 | extensions to standard headers. 20 | */ 21 | #ifdef _PDCLIB_EXTEND_STDALIGN_H 22 | #include _PDCLIB_EXTEND_STDALIGN_H 23 | #endif 24 | 25 | #endif 26 | 27 | -------------------------------------------------------------------------------- /include/stdarg.h: -------------------------------------------------------------------------------- 1 | /* Variable arguments 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef _PDCLIB_STDARG_H 8 | #define _PDCLIB_STDARG_H _PDCLIB_STDARG_H 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | #include "pdclib/_PDCLIB_config.h" 15 | 16 | typedef _PDCLIB_va_list va_list; 17 | 18 | #define va_arg( ap, type ) _PDCLIB_va_arg( ap, type ) 19 | #define va_copy( dest, src ) _PDCLIB_va_copy( dest, src ) 20 | #define va_end( ap ) _PDCLIB_va_end( ap ) 21 | #define va_start( ap, parmN ) _PDCLIB_va_start( ap, parmN ) 22 | 23 | /* Extension hook for downstream projects that want to have non-standard 24 | extensions to standard headers. 25 | */ 26 | #ifdef _PDCLIB_EXTEND_STDARG_H 27 | #include _PDCLIB_EXTEND_STDARG_H 28 | #endif 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/stdbool.h: -------------------------------------------------------------------------------- 1 | /* Boolean type and values 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef _PDCLIB_STDBOOL_H 8 | #define _PDCLIB_STDBOOL_H _PDCLIB_STDBOOL_H 9 | 10 | #ifndef __cplusplus 11 | #define bool _Bool 12 | #define true 1 13 | #define false 0 14 | #endif 15 | #define __bool_true_false_are_defined 1 16 | 17 | /* Extension hook for downstream projects that want to have non-standard 18 | extensions to standard headers. 19 | */ 20 | #ifdef _PDCLIB_EXTEND_STDBOOL_H 21 | #include _PDCLIB_EXTEND_STDBOOL_H 22 | #endif 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /include/stddef.h: -------------------------------------------------------------------------------- 1 | /* Common definitions 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef _PDCLIB_STDDEF_H 8 | #define _PDCLIB_STDDEF_H _PDCLIB_STDDEF_H 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | #include "pdclib/_PDCLIB_lib_ext1.h" 15 | #include "pdclib/_PDCLIB_int.h" 16 | 17 | typedef _PDCLIB_ptrdiff_t ptrdiff_t; 18 | 19 | #ifndef _PDCLIB_SIZE_T_DEFINED 20 | #define _PDCLIB_SIZE_T_DEFINED _PDCLIB_SIZE_T_DEFINED 21 | typedef _PDCLIB_size_t size_t; 22 | #endif 23 | 24 | #ifndef __cplusplus 25 | typedef _PDCLIB_wchar_t wchar_t; 26 | #endif 27 | 28 | #ifndef _PDCLIB_NULL_DEFINED 29 | #define _PDCLIB_NULL_DEFINED _PDCLIB_NULL_DEFINED 30 | #define NULL _PDCLIB_NULL 31 | #endif 32 | 33 | #define offsetof( type, member ) _PDCLIB_offsetof( type, member ) 34 | 35 | /* Annex K -- Bounds-checking interfaces */ 36 | 37 | #if ( __STDC_WANT_LIB_EXT1__ + 0 ) != 0 38 | #ifndef _PDCLIB_RSIZE_T_DEFINED 39 | #define _PDCLIB_RSIZE_T_DEFINED _PDCLIB_RSIZE_T_DEFINED 40 | typedef size_t rsize_t; 41 | #endif 42 | #endif 43 | 44 | /* Extension hook for downstream projects that want to have non-standard 45 | extensions to standard headers. 46 | */ 47 | #ifdef _PDCLIB_EXTEND_STDDEF_H 48 | #include _PDCLIB_EXTEND_STDDEF_H 49 | #endif 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /include/stdnoreturn.h: -------------------------------------------------------------------------------- 1 | /* _Noreturn 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef _PDCLIB_STDNORETURN_H 8 | #define _PDCLIB_STDNORETURN_H _PDCLIB_STDNORETURN_H 9 | 10 | #include "pdclib/_PDCLIB_aux.h" 11 | 12 | /* This basically breaks the letter of the standard (which states that 13 | noreturn be defined to _Noreturn). This defines noreturn -> _Noreturn 14 | on C11 compliant compilers only (as older compilers do not know about 15 | _Noreturn). 16 | */ 17 | #define noreturn _PDCLIB_Noreturn 18 | 19 | /* Extension hook for downstream projects that want to have non-standard 20 | extensions to standard headers. 21 | */ 22 | #ifdef _PDCLIB_EXTEND_STDNORETURN_H 23 | #include _PDCLIB_EXTEND_STDNORETURN_H 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /platform/example/Readme.txt: -------------------------------------------------------------------------------- 1 | "Example" Platform Overlay 2 | ========================== 3 | 4 | This is an example platform overlay, as described in the main Readme.txt of 5 | this archive. For ease of development, it applies (and tests) correctly on the 6 | machine of the author; no other guarantees can be given. 7 | It should give you a good idea of what is REQUIRED to make a copy of PDCLib 8 | work. There is a lot more you could do, and even some things you SHOULD do, in 9 | order to experience anything but abysmal performance: 10 | 11 | - Read / write operations on binary streams, and even on text streams for 12 | machines that do not do any text conversion, can be made much more efficient 13 | by using some sort of page buffer instead of the linear buffer implemented 14 | here. It requires some special and platform-dependent manipulations, though, 15 | which is why it is not done by default. 16 | 17 | - Anything relating to floating point logic is written in generic C. While 18 | this is (hopefully) highly portable and should get you started on your 19 | platform of choice, it is also highly inefficient and should be replaced by 20 | inline assembly. Just make sure that your assembly keeps all the promises 21 | the C library makes. 22 | -------------------------------------------------------------------------------- /platform/example/functions/_PDCLIB/_PDCLIB_Exit.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_Exit( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an example implementation of _PDCLIB_Exit() fit for use with POSIX 8 | kernels. 9 | */ 10 | 11 | #include 12 | 13 | #ifndef REGTEST 14 | 15 | #include "pdclib/_PDCLIB_glue.h" 16 | 17 | extern void _exit( int status ) _PDCLIB_NORETURN; 18 | 19 | void _PDCLIB_Exit( int status ) 20 | { 21 | _exit( status ); 22 | } 23 | 24 | #endif 25 | 26 | #ifdef TEST 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | int main( void ) 31 | { 32 | #ifndef REGTEST 33 | int UNEXPECTED_RETURN = 0; 34 | _PDCLIB_Exit( 0 ); 35 | TESTCASE( UNEXPECTED_RETURN ); 36 | #endif 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/_PDCLIB/_PDCLIB_close.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_close( _PDCLIB_fd_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an example implementation of _PDCLIB_close() fit for use with POSIX 8 | kernels. 9 | */ 10 | 11 | #include 12 | 13 | #ifndef REGTEST 14 | 15 | #include "pdclib/_PDCLIB_glue.h" 16 | 17 | extern int close( int fd ); 18 | 19 | int _PDCLIB_close( int fd ) 20 | { 21 | return close( fd ); 22 | } 23 | 24 | #endif 25 | 26 | #ifdef TEST 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | int main( void ) 31 | { 32 | /* No testdriver; tested in driver for _PDCLIB_open(). */ 33 | return TEST_RESULTS; 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /platform/example/functions/stdio/remove.c: -------------------------------------------------------------------------------- 1 | /* remove( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an example implementation of remove() fit for use with POSIX kernels. 8 | */ 9 | 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include 15 | 16 | #include "/usr/include/errno.h" 17 | 18 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 19 | 20 | extern int unlink( const char * pathname ); 21 | 22 | int remove( const char * pathname ) 23 | { 24 | int rc; 25 | struct _PDCLIB_file_t * current = _PDCLIB_filelist; 26 | while ( current != NULL ) 27 | { 28 | if ( ( current->filename != NULL ) && ( strcmp( current->filename, pathname ) == 0 ) ) 29 | { 30 | return EOF; 31 | } 32 | current = current->next; 33 | } 34 | if ( ( rc = unlink( pathname ) ) == -1 ) 35 | { 36 | /* The 1:1 mapping in _PDCLIB_config.h ensures this works. */ 37 | *_PDCLIB_errno_func() = errno; 38 | } 39 | return rc; 40 | } 41 | 42 | #endif 43 | 44 | #ifdef TEST 45 | 46 | #include "_PDCLIB_test.h" 47 | 48 | int main( void ) 49 | { 50 | /* Testing covered by ftell.c (and several others) */ 51 | return TEST_RESULTS; 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /platform/example/functions/stdlib/getenv.c: -------------------------------------------------------------------------------- 1 | /* getenv( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an example implementation of getenv() fit for use with POSIX kernels. 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #ifndef REGTEST 14 | 15 | extern char * * environ; 16 | 17 | /* The standard states (7.22.4.6 (3), "the implementation shall behave 18 | as if no library function calls the getenv function." That is, 19 | however, in context of the previous paragraph stating that getenv 20 | "need not avoid data races with other threads of execution that 21 | modify the environment list". 22 | PDCLib does not provide means of modifying the environment list. 23 | */ 24 | char * getenv( const char * name ) 25 | { 26 | size_t len = strlen( name ); 27 | size_t index = 0; 28 | while ( environ[ index ] != NULL ) 29 | { 30 | if ( strncmp( environ[ index ], name, len ) == 0 ) 31 | { 32 | return environ[ index ] + len + 1; 33 | } 34 | index++; 35 | } 36 | return NULL; 37 | } 38 | 39 | #endif 40 | 41 | #ifdef TEST 42 | 43 | #include "_PDCLIB_test.h" 44 | 45 | int main( void ) 46 | { 47 | TESTCASE( strcmp( getenv( "SHELL" ), "/bin/bash" ) == 0 ); 48 | /* TESTCASE( strcmp( getenv( "SHELL" ), "/bin/sh" ) == 0 ); */ 49 | return TEST_RESULTS; 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /platform/example/functions/threads/call_once.c: -------------------------------------------------------------------------------- 1 | /* call_once( once_flag *, void (*)( void ) ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter. */ 12 | extern int pthread_once( once_flag *, void (*)( void ) ); 13 | 14 | void call_once( once_flag * flag, void (*func)( void ) ) 15 | { 16 | pthread_once( flag, func ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | #ifndef REGTEST 28 | TESTCASE( NO_TESTDRIVER ); 29 | #endif 30 | return TEST_RESULTS; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /platform/example/functions/threads/cnd_broadcast.c: -------------------------------------------------------------------------------- 1 | /* cnd_broadcast( cnd_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter. */ 12 | extern int pthread_cond_broadcast( cnd_t *); 13 | 14 | int cnd_broadcast( cnd_t * cond ) 15 | { 16 | if ( pthread_cond_broadcast( cond ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | #ifndef REGTEST 35 | TESTCASE( NO_TESTDRIVER ); 36 | #endif 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/threads/cnd_destroy.c: -------------------------------------------------------------------------------- 1 | /* cnd_destroy( cnd_t * cond ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter. */ 12 | extern int pthread_cond_destroy( cnd_t * ); 13 | 14 | void cnd_destroy( cnd_t * cond ) 15 | { 16 | pthread_cond_destroy( cond ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | #ifndef REGTEST 28 | TESTCASE( NO_TESTDRIVER ); 29 | #endif 30 | return TEST_RESULTS; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /platform/example/functions/threads/cnd_init.c: -------------------------------------------------------------------------------- 1 | /* cnd_init( cnd_t * cond ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameters. */ 12 | extern int pthread_cond_init( cnd_t *, _PDCLIB_cnd_attr_t * ); 13 | 14 | int cnd_init( cnd_t * cond ) 15 | { 16 | if ( pthread_cond_init( cond, NULL ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | #ifndef REGTEST 35 | TESTCASE( NO_TESTDRIVER ); 36 | #endif 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/threads/cnd_signal.c: -------------------------------------------------------------------------------- 1 | /* cnd_signal( cnd_t * cond ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter. */ 12 | extern int pthread_cond_signal( cnd_t * ); 13 | 14 | int cnd_signal( cnd_t * cond ) 15 | { 16 | if ( pthread_cond_signal( cond ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | #ifndef REGTEST 35 | TESTCASE( NO_TESTDRIVER ); 36 | #endif 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/threads/cnd_timedwait.c: -------------------------------------------------------------------------------- 1 | /* cnd_timedwait( cnd_t *, mtx_t, const struct timespec * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | #include "/usr/include/errno.h" 12 | 13 | /* Implicitly casing the parameters. */ 14 | extern int pthread_cond_timedwait( cnd_t *, mtx_t *, const struct timespec * ); 15 | 16 | int cnd_timedwait( cnd_t * _PDCLIB_restrict cond, mtx_t * _PDCLIB_restrict mtx, const struct timespec * _PDCLIB_restrict ts ) 17 | { 18 | switch ( pthread_cond_timedwait( cond, mtx, ts ) ) 19 | { 20 | case 0: 21 | return thrd_success; 22 | case ETIMEDOUT: 23 | return thrd_timedout; 24 | default: 25 | return thrd_error; 26 | } 27 | } 28 | 29 | #endif 30 | 31 | #ifdef TEST 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | int main( void ) 36 | { 37 | #ifndef REGTEST 38 | TESTCASE( NO_TESTDRIVER ); 39 | #endif 40 | return TEST_RESULTS; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /platform/example/functions/threads/cnd_wait.c: -------------------------------------------------------------------------------- 1 | /* cnd_wait( cnd_t *, mtx_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameters. */ 12 | extern int pthread_cond_wait( cnd_t *, mtx_t * ); 13 | 14 | int cnd_wait( cnd_t * cond, mtx_t * mtx ) 15 | { 16 | if ( pthread_cond_wait( cond, mtx ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | #ifndef REGTEST 35 | TESTCASE( NO_TESTDRIVER ); 36 | #endif 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/threads/mtx_destroy.c: -------------------------------------------------------------------------------- 1 | /* mtx_destroy( mtx_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter. */ 12 | extern int pthread_mutex_destroy( mtx_t * ); 13 | 14 | void mtx_destroy( mtx_t * mtx ) 15 | { 16 | pthread_mutex_destroy( mtx ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | #ifndef REGTEST 28 | TESTCASE( NO_TESTDRIVER ); 29 | #endif 30 | return TEST_RESULTS; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /platform/example/functions/threads/mtx_lock.c: -------------------------------------------------------------------------------- 1 | /* mtx_lock( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter. */ 12 | extern int pthread_mutex_lock( mtx_t * ); 13 | 14 | int mtx_lock( mtx_t * mtx ) 15 | { 16 | if ( pthread_mutex_lock( mtx ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | #ifndef REGTEST 33 | 34 | #define COUNT 10 35 | thrd_t g_thread[COUNT]; 36 | mtx_t g_mutex; 37 | 38 | static int func( void * arg ) 39 | { 40 | TESTCASE( mtx_lock( &g_mutex ) == thrd_success ); 41 | thrd_yield(); 42 | TESTCASE( mtx_unlock( &g_mutex ) == thrd_success ); 43 | return 0; 44 | } 45 | 46 | #endif 47 | 48 | int main( void ) 49 | { 50 | #ifndef REGTEST 51 | TESTCASE( mtx_init( &g_mutex, mtx_plain ) == thrd_success ); 52 | 53 | for ( unsigned i = 0; i < COUNT; ++i ) 54 | { 55 | TESTCASE( thrd_create( &g_thread[i], func, NULL ) == thrd_success ); 56 | } 57 | 58 | for ( unsigned i = 0; i < COUNT; ++i ) 59 | { 60 | TESTCASE( thrd_join( g_thread[i], NULL ) == thrd_success ); 61 | } 62 | #endif 63 | return TEST_RESULTS; 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /platform/example/functions/threads/mtx_timedlock.c: -------------------------------------------------------------------------------- 1 | /* mtx_timedlock( mtx_t *, const struct timespec * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | #include "/usr/include/errno.h" 12 | 13 | /* Implicitly casting the first parameters. */ 14 | extern int pthread_mutex_timedlock( mtx_t *, const struct timespec * ); 15 | 16 | int mtx_timedlock( mtx_t * _PDCLIB_restrict mtx, const struct timespec * _PDCLIB_restrict ts ) 17 | { 18 | switch ( pthread_mutex_timedlock( mtx, ts ) ) 19 | { 20 | case 0: 21 | return thrd_success; 22 | case ETIMEDOUT: 23 | return thrd_timedout; 24 | default: 25 | return thrd_error; 26 | } 27 | } 28 | 29 | #endif 30 | 31 | #ifdef TEST 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | int main( void ) 36 | { 37 | #ifndef REGTEST 38 | TESTCASE( NO_TESTDRIVER ); 39 | #endif 40 | return TEST_RESULTS; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /platform/example/functions/threads/mtx_trylock.c: -------------------------------------------------------------------------------- 1 | /* mtx_trylock( mtx_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | #include "/usr/include/errno.h" 12 | 13 | /* Implicity casting the parameter. */ 14 | extern int pthread_mutex_trylock( mtx_t * ); 15 | 16 | int mtx_trylock( mtx_t * mtx ) 17 | { 18 | switch ( pthread_mutex_trylock( mtx ) ) 19 | { 20 | case 0: 21 | return thrd_success; 22 | case EBUSY: 23 | return thrd_busy; 24 | default: 25 | return thrd_error; 26 | } 27 | } 28 | 29 | #endif 30 | 31 | #ifdef TEST 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | int main( void ) 36 | { 37 | #ifndef REGTEST 38 | TESTCASE( NO_TESTDRIVER ); 39 | #endif 40 | return TEST_RESULTS; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /platform/example/functions/threads/mtx_unlock.c: -------------------------------------------------------------------------------- 1 | /* mtx_unlock( mtx_t * mtx ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicity casting the parameter. */ 12 | extern int pthread_mutex_unlock( mtx_t * ); 13 | 14 | int mtx_unlock( mtx_t * mtx ) 15 | { 16 | if ( pthread_mutex_unlock( mtx ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | /* Tested by the mtx_lock test driver. */ 35 | return TEST_RESULTS; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /platform/example/functions/threads/thrd_create.c: -------------------------------------------------------------------------------- 1 | /* thrd_create( thrd_t *, thrd_start_t, void * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the first parameters. */ 12 | extern int pthread_create( thrd_t *, const _PDCLIB_thrd_attr_t *, thrd_start_t, void *); 13 | 14 | int thrd_create( thrd_t * thr, thrd_start_t func, void * arg ) 15 | { 16 | if ( pthread_create( thr, NULL, func, arg ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | #ifndef REGTEST 33 | 34 | #define COUNT 10 35 | thrd_t g_thread[ COUNT ] = {0}; 36 | int g_count = 0; 37 | 38 | static int func( void * arg ) 39 | { 40 | ++g_count; 41 | return 0; 42 | } 43 | 44 | #endif 45 | 46 | int main( void ) 47 | { 48 | #ifndef REGTEST 49 | for ( unsigned i = 0; i < COUNT; ++i ) 50 | { 51 | TESTCASE( thrd_create( &g_thread[i], func, NULL ) == thrd_success ); 52 | } 53 | 54 | for ( unsigned i = 0; i < COUNT; ++i ) 55 | { 56 | TESTCASE( thrd_join( g_thread[i], NULL ) == thrd_success ); 57 | } 58 | 59 | TESTCASE( g_count == COUNT ); 60 | #endif 61 | return TEST_RESULTS; 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /platform/example/functions/threads/thrd_current.c: -------------------------------------------------------------------------------- 1 | /* thrd_current( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the return type. */ 12 | extern thrd_t pthread_self( void ); 13 | 14 | thrd_t thrd_current( void ) 15 | { 16 | return (thrd_t)pthread_self(); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | #ifndef REGTEST 26 | 27 | #define COUNT 10 28 | thrd_t g_thread[COUNT]; 29 | unsigned g_index[COUNT] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u }; 30 | 31 | static int func( void * arg ) 32 | { 33 | TESTCASE( thrd_equal( g_thread[ *(unsigned *)arg ], thrd_current() ) != 0 ); 34 | return 0; 35 | } 36 | 37 | #endif 38 | 39 | int main( void ) 40 | { 41 | #ifndef REGTEST 42 | for ( unsigned i = 0; i < COUNT; ++i ) 43 | { 44 | TESTCASE( thrd_create( &g_thread[i], func, &g_index[i] ) == thrd_success ); 45 | } 46 | 47 | for ( unsigned i = 0; i < COUNT; ++i ) 48 | { 49 | TESTCASE( thrd_join( g_thread[i], NULL ) == thrd_success ); 50 | } 51 | #endif 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /platform/example/functions/threads/thrd_detach.c: -------------------------------------------------------------------------------- 1 | /* thrd_detach( thrd_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter */ 12 | extern int pthread_detach( thrd_t ); 13 | 14 | int thrd_detach( thrd_t thr ) 15 | { 16 | if ( pthread_detach( thr ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | #ifndef REGTEST 35 | TESTCASE( NO_TESTDRIVER ); 36 | #endif 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/threads/thrd_equal.c: -------------------------------------------------------------------------------- 1 | /* thrd_equal( thrd_t, thrd_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameters. */ 12 | extern int pthread_equal( thrd_t, thrd_t ); 13 | 14 | int thrd_equal( thrd_t thr0, thrd_t thr1 ) 15 | { 16 | return pthread_equal( thr0, thr1 ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | /* Tested by thrd_current test driver. */ 28 | return TEST_RESULTS; 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /platform/example/functions/threads/thrd_exit.c: -------------------------------------------------------------------------------- 1 | /* thrd_exit( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | extern void pthread_exit( void * ) _PDCLIB_NORETURN; 12 | 13 | void thrd_exit( int res ) 14 | { 15 | /* TODO: return value */ 16 | pthread_exit( &res ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | #ifndef REGTEST 28 | TESTCASE( NO_TESTDRIVER ); 29 | #endif 30 | return TEST_RESULTS; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /platform/example/functions/threads/thrd_join.c: -------------------------------------------------------------------------------- 1 | /* thrd_join( thrd_t, int * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the first parameter. */ 12 | extern int pthread_join( thrd_t, void ** ); 13 | 14 | int thrd_join( thrd_t thr, int * res ) 15 | { 16 | /* TODO: return value */ 17 | if ( pthread_join( thr, (void **)&res ) == 0 ) 18 | { 19 | return thrd_success; 20 | } 21 | else 22 | { 23 | return thrd_error; 24 | } 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | int main( void ) 34 | { 35 | #ifndef REGTEST 36 | TESTCASE( NO_TESTDRIVER ); 37 | #endif 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /platform/example/functions/threads/thrd_sleep.c: -------------------------------------------------------------------------------- 1 | /* thrd_sleep( const struct timespec *, struct timespec * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | #include "/usr/include/errno.h" 12 | 13 | /* Implicitly casting the parameters. */ 14 | extern int nanosleep( const struct timespec *, struct timespec * ); 15 | 16 | int thrd_sleep( const struct timespec * duration, struct timespec * remaining ) 17 | { 18 | errno = 0; 19 | 20 | if ( nanosleep( duration, remaining ) == 0 ) 21 | { 22 | return 0; 23 | } 24 | 25 | if ( errno == EINTR ) 26 | { 27 | return -1; 28 | } 29 | 30 | return -2; 31 | } 32 | 33 | #endif 34 | 35 | #ifdef TEST 36 | 37 | #include "_PDCLIB_test.h" 38 | 39 | int main( void ) 40 | { 41 | #ifndef REGTEST 42 | TESTCASE( NO_TESTDRIVER ); 43 | #endif 44 | return TEST_RESULTS; 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /platform/example/functions/threads/thrd_yield.c: -------------------------------------------------------------------------------- 1 | /* thrd_yield( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | extern int pthread_yield( void ); 12 | 13 | void thrd_yield( void ) 14 | { 15 | pthread_yield(); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | #ifndef REGTEST 27 | TESTCASE( NO_TESTDRIVER ); 28 | #endif 29 | return TEST_RESULTS; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /platform/example/functions/threads/tss_create.c: -------------------------------------------------------------------------------- 1 | /* tss_create( tss_t *, tss_dtor_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameters. */ 12 | extern int pthread_key_create( tss_t *, tss_dtor_t ); 13 | 14 | int tss_create( tss_t * key, tss_dtor_t dtor ) 15 | { 16 | if ( pthread_key_create( key, dtor ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | #ifndef REGTEST 35 | TESTCASE( NO_TESTDRIVER ); 36 | #endif 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/threads/tss_delete.c: -------------------------------------------------------------------------------- 1 | /* tss_delete( tss_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter. */ 12 | extern int pthread_key_delete( tss_t ); 13 | 14 | void tss_delete( tss_t key ) 15 | { 16 | pthread_key_delete( key ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | #ifndef REGTEST 28 | TESTCASE( NO_TESTDRIVER ); 29 | #endif 30 | return TEST_RESULTS; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /platform/example/functions/threads/tss_get.c: -------------------------------------------------------------------------------- 1 | /* tss_get( tss_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the parameter. */ 12 | extern void * pthread_getspecific( tss_t ); 13 | 14 | void * tss_get( tss_t key ) 15 | { 16 | return pthread_getspecific( key ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | #ifndef REGTEST 28 | TESTCASE( NO_TESTDRIVER ); 29 | #endif 30 | return TEST_RESULTS; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /platform/example/functions/threads/tss_set.c: -------------------------------------------------------------------------------- 1 | /* tss_set( tss_t, void * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #ifndef REGTEST 8 | 9 | #include 10 | 11 | /* Implicitly casting the first parameter. */ 12 | extern int pthread_setspecific( tss_t, const void * ); 13 | 14 | int tss_set( tss_t key, void * val ) 15 | { 16 | if ( pthread_setspecific( key, val ) == 0 ) 17 | { 18 | return thrd_success; 19 | } 20 | else 21 | { 22 | return thrd_error; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | #ifndef REGTEST 35 | TESTCASE( NO_TESTDRIVER ); 36 | #endif 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/time/clock.c: -------------------------------------------------------------------------------- 1 | /* clock( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "sys/times.h" 12 | 13 | clock_t clock( void ) 14 | { 15 | struct tms buf; 16 | if ( times( &buf ) != (clock_t)-1 ) 17 | { 18 | return buf.tms_utime + buf.tms_stime; 19 | } 20 | return -1; 21 | } 22 | 23 | #endif 24 | 25 | #ifdef TEST 26 | 27 | #include "_PDCLIB_test.h" 28 | 29 | int main( void ) 30 | { 31 | TESTCASE( NO_TESTDRIVER ); 32 | return TEST_RESULTS; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /platform/example/functions/time/time.c: -------------------------------------------------------------------------------- 1 | /* time( time_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "sys/time.h" 12 | 13 | /* See comments in time.h on the semantics of time_t. */ 14 | 15 | time_t time( time_t * timer ) 16 | { 17 | struct timeval tv; 18 | if ( gettimeofday( &tv, NULL ) == 0 ) 19 | { 20 | if ( timer != NULL ) 21 | { 22 | *timer = tv.tv_sec; 23 | } 24 | return tv.tv_sec; 25 | } 26 | return -1; 27 | } 28 | 29 | #endif 30 | 31 | #ifdef TEST 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | int main( void ) 36 | { 37 | TESTCASE( NO_TESTDRIVER ); 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /platform/example/functions/time/timespec_get.c: -------------------------------------------------------------------------------- 1 | /* timespec_get( struct timespec *, int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include "sys/time.h" 12 | 13 | int timespec_get( struct timespec * ts, int base ) 14 | { 15 | if ( base == TIME_UTC ) 16 | { 17 | /* We can make do with a really thin wrapper here. */ 18 | struct timeval tv; 19 | if ( gettimeofday( &tv, NULL ) == 0 ) 20 | { 21 | ts->tv_sec = tv.tv_sec; 22 | ts->tv_nsec = tv.tv_usec * 1000; 23 | return base; 24 | } 25 | } 26 | /* Not supporting any other time base than TIME_UTC for now. */ 27 | return 0; 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | TESTCASE( NO_TESTDRIVER ); 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /platform/xbox/Readme.txt: -------------------------------------------------------------------------------- 1 | Xbox Platform Overlay 2 | ========================== 3 | 4 | This is a platform overlay for the Xbox, specifically for nxdk. 5 | -------------------------------------------------------------------------------- /platform/xbox/_tls_array.s: -------------------------------------------------------------------------------- 1 | // This creates a special symbol with the least significant bit set. 2 | // It signals to the linker that this code is SafeSEH-compatible. 3 | .text 4 | .def @feat.00; 5 | .scl 3; // IMAGE_SYM_CLASS_STATIC 6 | .type 0; // IMAGE_SYM_TYPE_NULL 7 | .endef 8 | .globl @feat.00 9 | .set @feat.00, 1 10 | 11 | .data 12 | // _tls_array is a constant describing the offset of StackBase inside the NT TEB 13 | .globl __tls_array 14 | .equ __tls_array, 0x04 15 | -------------------------------------------------------------------------------- /platform/xbox/crt_initializers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef int (__cdecl *_PIFV)(void); 4 | typedef void (__cdecl *_PVFV)(void); 5 | 6 | #pragma comment(linker, "/merge:.CRT=.rdata") 7 | 8 | __attribute__((section(".CRT$XIA"))) _PIFV __xi_a[] = {0}; 9 | __attribute__((section(".CRT$XIZ"))) _PIFV __xi_z[] = {0}; 10 | __attribute__((section(".CRT$XCA"))) _PVFV __xc_a[] = {0}; 11 | __attribute__((section(".CRT$XCZ"))) _PVFV __xc_z[] = {0}; 12 | __attribute__((section(".CRT$XXA"))) _PVFV __xx_a[] = {0}; 13 | __attribute__((section(".CRT$XXZ"))) _PVFV __xx_z[] = {0}; 14 | 15 | void _PDCLIB_xbox_run_pre_initializers (void) 16 | { 17 | for (_PVFV *pf = __xx_a; pf < __xx_z; pf++) 18 | { 19 | if (*pf) 20 | { 21 | (**pf)(); 22 | } 23 | } 24 | } 25 | 26 | void _PDCLIB_xbox_run_crt_initializers (void) 27 | { 28 | int ret = 0; 29 | 30 | // Run C initializers 31 | for (_PIFV *pf = __xi_a; pf < __xi_z && ret == 0; pf++) 32 | { 33 | if (*pf) 34 | { 35 | ret = (**pf)(); 36 | } 37 | } 38 | assert(ret == 0); 39 | 40 | // Run C++ initializers 41 | for (_PVFV *pf = __xc_a; pf < __xc_z; pf++) 42 | { 43 | if (*pf) 44 | { 45 | (**pf)(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /platform/xbox/functions/_PDCLIB/_PDCLIB_Exit.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_Exit( int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an implementation of _PDCLIB_Exit() for use with nxdk 8 | */ 9 | 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include "pdclib/_PDCLIB_glue.h" 15 | #include 16 | 17 | void _PDCLIB_Exit( int status ) 18 | { 19 | HalReturnToFirmware( HalQuickRebootRoutine ); 20 | } 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /platform/xbox/functions/_PDCLIB/_PDCLIB_close.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_close( _PDCLIB_fd_t ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an implementation of _PDCLIB_close() for use with nxdk 8 | */ 9 | 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include "pdclib/_PDCLIB_glue.h" 15 | #include 16 | #include 17 | 18 | int _PDCLIB_w32errno( DWORD werror ); 19 | 20 | int _PDCLIB_close( void *fd ) 21 | { 22 | if ( CloseHandle( fd ) ) 23 | { 24 | return 0; 25 | } 26 | else 27 | { 28 | *_PDCLIB_errno_func() = _PDCLIB_w32errno(GetLastError()); 29 | return -1; 30 | } 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /platform/xbox/functions/_PDCLIB/_PDCLIB_fillbuffer.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_fillbuffer( struct _PDCLIB_file_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an implementation of _PDCLIB_fillbuffer() for use with nxdk 8 | */ 9 | 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include "pdclib/_PDCLIB_glue.h" 15 | 16 | #include 17 | #include 18 | 19 | int _PDCLIB_w32errno( DWORD werror ); 20 | 21 | int _PDCLIB_fillbuffer( struct _PDCLIB_file_t * stream ) 22 | { 23 | /* No need to handle buffers > INT_MAX, as PDCLib doesn't allow them */ 24 | BOOL status; 25 | DWORD amount_read; 26 | status = ReadFile(stream->handle, stream->buffer, stream->bufsize, &amount_read, NULL); 27 | if (status) 28 | { 29 | if (amount_read == 0) 30 | { 31 | /* End-of-File */ 32 | stream->status |= _PDCLIB_EOFFLAG; 33 | return EOF; 34 | } 35 | 36 | /* Reading successful. */ 37 | if ( ! ( stream->status & _PDCLIB_FBIN ) ) 38 | { 39 | /* TODO: Text stream conversion here */ 40 | } 41 | stream->pos.offset += amount_read; 42 | stream->bufend = amount_read; 43 | stream->bufidx = 0; 44 | return 0; 45 | } 46 | else 47 | { 48 | *_PDCLIB_errno_func() = _PDCLIB_w32errno(GetLastError()); 49 | stream->status |= _PDCLIB_ERRORFLAG; 50 | return EOF; 51 | } 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /platform/xbox/functions/_PDCLIB/_PDCLIB_rename.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_rename( const char *, const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an implementation of _PDCLIB_rename() for use with nxdk 8 | */ 9 | 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include "pdclib/_PDCLIB_glue.h" 15 | 16 | #include 17 | #include 18 | 19 | int _PDCLIB_w32errno( DWORD werror ); 20 | 21 | int _PDCLIB_rename( const char * old, const char * new ) 22 | { 23 | if ( MoveFileA(old, new) ) 24 | { 25 | return 0; 26 | } 27 | else 28 | { 29 | *_PDCLIB_errno_func() = _PDCLIB_w32errno(GetLastError()); 30 | return -1; 31 | } 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /platform/xbox/functions/_PDCLIB/_PDCLIB_seek.c: -------------------------------------------------------------------------------- 1 | /* int64_t _PDCLIB_seek( FILE *, int64_t, int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an implementation of _PDCLIB_seek() for use with nxdk 8 | */ 9 | 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include "pdclib/_PDCLIB_glue.h" 15 | 16 | #include 17 | #include 18 | 19 | int _PDCLIB_w32errno( DWORD werror ); 20 | 21 | _PDCLIB_int64_t _PDCLIB_seek( struct _PDCLIB_file_t * stream, _PDCLIB_int64_t offset, int whence ) 22 | { 23 | int method; 24 | switch ( whence ) 25 | { 26 | case SEEK_SET: method = FILE_BEGIN; break; 27 | case SEEK_CUR: method = FILE_CURRENT; break; 28 | case SEEK_END: method = FILE_END; break; 29 | default: 30 | *_PDCLIB_errno_func() = EINVAL; 31 | return EOF; 32 | } 33 | 34 | LARGE_INTEGER new_pos; 35 | new_pos.QuadPart = offset; 36 | if ( SetFilePointerEx( stream->handle, new_pos, &new_pos, method) ) 37 | { 38 | stream->ungetidx = 0; 39 | stream->bufidx = 0; 40 | stream->bufend = 0; 41 | stream->pos.offset = new_pos.QuadPart; 42 | return new_pos.QuadPart; 43 | } 44 | else 45 | { 46 | *_PDCLIB_errno_func() = _PDCLIB_w32errno(GetLastError()); 47 | return EOF; 48 | } 49 | } 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /platform/xbox/functions/assert/assert.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include 7 | 8 | void _xbox_assert(char const * const expression, char const * const file_name, char const * const function_name, unsigned long line) 9 | { 10 | #ifdef DEBUG_CONSOLE 11 | char buffer[512]; 12 | snprintf(buffer, 512, "In function '%s': ", function_name); 13 | RtlAssert((PVOID)expression, (PVOID)file_name, line, buffer); 14 | #else 15 | debugPrint("\nAssertion failed: '%s' in function '%s', file '%s', line %lu\n", expression, function_name, file_name, line); 16 | __asm__ ("cli\n" 17 | "1:\n" 18 | "hlt\n" 19 | "jmp 1b\n"); 20 | #endif 21 | } 22 | -------------------------------------------------------------------------------- /platform/xbox/functions/fenv/env.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fegetenv (fenv_t *envp) 4 | { 5 | __asm__ ("fnstenv (%0);" : : "r"(envp)); 6 | return 0; 7 | } 8 | 9 | int feholdexcept (fenv_t *envp) 10 | { 11 | fegetenv(envp); 12 | feclearexcept(FE_ALL_EXCEPT); 13 | return 0; 14 | } 15 | 16 | int fesetenv (const fenv_t *envp) 17 | { 18 | __asm__ ("fldenv (%0);" : : "r"(envp)); 19 | return 0; 20 | } 21 | 22 | int feupdateenv (const fenv_t *envp) 23 | { 24 | int exceptions = fetestexcept(FE_ALL_EXCEPT); 25 | fesetenv(envp); 26 | feraiseexcept(exceptions); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /platform/xbox/functions/fenv/rounding.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fegetround (void) 4 | { 5 | unsigned short fcw; 6 | __asm__ ("fnstcw %0;" : : "m"(fcw)); 7 | return fcw & 0xc00; 8 | } 9 | 10 | int fesetround (int round) 11 | { 12 | if (round != FE_DOWNWARD && round != FE_TONEAREST && round != FE_TOWARDZERO && round != FE_UPWARD) { 13 | return -1; 14 | } 15 | 16 | unsigned short fcw; 17 | __asm__ ("fnstcw %0;" // store control word 18 | "andl %1, %0;" // mask out all rounding flags 19 | "orl %2, %0;" // set rounding mode flags 20 | "fldcw %0;" // load control word 21 | : : "m"(fcw), "r"(0xc00), "r"(round)); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/acos.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double acos(double x) 4 | { 5 | if (x < -1.0 || x > 1.0) 6 | return NAN; 7 | 8 | return 1.570796326794896619 - asin(x); 9 | } 10 | 11 | float acosf(float x) 12 | { 13 | if (x < -1.0f || x > 1.0f) 14 | return NAN; 15 | 16 | return 1.570796326794896619f - asinf(x); 17 | } 18 | 19 | long double acosl(long double x) 20 | { 21 | if (x < -1.0 || x > 1.0) 22 | return NAN; 23 | 24 | return 1.570796326794896619 - asinl(x); 25 | } 26 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/acosh.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double acosh(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float acoshf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double acoshl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/asin.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double asin(double x) 4 | { 5 | if (x < -1.0 || x > 1.0) 6 | return NAN; 7 | 8 | return 2.0 * atan(x / (sqrt(1.0-(x*x))+1.0)); 9 | } 10 | 11 | float asinf(float x) 12 | { 13 | if (x < -1.0f || x > 1.0f) 14 | return NAN; 15 | 16 | return 2.0f * atanf(x / (sqrtf(1.0f-(x*x))+1.0f)); 17 | } 18 | 19 | long double asinl(long double x) 20 | { 21 | if (x < -1.0 || x > 1.0) 22 | return NAN; 23 | 24 | return 2.0 * atanl(x / (sqrtl(1.0-(x*x))+1.0)); 25 | } 26 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/asinh.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double asinh(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float asinhf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double asinhl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/atan.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double atan(double x) 4 | { 5 | __asm__ ("fld1;" 6 | "fpatan" : "+t"(x)); 7 | return x; 8 | } 9 | 10 | float atanf(float x) 11 | { 12 | __asm__ ("fld1;" 13 | "fpatan" : "+t"(x)); 14 | return x; 15 | } 16 | 17 | long double atanl(long double x) 18 | { 19 | __asm__ ("fld1;" 20 | "fpatan" : "+t"(x)); 21 | return x; 22 | } 23 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/atan2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double atan2(double y, double x) 4 | { 5 | __asm__ ("fpatan;" : "=t"(y) : "0"(x), "u"(y)); 6 | return y; 7 | } 8 | 9 | float atan2f(float y, float x) 10 | { 11 | __asm__ ("fpatan;" : "=t"(y) : "0"(x), "u"(y)); 12 | return y; 13 | } 14 | 15 | long double atan2l(long double y, long double x) 16 | { 17 | __asm__ ("fpatan;" : "=t"(y) : "0"(x), "u"(y)); 18 | return y; 19 | } 20 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/atanh.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double atanh(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float atanhf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double atanhl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/cbrt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double cbrt(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float cbrtf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double cbrtl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/copysign.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | double copysign(double x, double y) 6 | { 7 | uint32_t *x_raw = (uint32_t *)&x; 8 | uint32_t *y_raw = (uint32_t *)&y; 9 | 10 | x_raw[1] = (x_raw[1] & 0x7FFFFFFF) | (y_raw[1] & 0x80000000); 11 | return x; 12 | } 13 | 14 | float copysignf(float x, float y) 15 | { 16 | uint32_t *x_raw = (uint32_t *)&x; 17 | uint32_t *y_raw = (uint32_t *)&y; 18 | 19 | *x_raw = (*x_raw & 0x7FFFFFFF) | (*y_raw & 0x80000000); 20 | return x; 21 | } 22 | 23 | long double copysignl(long double x, long double y) 24 | { 25 | uint32_t *x_raw = (uint32_t *)&x; 26 | uint32_t *y_raw = (uint32_t *)&y; 27 | 28 | x_raw[1] = (x_raw[1] & 0x7FFFFFFF) | (y_raw[1] & 0x80000000); 29 | return x; 30 | } 31 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/cos.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double cos(double x) 4 | { 5 | __asm__ __volatile__ ("fcos" : "+t" (x)); 6 | return x; 7 | } 8 | 9 | float cosf(float x) 10 | { 11 | __asm__ __volatile__ ("fcos" : "+t" (x)); 12 | return x; 13 | } 14 | 15 | long double cosl(long double x) 16 | { 17 | __asm__ __volatile__ ("fcos" : "+t" (x)); 18 | return x; 19 | } 20 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/cosh.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double cosh(double x) 4 | { 5 | if (x == NAN) 6 | return NAN; 7 | 8 | double r = (exp(x) + exp(-x)) / 2.0; 9 | 10 | if (((r==INFINITY) || (r==-INFINITY)) && (x!=INFINITY) && (x!=-INFINITY)) 11 | return HUGE_VAL; 12 | 13 | return r; 14 | } 15 | 16 | float coshf(float x) 17 | { 18 | if (x == NAN) 19 | return NAN; 20 | 21 | float r = (expf(x) + expf(-x)) / 2.0f; 22 | 23 | if (((r==INFINITY) || (r==-INFINITY)) && (x!=INFINITY) && (x!=-INFINITY)) 24 | return HUGE_VALF; 25 | 26 | return r; 27 | } 28 | 29 | long double coshl(long double x) 30 | { 31 | if (x == NAN) 32 | return NAN; 33 | 34 | long double r = (expl(x) + expl(-x)) / 2.0; 35 | 36 | if (((r==INFINITY) || (r==-INFINITY)) && (x!=INFINITY) && (x!=-INFINITY)) 37 | return HUGE_VAL; 38 | 39 | return r; 40 | } 41 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/erf.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double erf(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float erff(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double erfl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/erfc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double erfc(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float erfcf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double erfcl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/exp2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double exp2(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float exp2f(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double exp2l(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/expm1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double expm1(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float expm1f(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double expm1l(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/fabs.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double fabs(double x) 4 | { 5 | __asm__ ("fabs": "+t"(x)); 6 | return x; 7 | } 8 | 9 | float fabsf(float x) 10 | { 11 | __asm__ ("fabs": "+t"(x)); 12 | return x; 13 | } 14 | 15 | long double fabsl(long double x) 16 | { 17 | __asm__ ("fabs": "+t"(x)); 18 | return x; 19 | } 20 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/fdim.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double fdim(double x, double y) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float fdimf(float x, float y) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double fdiml(long double x, long double y) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/fma.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double fma(double x, double y, double z) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float fmaf(float x, float y, float z) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double fmal(long double x, long double y, long double z) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/fmax.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double fmax(double x, double y) 5 | { 6 | if (isnan(x)) { 7 | return y; 8 | } 9 | 10 | if (isnan(y)) { 11 | return x; 12 | } 13 | 14 | if (signbit(x) != signbit(y)) { 15 | return signbit(x) ? y : x; 16 | } 17 | 18 | return x < y ? y : x; 19 | } 20 | 21 | float fmaxf(float x, float y) 22 | { 23 | if (isnan(x)) { 24 | return y; 25 | } 26 | 27 | if (isnan(y)) { 28 | return x; 29 | } 30 | 31 | if (signbit(x) != signbit(y)) { 32 | return signbit(x) ? y : x; 33 | } 34 | 35 | return x < y ? y : x; 36 | } 37 | 38 | long double fmaxl(long double x, long double y) 39 | { 40 | if (isnan(x)) { 41 | return y; 42 | } 43 | 44 | if (isnan(y)) { 45 | return x; 46 | } 47 | 48 | if (signbit(x) != signbit(y)) { 49 | return signbit(x) ? y : x; 50 | } 51 | 52 | return x < y ? y : x; 53 | } 54 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/fmin.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double fmin(double x, double y) 5 | { 6 | if (isnan(x)) { 7 | return y; 8 | } 9 | 10 | if (isnan(y)) { 11 | return x; 12 | } 13 | 14 | if (signbit(x) != signbit(y)) { 15 | return signbit(x) ? x : y; 16 | } 17 | 18 | return x < y ? x : y; 19 | } 20 | 21 | float fminf(float x, float y) 22 | { 23 | if (isnan(x)) { 24 | return y; 25 | } 26 | 27 | if (isnan(y)) { 28 | return x; 29 | } 30 | 31 | if (signbit(x) != signbit(y)) { 32 | return signbit(x) ? x : y; 33 | } 34 | 35 | return x < y ? x : y; 36 | } 37 | 38 | long double fminl(long double x, long double y) 39 | { 40 | if (isnan(x)) { 41 | return y; 42 | } 43 | 44 | if (isnan(y)) { 45 | return x; 46 | } 47 | 48 | if (signbit(x) != signbit(y)) { 49 | return signbit(x) ? x : y; 50 | } 51 | 52 | return x < y ? x : y; 53 | } 54 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/fmod.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double fmod(double x, double y) 4 | { 5 | if (y == 0.0) 6 | return NAN; 7 | 8 | __asm__ ("fprem;" : "=t"(x) : "0"(x), "u"(y)); 9 | return x; 10 | } 11 | 12 | float fmodf(float x, float y) 13 | { 14 | if (y == 0.0f) 15 | return NAN; 16 | 17 | __asm__ ("fprem;" : "=t"(x) : "0"(x), "u"(y)); 18 | return x; 19 | } 20 | 21 | long double fmodl(long double x, long double y) 22 | { 23 | if (y == 0.0) 24 | return NAN; 25 | 26 | __asm__ ("fprem;" : "=t"(x) : "0"(x), "u"(y)); 27 | return x; 28 | } 29 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/fpclassify.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int __fpclassifyf (float x) 4 | { 5 | unsigned short sw; 6 | __asm__ ("fxam;" // examine st(0) 7 | "fstsw;" // store status word in ax (sw variable) and do exception check 8 | : "=a"(sw) : "t"(x)); 9 | return sw & (FP_NAN | FP_NORMAL | FP_ZERO); 10 | } 11 | 12 | int __fpclassify (double x) 13 | { 14 | unsigned short sw; 15 | __asm__ ("fxam;" // examine st(0) 16 | "fstsw;" // store status word in ax (sw variable) and do exception check 17 | : "=a"(sw) : "t"(x)); 18 | return sw & (FP_NAN | FP_NORMAL | FP_ZERO); 19 | } 20 | 21 | int __fpclassifyl (long double x) 22 | { 23 | unsigned short sw; 24 | __asm__ ("fxam;" // examine st(0) 25 | "fstsw;" // store status word in ax (sw variable) and do exception check 26 | : "=a"(sw) : "t"(x)); 27 | return sw & (FP_NAN | FP_NORMAL | FP_ZERO); 28 | } 29 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/frexp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double frexp(double value, int *exp) 4 | { 5 | double y; 6 | __asm__ ("fxtract;" 7 | : "=t"(value), "=u"(y) : "0"(value)); 8 | 9 | if (value >= 1.0 || value<=-1.0) 10 | { 11 | value /= 2.0; 12 | y += 1; 13 | } 14 | 15 | *exp = (int)y; 16 | return value; 17 | } 18 | 19 | float frexpf(float value, int *exp) 20 | { 21 | float y; 22 | __asm__ ("fxtract;" 23 | : "=t"(value), "=u"(y) : "0"(value)); 24 | 25 | if (value >= 1.0f || value<=-1.0f) 26 | { 27 | value /= 2.0f; 28 | y += 1; 29 | } 30 | 31 | *exp = (int)y; 32 | return value; 33 | } 34 | 35 | long double frexpl(long double value, int *exp) 36 | { 37 | long double y; 38 | __asm__ ("fxtract;" 39 | : "=t"(value), "=u"(y) : "0"(value)); 40 | 41 | if (value >= 1.0 || value<=-1.0) 42 | { 43 | value /= 2.0; 44 | y += 1; 45 | } 46 | 47 | *exp = (int)y; 48 | return value; 49 | } 50 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/hypot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double hypot(double x, double y) 5 | { 6 | return sqrt(x * x + y * y); 7 | } 8 | 9 | float hypotf(float x, float y) 10 | { 11 | return sqrtf(x * x + y * y); 12 | } 13 | 14 | long double hypotl(long double x, long double y) 15 | { 16 | return sqrtl(x * x + y * y); 17 | } 18 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/ilogb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int ilogb(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | int ilogbf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | int ilogbl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/ldexp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double ldexp(double x, int exp) 4 | { 5 | __asm__ ("fscale;" 6 | "fst %%st(0);" : "=t"(x) : "0"(x), "u"((double)exp)); 7 | 8 | return x; 9 | } 10 | 11 | float ldexpf(float x, int exp) 12 | { 13 | __asm__ ("fscale;" 14 | "fst %%st(0);" : "=t"(x) : "0"(x), "u"((float)exp)); 15 | 16 | return x; 17 | } 18 | 19 | long double ldexpl(long double x, int exp) 20 | { 21 | __asm__ ("fscale;" 22 | "fst %%st(0);" : "=t"(x) : "0"(x), "u"((long double)exp)); 23 | 24 | return x; 25 | } 26 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/lgamma.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double lgamma(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float lgammaf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double lgammal(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/log.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double log(double x) 4 | { 5 | __asm__ ("fldln2;" 6 | "fxch;" 7 | "fyl2x;" : "=t"(x) : "0"(x)); 8 | return x; 9 | } 10 | 11 | float logf(float x) 12 | { 13 | __asm__ ("fldln2;" 14 | "fxch;" 15 | "fyl2x;" : "=t"(x) : "0"(x)); 16 | return x; 17 | } 18 | 19 | long double logl(long double x) 20 | { 21 | __asm__ ("fldln2;" 22 | "fxch;" 23 | "fyl2x;" : "=t"(x) : "0"(x)); 24 | return x; 25 | } 26 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/log10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double log10(double x) 4 | { 5 | if (x <= 0.0) 6 | return NAN; 7 | 8 | __asm__ ("fyl2x;" 9 | "fldl2t;" 10 | "fdivrp;" : "=t"(x) : "0"(x), "u"(1.0)); 11 | 12 | return x; 13 | } 14 | 15 | float log10f(float x) 16 | { 17 | if (x <= 0.0f) 18 | return NAN; 19 | 20 | __asm__ ("fyl2x;" 21 | "fldl2t;" 22 | "fdivrp;" : "=t"(x) : "0"(x), "u"(1.0f)); 23 | 24 | return x; 25 | } 26 | 27 | long double log10l(long double x) 28 | { 29 | if (x <= 0.0) 30 | return NAN; 31 | 32 | __asm__ ("fyl2x;" 33 | "fldl2t;" 34 | "fdivrp;" : "=t"(x) : "0"(x), "u"(1.0)); 35 | 36 | return x; 37 | } 38 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/log1p.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double log1p(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float log1pf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double log1pl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/log2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double log2(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float log2f(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double log2l(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/logb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double logb(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float logbf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double logbl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/lrint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long int lrint(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | long int lrintf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long int lrintl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | 22 | long long int llrint(double x) 23 | { 24 | assert(0); // Not implemented 25 | return 0.0; 26 | } 27 | 28 | long long int llrintf(float x) 29 | { 30 | assert(0); // Not implemented 31 | return 0.0; 32 | } 33 | 34 | long long int llrintl(long double x) 35 | { 36 | assert(0); // Not implemented 37 | return 0.0; 38 | } 39 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/lround.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long int lround(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | long int lroundf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long int lroundl(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | 22 | long long int llround(double x) 23 | { 24 | assert(0); // Not implemented 25 | return 0.0; 26 | } 27 | 28 | long long int llroundf(float x) 29 | { 30 | assert(0); // Not implemented 31 | return 0.0; 32 | } 33 | 34 | long long int llroundl(long double x) 35 | { 36 | assert(0); // Not implemented 37 | return 0.0; 38 | } 39 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/modf.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double modf(double value, double *iptr) 4 | { 5 | double i = trunc(value); 6 | 7 | *iptr = i; 8 | return value-i; 9 | } 10 | 11 | float modff(float value, float *iptr) 12 | { 13 | float i = truncf(value); 14 | 15 | *iptr = i; 16 | return value-i; 17 | } 18 | 19 | long double modfl(long double value, long double *iptr) 20 | { 21 | long double i = truncl(value); 22 | 23 | *iptr = i; 24 | return value-i; 25 | } 26 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/nan.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double nan(const char *tagp) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float nanf(const char *tagp) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double nanl(const char *tagp) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/nearbyint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #pragma STDC FENV_ACCESS ON 5 | 6 | double nearbyint(double x) 7 | { 8 | int inexact_set; 9 | 10 | inexact_set = fetestexcept(FE_INEXACT); 11 | x = rint(x); 12 | if (!inexact_set) feclearexcept(FE_INEXACT); 13 | 14 | return x; 15 | } 16 | 17 | float nearbyintf(float x) 18 | { 19 | int inexact_set; 20 | 21 | inexact_set = fetestexcept(FE_INEXACT); 22 | x = rintf(x); 23 | if (!inexact_set) feclearexcept(FE_INEXACT); 24 | 25 | return x; 26 | } 27 | 28 | long double nearbyintl(long double x) 29 | { 30 | int inexact_set; 31 | 32 | inexact_set = fetestexcept(FE_INEXACT); 33 | x = rintl(x); 34 | if (!inexact_set) feclearexcept(FE_INEXACT); 35 | 36 | return x; 37 | } 38 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/nextafter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double nextafter(double x, double y) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float nextafterf(float x, float y) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double nextafterl(long double x, long double y) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/pow.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double pow(double x, double y) 4 | { 5 | if (x < 0.0 && y != trunc(y)) { 6 | // Negative base and non-integer exponent 7 | return -NAN; 8 | } 9 | 10 | float r = exp(y * log(fabs(x))); 11 | 12 | if (x < 0.0 && remainder(y, 2.0) != 0) { 13 | r = -r; 14 | } 15 | 16 | return r; 17 | } 18 | 19 | float powf(float x, float y) 20 | { 21 | if (x < 0.0f && y != truncf(y)) { 22 | // Negative base and non-integer exponent 23 | return -NAN; 24 | } 25 | 26 | float r = expf(y * logf(fabsf(x))); 27 | 28 | if (x < 0.0f && remainderf(y, 2.0f) != 0) { 29 | r = -r; 30 | } 31 | 32 | return r; 33 | } 34 | 35 | long double powl(long double x, long double y) 36 | { 37 | if (x < 0.0 && y != truncl(y)) { 38 | // Negative base and non-integer exponent 39 | return -NAN; 40 | } 41 | 42 | float r = expl(y * logl(fabsl(x))); 43 | 44 | if (x < 0.0 && remainderl(y, 2.0) != 0) { 45 | r = -r; 46 | } 47 | 48 | return r; 49 | } 50 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/remainder.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double remainder(double x, double y) 5 | { 6 | __asm__ ("fprem1;" : "=t"(x) : "0"(x), "u"(y)); 7 | return x; 8 | } 9 | 10 | float remainderf(float x, float y) 11 | { 12 | __asm__ ("fprem1;" : "=t"(x) : "0"(x), "u"(y)); 13 | return x; 14 | } 15 | 16 | long double remainderl(long double x, long double y) 17 | { 18 | __asm__ ("fprem1;" : "=t"(x) : "0"(x), "u"(y)); 19 | return x; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/remquo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double remquo(double x, double y, int *quo) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float remquof(float x, float y, int *quo) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double remquol(long double x, long double y, int *quo) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/rint.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double rint(double x) 5 | { 6 | __asm__ ("frndint;" 7 | : "=t"(x) : "0"(x)); 8 | return x; 9 | } 10 | 11 | float rintf(float x) 12 | { 13 | __asm__ ("frndint;" 14 | : "=t"(x) : "0"(x)); 15 | return x; 16 | } 17 | 18 | long double rintl(long double x) 19 | { 20 | __asm__ ("frndint;" 21 | : "=t"(x) : "0"(x)); 22 | return x; 23 | } 24 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/scalbln.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double scalbln(double x, long int n) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float scalblnf(float x, long int n) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double scalblnl(long double x, long int n) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/scalbn.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double scalbn(double x, int n) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float scalbnf(float x, int n) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double scalbnl(long double x, int n) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/signbit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int __signbitf (float x) 4 | { 5 | unsigned short sw; 6 | __asm__ ("fxam;" // examine st(0) 7 | "fstsw;" // store status word in ax (sw variable) and do exception check 8 | : "=a"(sw) : "t"(x)); 9 | return sw & 0x0200; // mask all except C1, which contains the sign bit 10 | } 11 | 12 | int __signbit (double x) 13 | { 14 | unsigned short sw; 15 | __asm__ ("fxam;" // examine st(0) 16 | "fstsw;" // store status word in ax (sw variable) and do exception check 17 | : "=a"(sw) : "t"(x)); 18 | return sw & 0x0200; // mask all except C1, which contains the sign bit 19 | } 20 | 21 | int __signbitl (long double x) 22 | { 23 | unsigned short sw; 24 | __asm__ ("fxam;" // examine st(0) 25 | "fstsw;" // store status word in ax (sw variable) and do exception check 26 | : "=a"(sw) : "t"(x)); 27 | return sw & 0x0200; // mask all except C1, which contains the sign bit 28 | } 29 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/sin.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double sin(double x) 4 | { 5 | __asm__ ("fsin" : "+t" (x)); 6 | return x; 7 | } 8 | 9 | float sinf(float x) 10 | { 11 | __asm__ ("fsin" : "+t" (x)); 12 | return x; 13 | } 14 | 15 | long double sinl(long double x) 16 | { 17 | __asm__ ("fsin" : "+t" (x)); 18 | return x; 19 | } 20 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/sinh.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double sinh(double x) 4 | { 5 | if (x == NAN) 6 | return NAN; 7 | 8 | double r = (exp(x) - exp(-x)) / 2.0; 9 | 10 | if (((r==INFINITY) || (r==-INFINITY)) && (x!=INFINITY) && (x!=-INFINITY)) 11 | { 12 | if (x > 0.0) 13 | return HUGE_VAL; 14 | else 15 | return -HUGE_VAL; 16 | } 17 | 18 | return r; 19 | } 20 | 21 | float sinhf(float x) 22 | { 23 | if (x == NAN) 24 | return NAN; 25 | 26 | float r = (expf(x) - expf(-x)) / 2.0f; 27 | 28 | if (((r==INFINITY) || (r==-INFINITY)) && (x!=INFINITY) && (x!=-INFINITY)) 29 | { 30 | if (x > 0.0f) 31 | return HUGE_VALF; 32 | else 33 | return -HUGE_VALF; 34 | } 35 | 36 | return r; 37 | } 38 | 39 | long double sinhl(long double x) 40 | { 41 | if (x == NAN) 42 | return NAN; 43 | 44 | double r = (expl(x) - expl(-x)) / 2.0; 45 | 46 | if (((r==INFINITY) || (r==-INFINITY)) && (x!=INFINITY) && (x!=-INFINITY)) 47 | { 48 | if (x > 0.0) 49 | return HUGE_VAL; 50 | else 51 | return -HUGE_VAL; 52 | } 53 | 54 | return r; 55 | } 56 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/sqrt.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double sqrt(double x) 4 | { 5 | __asm__ ("fsqrt" : "+t"(x)); 6 | return x; 7 | } 8 | 9 | float sqrtf(float x) 10 | { 11 | __asm__ ("fsqrt" : "+t"(x)); 12 | return x; 13 | } 14 | 15 | long double sqrtl(long double x) 16 | { 17 | __asm__ ("fsqrt" : "+t"(x)); 18 | return x; 19 | } 20 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/tan.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double tan(double x) 4 | { 5 | __asm__ ("fptan;" 6 | "fstp %%st(0);" : "+t"(x)); 7 | return x; 8 | } 9 | 10 | float tanf(float x) 11 | { 12 | __asm__ ("fptan;" 13 | "fstp %%st(0);" : "+t"(x)); 14 | return x; 15 | } 16 | 17 | long double tanl(long double x) 18 | { 19 | __asm__ ("fptan;" 20 | "fstp %%st(0);" : "+t"(x)); 21 | return x; 22 | } 23 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/tanh.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double tanh(double x) 4 | { 5 | if (x == NAN) 6 | return NAN; 7 | else if (x == INFINITY) 8 | return 1.0; 9 | else if (x == -INFINITY) 10 | return -1.0; 11 | else 12 | return sinh(x) / cosh(x); 13 | } 14 | 15 | float tanhf(float x) 16 | { 17 | if (x == NAN) 18 | return NAN; 19 | else if (x == INFINITY) 20 | return 1.0f; 21 | else if (x == -INFINITY) 22 | return -1.0f; 23 | else 24 | return sinhf(x) / coshf(x); 25 | } 26 | 27 | long double tanhl(long double x) 28 | { 29 | if (x == NAN) 30 | return NAN; 31 | else if (x == INFINITY) 32 | return 1.0; 33 | else if (x == -INFINITY) 34 | return -1.0; 35 | else 36 | return sinhl(x) / coshl(x); 37 | } 38 | -------------------------------------------------------------------------------- /platform/xbox/functions/math/tgamma.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double tgamma(double x) 5 | { 6 | assert(0); // Not implemented 7 | return 0.0; 8 | } 9 | 10 | float tgammaf(float x) 11 | { 12 | assert(0); // Not implemented 13 | return 0.0; 14 | } 15 | 16 | long double tgammal(long double x) 17 | { 18 | assert(0); // Not implemented 19 | return 0.0; 20 | } 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/setjmp/longjmp.s: -------------------------------------------------------------------------------- 1 | // This creates a special symbol with the least significant bit set. 2 | // It signals to the linker that this code is SafeSEH-compatible. 3 | .text 4 | .def @feat.00; 5 | .scl 3; // IMAGE_SYM_CLASS_STATIC 6 | .type 0; // IMAGE_SYM_TYPE_NULL 7 | .endef 8 | .globl @feat.00 9 | .set @feat.00, 1 10 | 11 | 12 | .text 13 | 14 | /* 15 | Buffer layout: 16 | 0: unsigned long ebp 17 | 4: unsigned long ebx 18 | 8: unsigned long edi 19 | 12: unsigned long esi 20 | 16: unsigned long esp 21 | 20: unsigned long eip 22 | */ 23 | 24 | .globl _longjmp 25 | _longjmp: 26 | movl 4(%esp), %ecx 27 | movl 8(%esp), %eax 28 | 29 | movl (%ecx), %ebp 30 | movl 4(%ecx), %ebx 31 | movl 8(%ecx), %edi 32 | movl 12(%ecx), %esi 33 | movl 16(%ecx), %esp 34 | movl 20(%ecx), %edx 35 | testl %eax, %eax 36 | jnz .r 37 | incl %eax 38 | .r: jmp *%edx 39 | -------------------------------------------------------------------------------- /platform/xbox/functions/setjmp/setjmp.s: -------------------------------------------------------------------------------- 1 | // This creates a special symbol with the least significant bit set. 2 | // It signals to the linker that this code is SafeSEH-compatible. 3 | .text 4 | .def @feat.00; 5 | .scl 3; // IMAGE_SYM_CLASS_STATIC 6 | .type 0; // IMAGE_SYM_TYPE_NULL 7 | .endef 8 | .globl @feat.00 9 | .set @feat.00, 1 10 | 11 | 12 | .text 13 | 14 | /* 15 | Buffer layout: 16 | 0: unsigned long ebp 17 | 4: unsigned long ebx 18 | 8: unsigned long edi 19 | 12: unsigned long esi 20 | 16: unsigned long esp 21 | 20: unsigned long eip 22 | */ 23 | 24 | .globl _setjmp 25 | _setjmp: 26 | movl 4(%esp), %eax 27 | 28 | movl %ebp, (%eax) 29 | movl %ebx, 4(%eax) 30 | movl %edi, 8(%eax) 31 | movl %esi, 12(%eax) 32 | leal 4(%esp), %ecx 33 | movl %ecx, 16(%eax) 34 | movl (%esp), %ecx 35 | movl %ecx, 20(%eax) 36 | 37 | xorl %eax, %eax 38 | ret 39 | -------------------------------------------------------------------------------- /platform/xbox/functions/stdio/remove.c: -------------------------------------------------------------------------------- 1 | /* remove( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an implementation of remove() for nxdk 8 | */ 9 | 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | 19 | int _PDCLIB_w32errno( DWORD werror ); 20 | 21 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 22 | 23 | int remove( const char * pathname ) 24 | { 25 | int rc; 26 | 27 | struct _PDCLIB_file_t * current = _PDCLIB_filelist; 28 | while ( current != NULL ) 29 | { 30 | if ( ( current->filename != NULL ) && ( strcmp( current->filename, pathname ) == 0 ) ) 31 | { 32 | return EOF; 33 | } 34 | current = current->next; 35 | } 36 | 37 | if ( DeleteFileA( pathname ) ) 38 | { 39 | return 0; 40 | } 41 | else 42 | { 43 | *_PDCLIB_errno_func() = _PDCLIB_w32errno(GetLastError()); 44 | return -1; 45 | } 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /platform/xbox/functions/stdlib/getenv.c: -------------------------------------------------------------------------------- 1 | /* getenv( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | /* This is an example implementation of getenv() fit for use with POSIX kernels. 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #ifndef REGTEST 14 | 15 | char * getenv( const char * name ) 16 | { 17 | return NULL; 18 | } 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /platform/xbox/functions/stdlib/system.c: -------------------------------------------------------------------------------- 1 | /* system( const char * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | int system( const char * string ) 10 | { 11 | return -1; 12 | } 13 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/_PDCLIB_xbox_tss_cleanup.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | tss_dtor_t tss_dtors[FLS_MAXIMUM_AVAILABLE]; 7 | 8 | void _PDCLIB_xbox_tss_cleanup (void) 9 | { 10 | for (int dtor_i = 0; dtor_i < TSS_DTOR_ITERATIONS; dtor_i++) { 11 | for (int i = 0; i < FLS_MAXIMUM_AVAILABLE; i++) { 12 | if (!tss_dtors[i]) continue; 13 | if (!TlsGetValue(i)) continue; 14 | 15 | void *val = TlsGetValue(i); 16 | TlsSetValue(i, NULL); 17 | tss_dtors[i](val); 18 | } 19 | 20 | bool done = true; 21 | 22 | for (int i = 0; i < FLS_MAXIMUM_AVAILABLE; i++) { 23 | if (tss_dtors[i] && TlsGetValue(i)) { 24 | done = false; 25 | break; 26 | } 27 | } 28 | 29 | if (done) break; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/call_once.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void call_once (once_flag *flag, void (*func)(void)) 4 | { 5 | if (__sync_bool_compare_and_swap(flag, 0, 1)) 6 | { 7 | func(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/cnd_broadcast.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int cnd_broadcast (cnd_t *cond) 5 | { 6 | NTSTATUS status; 7 | status = NtSetEvent(cond->eventHandles[1], NULL); 8 | if (NT_SUCCESS(status)) 9 | return thrd_success; 10 | else 11 | return thrd_error; 12 | } 13 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/cnd_destroy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void cnd_destroy (cnd_t *cond) 5 | { 6 | NtClose(cond->eventHandles[0]); 7 | NtClose(cond->eventHandles[1]); 8 | } 9 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/cnd_init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int cnd_init (cnd_t *cond) 6 | { 7 | NTSTATUS status; 8 | 9 | atomic_store(&cond->waitCount, 0); 10 | 11 | status = NtCreateEvent(&cond->eventHandles[0], NULL, SynchronizationEvent, FALSE); 12 | if (!NT_SUCCESS(status)) 13 | { 14 | return thrd_error; 15 | } 16 | 17 | // Notification-events are NOT auto-resetting! 18 | status = NtCreateEvent(&cond->eventHandles[1], NULL, NotificationEvent, FALSE); 19 | if (!NT_SUCCESS(status)) 20 | { 21 | NtClose(cond->eventHandles[0]); 22 | return thrd_error; 23 | } 24 | 25 | return thrd_success; 26 | } 27 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/cnd_signal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int cnd_signal (cnd_t *cond) 5 | { 6 | NTSTATUS status; 7 | status = NtSetEvent(cond->eventHandles[0], NULL); 8 | if (NT_SUCCESS(status)) 9 | return thrd_success; 10 | else 11 | return thrd_error; 12 | } 13 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/mtx_destroy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void mtx_destroy (mtx_t *mtx) 5 | { 6 | NtClose(mtx->handle); 7 | } 8 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/mtx_init.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int mtx_init (mtx_t *mtx, int type) 5 | { 6 | // FIXME: At the moment, all mutexes are timed and recursive! Critical sections are faster when that's not needed. 7 | NTSTATUS status; 8 | status = NtCreateMutant(&mtx->handle, NULL, FALSE); 9 | 10 | if (NT_SUCCESS(status)) 11 | { 12 | return thrd_success; 13 | } 14 | 15 | return thrd_error; 16 | } 17 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/mtx_lock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int mtx_lock (mtx_t *mtx) 5 | { 6 | NTSTATUS status = NtWaitForSingleObject(mtx->handle, FALSE, NULL); 7 | if (status != STATUS_WAIT_0) 8 | { 9 | return thrd_success; 10 | } 11 | 12 | return thrd_error; 13 | } 14 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/mtx_timedlock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int mtx_timedlock (mtx_t *restrict mtx, const struct timespec *restrict ts) 6 | { 7 | NTSTATUS status; 8 | LARGE_INTEGER interval; 9 | struct timespec currentTime; 10 | 11 | timespec_get(¤tTime, TIME_UTC); 12 | 13 | if ((currentTime.tv_sec > ts->tv_sec) || ((currentTime.tv_sec == ts->tv_sec) && (currentTime.tv_nsec >= ts->tv_nsec))) 14 | { 15 | interval.QuadPart = 0; 16 | } 17 | else 18 | { 19 | // Convert to 100ns resolution 20 | long long current = ((long long)currentTime.tv_sec)*10000000 + currentTime.tv_nsec/100; 21 | if (currentTime.tv_nsec % 100 != 0) current++; 22 | 23 | long long until = ((long long)ts->tv_sec)*10000000 + ts->tv_nsec/100; 24 | if (ts->tv_nsec % 100 != 0) until++; 25 | 26 | interval.QuadPart = until-current; 27 | interval.QuadPart = -interval.QuadPart; 28 | } 29 | 30 | status = NtWaitForSingleObject(mtx->handle, FALSE, &interval); 31 | if (status == STATUS_WAIT_0) 32 | { 33 | return thrd_success; 34 | } 35 | else if (status == STATUS_TIMEOUT) 36 | { 37 | return thrd_timedout; 38 | } 39 | 40 | return thrd_error; 41 | } 42 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/mtx_trylock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int mtx_trylock (mtx_t *mtx) 5 | { 6 | NTSTATUS status; 7 | LARGE_INTEGER interval; 8 | interval.QuadPart = 0; 9 | 10 | status = NtWaitForSingleObject(mtx->handle, FALSE, &interval); 11 | if (status == STATUS_WAIT_0) 12 | { 13 | return thrd_success; 14 | } 15 | 16 | return thrd_busy; 17 | } 18 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/mtx_unlock.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int mtx_unlock (mtx_t *mtx) 5 | { 6 | if (NT_SUCCESS(NtReleaseMutant(mtx->handle, NULL))) 7 | { 8 | return thrd_success; 9 | } 10 | 11 | return thrd_error; 12 | } 13 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/thrd_create.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | struct thread_info_t { 8 | thrd_start_t func; 9 | void *arg; 10 | }; 11 | 12 | static __stdcall DWORD thread_wrapper (LPVOID lpThreadParameter) 13 | { 14 | struct thread_info_t *thread_info = (struct thread_info_t *)lpThreadParameter; 15 | 16 | thrd_start_t func = thread_info->func; 17 | void *arg = thread_info->arg; 18 | 19 | free(lpThreadParameter); 20 | 21 | int res = func(arg); 22 | 23 | _PDCLIB_xbox_tss_cleanup(); 24 | 25 | return (DWORD)res; 26 | } 27 | 28 | int thrd_create (thrd_t *thr, thrd_start_t func, void *arg) 29 | { 30 | if (thr == NULL) { 31 | return thrd_error; 32 | } 33 | 34 | struct thread_info_t *thread_info = malloc(sizeof(struct thread_info_t)); 35 | if (!thread_info) { 36 | return thrd_nomem; 37 | } 38 | 39 | thread_info->func = func; 40 | thread_info->arg = arg; 41 | 42 | *thr = CreateThread(NULL, 0, thread_wrapper, (void *)thread_info, 0, NULL); 43 | if (!*thr) { 44 | free(thread_info); 45 | return thrd_error; 46 | } 47 | 48 | return thrd_success; 49 | } 50 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/thrd_current.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | thrd_t thrd_current (void) 5 | { 6 | return GetCurrentThread(); 7 | } 8 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/thrd_detach.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int thrd_detach (thrd_t thr) 5 | { 6 | if (CloseHandle(thr)) { 7 | return thrd_success; 8 | } 9 | 10 | return thrd_error; 11 | } 12 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/thrd_equal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int thrd_equal (thrd_t thr0, thrd_t thr1) 6 | { 7 | return (GetThreadId(thr0) == GetThreadId(thr1)); 8 | } 9 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/thrd_exit.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | _Noreturn void thrd_exit (int res) 6 | { 7 | _PDCLIB_xbox_tss_cleanup(); 8 | ExitThread((DWORD)res); 9 | while (1); // Suppresses noreturn warning 10 | } 11 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/thrd_join.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int thrd_join (thrd_t thr, int *res) 5 | { 6 | if (WaitForSingleObject(thr, INFINITE) == WAIT_FAILED) { 7 | return thrd_error; 8 | } 9 | 10 | if (res) { 11 | DWORD thread_result; 12 | if (GetExitCodeThread(thr, &thread_result) != 0) { 13 | *res = (int)thread_result; 14 | } else { 15 | return thrd_error; 16 | } 17 | } 18 | 19 | CloseHandle(thr); 20 | return thrd_success; 21 | } 22 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/thrd_yield.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void thrd_yield (void) 5 | { 6 | SwitchToThread(); 7 | } 8 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/tss_create.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int tss_create (tss_t *key, tss_dtor_t dtor) 9 | { 10 | *key = TlsAlloc(); 11 | if (*key == TLS_OUT_OF_INDEXES) { 12 | return thrd_error; 13 | } 14 | 15 | tss_dtors[*key] = dtor; 16 | return thrd_success; 17 | } 18 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/tss_delete.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void tss_delete (tss_t key) 7 | { 8 | TlsFree(key); 9 | tss_dtors[key] = NULL; 10 | } 11 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/tss_get.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void *tss_get (tss_t key) 6 | { 7 | return TlsGetValue(key); 8 | } 9 | -------------------------------------------------------------------------------- /platform/xbox/functions/threads/tss_set.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int tss_set (tss_t key, void *val) 6 | { 7 | if (TlsSetValue(key, val)) { 8 | return thrd_success; 9 | } 10 | 11 | return thrd_error; 12 | } 13 | -------------------------------------------------------------------------------- /platform/xbox/functions/time/clock.c: -------------------------------------------------------------------------------- 1 | /* clock( void ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | clock_t clock( void ) 14 | { 15 | return KeTickCount; 16 | } 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /platform/xbox/functions/time/time.c: -------------------------------------------------------------------------------- 1 | /* time( time_t * ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | #define NT_EPOCH_TIME_OFFSET ((LONGLONG)(369 * 365 + 89) * 24 * 3600) 13 | 14 | time_t time( time_t * timer ) 15 | { 16 | time_t result; 17 | LARGE_INTEGER cur_time; 18 | 19 | KeQuerySystemTime(&cur_time); 20 | 21 | result = (time_t)((cur_time.QuadPart / 10000000) - NT_EPOCH_TIME_OFFSET); 22 | 23 | if (timer != NULL) 24 | { 25 | *timer = result; 26 | } 27 | 28 | return result; 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /platform/xbox/functions/time/timespec_get.c: -------------------------------------------------------------------------------- 1 | /* timespec_get( struct timespec *, int ) 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include 8 | 9 | #ifndef REGTEST 10 | 11 | #include 12 | 13 | int timespec_get( struct timespec * ts, int base ) 14 | { 15 | if ( base == TIME_UTC ) 16 | { 17 | LARGE_INTEGER cur_time; 18 | 19 | KeQuerySystemTime(&cur_time); 20 | 21 | ts->tv_sec = cur_time.QuadPart / 10000000; 22 | ts->tv_nsec = (cur_time.QuadPart % 10000000) * 100; 23 | 24 | return base; 25 | } 26 | 27 | /* Not supporting any other time base than TIME_UTC for now. */ 28 | return 0; 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /platform/xbox/include/assert.h: -------------------------------------------------------------------------------- 1 | /* Diagnostics 2 | 3 | This file is part of the Public Domain C Library (PDCLib). 4 | Permission is granted to use, modify, and / or redistribute at will. 5 | */ 6 | 7 | #include "pdclib/_PDCLIB_aux.h" 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | #ifndef _PDCLIB_ASSERT_H 14 | #define _PDCLIB_ASSERT_H 15 | void _xbox_assert(char const * const expression, char const * const file_name, char const * const function_name, unsigned long line); 16 | #endif 17 | 18 | /* If NDEBUG is set, assert() is a null operation. */ 19 | #undef assert 20 | 21 | #ifdef NDEBUG 22 | #define assert(ignore) ((void)0) 23 | #else 24 | #define assert(expression) \ 25 | ((void) ((expression) ? 0 : \ 26 | (_xbox_assert(_PDCLIB_symbol2string(expression), \ 27 | __FILE__, \ 28 | __func__, \ 29 | __LINE__), 0))) 30 | #endif 31 | 32 | #ifndef __cplusplus 33 | #define static_assert _Static_assert 34 | #endif 35 | 36 | #ifdef __cplusplus 37 | } 38 | #endif 39 | -------------------------------------------------------------------------------- /platform/xbox/include/pdclib/_PDCLIB_xbox_tss.h: -------------------------------------------------------------------------------- 1 | #ifndef _PDCLIB_TSS_H 2 | #define _PDCLIB_TSS_H _PDCLIB_TSS_H 3 | 4 | #include 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | void _PDCLIB_xbox_tss_cleanup (void); 12 | 13 | extern tss_dtor_t tss_dtors[FLS_MAXIMUM_AVAILABLE]; 14 | 15 | #ifdef __cplusplus 16 | } 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /platform/xbox/include/setjmp.h: -------------------------------------------------------------------------------- 1 | #ifndef _PDCLIB_SETJMP_H 2 | #define _PDCLIB_SETJMP_H _PDCLIB_SETJMP_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | /* 9 | Buffer layout: 10 | 0: unsigned long ebp 11 | 4: unsigned long ebx 12 | 8: unsigned long edi 13 | 12: unsigned long esi 14 | 16: unsigned long esp 15 | 20: unsigned long eip 16 | */ 17 | typedef unsigned long jmp_buf[6]; 18 | 19 | int setjmp( jmp_buf env ); 20 | _Noreturn void longjmp( jmp_buf env, int val ); 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /platform/xbox/tls.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #pragma comment(linker, "/include:__tls_index") 5 | ULONG _tls_index = 0; 6 | 7 | __attribute__((section(".tls"))) char _tls_start = 0; 8 | __attribute__((section(".tls$ZZZ"))) char _tls_end = 0; 9 | 10 | const IMAGE_TLS_DIRECTORY_32 _tls_used = 11 | { 12 | (DWORD) &_tls_start, 13 | (DWORD) &_tls_end, 14 | (DWORD) &_tls_index, 15 | (DWORD) NULL, 16 | (DWORD) 0, 17 | (DWORD) 0 18 | }; 19 | --------------------------------------------------------------------------------