├── .gitattributes ├── .gitignore ├── 3rdPartySources.txt ├── CMakeLists.txt ├── COPYING.CC0 ├── Internals.txt ├── Notes.txt ├── Quickstart.txt ├── Readme.txt ├── auxiliary ├── errno │ ├── CMakeLists.txt │ └── errno_readout.c ├── fpconvert │ ├── CMakeLists.txt │ └── fpconvert.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_bigint.c │ ├── _PDCLIB_bigint10.c │ ├── _PDCLIB_bigint2.c │ ├── _PDCLIB_bigint32.c │ ├── _PDCLIB_bigint64.c │ ├── _PDCLIB_bigint_add.c │ ├── _PDCLIB_bigint_cmp.c │ ├── _PDCLIB_bigint_div.c │ ├── _PDCLIB_bigint_div_dig.c │ ├── _PDCLIB_bigint_invlog2.c │ ├── _PDCLIB_bigint_log2.c │ ├── _PDCLIB_bigint_mant.c │ ├── _PDCLIB_bigint_mul.c │ ├── _PDCLIB_bigint_mul_dig.c │ ├── _PDCLIB_bigint_shl.c │ ├── _PDCLIB_bigint_sub.c │ ├── _PDCLIB_bigint_tostring.c │ ├── _PDCLIB_closeall.c │ ├── _PDCLIB_digits.c │ ├── _PDCLIB_filemode.c │ ├── _PDCLIB_getstream.c │ ├── _PDCLIB_init_file_t.c │ ├── _PDCLIB_is_leap.c │ ├── _PDCLIB_isstream.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_print_fp.c │ ├── _PDCLIB_print_integer.c │ ├── _PDCLIB_print_string.c │ ├── _PDCLIB_scan.c │ ├── _PDCLIB_seed.c │ ├── _PDCLIB_strtod_main.c │ ├── _PDCLIB_strtod_scan.c │ ├── _PDCLIB_strtok.c │ ├── _PDCLIB_strtox_main.c │ ├── _PDCLIB_strtox_prelim.c │ ├── assert.c │ ├── errno.c │ └── stdarg.c ├── _dlmalloc │ ├── malloc-2.8.6.patch │ └── malloc.c ├── _dtoa │ ├── Readme.txt │ ├── dtoa-20240224.patch │ └── dtoa.c ├── _tzcode │ ├── Readme.txt │ ├── _PDCLIB_gmtcheck.c │ ├── _PDCLIB_gmtsub.c │ ├── _PDCLIB_increment_overflow.c │ ├── _PDCLIB_init_ttinfo.c │ ├── _PDCLIB_localsub.c │ ├── _PDCLIB_localtime_tzset.c │ ├── _PDCLIB_mktime_tzname.c │ ├── _PDCLIB_timesub.c │ ├── _PDCLIB_tzload.c │ ├── _PDCLIB_tzparse.c │ ├── _PDCLIB_tzset_unlocked.c │ └── _PDCLIB_update_tzname_etc.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 │ ├── fopen_s.c │ ├── fprintf.c │ ├── fputc.c │ ├── fputs.c │ ├── fread.c │ ├── freopen.c │ ├── freopen_s.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 │ ├── remove.c │ ├── rename.c │ ├── rewind.c │ ├── scanf.c │ ├── setbuf.c │ ├── setvbuf.c │ ├── snprintf.c │ ├── sprintf.c │ ├── sscanf.c │ ├── tmpfile_s.c │ ├── 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 │ ├── strtod.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 │ ├── asctime_s.c │ ├── ctime.c │ ├── ctime_s.c │ ├── difftime.c │ ├── gmtime.c │ ├── gmtime_s.c │ ├── localtime.c │ ├── localtime_s.c │ ├── mktime.c │ └── strftime.c ├── include ├── assert.h ├── ctype.h ├── errno.h ├── float.h ├── inttypes.h ├── iso646.h ├── limits.h ├── locale.h ├── pdclib │ ├── _PDCLIB_glue.h │ ├── _PDCLIB_internal.h │ ├── _PDCLIB_lib_ext1.h │ ├── _PDCLIB_print.h │ └── _PDCLIB_tzcode.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_changemode.c │ │ ├── _PDCLIB_close.c │ │ ├── _PDCLIB_fillbuffer.c │ │ ├── _PDCLIB_flushbuffer.c │ │ ├── _PDCLIB_open.c │ │ ├── _PDCLIB_realpath.c │ │ ├── _PDCLIB_remove.c │ │ ├── _PDCLIB_rename.c │ │ ├── _PDCLIB_seek.c │ │ └── _PDCLIB_stdinit.c │ ├── signal │ │ ├── raise.c │ │ └── signal.c │ ├── stdio │ │ └── 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 │ └── _PDCLIB_defguard.h │ ├── signal.h │ └── threads.h └── test_support ├── _PDCLIB_iotest.h ├── _PDCLIB_test.h ├── printf_testcases.h └── scanf_testcases.h /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.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 | # Build directory 61 | build 62 | 63 | # Auxiliary Targets 64 | get-uctypes 65 | 66 | # Local files 67 | workbench 68 | -------------------------------------------------------------------------------- /3rdPartySources.txt: -------------------------------------------------------------------------------- 1 | PDCLib contains three parts of third party software: 2 | 3 | - calloc(), free(), malloc(), and realloc() are included courtesy 4 | of Doug Lea, https://gee.cs.oswego.edu/dl/ (dlmalloc.c), which 5 | was released under CC0 license. 6 | 7 | - Large parts of the functionality behind is included 8 | courtesy of the IANA, https://www.iana.org/time-zones/ 9 | (tzcode2020a.tar.gz), which was released into the Public Domain. 10 | 11 | - strtod() is included courtesy of David M. Gay, https://netlib.org/fp/ 12 | (dtoa.c), which was released under a modified MIT license (as far 13 | as I could tell). Please check functions/_dtoa/dtoa.c for details. 14 | -------------------------------------------------------------------------------- /auxiliary/errno/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.9...3.27 FATAL_ERROR ) 2 | 3 | project( errno_readout LANGUAGES C ) 4 | 5 | add_executable( errno_readout errno_readout.c ) 6 | -------------------------------------------------------------------------------- /auxiliary/fpconvert/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.9...3.27 FATAL_ERROR ) 2 | 3 | project( fpconvert LANGUAGES C ) 4 | 5 | include( CheckIncludeFile ) 6 | 7 | set( CMAKE_REQUIRED_QUIET ON ) 8 | CHECK_INCLUDE_FILE( quadmath.h QUADMATH ) 9 | 10 | add_executable( fpconvert fpconvert.c ) 11 | 12 | if ( QUADMATH ) 13 | target_link_libraries( fpconvert quadmath ) 14 | endif() 15 | -------------------------------------------------------------------------------- /auxiliary/pthread/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.9...3.27 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_BINARY_DIR}/configured_include include/pdclib 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 | 21 | if ( NOT CMAKE_USE_PTHREADS_INIT ) 22 | message( FATAL_ERROR "Cannot find pthread." ) 23 | endif() 24 | 25 | target_link_libraries( pthread_readout Threads::Threads ) 26 | -------------------------------------------------------------------------------- /auxiliary/uctype/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.9...3.27 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 | 38 | if ( IGNORE_NO_TESTDRIVER ) 39 | set_property( TARGET ${basename}_t APPEND_STRING PROPERTY COMPILE_FLAGS " -DNO_TESTDRIVER=1" ) 40 | endif() 41 | 42 | target_include_directories( ${basename}_t PRIVATE ${CMAKE_SOURCE_DIR} ) 43 | target_link_libraries( ${basename}_t uctype ) 44 | add_test( ${basename}_t ${basename}_t ) 45 | endforeach() 46 | -------------------------------------------------------------------------------- /auxiliary/uctype/Makefile: -------------------------------------------------------------------------------- 1 | TARGET := get-uctypes 2 | # All source files of the project 3 | SRCFILES := $(wildcard *.c) 4 | # All header files of the project 5 | HDRFILES := $(wildcard *.h) 6 | # All object files in the project 7 | OBJFILES := $(patsubst %.c,%.o,$(SRCFILES)) 8 | # All test drivers (_t) 9 | TSTFILES := $(patsubst %.c,%_t,$(SRCFILES)) 10 | # All dependency files (.d) 11 | DEPFILES := $(patsubst %.c,%.d,$(SRCFILES)) 12 | # All test driver dependency files (_t.d) 13 | TSTDEPFILES := $(patsubst %,%.d,$(TSTFILES)) 14 | # All test driver dependency files (_t.d) 15 | 16 | WARNINGS := -Wall -Wextra -pedantic -Wno-unused-parameter -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wuninitialized -Wstrict-prototypes -Wdeclaration-after-statement 17 | CFLAGS := -g -std=c99 $(WARNINGS) $(USERFLAGS) -I. 18 | 19 | .PHONY: all clean tests 20 | 21 | all: $(TARGET) 22 | 23 | $(TARGET): $(OBJFILES) 24 | @echo " CC $@" 25 | @$(CC) $^ -o $@ 26 | @echo 27 | 28 | tests: testdrivers 29 | -@rc=0; count=0; failed=""; for file in $(TSTFILES); do echo " TST $$file"; ./$$file; test=$$?; if [ $$test != 0 ]; then rc=`expr $$rc + $$test`; failed="$$failed $$file"; fi; count=`expr $$count + 1`; done; echo; echo "Tests executed: $$count Tests failed: $$rc"; echo; for file in $$failed; do echo "Failed: $$file"; done; echo 30 | 31 | testdrivers: $(TSTFILES) 32 | @echo 33 | 34 | -include $(DEPFILES) $(TSTDEPFILES) 35 | 36 | clean: 37 | -@$(RM) $(wildcard $(OBJFILES) $(DEPFILES) $(TSTFILES) $(TSTDEPFILES) $(TARGET) aux.a) 38 | 39 | %.o: %.c Makefile 40 | @echo " CC $@" 41 | @$(CC) $(CFLAGS) -MMD -MP -c $< -o $@ 42 | 43 | %_t: %.c Makefile aux.a 44 | @echo " CC $@" 45 | @$(CC) $(CFLAGS) -MMD -MP -DTEST $< aux.a -o $@ 46 | 47 | aux.a: $(OBJFILES) 48 | @ar rc $@ $^ 49 | -------------------------------------------------------------------------------- /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 | { 25 | -1, /* code point or code point range */ 26 | -1 /* property name */ 27 | }; 28 | 29 | struct derived_properties_t * read_derived_properties( const char * filename ); 30 | 31 | int lookup_property( struct derived_properties_t * dp, const char * property, size_t codepoint ); 32 | 33 | void release_derived_properties( struct derived_properties_t * dp ); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /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 ) \ 19 | if ( x ) \ 20 | { \ 21 | /* EMPTY */ \ 22 | } \ 23 | else \ 24 | { \ 25 | TEST_RESULTS += 1; \ 26 | printf( "FAILED: " __FILE__ ", line %d - %s\n", __LINE__, #x ); \ 27 | } 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /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 | #endif 28 | -------------------------------------------------------------------------------- /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 | 18 | /* TODO: In other than "C" locale, additional patterns may be defined */ 19 | while ( isspace( (unsigned char)*s ) ) 20 | { 21 | ++s; 22 | } 23 | 24 | if ( *s == '+' ) 25 | { 26 | ++s; 27 | } 28 | else if ( *s == '-' ) 29 | { 30 | sign = *( s++ ); 31 | } 32 | 33 | /* TODO: Earlier version was missing tolower() but was not caught by tests */ 34 | while ( ( x = (const char *)memchr( _PDCLIB_digits, tolower( (unsigned char)*( s++ ) ), 10 ) ) != NULL ) 35 | { 36 | rc = rc * 10 + ( x - _PDCLIB_digits ); 37 | } 38 | 39 | return ( sign == '+' ) ? rc : -rc; 40 | } 41 | 42 | #endif 43 | 44 | #ifdef TEST 45 | 46 | #include "_PDCLIB_test.h" 47 | 48 | int main( void ) 49 | { 50 | #ifndef REGTEST 51 | /* basic functionality */ 52 | TESTCASE( _PDCLIB_atomax( "123" ) == 123 ); 53 | /* testing skipping of leading whitespace and trailing garbage */ 54 | TESTCASE( _PDCLIB_atomax( " \n\v\t\f123xyz" ) == 123 ); 55 | #endif 56 | return TEST_RESULTS; 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_bigint.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_bigint( _PDCLIB_bigint_t *, _PDCLIB_bigint_t const * ) 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_internal.h" 10 | 11 | _PDCLIB_bigint_t * _PDCLIB_bigint( _PDCLIB_bigint_t * _PDCLIB_restrict bigint, _PDCLIB_bigint_t const * _PDCLIB_restrict value ) 12 | { 13 | for ( bigint->size = 0; bigint->size < value->size; ++bigint->size ) 14 | { 15 | bigint->data[ bigint->size ] = value->data[ bigint->size ]; 16 | } 17 | 18 | return bigint; 19 | } 20 | 21 | #endif 22 | 23 | #ifdef TEST 24 | 25 | #include "_PDCLIB_test.h" 26 | 27 | #include 28 | 29 | int main( void ) 30 | { 31 | #ifndef REGTEST 32 | _PDCLIB_bigint_t big; 33 | _PDCLIB_bigint_t other; 34 | _PDCLIB_bigint32( &other, UINT32_C( 0x12345678 ) ); 35 | _PDCLIB_bigint( &big, &other ); 36 | TESTCASE( _PDCLIB_bigint_cmp( &big, &other ) == 0 ); 37 | _PDCLIB_bigint64( &other, UINT32_C( 0x12345678 ), UINT32_C( 0x90abcdef ) ); 38 | _PDCLIB_bigint( &big, &other ); 39 | TESTCASE( _PDCLIB_bigint_cmp( &big, &other ) == 0 ); 40 | #endif 41 | return TEST_RESULTS; 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_bigint32.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_bigint32( _PDCLIB_bigint_t *, uint_least32_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 "pdclib/_PDCLIB_internal.h" 10 | 11 | #include 12 | 13 | #define DIGITS_PER_32BIT ( 32 / _PDCLIB_BIGINT_DIGIT_BITS ) 14 | 15 | _PDCLIB_bigint_t * _PDCLIB_bigint32( _PDCLIB_bigint_t * bigint, uint_least32_t value ) 16 | { 17 | for ( bigint->size = 0; bigint->size < DIGITS_PER_32BIT; ++bigint->size ) 18 | { 19 | bigint->data[ bigint->size ] = value & _PDCLIB_BIGINT_DIGIT_MAX; 20 | #if _PDCLIB_BIGINT_DIGIT_BITS < 32 21 | value >>= _PDCLIB_BIGINT_DIGIT_BITS; 22 | #endif 23 | } 24 | 25 | while ( bigint->size > 0 && bigint->data[ bigint->size - 1 ] == 0 ) 26 | { 27 | --bigint->size; 28 | } 29 | 30 | return bigint; 31 | } 32 | 33 | #endif 34 | 35 | #ifdef TEST 36 | 37 | #include "_PDCLIB_test.h" 38 | 39 | #include 40 | 41 | int main( void ) 42 | { 43 | #ifndef REGTEST 44 | _PDCLIB_bigint_t big; 45 | uint_least32_t value; 46 | unsigned n; 47 | 48 | for ( n = 0; n < 32; ++n ) 49 | { 50 | value = UINT32_C( 1 ) << n; 51 | _PDCLIB_bigint32( &big, value ); 52 | TESTCASE( big.size == n / _PDCLIB_BIGINT_DIGIT_BITS + 1 ); 53 | } 54 | #endif 55 | 56 | return TEST_RESULTS; 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_bigint64.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_bigint64( _PDCLIB_bigint_t *, uint_least32_t, uint_least32_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 "pdclib/_PDCLIB_internal.h" 10 | 11 | #include 12 | 13 | #define DIGITS_PER_32BIT ( 32 / _PDCLIB_BIGINT_DIGIT_BITS ) 14 | 15 | _PDCLIB_bigint_t * _PDCLIB_bigint64( _PDCLIB_bigint_t * bigint, uint_least32_t high, uint_least32_t low ) 16 | { 17 | for ( bigint->size = 0; bigint->size < DIGITS_PER_32BIT; ++bigint->size ) 18 | { 19 | bigint->data[ bigint->size ] = low & _PDCLIB_BIGINT_DIGIT_MAX; 20 | bigint->data[ bigint->size + DIGITS_PER_32BIT ] = high & _PDCLIB_BIGINT_DIGIT_MAX; 21 | #if _PDCLIB_BIGINT_DIGIT_BITS < 32 22 | low >>= _PDCLIB_BIGINT_DIGIT_BITS; 23 | high >>= _PDCLIB_BIGINT_DIGIT_BITS; 24 | #endif 25 | } 26 | 27 | bigint->size *= 2; 28 | 29 | while ( bigint->size > 0 && bigint->data[ bigint->size - 1 ] == 0 ) 30 | { 31 | --bigint->size; 32 | } 33 | 34 | return bigint; 35 | } 36 | 37 | #endif 38 | 39 | #ifdef TEST 40 | 41 | #include "_PDCLIB_test.h" 42 | 43 | #include 44 | 45 | int main( void ) 46 | { 47 | #ifndef REGTEST 48 | _PDCLIB_bigint_t big; 49 | uint_least32_t value; 50 | unsigned n; 51 | 52 | for ( n = 0; n < 32; ++n ) 53 | { 54 | value = UINT32_C( 1 ) << n; 55 | _PDCLIB_bigint64( &big, 0, value ); 56 | TESTCASE( big.size == n / _PDCLIB_BIGINT_DIGIT_BITS + 1 ); 57 | _PDCLIB_bigint64( &big, value, 0 ); 58 | TESTCASE( big.size == n / _PDCLIB_BIGINT_DIGIT_BITS + DIGITS_PER_32BIT + 1 ); 59 | } 60 | #endif 61 | 62 | return TEST_RESULTS; 63 | } 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_bigint_cmp.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_bigint_cmp( _PDCLIB_bigint_t const *, _PDCLIB_bigint_t const * ) 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_internal.h" 10 | 11 | int _PDCLIB_bigint_cmp( _PDCLIB_bigint_t const * _PDCLIB_restrict lhs, _PDCLIB_bigint_t const * _PDCLIB_restrict rhs ) 12 | { 13 | int i; 14 | 15 | if ( lhs->size != rhs->size ) 16 | { 17 | return lhs->size - rhs->size; 18 | } 19 | 20 | for ( i = lhs->size - 1; i >= 0; --i ) 21 | { 22 | if ( lhs->data[i] != rhs->data[i] ) 23 | { 24 | return lhs->data[i] - rhs->data[i]; 25 | } 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | #endif 32 | 33 | #ifdef TEST 34 | 35 | #include "_PDCLIB_test.h" 36 | 37 | #include 38 | 39 | int main( void ) 40 | { 41 | #ifndef REGTEST 42 | _PDCLIB_bigint_t lhs, rhs; 43 | _PDCLIB_bigint32( &lhs, 0 ); 44 | _PDCLIB_bigint64( &rhs, 0, 0 ); 45 | TESTCASE( _PDCLIB_bigint_cmp( &lhs, &rhs ) == 0 ); 46 | _PDCLIB_bigint32( &lhs, 1 ); 47 | TESTCASE( _PDCLIB_bigint_cmp( &lhs, &rhs ) > 0 ); 48 | _PDCLIB_bigint32( &rhs, UINT32_C( 0x8000000 ) ); 49 | TESTCASE( _PDCLIB_bigint_cmp( &lhs, &rhs ) < 0 ); 50 | _PDCLIB_bigint64( &lhs, 0, 1 ); 51 | TESTCASE( _PDCLIB_bigint_cmp( &lhs, &rhs ) < 0 ); 52 | _PDCLIB_bigint64( &lhs, 1, UINT32_C( 0x80000000 ) ); 53 | TESTCASE( _PDCLIB_bigint_cmp( &lhs, &rhs ) > 0 ); 54 | #endif 55 | return TEST_RESULTS; 56 | } 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_bigint_shl.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_bigint_shl( _PDCLIB_bigint_t *, unsigned ) 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_internal.h" 10 | 11 | #include 12 | #include 13 | 14 | _PDCLIB_bigint_t * _PDCLIB_bigint_shl( _PDCLIB_bigint_t * lhs, unsigned rhs ) 15 | { 16 | unsigned word_shifts = rhs / _PDCLIB_BIGINT_DIGIT_BITS; 17 | unsigned bit_shifts = rhs - ( word_shifts * _PDCLIB_BIGINT_DIGIT_BITS ); 18 | _PDCLIB_bigint_arith_t digit; 19 | _PDCLIB_bigint_digit_t carry = 0; 20 | size_t i; 21 | 22 | if ( lhs->size == 0 ) 23 | { 24 | return lhs; 25 | } 26 | 27 | if ( word_shifts > 0 ) 28 | { 29 | memmove( lhs->data + word_shifts, lhs->data, sizeof( _PDCLIB_bigint_digit_t ) * lhs->size ); 30 | memset( lhs->data, 0, sizeof( _PDCLIB_bigint_digit_t ) * word_shifts ); 31 | lhs->size += word_shifts; 32 | } 33 | 34 | for ( i = 0 + word_shifts; i < lhs->size; ++i ) 35 | { 36 | digit = ( (_PDCLIB_bigint_arith_t)lhs->data[i] << bit_shifts ) | carry; 37 | carry = digit >> _PDCLIB_BIGINT_DIGIT_BITS; 38 | lhs->data[i] = digit & _PDCLIB_BIGINT_DIGIT_MAX; 39 | } 40 | 41 | if ( carry > 0 ) 42 | { 43 | lhs->data[ lhs->size++ ] = carry; 44 | } 45 | 46 | return lhs; 47 | } 48 | 49 | #endif 50 | 51 | #ifdef TEST 52 | 53 | #include "_PDCLIB_test.h" 54 | 55 | int main( void ) 56 | { 57 | #ifndef REGTEST 58 | _PDCLIB_bigint_t lhs, expected; 59 | 60 | _PDCLIB_bigint32( &lhs, 12 ); 61 | _PDCLIB_bigint32( &expected, 48 ); 62 | _PDCLIB_bigint_shl( &lhs, 2 ); 63 | TESTCASE( _PDCLIB_bigint_cmp( &lhs, &expected ) == 0 ); 64 | 65 | _PDCLIB_bigint32( &lhs, UINT32_C( 0x80000001 ) ); 66 | _PDCLIB_bigint64( &expected, 1, 2 ); 67 | _PDCLIB_bigint_shl( &lhs, 1 ); 68 | TESTCASE( _PDCLIB_bigint_cmp( &lhs, &expected ) == 0 ); 69 | #endif 70 | return TEST_RESULTS; 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /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 | 18 | while ( stream != NULL ) 19 | { 20 | next = stream->next; 21 | fclose( stream ); 22 | stream = next; 23 | } 24 | } 25 | 26 | #endif 27 | 28 | #ifdef TEST 29 | 30 | #include "_PDCLIB_test.h" 31 | 32 | int main( void ) 33 | { 34 | /* No testdriver */ 35 | return TEST_RESULTS; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /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_internal.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_internal.h" 12 | 13 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 14 | 15 | int _PDCLIB_getstream( struct _PDCLIB_file_t * stream ) 16 | { 17 | struct _PDCLIB_file_t * previous; 18 | 19 | if ( ! _PDCLIB_isstream( stream, &previous ) ) 20 | { 21 | *_PDCLIB_errno_func() = _PDCLIB_EBADF; 22 | return EOF; 23 | } 24 | 25 | if ( previous != NULL ) 26 | { 27 | previous->next = stream->next; 28 | } 29 | else 30 | { 31 | _PDCLIB_filelist = stream->next; 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | #endif 38 | 39 | #ifdef TEST 40 | 41 | #include "_PDCLIB_test.h" 42 | 43 | int main( void ) 44 | { 45 | /* No testdriver */ 46 | return TEST_RESULTS; 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_init_file_t.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_init_file_t( _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 | #ifndef REGTEST 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | #endif 16 | 17 | struct _PDCLIB_file_t * _PDCLIB_init_file_t( struct _PDCLIB_file_t * stream ) 18 | { 19 | struct _PDCLIB_file_t * rc = stream; 20 | 21 | if ( rc == NULL ) 22 | { 23 | if ( ( rc = (struct _PDCLIB_file_t *)malloc( sizeof( struct _PDCLIB_file_t ) ) ) == NULL ) 24 | { 25 | /* No memory */ 26 | return NULL; 27 | } 28 | } 29 | 30 | if ( ( rc->buffer = (char *)malloc( BUFSIZ ) ) == NULL ) 31 | { 32 | /* No memory */ 33 | free( rc ); 34 | return NULL; 35 | } 36 | 37 | rc->bufsize = BUFSIZ; 38 | rc->bufidx = 0; 39 | rc->bufend = 0; 40 | rc->pos.offset = 0; 41 | rc->pos.status = 0; 42 | rc->ungetidx = 0; 43 | rc->status = _PDCLIB_FREEBUFFER; 44 | 45 | #ifndef __STDC_NO_THREADS__ 46 | 47 | if ( stream == NULL ) 48 | { 49 | /* If called by freopen() (stream not NULL), mutex is already 50 | initialized. 51 | */ 52 | if ( mtx_init( &rc->mtx, mtx_plain | mtx_recursive ) != thrd_success ) 53 | { 54 | /* could not initialize stream mutex */ 55 | free( rc->buffer ); 56 | free( rc ); 57 | return NULL; 58 | } 59 | } 60 | 61 | #endif 62 | 63 | /* TODO: Setting mbstate */ 64 | 65 | return rc; 66 | } 67 | 68 | #endif 69 | 70 | #ifdef TEST 71 | 72 | #include "_PDCLIB_test.h" 73 | 74 | int main( void ) 75 | { 76 | #ifndef REGTEST 77 | TESTCASE( NO_TESTDRIVER ); 78 | #endif 79 | return TEST_RESULTS; 80 | } 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /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_internal.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_isstream.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_isstream( FILE *, 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_internal.h" 12 | 13 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 14 | 15 | int _PDCLIB_isstream( struct _PDCLIB_file_t * stream, struct _PDCLIB_file_t ** previous ) 16 | { 17 | struct _PDCLIB_file_t * current = _PDCLIB_filelist; 18 | 19 | if ( previous != NULL ) 20 | { 21 | *previous = NULL; 22 | } 23 | 24 | while ( ( current != NULL ) && ( current != stream ) ) 25 | { 26 | if ( previous != NULL ) 27 | { 28 | *previous = current; 29 | } 30 | 31 | current = current->next; 32 | } 33 | 34 | return current != NULL; 35 | } 36 | 37 | #endif 38 | 39 | #ifdef TEST 40 | 41 | #include "_PDCLIB_test.h" 42 | 43 | int main( void ) 44 | { 45 | /* No testdriver */ 46 | return TEST_RESULTS; 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /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_internal.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 = (char *)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 = (struct _PDCLIB_lc_collate_t *)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_load_lines.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_load_lines( FILE *, 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 | #include 9 | 10 | #ifndef REGTEST 11 | 12 | char * _PDCLIB_load_lines( FILE * fh, size_t lines ) 13 | { 14 | size_t required = 0; 15 | long pos = ftell( fh ); 16 | char * rc = NULL; 17 | int c; 18 | 19 | /* Count the number of characters */ 20 | while ( lines && ( c = fgetc( fh ) ) != EOF ) 21 | { 22 | if ( c == '\n' ) 23 | { 24 | --lines; 25 | } 26 | 27 | ++required; 28 | } 29 | 30 | if ( ! feof( fh ) ) 31 | { 32 | if ( ( rc = (char *)malloc( required ) ) != NULL ) 33 | { 34 | size_t i; 35 | 36 | fseek( fh, pos, SEEK_SET ); 37 | fread( rc, 1, required, fh ); 38 | 39 | for ( i = 0; i < required; ++i ) 40 | { 41 | if ( rc[ i ] == '\n' ) 42 | { 43 | rc[ i ] = '\0'; 44 | } 45 | } 46 | } 47 | } 48 | 49 | return rc; 50 | } 51 | 52 | #endif 53 | 54 | #ifdef TEST 55 | 56 | #include "_PDCLIB_test.h" 57 | 58 | int main( void ) 59 | { 60 | #ifndef REGTEST 61 | FILE * fh = fopen( "test_lines.txt", "w+" ); 62 | char * rc; 63 | 64 | TESTCASE( fh != NULL ); 65 | TESTCASE( fputs( "Foo\n\nBar\n", fh ) != EOF ); 66 | 67 | rewind( fh ); 68 | rc = _PDCLIB_load_lines( fh, 3 ); 69 | fclose( fh ); 70 | remove( "test_lines.txt" ); 71 | 72 | TESTCASE( rc != NULL ); 73 | TESTCASE( strcmp( rc, "Foo" ) == 0 ); 74 | TESTCASE( strcmp( rc + 4, "" ) == 0 ); 75 | TESTCASE( strcmp( rc + 5, "Bar" ) == 0 ); 76 | 77 | #endif 78 | return TEST_RESULTS; 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /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 | 25 | stream->status |= _PDCLIB_FREAD | _PDCLIB_BYTESTREAM; 26 | 27 | return 0; 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | /* Testing covered by ftell.c */ 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /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 | 23 | stream->status |= _PDCLIB_FWRITE | _PDCLIB_BYTESTREAM; 24 | return 0; 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | int main( void ) 34 | { 35 | /* Testing covered by ftell.c */ 36 | return TEST_RESULTS; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /functions/_PDCLIB/_PDCLIB_print_string.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_print_string 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 | 13 | #include "pdclib/_PDCLIB_print.h" 14 | 15 | /* Print a "string" (%c, %s) under control of a given status struct. See 16 | INT2BASE(). 17 | */ 18 | void _PDCLIB_print_string( const char * s, struct _PDCLIB_status_t * status ) 19 | { 20 | if ( status->flags & E_char ) 21 | { 22 | status->prec = 1; 23 | } 24 | else 25 | { 26 | if ( status->prec < 0 ) 27 | { 28 | status->prec = strlen( s ); 29 | } 30 | else 31 | { 32 | int i; 33 | 34 | for ( i = 0; i < status->prec; ++i ) 35 | { 36 | if ( s[i] == 0 ) 37 | { 38 | status->prec = i; 39 | break; 40 | } 41 | } 42 | } 43 | } 44 | 45 | if ( !( status->flags & E_minus ) && ( status->width > ( _PDCLIB_size_t )status->prec ) ) 46 | { 47 | while ( status->current < ( status->width - status->prec ) ) 48 | { 49 | PUT( ' ' ); 50 | ++( status->current ); 51 | } 52 | } 53 | 54 | while ( status->prec > 0 ) 55 | { 56 | PUT( *( s++ ) ); 57 | --( status->prec ); 58 | ++( status->current ); 59 | } 60 | 61 | if ( status->flags & E_minus ) 62 | { 63 | while ( status->width > status->current ) 64 | { 65 | PUT( ' ' ); 66 | ++( status->current ); 67 | } 68 | } 69 | } 70 | 71 | #endif 72 | 73 | #ifdef TEST 74 | 75 | #include 76 | 77 | #include "_PDCLIB_test.h" 78 | 79 | int main( void ) 80 | { 81 | /* Tested by _PDCLIB_print testdriver */ 82 | return TEST_RESULTS; 83 | } 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /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/assert.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_assert( 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 | void _PDCLIB_assert99( const char * const message1, const char * const function, const char * const message2 ) 14 | { 15 | fputs( message1, stderr ); 16 | fputs( function, stderr ); 17 | fputs( message2, stderr ); 18 | abort(); 19 | } 20 | 21 | void _PDCLIB_assert89( const char * const message ) 22 | { 23 | fputs( message, stderr ); 24 | abort(); 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | #include 34 | 35 | static int EXPECTED_ABORT = 0; 36 | static int UNEXPECTED_ABORT = 1; 37 | 38 | static void aborthandler( int sig ) 39 | { 40 | TESTCASE( ! EXPECTED_ABORT ); 41 | exit( ( signed int )TEST_RESULTS ); 42 | } 43 | 44 | #ifdef NDEBUG 45 | #error Compiling test drivers with NDEBUG set is a questionable choice... 46 | #endif 47 | 48 | #define NDEBUG 49 | 50 | #include 51 | 52 | static int disabled_test( void ) 53 | { 54 | int i = 0; 55 | assert( i == 0 ); /* NDEBUG set, condition met */ 56 | assert( i == 1 ); /* NDEBUG set, condition fails */ 57 | return i; 58 | } 59 | 60 | #undef NDEBUG 61 | 62 | #include 63 | 64 | int main( void ) 65 | { 66 | TESTCASE( signal( SIGABRT, &aborthandler ) != SIG_ERR ); 67 | TESTCASE( disabled_test() == 0 ); 68 | assert( UNEXPECTED_ABORT ); /* NDEBUG not set, condition met */ 69 | assert( EXPECTED_ABORT ); /* NDEBUG not set, condition fails - should abort */ 70 | return TEST_RESULTS; 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /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_internal.h" 10 | 11 | #if __STDC_VERSION__ >= 201112L && ! defined( __STDC_NO_THREADS__ ) 12 | _Thread_local int _PDCLIB_errno = 0; 13 | #else 14 | static int _PDCLIB_errno = 0; 15 | #endif 16 | 17 | int * _PDCLIB_errno_func() 18 | { 19 | return &_PDCLIB_errno; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | 26 | #include "_PDCLIB_test.h" 27 | 28 | #include 29 | 30 | #if ! defined( REGTEST ) && __STDC_VERSION__ >= 201112L && ! defined( __STDC_NO_THREADS__ ) 31 | 32 | #include 33 | 34 | static int thread_func( void * arg ) 35 | { 36 | TESTCASE( errno == 0 ); 37 | *_PDCLIB_errno_func() = 1; 38 | TESTCASE( errno == 1 ); 39 | thrd_exit( 0 ); 40 | } 41 | 42 | #endif 43 | 44 | int main( void ) 45 | { 46 | errno = 0; 47 | TESTCASE( errno == 0 ); 48 | errno = EDOM; 49 | TESTCASE( errno == EDOM ); 50 | errno = ERANGE; 51 | TESTCASE( errno == ERANGE ); 52 | 53 | #if ! defined( REGTEST ) && __STDC_VERSION__ >= 201112L && ! defined( __STDC_NO_THREADS__ ) 54 | { 55 | thrd_t t; 56 | struct timespec spec = { 1, 0 }; 57 | int rc; 58 | 59 | TESTCASE( thrd_create( &t, thread_func, NULL ) == thrd_success ); 60 | 61 | TESTCASE( thrd_sleep( &spec, NULL ) == 0 ); 62 | TESTCASE( errno == ERANGE ); 63 | TESTCASE( thrd_join( t, &rc ) == thrd_success ); 64 | TESTCASE( rc == 0 ); 65 | } 66 | #endif 67 | 68 | return TEST_RESULTS; 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /functions/_dtoa/Readme.txt: -------------------------------------------------------------------------------- 1 | dtoa.c is the work of David M. Gay, availabe at 2 | 3 | https://netlib.org/fp/dtoa.c 4 | 5 | The version included here is dtoa-20240224. You can check if newer versions 6 | are available at 7 | 8 | https://netlib.org/fp/changes 9 | 10 | A diff of the version included here to the original file downloaded on 11 | 2024-04-07 is included for your reference. 12 | -------------------------------------------------------------------------------- /functions/_tzcode/Readme.txt: -------------------------------------------------------------------------------- 1 | Major parts of PDCLib's implementation are based on IANA's reference 2 | implementation, tzcode. 3 | 4 | https://data.iana.org/time-zones/tz-link.html 5 | 6 | The latest version of tzcode can be downloaded at: 7 | 8 | https://www.iana.org/time-zones/repository/tzcode-latest.tar.gz 9 | 10 | At the time of this writing, the latest tzcode version used as basis is 2020a. 11 | -------------------------------------------------------------------------------- /functions/_tzcode/_PDCLIB_gmtcheck.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_gmtcheck( 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 "pdclib/_PDCLIB_tzcode.h" 10 | 11 | #ifndef __STDC_NO_THREADS__ 12 | #include 13 | extern mtx_t _PDCLIB_time_mtx; 14 | #endif 15 | 16 | static void gmtload( struct state * sp ) 17 | { 18 | if ( _PDCLIB_tzload( gmt, sp, true ) != 0 ) 19 | { 20 | _PDCLIB_tzparse( gmt, sp, true ); 21 | } 22 | } 23 | 24 | void _PDCLIB_gmtcheck( void ) 25 | { 26 | static bool gmt_is_set; 27 | 28 | _PDCLIB_LOCK( _PDCLIB_time_mtx ); 29 | 30 | if ( ! gmt_is_set ) 31 | { 32 | gmtload( &_PDCLIB_gmtmem ); 33 | gmt_is_set = true; 34 | } 35 | 36 | _PDCLIB_UNLOCK( _PDCLIB_time_mtx ); 37 | } 38 | 39 | #endif 40 | 41 | #ifdef TEST 42 | 43 | #include "_PDCLIB_test.h" 44 | 45 | int main( void ) 46 | { 47 | #ifndef REGTEST 48 | #endif 49 | 50 | return TEST_RESULTS; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /functions/_tzcode/_PDCLIB_gmtsub.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_gmtsub( struct state const *, time_t const *, int_fast32_t, 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 | #ifndef REGTEST 8 | 9 | #include "pdclib/_PDCLIB_tzcode.h" 10 | 11 | /* gmtsub is to gmtime as localsub is to localtime. */ 12 | struct tm * _PDCLIB_gmtsub( struct state const * sp, time_t const * timep, int_fast32_t offset, struct tm * tmp ) 13 | { 14 | struct tm * result; 15 | 16 | result = _PDCLIB_timesub( timep, offset, &_PDCLIB_gmtmem, tmp ); 17 | 18 | #ifdef TM_ZONE 19 | /* Could get fancy here and deliver something such as 20 | "+xx" or "-xx" if offset is non-zero, 21 | but this is no time for a treasure hunt. 22 | */ 23 | tmp->TM_ZONE = ( (char *)( offset ? wildabbr : &_PDCLIB_gmtmem ? _PDCLIB_gmtptr.chars : gmt ) ); 24 | #endif 25 | 26 | return result; 27 | } 28 | 29 | #endif 30 | 31 | #ifdef TEST 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | int main( void ) 36 | { 37 | #ifndef REGTEST 38 | #endif 39 | 40 | return TEST_RESULTS; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /functions/_tzcode/_PDCLIB_increment_overflow.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_increment_overflow( 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 | #ifndef REGTEST 8 | 9 | #include "pdclib/_PDCLIB_tzcode.h" 10 | 11 | /* Normalize logic courtesy Paul Eggert. */ 12 | 13 | bool _PDCLIB_increment_overflow( int * ip, int j ) 14 | { 15 | int const i = *ip; 16 | 17 | /* If i >= 0 there can only be overflow if i + j > INT_MAX 18 | or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow. 19 | If i < 0 there can only be overflow if i + j < INT_MIN 20 | or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow. 21 | */ 22 | if ( ( i >= 0 ) ? ( j > _PDCLIB_INT_MAX - i ) : ( j < _PDCLIB_INT_MIN - i ) ) 23 | { 24 | return true; 25 | } 26 | 27 | *ip += j; 28 | return false; 29 | } 30 | 31 | #endif 32 | 33 | #ifdef TEST 34 | 35 | #include "_PDCLIB_test.h" 36 | 37 | int main( void ) 38 | { 39 | #ifndef REGTEST 40 | #endif 41 | 42 | return TEST_RESULTS; 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /functions/_tzcode/_PDCLIB_init_ttinfo.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_init_ttinfo( struct ttinfo *, int_fast32_t, bool, 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_tzcode.h" 10 | 11 | /* Initialize *S to a value based on UTOFF, ISDST, and DESIGIDX. */ 12 | void _PDCLIB_init_ttinfo( struct ttinfo * s, int_fast32_t utoff, bool isdst, int desigidx ) 13 | { 14 | s->utoff = utoff; 15 | s->isdst = isdst; 16 | s->desigidx = desigidx; 17 | s->ttisstd = false; 18 | s->ttisut = false; 19 | } 20 | 21 | #endif 22 | 23 | #ifdef TEST 24 | 25 | #include "_PDCLIB_test.h" 26 | 27 | int main( void ) 28 | { 29 | #ifndef REGTEST 30 | #endif 31 | 32 | return TEST_RESULTS; 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /functions/_tzcode/_PDCLIB_localtime_tzset.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_localtime_tzset( time_t const *, struct tm *, bool ) 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_tzcode.h" 10 | 11 | #ifndef __STDC_NO_THREADS__ 12 | #include 13 | extern mtx_t _PDCLIB_time_mtx; 14 | #endif 15 | 16 | #include 17 | 18 | struct tm * _PDCLIB_localtime_tzset( time_t const * timep, struct tm * tmp, bool setname ) 19 | { 20 | _PDCLIB_LOCK( _PDCLIB_time_mtx ); 21 | 22 | if ( setname || ! lcl_is_set ) 23 | { 24 | _PDCLIB_tzset_unlocked(); 25 | } 26 | 27 | tmp = _PDCLIB_localsub( &_PDCLIB_lclmem, timep, setname, tmp ); 28 | _PDCLIB_UNLOCK( _PDCLIB_time_mtx ); 29 | return tmp; 30 | } 31 | 32 | #endif 33 | 34 | #ifdef TEST 35 | 36 | #include "_PDCLIB_test.h" 37 | 38 | int main( void ) 39 | { 40 | #ifndef REGTEST 41 | #endif 42 | 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /functions/_tzcode/_PDCLIB_update_tzname_etc.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_update_tzname_etc( struct state const *, struct ttinfo const * ) 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_tzcode.h" 10 | 11 | void _PDCLIB_update_tzname_etc( struct state const * sp, struct ttinfo const * ttisp ) 12 | { 13 | #if HAVE_TZNAME 14 | tzname[ ttisp->isdst ] = (char *) &sp->chars[ ttisp->desigidx ]; 15 | #endif 16 | #if USG_COMPAT 17 | if ( ! ttisp->isdst ) 18 | { 19 | timezone = - ttisp->utoff; 20 | } 21 | #endif 22 | #if ALTZONE 23 | if ( ttisp->isdst ) 24 | { 25 | altzone = - ttisp->utoff; 26 | } 27 | #endif 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | #ifndef REGTEST 39 | #endif 40 | 41 | return TEST_RESULTS; 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /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 | 21 | #endif 22 | 23 | #ifdef TEST 24 | 25 | #include "_PDCLIB_test.h" 26 | 27 | int main( void ) 28 | { 29 | TESTCASE( isxdigit( '0' ) ); 30 | TESTCASE( isxdigit( '9' ) ); 31 | TESTCASE( isxdigit( 'a' ) ); 32 | TESTCASE( isxdigit( 'f' ) ); 33 | TESTCASE( ! isxdigit( 'g' ) ); 34 | TESTCASE( isxdigit( 'A' ) ); 35 | TESTCASE( isxdigit( 'F' ) ); 36 | TESTCASE( ! isxdigit( 'G' ) ); 37 | TESTCASE( ! isxdigit( '@' ) ); 38 | TESTCASE( ! isxdigit( ' ' ) ); 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /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 ) || ( strcmp( lconv->grouping, "\x7f" ) == 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 | #ifndef __ANDROID__ 39 | /* reopen() the file write-only -- this fails on Termix */ 40 | TESTCASE( ( fh = freopen( NULL, "w", fh ) ) != NULL ); 41 | /* Reading from write-only stream - should provoke error */ 42 | TESTCASE( fgetc( fh ) == EOF ); 43 | TESTCASE( ferror( fh ) ); 44 | TESTCASE( ! feof( fh ) ); 45 | #endif 46 | /* clearerr() should clear flags */ 47 | clearerr( fh ); 48 | TESTCASE( ! ferror( fh ) ); 49 | TESTCASE( ! feof( fh ) ); 50 | TESTCASE( fclose( fh ) == 0 ); 51 | return TEST_RESULTS; 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /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 | extern mtx_t _PDCLIB_filelist_mtx; 16 | #endif 17 | 18 | extern struct _PDCLIB_file_t * _PDCLIB_filelist; 19 | 20 | int fflush( struct _PDCLIB_file_t * stream ) 21 | { 22 | int rc = 0; 23 | 24 | if ( stream == NULL ) 25 | { 26 | _PDCLIB_LOCK( _PDCLIB_filelist_mtx ); 27 | stream = _PDCLIB_filelist; 28 | 29 | /* TODO: Check what happens when fflush( NULL ) encounters write errors, in other libs */ 30 | while ( stream != NULL ) 31 | { 32 | _PDCLIB_LOCK( stream->mtx ); 33 | 34 | if ( stream->status & _PDCLIB_FWRITE ) 35 | { 36 | if ( _PDCLIB_flushbuffer( stream ) == EOF ) 37 | { 38 | rc = EOF; 39 | } 40 | } 41 | 42 | _PDCLIB_UNLOCK( stream->mtx ); 43 | 44 | stream = stream->next; 45 | } 46 | 47 | _PDCLIB_UNLOCK( _PDCLIB_filelist_mtx ); 48 | } 49 | else 50 | { 51 | _PDCLIB_LOCK( stream->mtx ); 52 | rc = _PDCLIB_flushbuffer( stream ); 53 | _PDCLIB_UNLOCK( stream->mtx ); 54 | } 55 | 56 | return rc; 57 | } 58 | 59 | #endif 60 | 61 | #ifdef TEST 62 | 63 | #include "_PDCLIB_test.h" 64 | 65 | int main( void ) 66 | { 67 | /* Testing covered by ftell.c */ 68 | return TEST_RESULTS; 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /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 = EOF; 20 | 21 | _PDCLIB_LOCK( stream->mtx ); 22 | 23 | if ( _PDCLIB_prepread( stream ) != EOF ) 24 | { 25 | if ( _PDCLIB_CHECKBUFFER( stream ) != EOF ) 26 | { 27 | rc = _PDCLIB_GETC( stream ); 28 | } 29 | } 30 | 31 | _PDCLIB_UNLOCK( stream->mtx ); 32 | 33 | return rc; 34 | } 35 | 36 | #endif 37 | 38 | #ifdef TEST 39 | 40 | #include "_PDCLIB_test.h" 41 | 42 | int main( void ) 43 | { 44 | /* Testing covered by ftell.c */ 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 - ( ( ( int )stream->bufend - ( int )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 | 26 | #include 27 | #include 28 | #include 29 | 30 | #define _PDCLIB_FILEID "stdio/fprintf.c" 31 | #define _PDCLIB_FILEIO 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | #define testprintf( stream, ... ) fprintf( stream, __VA_ARGS__ ) 36 | 37 | int main( void ) 38 | { 39 | FILE * target; 40 | TESTCASE( ( target = tmpfile() ) != NULL ); 41 | #include "printf_testcases.h" 42 | TESTCASE( fclose( target ) == 0 ); 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /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 | 29 | if ( ( stream->bufidx == stream->bufsize ) /* _IOFBF */ 30 | || ( ( stream->status & _IOLBF ) && ( ( char )c == '\n' ) ) /* _IOLBF */ 31 | || ( stream->status & _IONBF ) /* _IONBF */ 32 | ) 33 | { 34 | /* buffer filled, unbuffered stream, or end-of-line. */ 35 | c = ( _PDCLIB_flushbuffer( stream ) == 0 ) ? c : EOF; 36 | } 37 | 38 | _PDCLIB_UNLOCK( stream->mtx ); 39 | 40 | return c; 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/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 = fopen( testfile, "wb+" ) ) != 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 | 30 | if ( _PDCLIB_seek( stream, pos->offset, SEEK_SET ) == EOF ) 31 | { 32 | _PDCLIB_UNLOCK( stream->mtx ); 33 | return EOF; 34 | } 35 | 36 | stream->pos.status = pos->status; 37 | /* TODO: Add mbstate. */ 38 | 39 | _PDCLIB_UNLOCK( stream->mtx ); 40 | 41 | return 0; 42 | } 43 | 44 | #endif 45 | 46 | #ifdef TEST 47 | 48 | #include "_PDCLIB_test.h" 49 | 50 | int main( void ) 51 | { 52 | /* fsetpos() tested together with fsetpos(). */ 53 | return TEST_RESULTS; 54 | } 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /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/perror.c: -------------------------------------------------------------------------------- 1 | /* perror( 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 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | #endif 16 | 17 | /* TODO: Doing this via a static array is not the way to do it. */ 18 | void perror( const char * s ) 19 | { 20 | _PDCLIB_LOCK( stderr->mtx ); 21 | 22 | if ( ( s != NULL ) && ( s[0] != '\n' ) ) 23 | { 24 | fprintf( stderr, "%s: ", s ); 25 | } 26 | 27 | if ( errno >= _PDCLIB_ERRNO_MAX || errno < 0 ) 28 | { 29 | fprintf( stderr, "%s\n", _PDCLIB_EUNKNOWN_TEXT ); 30 | } 31 | else 32 | { 33 | fprintf( stderr, "%s\n", _PDCLIB_lc_messages->errno_texts[errno] ); 34 | } 35 | 36 | _PDCLIB_UNLOCK( stderr->mtx ); 37 | return; 38 | } 39 | 40 | #endif 41 | 42 | #ifdef TEST 43 | 44 | #include "_PDCLIB_test.h" 45 | 46 | #include 47 | #include 48 | #include 49 | 50 | int main( void ) 51 | { 52 | FILE * fh; 53 | unsigned long long max = ULLONG_MAX; 54 | char buffer[100]; 55 | sprintf( buffer, "%llu", max ); 56 | TESTCASE( ( fh = freopen( testfile, "wb+", stderr ) ) != NULL ); 57 | TESTCASE( strtol( buffer, NULL, 10 ) == LONG_MAX ); 58 | perror( "Test" ); 59 | rewind( fh ); 60 | TESTCASE( fread( buffer, 1, 7, fh ) == 7 ); 61 | TESTCASE( memcmp( buffer, "Test: ", 6 ) == 0 ); 62 | TESTCASE( fclose( fh ) == 0 ); 63 | TESTCASE( remove( testfile ) == 0 ); 64 | return TEST_RESULTS; 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /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 | 26 | #include 27 | #include 28 | #include 29 | 30 | #define _PDCLIB_FILEID "stdio/printf.c" 31 | #define _PDCLIB_FILEIO 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | #define testprintf( stream, ... ) printf( __VA_ARGS__ ) 36 | 37 | int main( void ) 38 | { 39 | FILE * target; 40 | TESTCASE( ( target = freopen( testfile, "wb+", stdout ) ) != NULL ); 41 | #include "printf_testcases.h" 42 | TESTCASE( fclose( target ) == 0 ); 43 | TESTCASE( remove( testfile ) == 0 ); 44 | return TEST_RESULTS; 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /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/puts.c: -------------------------------------------------------------------------------- 1 | /* puts( 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 "pdclib/_PDCLIB_glue.h" 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | #endif 16 | 17 | int puts( const char * s ) 18 | { 19 | _PDCLIB_LOCK( stdout->mtx ); 20 | 21 | if ( _PDCLIB_prepwrite( stdout ) == EOF ) 22 | { 23 | _PDCLIB_UNLOCK( stdout->mtx ); 24 | return EOF; 25 | } 26 | 27 | while ( *s != '\0' ) 28 | { 29 | stdout->buffer[ stdout->bufidx++ ] = *s++; 30 | 31 | if ( stdout->bufidx == stdout->bufsize ) 32 | { 33 | if ( _PDCLIB_flushbuffer( stdout ) == EOF ) 34 | { 35 | _PDCLIB_UNLOCK( stdout->mtx ); 36 | return EOF; 37 | } 38 | } 39 | } 40 | 41 | stdout->buffer[ stdout->bufidx++ ] = '\n'; 42 | 43 | if ( ( stdout->bufidx == stdout->bufsize ) || 44 | ( stdout->status & ( _IOLBF | _IONBF ) ) ) 45 | { 46 | int rc = _PDCLIB_flushbuffer( stdout ); 47 | _PDCLIB_UNLOCK( stdout->mtx ); 48 | return rc; 49 | } 50 | else 51 | { 52 | _PDCLIB_UNLOCK( stdout->mtx ); 53 | return 0; 54 | } 55 | } 56 | 57 | #endif 58 | 59 | #ifdef TEST 60 | 61 | #include "_PDCLIB_test.h" 62 | 63 | int main( void ) 64 | { 65 | FILE * fh; 66 | const char * message = "SUCCESS testing puts()"; 67 | char buffer[23]; 68 | buffer[22] = 'x'; 69 | TESTCASE( ( fh = freopen( testfile, "wb+", stdout ) ) != NULL ); 70 | TESTCASE( puts( message ) >= 0 ); 71 | rewind( fh ); 72 | TESTCASE( fread( buffer, 1, 22, fh ) == 22 ); 73 | TESTCASE( memcmp( buffer, message, 22 ) == 0 ); 74 | TESTCASE( buffer[22] == 'x' ); 75 | TESTCASE( fclose( fh ) == 0 ); 76 | TESTCASE( remove( testfile ) == 0 ); 77 | return TEST_RESULTS; 78 | } 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | extern int unlink( const char * pathname ); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | int remove( const char * pathname ) 31 | { 32 | int rc; 33 | struct _PDCLIB_file_t * current = _PDCLIB_filelist; 34 | 35 | while ( current != NULL ) 36 | { 37 | if ( ( current->filename != NULL ) && ( strcmp( current->filename, pathname ) == 0 ) ) 38 | { 39 | return EOF; 40 | } 41 | 42 | current = current->next; 43 | } 44 | 45 | if ( ( rc = unlink( pathname ) ) == -1 ) 46 | { 47 | /* The 1:1 mapping in _PDCLIB_config.h ensures this works. */ 48 | *_PDCLIB_errno_func() = errno; 49 | } 50 | 51 | return rc; 52 | } 53 | 54 | #endif 55 | 56 | #ifdef TEST 57 | 58 | #include "_PDCLIB_test.h" 59 | 60 | int main( void ) 61 | { 62 | /* Testing covered by ftell.c (and several others) */ 63 | return TEST_RESULTS; 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /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 | int rc; 15 | va_list ap; 16 | va_start( ap, format ); 17 | rc = vfscanf( stdin, format, ap ); 18 | va_end( ap ); 19 | return rc; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | #define _PDCLIB_FILEID "stdio/scanf.c" 26 | #define _PDCLIB_FILEIO 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | #define testscanf( stream, format, ... ) scanf( format, __VA_ARGS__ ) 31 | 32 | int main( void ) 33 | { 34 | FILE * source; 35 | TESTCASE( ( source = freopen( testfile, "wb+", stdin ) ) != NULL ); 36 | #include "scanf_testcases.h" 37 | TESTCASE( fclose( source ) == 0 ); 38 | TESTCASE( remove( testfile ) == 0 ); 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /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 | 26 | #include 27 | #include 28 | #include 29 | 30 | #define _PDCLIB_FILEID "stdio/snprintf.c" 31 | #define _PDCLIB_STRINGIO 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | #define testprintf( s, ... ) snprintf( s, 100, __VA_ARGS__ ) 36 | 37 | int main( void ) 38 | { 39 | char target[100]; 40 | #include "printf_testcases.h" 41 | TESTCASE( snprintf( NULL, 0, "foo" ) == 3 ); 42 | TESTCASE( snprintf( NULL, 0, "%d", 100 ) == 3 ); 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /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 | 27 | #include 28 | #include 29 | 30 | #define _PDCLIB_FILEID "stdio/sprintf.c" 31 | #define _PDCLIB_STRINGIO 32 | 33 | #include "_PDCLIB_test.h" 34 | 35 | #define testprintf( s, ... ) sprintf( s, __VA_ARGS__ ) 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/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/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 | 20 | if ( s == NULL ) 21 | { 22 | s = filename; 23 | } 24 | 25 | strcpy( s, file->filename ); 26 | fclose( file ); 27 | return s; 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | #include 37 | 38 | int main( void ) 39 | { 40 | #pragma GCC diagnostic push 41 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 42 | TESTCASE( strlen( tmpnam( NULL ) ) < L_tmpnam ); 43 | #pragma GCC diagnostic pop 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 | 21 | #include 22 | #include 23 | #include 24 | 25 | #define _PDCLIB_FILEID "stdio/vprintf.c" 26 | #define _PDCLIB_FILEIO 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | static int testprintf( FILE * stream, const char * format, ... ) 31 | { 32 | int i; 33 | va_list arg; 34 | va_start( arg, format ); 35 | i = vprintf( format, arg ); 36 | va_end( arg ); 37 | return i; 38 | } 39 | 40 | int main( void ) 41 | { 42 | FILE * target; 43 | TESTCASE( ( target = freopen( testfile, "wb+", stdout ) ) != NULL ); 44 | #include "printf_testcases.h" 45 | TESTCASE( fclose( target ) == 0 ); 46 | TESTCASE( remove( testfile ) == 0 ); 47 | return TEST_RESULTS; 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /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 | 22 | #include 23 | #include 24 | 25 | #define _PDCLIB_FILEID "stdio/vsprintf.c" 26 | #define _PDCLIB_STRINGIO 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | static int testprintf( char * s, const char * format, ... ) 31 | { 32 | int i; 33 | va_list arg; 34 | va_start( arg, format ); 35 | i = vsprintf( s, format, arg ); 36 | va_end( arg ); 37 | return i; 38 | } 39 | 40 | int main( void ) 41 | { 42 | char target[100]; 43 | #include "printf_testcases.h" 44 | return TEST_RESULTS; 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /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 | 50 | for ( i = 0; i < 32; ++i ) 51 | { 52 | assert( flags[ i ] == i ); 53 | } 54 | } 55 | 56 | #endif 57 | 58 | int main( void ) 59 | { 60 | #if ! defined( REGTEST ) || __STDC_VERSION__ >= 201112L 61 | int i; 62 | TESTCASE( at_quick_exit( &checkhandler ) == 0 ); 63 | 64 | for ( i = 0; i < 32; ++i ) 65 | { 66 | TESTCASE( at_quick_exit( &counthandler ) == 0 ); 67 | } 68 | 69 | #endif 70 | return TEST_RESULTS; 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /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 | 48 | for ( i = 0; i < 32; ++i ) 49 | { 50 | assert( flags[ i ] == i ); 51 | } 52 | } 53 | 54 | int main( void ) 55 | { 56 | int i; 57 | TESTCASE( atexit( &checkhandler ) == 0 ); 58 | 59 | for ( i = 0; i < 32; ++i ) 60 | { 61 | TESTCASE( atexit( &counthandler ) == 0 ); 62 | } 63 | 64 | return TEST_RESULTS; 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /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/bsearch.c: -------------------------------------------------------------------------------- 1 | /* bsearch( const void *, const void *, size_t, size_t, int(*)( const void *, const 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 | void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int ( *compar )( const void *, const void * ) ) 12 | { 13 | const void * pivot; 14 | int rc; 15 | size_t corr; 16 | 17 | while ( nmemb ) 18 | { 19 | /* algorithm needs -1 correction if remaining elements are an even number. */ 20 | corr = nmemb % 2; 21 | nmemb /= 2; 22 | pivot = ( const char * )base + ( nmemb * size ); 23 | rc = compar( key, pivot ); 24 | 25 | if ( rc > 0 ) 26 | { 27 | base = ( const char * )pivot + size; 28 | /* applying correction */ 29 | nmemb -= ( 1 - corr ); 30 | } 31 | else if ( rc == 0 ) 32 | { 33 | return ( void * )pivot; 34 | } 35 | } 36 | 37 | return NULL; 38 | } 39 | 40 | #endif 41 | 42 | #ifdef TEST 43 | 44 | #include "_PDCLIB_test.h" 45 | 46 | static int compare( const void * left, const void * right ) 47 | { 48 | return *( ( unsigned char * )left ) - *( ( unsigned char * )right ); 49 | } 50 | 51 | int main( void ) 52 | { 53 | TESTCASE( bsearch( "e", abcde, 4, 1, compare ) == NULL ); 54 | TESTCASE( bsearch( "e", abcde, 5, 1, compare ) == &abcde[4] ); 55 | TESTCASE( bsearch( "a", abcde + 1, 4, 1, compare ) == NULL ); 56 | TESTCASE( bsearch( "0", abcde, 1, 1, compare ) == NULL ); 57 | TESTCASE( bsearch( "a", abcde, 1, 1, compare ) == &abcde[0] ); 58 | TESTCASE( bsearch( "a", abcde, 0, 1, compare ) == NULL ); 59 | TESTCASE( bsearch( "e", abcde, 3, 2, compare ) == &abcde[4] ); 60 | TESTCASE( bsearch( "b", abcde, 3, 2, compare ) == NULL ); 61 | return TEST_RESULTS; 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /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 | 25 | _Exit( status ); 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | /* Unwinding of regstack tested in atexit(). */ 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /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 | 25 | _Exit( status ); 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | /* Unwinding of regstack tested in at_quick_exit(). */ 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /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/stdlib/strtod.c: -------------------------------------------------------------------------------- 1 | /* strtod( const char *, 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 | /* strtod() is implemented in functions/_dtoa/dtoa.c. */ 8 | -------------------------------------------------------------------------------- /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 | 15 | while ( n-- ) 16 | { 17 | if ( *p == ( unsigned char ) c ) 18 | { 19 | return ( void * ) p; 20 | } 21 | 22 | ++p; 23 | } 24 | 25 | return NULL; 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | TESTCASE( memchr( abcde, 'c', 5 ) == &abcde[2] ); 37 | TESTCASE( memchr( abcde, 'a', 1 ) == &abcde[0] ); 38 | TESTCASE( memchr( abcde, 'a', 0 ) == NULL ); 39 | TESTCASE( memchr( abcde, '\0', 5 ) == NULL ); 40 | TESTCASE( memchr( abcde, '\0', 6 ) == &abcde[5] ); 41 | return TEST_RESULTS; 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /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 | 16 | while ( n-- ) 17 | { 18 | if ( *p1 != *p2 ) 19 | { 20 | return *p1 - *p2; 21 | } 22 | 23 | ++p1; 24 | ++p2; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | const char xxxxx[] = "xxxxx"; 39 | TESTCASE( memcmp( abcde, abcdx, 5 ) < 0 ); 40 | TESTCASE( memcmp( abcde, abcdx, 4 ) == 0 ); 41 | TESTCASE( memcmp( abcde, xxxxx, 0 ) == 0 ); 42 | TESTCASE( memcmp( xxxxx, abcde, 1 ) > 0 ); 43 | return 0; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /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 | 16 | while ( n-- ) 17 | { 18 | *dest++ = *src++; 19 | } 20 | 21 | return s1; 22 | } 23 | 24 | #endif 25 | 26 | #ifdef TEST 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | int main( void ) 31 | { 32 | char s[] = "xxxxxxxxxxx"; 33 | TESTCASE( memcpy( s, abcde, 6 ) == s ); 34 | TESTCASE( s[4] == 'e' ); 35 | TESTCASE( s[5] == '\0' ); 36 | TESTCASE( memcpy( s + 5, abcde, 5 ) == s + 5 ); 37 | TESTCASE( s[9] == 'e' ); 38 | TESTCASE( s[10] == 'x' ); 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /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 | 16 | if ( dest <= src ) 17 | { 18 | while ( n-- ) 19 | { 20 | *dest++ = *src++; 21 | } 22 | } 23 | else 24 | { 25 | src += n; 26 | dest += n; 27 | 28 | while ( n-- ) 29 | { 30 | *--dest = *--src; 31 | } 32 | } 33 | 34 | return s1; 35 | } 36 | 37 | #endif 38 | 39 | #ifdef TEST 40 | 41 | #include "_PDCLIB_test.h" 42 | 43 | int main( void ) 44 | { 45 | char s[] = "xxxxabcde"; 46 | TESTCASE( memmove( s, s + 4, 5 ) == s ); 47 | TESTCASE( s[0] == 'a' ); 48 | TESTCASE( s[4] == 'e' ); 49 | TESTCASE( s[5] == 'b' ); 50 | TESTCASE( memmove( s + 4, s, 5 ) == s + 4 ); 51 | TESTCASE( s[4] == 'a' ); 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /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 | 15 | while ( n-- ) 16 | { 17 | *p++ = ( unsigned char ) c; 18 | } 19 | 20 | return s; 21 | } 22 | 23 | #endif 24 | 25 | #ifdef TEST 26 | 27 | #include "_PDCLIB_test.h" 28 | 29 | int main( void ) 30 | { 31 | char s[] = "xxxxxxxxx"; 32 | TESTCASE( memset( s, 'o', 10 ) == s ); 33 | TESTCASE( s[9] == 'o' ); 34 | TESTCASE( memset( s, '_', ( 0 ) ) == s ); 35 | TESTCASE( s[0] == 'o' ); 36 | TESTCASE( memset( s, '_', 1 ) == s ); 37 | TESTCASE( s[0] == '_' ); 38 | TESTCASE( s[1] == 'o' ); 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /functions/string/memset_s.c: -------------------------------------------------------------------------------- 1 | /* memset_s( void *, rsize_t, int, rsize_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 | #include 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | errno_t memset_s( void * s, rsize_t smax, int c, rsize_t n ) 15 | { 16 | unsigned char * p = ( unsigned char * ) s; 17 | 18 | if ( s == NULL || smax > RSIZE_MAX || n > RSIZE_MAX || n > smax ) 19 | { 20 | if ( s != NULL && smax <= RSIZE_MAX ) 21 | { 22 | memset( s, c, smax ); 23 | } 24 | 25 | _PDCLIB_constraint_handler( _PDCLIB_CONSTRAINT_VIOLATION( _PDCLIB_EINVAL ) ); 26 | return _PDCLIB_EINVAL; 27 | } 28 | 29 | while ( n-- ) 30 | { 31 | *p++ = ( unsigned char ) c; 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | #endif 38 | 39 | #ifdef TEST 40 | 41 | #include "_PDCLIB_test.h" 42 | 43 | #if ! defined( REGTEST ) || defined( __STDC_LIB_EXT1__ ) 44 | 45 | static int HANDLER_CALLS = 0; 46 | 47 | static void test_handler( const char * _PDCLIB_restrict msg, void * _PDCLIB_restrict ptr, errno_t error ) 48 | { 49 | ++HANDLER_CALLS; 50 | } 51 | 52 | #endif 53 | 54 | int main( void ) 55 | { 56 | #if ! defined( REGTEST ) || defined( __STDC_LIB_EXT1__ ) 57 | char s[] = "xxxxxxxxx"; 58 | set_constraint_handler_s( test_handler ); 59 | 60 | TESTCASE( memset_s( s, 10, 'o', 10 ) == 0 ); 61 | TESTCASE( s[9] == 'o' ); 62 | TESTCASE( memset_s( s, 10, '_', ( 0 ) ) == 0 ); 63 | TESTCASE( s[0] == 'o' ); 64 | TESTCASE( memset_s( s, 10, '_', 1 ) == 0 ); 65 | TESTCASE( s[0] == '_' ); 66 | TESTCASE( s[1] == 'o' ); 67 | 68 | TESTCASE( HANDLER_CALLS == 0 ); 69 | #endif 70 | return TEST_RESULTS; 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /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 | 15 | if ( *s1 ) 16 | { 17 | while ( *++s1 ) 18 | { 19 | /* EMPTY */ 20 | } 21 | } 22 | 23 | while ( ( *s1++ = *s2++ ) ) 24 | { 25 | /* EMPTY */ 26 | } 27 | 28 | return rc; 29 | } 30 | 31 | #endif 32 | 33 | #ifdef TEST 34 | 35 | #include "_PDCLIB_test.h" 36 | 37 | int main( void ) 38 | { 39 | char s[] = "xx\0xxxxxx"; 40 | TESTCASE( strcat( s, abcde ) == s ); 41 | TESTCASE( s[2] == 'a' ); 42 | TESTCASE( s[6] == 'e' ); 43 | TESTCASE( s[7] == '\0' ); 44 | TESTCASE( s[8] == 'x' ); 45 | s[0] = '\0'; 46 | TESTCASE( strcat( s, abcdx ) == s ); 47 | TESTCASE( s[4] == 'x' ); 48 | TESTCASE( s[5] == '\0' ); 49 | TESTCASE( strcat( s, "\0" ) == s ); 50 | TESTCASE( s[5] == '\0' ); 51 | TESTCASE( s[6] == 'e' ); 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /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 | 21 | return NULL; 22 | } 23 | 24 | #endif 25 | 26 | #ifdef TEST 27 | 28 | #include "_PDCLIB_test.h" 29 | 30 | int main( void ) 31 | { 32 | char abccd[] = "abccd"; 33 | TESTCASE( strchr( abccd, 'x' ) == NULL ); 34 | TESTCASE( strchr( abccd, 'a' ) == &abccd[0] ); 35 | TESTCASE( strchr( abccd, 'd' ) == &abccd[4] ); 36 | TESTCASE( strchr( abccd, '\0' ) == &abccd[5] ); 37 | TESTCASE( strchr( abccd, 'c' ) == &abccd[2] ); 38 | return TEST_RESULTS; 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /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 | 19 | return ( *( unsigned char * )s1 - * ( unsigned char * )s2 ); 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | 26 | #include "_PDCLIB_test.h" 27 | 28 | int main( void ) 29 | { 30 | char cmpabcde[] = "abcde"; 31 | char cmpabcd_[] = "abcd\xfc"; 32 | char empty[] = ""; 33 | TESTCASE( strcmp( abcde, cmpabcde ) == 0 ); 34 | TESTCASE( strcmp( abcde, abcdx ) < 0 ); 35 | TESTCASE( strcmp( abcdx, abcde ) > 0 ); 36 | TESTCASE( strcmp( empty, abcde ) < 0 ); 37 | TESTCASE( strcmp( abcde, empty ) > 0 ); 38 | TESTCASE( strcmp( abcde, cmpabcd_ ) < 0 ); 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /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 | /* FIXME: This should access _PDCLIB_lc_collate. */ 16 | return strcmp( s1, s2 ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | char cmpabcde[] = "abcde"; 28 | char empty[] = ""; 29 | TESTCASE( strcmp( abcde, cmpabcde ) == 0 ); 30 | TESTCASE( strcmp( abcde, abcdx ) < 0 ); 31 | TESTCASE( strcmp( abcdx, abcde ) > 0 ); 32 | TESTCASE( strcmp( empty, abcde ) < 0 ); 33 | TESTCASE( strcmp( abcde, empty ) > 0 ); 34 | return TEST_RESULTS; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /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 | 15 | while ( ( *s1++ = *s2++ ) ) 16 | { 17 | /* EMPTY */ 18 | } 19 | 20 | return rc; 21 | } 22 | 23 | #endif 24 | 25 | #ifdef TEST 26 | 27 | #include "_PDCLIB_test.h" 28 | 29 | int main( void ) 30 | { 31 | char s[] = "xxxxx"; 32 | TESTCASE( strcpy( s, "" ) == s ); 33 | TESTCASE( s[0] == '\0' ); 34 | TESTCASE( s[1] == 'x' ); 35 | TESTCASE( strcpy( s, abcde ) == s ); 36 | TESTCASE( s[0] == 'a' ); 37 | TESTCASE( s[4] == 'e' ); 38 | TESTCASE( s[5] == '\0' ); 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /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 | 16 | while ( s1[len] ) 17 | { 18 | p = s2; 19 | 20 | while ( *p ) 21 | { 22 | if ( s1[len] == *p++ ) 23 | { 24 | return len; 25 | } 26 | } 27 | 28 | ++len; 29 | } 30 | 31 | return len; 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | TESTCASE( strcspn( abcde, "x" ) == 5 ); 43 | TESTCASE( strcspn( abcde, "xyz" ) == 5 ); 44 | TESTCASE( strcspn( abcde, "zyx" ) == 5 ); 45 | TESTCASE( strcspn( abcdx, "x" ) == 4 ); 46 | TESTCASE( strcspn( abcdx, "xyz" ) == 4 ); 47 | TESTCASE( strcspn( abcdx, "zyx" ) == 4 ); 48 | TESTCASE( strcspn( abcde, "a" ) == 0 ); 49 | TESTCASE( strcspn( abcde, "abc" ) == 0 ); 50 | TESTCASE( strcspn( abcde, "cba" ) == 0 ); 51 | return TEST_RESULTS; 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /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/strerror_s.c: -------------------------------------------------------------------------------- 1 | /* strerror_s( char *, rsize_t, 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 | #include 10 | #include 11 | #include 12 | 13 | #ifndef REGTEST 14 | 15 | #include 16 | 17 | errno_t strerror_s( char * s, rsize_t maxsize, errno_t errnum ) 18 | { 19 | size_t len = strerrorlen_s( errnum ); 20 | 21 | if ( s == NULL || maxsize > RSIZE_MAX || maxsize == 0 ) 22 | { 23 | _PDCLIB_constraint_handler( _PDCLIB_CONSTRAINT_VIOLATION( _PDCLIB_EINVAL ) ); 24 | return _PDCLIB_EINVAL; 25 | } 26 | 27 | if ( len < maxsize ) 28 | { 29 | strcpy( s, strerror( errnum ) ); 30 | } 31 | else 32 | { 33 | strncpy( s, strerror( errnum ), maxsize - 1 ); 34 | 35 | if ( maxsize > 3 ) 36 | { 37 | strcpy( &s[ maxsize - 4 ], "..." ); 38 | } 39 | else 40 | { 41 | s[ maxsize - 1 ] = '\0'; 42 | } 43 | } 44 | 45 | return 0; 46 | } 47 | 48 | #endif 49 | 50 | #ifdef TEST 51 | 52 | #include "_PDCLIB_test.h" 53 | 54 | #include 55 | #include 56 | 57 | int main( void ) 58 | { 59 | #if ! defined( REGTEST ) || defined( __STDC_LIB_EXT1__ ) 60 | char s[14]; 61 | TESTCASE( strerror_s( s, 14, _PDCLIB_ERRNO_MAX ) == 0 ); 62 | TESTCASE( strcmp( s, "unknown error" ) == 0 ); 63 | TESTCASE( strerror_s( s, 13, _PDCLIB_ERRNO_MAX ) == 0 ); 64 | TESTCASE( strcmp( s, "unknown e..." ) == 0 ); 65 | #endif 66 | return TEST_RESULTS; 67 | } 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /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 | 15 | while ( s[rc] ) 16 | { 17 | ++rc; 18 | } 19 | 20 | return rc; 21 | } 22 | 23 | #endif 24 | 25 | #ifdef TEST 26 | 27 | #include "_PDCLIB_test.h" 28 | 29 | int main( void ) 30 | { 31 | TESTCASE( strlen( abcde ) == 5 ); 32 | TESTCASE( strlen( "" ) == 0 ); 33 | return TEST_RESULTS; 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /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 | 15 | while ( *s1 ) 16 | { 17 | ++s1; 18 | } 19 | 20 | while ( n && ( *s1++ = *s2++ ) ) 21 | { 22 | --n; 23 | } 24 | 25 | if ( n == 0 ) 26 | { 27 | *s1 = '\0'; 28 | } 29 | 30 | return rc; 31 | } 32 | 33 | #endif 34 | 35 | #ifdef TEST 36 | 37 | #include "_PDCLIB_test.h" 38 | 39 | int main( void ) 40 | { 41 | char s[] = "xx\0xxxxxx"; 42 | TESTCASE( strncat( s, abcde, 10 ) == s ); 43 | TESTCASE( s[2] == 'a' ); 44 | TESTCASE( s[6] == 'e' ); 45 | TESTCASE( s[7] == '\0' ); 46 | TESTCASE( s[8] == 'x' ); 47 | s[0] = '\0'; 48 | TESTCASE( strncat( s, abcdx, 10 ) == s ); 49 | TESTCASE( s[4] == 'x' ); 50 | TESTCASE( s[5] == '\0' ); 51 | TESTCASE( strncat( s, "\0", 10 ) == s ); 52 | TESTCASE( s[5] == '\0' ); 53 | TESTCASE( s[6] == 'e' ); 54 | TESTCASE( strncat( s, abcde, 0 ) == s ); 55 | TESTCASE( s[5] == '\0' ); 56 | TESTCASE( s[6] == 'e' ); 57 | TESTCASE( strncat( s, abcde, 3 ) == s ); 58 | TESTCASE( s[5] == 'a' ); 59 | TESTCASE( s[7] == 'c' ); 60 | TESTCASE( s[8] == '\0' ); 61 | return TEST_RESULTS; 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /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 | 20 | if ( n == 0 ) 21 | { 22 | return 0; 23 | } 24 | else 25 | { 26 | return ( *( unsigned char * )s1 - * ( unsigned char * )s2 ); 27 | } 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | char cmpabcde[] = "abcde\0f"; 39 | char cmpabcd_[] = "abcde\xfc"; 40 | char empty[] = ""; 41 | char x[] = "x"; 42 | TESTCASE( strncmp( abcde, cmpabcde, 5 ) == 0 ); 43 | TESTCASE( strncmp( abcde, cmpabcde, 10 ) == 0 ); 44 | TESTCASE( strncmp( abcde, abcdx, 5 ) < 0 ); 45 | TESTCASE( strncmp( abcdx, abcde, 5 ) > 0 ); 46 | TESTCASE( strncmp( empty, abcde, 5 ) < 0 ); 47 | TESTCASE( strncmp( abcde, empty, 5 ) > 0 ); 48 | TESTCASE( strncmp( abcde, abcdx, 4 ) == 0 ); 49 | TESTCASE( strncmp( abcde, x, 0 ) == 0 ); 50 | TESTCASE( strncmp( abcde, x, 1 ) < 0 ); 51 | TESTCASE( strncmp( abcde, cmpabcd_, 10 ) < 0 ); 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /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 | 15 | while ( n && ( *s1++ = *s2++ ) ) 16 | { 17 | /* Cannot do "n--" in the conditional as size_t is unsigned and we have 18 | to check it again for >0 in the next loop below, so we must not risk 19 | underflow. 20 | */ 21 | --n; 22 | } 23 | 24 | /* Checking against 1 as we missed the last --n in the loop above. */ 25 | while ( n-- > 1 ) 26 | { 27 | *s1++ = '\0'; 28 | } 29 | 30 | return rc; 31 | } 32 | 33 | #endif 34 | 35 | #ifdef TEST 36 | 37 | #include "_PDCLIB_test.h" 38 | 39 | int main( void ) 40 | { 41 | char s[] = "xxxxxxx"; 42 | TESTCASE( strncpy( s, "", 1 ) == s ); 43 | TESTCASE( s[0] == '\0' ); 44 | TESTCASE( s[1] == 'x' ); 45 | TESTCASE( strncpy( s, abcde, 6 ) == s ); 46 | TESTCASE( s[0] == 'a' ); 47 | TESTCASE( s[4] == 'e' ); 48 | TESTCASE( s[5] == '\0' ); 49 | TESTCASE( s[6] == 'x' ); 50 | TESTCASE( strncpy( s, abcde, 7 ) == s ); 51 | TESTCASE( s[6] == '\0' ); 52 | TESTCASE( strncpy( s, "xxxx", 3 ) == s ); 53 | TESTCASE( s[0] == 'x' ); 54 | TESTCASE( s[2] == 'x' ); 55 | TESTCASE( s[3] == 'd' ); 56 | return TEST_RESULTS; 57 | } 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /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 | 16 | while ( *p1 ) 17 | { 18 | p2 = s2; 19 | 20 | while ( *p2 ) 21 | { 22 | if ( *p1 == *p2++ ) 23 | { 24 | return ( char * ) p1; 25 | } 26 | } 27 | 28 | ++p1; 29 | } 30 | 31 | return NULL; 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | TESTCASE( strpbrk( abcde, "x" ) == NULL ); 43 | TESTCASE( strpbrk( abcde, "xyz" ) == NULL ); 44 | TESTCASE( strpbrk( abcdx, "x" ) == &abcdx[4] ); 45 | TESTCASE( strpbrk( abcdx, "xyz" ) == &abcdx[4] ); 46 | TESTCASE( strpbrk( abcdx, "zyx" ) == &abcdx[4] ); 47 | TESTCASE( strpbrk( abcde, "a" ) == &abcde[0] ); 48 | TESTCASE( strpbrk( abcde, "abc" ) == &abcde[0] ); 49 | TESTCASE( strpbrk( abcde, "cba" ) == &abcde[0] ); 50 | return TEST_RESULTS; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /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 | 15 | while ( s[i++] ) 16 | { 17 | /* EMPTY */ 18 | } 19 | 20 | do 21 | { 22 | if ( s[--i] == ( char ) c ) 23 | { 24 | return ( char * ) s + i; 25 | } 26 | } while ( i ); 27 | 28 | return NULL; 29 | } 30 | 31 | #endif 32 | 33 | #ifdef TEST 34 | 35 | #include "_PDCLIB_test.h" 36 | 37 | int main( void ) 38 | { 39 | char abccd[] = "abccd"; 40 | TESTCASE( strrchr( abcde, '\0' ) == &abcde[5] ); 41 | TESTCASE( strrchr( abcde, 'e' ) == &abcde[4] ); 42 | TESTCASE( strrchr( abcde, 'a' ) == &abcde[0] ); 43 | TESTCASE( strrchr( abccd, 'c' ) == &abccd[3] ); 44 | return TEST_RESULTS; 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /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 | 16 | while ( s1[ len ] ) 17 | { 18 | p = s2; 19 | 20 | while ( *p ) 21 | { 22 | if ( s1[len] == *p ) 23 | { 24 | break; 25 | } 26 | 27 | ++p; 28 | } 29 | 30 | if ( ! *p ) 31 | { 32 | return len; 33 | } 34 | 35 | ++len; 36 | } 37 | 38 | return len; 39 | } 40 | 41 | #endif 42 | 43 | #ifdef TEST 44 | 45 | #include "_PDCLIB_test.h" 46 | 47 | int main( void ) 48 | { 49 | TESTCASE( strspn( abcde, "abc" ) == 3 ); 50 | TESTCASE( strspn( abcde, "b" ) == 0 ); 51 | TESTCASE( strspn( abcde, abcde ) == 5 ); 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /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 | 16 | while ( *s1 ) 17 | { 18 | p2 = s2; 19 | 20 | while ( *p2 && ( *p1 == *p2 ) ) 21 | { 22 | ++p1; 23 | ++p2; 24 | } 25 | 26 | if ( ! *p2 ) 27 | { 28 | return ( char * ) s1; 29 | } 30 | 31 | ++s1; 32 | p1 = s1; 33 | } 34 | 35 | return NULL; 36 | } 37 | 38 | #endif 39 | 40 | #ifdef TEST 41 | 42 | #include "_PDCLIB_test.h" 43 | 44 | int main( void ) 45 | { 46 | char s[] = "abcabcabcdabcde"; 47 | TESTCASE( strstr( s, "x" ) == NULL ); 48 | TESTCASE( strstr( s, "xyz" ) == NULL ); 49 | TESTCASE( strstr( s, "a" ) == &s[0] ); 50 | TESTCASE( strstr( s, "abc" ) == &s[0] ); 51 | TESTCASE( strstr( s, "abcd" ) == &s[6] ); 52 | TESTCASE( strstr( s, "abcde" ) == &s[10] ); 53 | return TEST_RESULTS; 54 | } 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /functions/string/strtok.c: -------------------------------------------------------------------------------- 1 | /* strtok( 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 | #define __STDC_WANT_LIB_EXT1__ 1 9 | #endif 10 | 11 | #include 12 | 13 | #ifndef REGTEST 14 | 15 | char * strtok( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 ) 16 | { 17 | static char * tmp = NULL; 18 | static rsize_t max; 19 | 20 | if ( s1 != NULL ) 21 | { 22 | /* new string */ 23 | tmp = s1; 24 | max = strlen( tmp ); 25 | } 26 | 27 | return _PDCLIB_strtok( s1, &max, s2, &tmp ); 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | char s[] = "_a_bc__d_"; 39 | TESTCASE( strtok( s, "_" ) == &s[1] ); 40 | TESTCASE( s[1] == 'a' ); 41 | TESTCASE( s[2] == '\0' ); 42 | TESTCASE( strtok( NULL, "_" ) == &s[3] ); 43 | TESTCASE( s[3] == 'b' ); 44 | TESTCASE( s[4] == 'c' ); 45 | TESTCASE( s[5] == '\0' ); 46 | TESTCASE( strtok( NULL, "_" ) == &s[7] ); 47 | TESTCASE( s[6] == '_' ); 48 | TESTCASE( s[7] == 'd' ); 49 | TESTCASE( s[8] == '\0' ); 50 | TESTCASE( strtok( NULL, "_" ) == NULL ); 51 | strcpy( s, "ab_cd" ); 52 | TESTCASE( strtok( s, "_" ) == &s[0] ); 53 | TESTCASE( s[0] == 'a' ); 54 | TESTCASE( s[1] == 'b' ); 55 | TESTCASE( s[2] == '\0' ); 56 | TESTCASE( strtok( NULL, "_" ) == &s[3] ); 57 | TESTCASE( s[3] == 'c' ); 58 | TESTCASE( s[4] == 'd' ); 59 | TESTCASE( s[5] == '\0' ); 60 | TESTCASE( strtok( NULL, "_" ) == NULL ); 61 | return TEST_RESULTS; 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /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 | 17 | if ( len < n ) 18 | { 19 | /* Cannot use strncpy() here as the filling of s1 with '\0' is not part 20 | of the spec. 21 | */ 22 | /* FIXME: This should access _PDCLIB_lc_collate. */ 23 | while ( n-- && ( *s1++ = ( unsigned char )*s2++ ) ) 24 | { 25 | /* EMPTY */ 26 | } 27 | } 28 | 29 | return len; 30 | } 31 | 32 | #endif 33 | 34 | #ifdef TEST 35 | 36 | #include "_PDCLIB_test.h" 37 | 38 | int main( void ) 39 | { 40 | char s[] = "xxxxxxxxxxx"; 41 | TESTCASE( strxfrm( NULL, "123456789012", 0 ) == 12 ); 42 | TESTCASE( strxfrm( s, "123456789012", 12 ) == 12 ); 43 | /* 44 | The following test case is true in *this* implementation, but doesn't have to. 45 | TESTCASE( s[0] == 'x' ); 46 | */ 47 | TESTCASE( strxfrm( s, "1234567890", 11 ) == 10 ); 48 | TESTCASE( s[0] == '1' ); 49 | TESTCASE( s[9] == '0' ); 50 | TESTCASE( s[10] == '\0' ); 51 | return TEST_RESULTS; 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /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 | #include 12 | 13 | char * asctime( const struct tm * timeptr ) 14 | { 15 | static char rc[26]; 16 | sprintf( rc, "%s %s%3d %.2d:%.2d:%.2d %d\n", 17 | _PDCLIB_lc_time_C.day_name_abbr[ timeptr->tm_wday ], 18 | _PDCLIB_lc_time_C.month_name_abbr[ timeptr->tm_mon ], 19 | timeptr->tm_mday, 20 | timeptr->tm_hour, 21 | timeptr->tm_min, 22 | timeptr->tm_sec, 23 | timeptr->tm_year + 1900 ); 24 | return rc; 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | #include 34 | 35 | int main( void ) 36 | { 37 | struct tm time; 38 | MKTIME( time, 52, 3, 1, 16, 8, 73, 0, 0 ); 39 | TESTCASE( strcmp( asctime( &time ), "Sun Sep 16 01:03:52 1973\n" ) == 0 ); 40 | MKTIME( time, 0, 0, 0, 1, 0, 0, 0, 0 ); 41 | TESTCASE( strcmp( asctime( &time ), "Sun Jan 1 00:00:00 1900\n" ) == 0 ); 42 | MKTIME( time, 60, 59, 23, 31, 11, 8099, 6, 0 ); 43 | TESTCASE( strcmp( asctime( &time ), "Sat Dec 31 23:59:60 9999\n" ) == 0 ); 44 | return TEST_RESULTS; 45 | } 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /functions/time/asctime_s.c: -------------------------------------------------------------------------------- 1 | /* asctime_s( char *, rsize_t, 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 | #define __STDC_WANT_LIB_EXT1__ 1 8 | #include 9 | #include 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include 15 | 16 | errno_t asctime_s( char * s, rsize_t maxsize, const struct tm * timeptr ) 17 | { 18 | if ( s == NULL || timeptr == NULL || maxsize < 26 || maxsize > RSIZE_MAX || timeptr->tm_year < -1900 || timeptr->tm_year > 8099 /* TODO: || not normalized */ ) 19 | { 20 | if ( s != NULL && maxsize > 0 && maxsize <= RSIZE_MAX ) 21 | { 22 | s[0] = '\0'; 23 | } 24 | 25 | _PDCLIB_constraint_handler( _PDCLIB_CONSTRAINT_VIOLATION( _PDCLIB_EINVAL ) ); 26 | return _PDCLIB_EINVAL; 27 | } 28 | 29 | sprintf( s, "%s %s %2d %.2d:%.2d:%.2d %4d\n", 30 | _PDCLIB_lc_time_C.day_name_abbr[ timeptr->tm_wday ], 31 | _PDCLIB_lc_time_C.month_name_abbr[ timeptr->tm_mon ], 32 | timeptr->tm_mday, 33 | timeptr->tm_hour, 34 | timeptr->tm_min, 35 | timeptr->tm_sec, 36 | timeptr->tm_year + 1900 ); 37 | 38 | return 0; 39 | } 40 | 41 | #endif 42 | 43 | #ifdef TEST 44 | 45 | #include "_PDCLIB_test.h" 46 | 47 | #include 48 | 49 | int main( void ) 50 | { 51 | /* TODO: Constraint handling */ 52 | 53 | struct tm time; 54 | MKTIME( time, 52, 3, 1, 16, 8, 73, 0, 0 ); 55 | TESTCASE( strcmp( asctime( &time ), "Sun Sep 16 01:03:52 1973\n" ) == 0 ); 56 | MKTIME( time, 0, 0, 0, 1, 0, 0, 0, 0 ); 57 | TESTCASE( strcmp( asctime( &time ), "Sun Jan 1 00:00:00 1900\n" ) == 0 ); 58 | MKTIME( time, 60, 59, 23, 31, 11, 8099, 6, 0 ); 59 | TESTCASE( strcmp( asctime( &time ), "Sat Dec 31 23:59:60 9999\n" ) == 0 ); 60 | return TEST_RESULTS; 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /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 | /* Section 4.12.3.2 of X3.159-1989 requires that 14 | The ctime function converts the calendar time pointed to by timer 15 | to local time in the form of a string. It is equivalent to 16 | asctime(localtime(timer)) 17 | */ 18 | struct tm * tmp = localtime( timer ); 19 | return tmp ? asctime( tmp ) : NULL; 20 | } 21 | 22 | #endif 23 | 24 | #ifdef TEST 25 | 26 | #include "_PDCLIB_test.h" 27 | 28 | #include 29 | 30 | int main( void ) 31 | { 32 | /* TODO: System Clock DST */ 33 | time_t t; 34 | 35 | t = -2147483648l; 36 | TESTCASE( strcmp( ctime( &t ), "Fri Dec 13 21:45:52 1901\n" ) == 0 ); 37 | 38 | t = 2147483647l; 39 | TESTCASE( strcmp( ctime( &t ), "Tue Jan 19 04:14:07 2038\n" ) == 0 ); 40 | 41 | return TEST_RESULTS; 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /functions/time/ctime_s.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 | #define __STDC_WANT_LIB_EXT1__ 1 8 | #include 9 | #include 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | errno_t ctime_s( char * s, rsize_t maxsize, const time_t * timer ) 15 | { 16 | struct tm tm; 17 | 18 | if ( s == NULL || timer == NULL || maxsize < 26 || maxsize > RSIZE_MAX ) 19 | { 20 | if ( s != NULL && maxsize > 0 && maxsize <= RSIZE_MAX ) 21 | { 22 | s[0] = '\0'; 23 | } 24 | 25 | _PDCLIB_constraint_handler( _PDCLIB_CONSTRAINT_VIOLATION( _PDCLIB_EINVAL ) ); 26 | return _PDCLIB_EINVAL; 27 | } 28 | 29 | return asctime_s( s, maxsize, localtime_s( timer, &tm ) ); 30 | } 31 | 32 | #endif 33 | 34 | #ifdef TEST 35 | 36 | #include "_PDCLIB_test.h" 37 | 38 | #include 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | /* TODO: System Clock DST */ 44 | time_t t; 45 | char s[27]; 46 | 47 | /* TODO: Constraint handling */ 48 | 49 | t = -2147483648l; 50 | TESTCASE( ctime_s( s, 27, &t ) == 0 ); 51 | TESTCASE( strcmp( s, "Fri Dec 13 21:45:52 1901\n" ) == 0 ); 52 | 53 | t = 2147483647l; 54 | TESTCASE( ctime_s( s, 27, &t ) == 0 ); 55 | TESTCASE( strcmp( s, "Tue Jan 19 04:14:07 2038\n" ) == 0 ); 56 | #endif 57 | 58 | return TEST_RESULTS; 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /functions/time/gmtime.c: -------------------------------------------------------------------------------- 1 | /* gmtime( 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 | #include "pdclib/_PDCLIB_tzcode.h" 12 | 13 | struct tm * gmtime( const time_t * timer ) 14 | { 15 | _PDCLIB_gmtcheck(); 16 | return _PDCLIB_gmtsub( &_PDCLIB_gmtmem, timer, 0, &_PDCLIB_tm ); 17 | } 18 | 19 | #endif 20 | 21 | #ifdef TEST 22 | 23 | #include "_PDCLIB_test.h" 24 | 25 | int main( void ) 26 | { 27 | time_t t; 28 | struct tm * time; 29 | 30 | t = -2147483648l; 31 | time = gmtime( &t ); 32 | TESTCASE( time->tm_sec == 52 ); 33 | TESTCASE( time->tm_min == 45 ); 34 | TESTCASE( time->tm_hour == 20 ); 35 | TESTCASE( time->tm_mday == 13 ); 36 | TESTCASE( time->tm_mon == 11 ); 37 | TESTCASE( time->tm_year == 1 ); 38 | TESTCASE( time->tm_wday == 5 ); 39 | TESTCASE( time->tm_yday == 346 ); 40 | 41 | t = 2147483647l; 42 | time = gmtime( &t ); 43 | TESTCASE( time->tm_sec == 7 ); 44 | TESTCASE( time->tm_min == 14 ); 45 | TESTCASE( time->tm_hour == 3 ); 46 | TESTCASE( time->tm_mday == 19 ); 47 | TESTCASE( time->tm_mon == 0 ); 48 | TESTCASE( time->tm_year == 138 ); 49 | TESTCASE( time->tm_wday == 2 ); 50 | TESTCASE( time->tm_yday == 18 ); 51 | 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /functions/time/gmtime_s.c: -------------------------------------------------------------------------------- 1 | /* gmtime_s( const time_t *, 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 | #define __STDC_WANT_LIB_EXT1__ 1 8 | #include 9 | #include 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include "pdclib/_PDCLIB_tzcode.h" 15 | 16 | struct tm * gmtime_s( const time_t * _PDCLIB_restrict timer, struct tm * _PDCLIB_restrict result ) 17 | { 18 | if ( timer == NULL || result == NULL ) 19 | { 20 | _PDCLIB_constraint_handler( _PDCLIB_CONSTRAINT_VIOLATION( _PDCLIB_EINVAL ) ); 21 | return NULL; 22 | } 23 | 24 | _PDCLIB_gmtcheck(); 25 | return _PDCLIB_gmtsub( &_PDCLIB_gmtmem, timer, 0, result ); 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | #ifndef REGTEST 37 | time_t t; 38 | struct tm time; 39 | 40 | /* TODO: Constraint handling */ 41 | 42 | t = -2147483648l; 43 | TESTCASE( gmtime_s( &t, &time ) != NULL ); 44 | TESTCASE( time.tm_sec == 52 ); 45 | TESTCASE( time.tm_min == 45 ); 46 | TESTCASE( time.tm_hour == 20 ); 47 | TESTCASE( time.tm_mday == 13 ); 48 | TESTCASE( time.tm_mon == 11 ); 49 | TESTCASE( time.tm_year == 1 ); 50 | TESTCASE( time.tm_wday == 5 ); 51 | TESTCASE( time.tm_yday == 346 ); 52 | 53 | t = 2147483647l; 54 | TESTCASE( gmtime_s( &t, &time ) != NULL ); 55 | TESTCASE( time.tm_sec == 7 ); 56 | TESTCASE( time.tm_min == 14 ); 57 | TESTCASE( time.tm_hour == 3 ); 58 | TESTCASE( time.tm_mday == 19 ); 59 | TESTCASE( time.tm_mon == 0 ); 60 | TESTCASE( time.tm_year == 138 ); 61 | TESTCASE( time.tm_wday == 2 ); 62 | TESTCASE( time.tm_yday == 18 ); 63 | #endif 64 | 65 | return TEST_RESULTS; 66 | } 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /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 | #include "pdclib/_PDCLIB_tzcode.h" 12 | 13 | struct tm * localtime( const time_t * timer ) 14 | { 15 | return _PDCLIB_localtime_tzset( timer, &_PDCLIB_tm, true ); 16 | } 17 | 18 | #endif 19 | 20 | #ifdef TEST 21 | 22 | #include "_PDCLIB_test.h" 23 | 24 | int main( void ) 25 | { 26 | /* TODO: System Clock DST */ 27 | 28 | time_t t; 29 | struct tm * time; 30 | 31 | t = -2147483648l; 32 | time = localtime( &t ); 33 | TESTCASE( time->tm_sec == 52 ); 34 | TESTCASE( time->tm_min == 45 ); 35 | TESTCASE( time->tm_hour == 21 ); 36 | TESTCASE( time->tm_mday == 13 ); 37 | TESTCASE( time->tm_mon == 11 ); 38 | TESTCASE( time->tm_year == 1 ); 39 | TESTCASE( time->tm_wday == 5 ); 40 | TESTCASE( time->tm_yday == 346 ); 41 | 42 | t = 2147483647l; 43 | time = localtime( &t ); 44 | TESTCASE( time->tm_sec == 7 ); 45 | TESTCASE( time->tm_min == 14 ); 46 | TESTCASE( time->tm_hour == 4 ); 47 | TESTCASE( time->tm_mday == 19 ); 48 | TESTCASE( time->tm_mon == 0 ); 49 | TESTCASE( time->tm_year == 138 ); 50 | TESTCASE( time->tm_wday == 2 ); 51 | TESTCASE( time->tm_yday == 18 ); 52 | 53 | return TEST_RESULTS; 54 | } 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /functions/time/localtime_s.c: -------------------------------------------------------------------------------- 1 | /* localtime_s( const time_t *, 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 | #define __STDC_WANT_LIB_EXT1__ 1 8 | #include 9 | #include 10 | #include 11 | 12 | #ifndef REGTEST 13 | 14 | #include "pdclib/_PDCLIB_tzcode.h" 15 | 16 | struct tm * localtime_s( const time_t * _PDCLIB_restrict timer, struct tm * _PDCLIB_restrict result ) 17 | { 18 | if ( timer == NULL || result == NULL ) 19 | { 20 | _PDCLIB_constraint_handler( _PDCLIB_CONSTRAINT_VIOLATION( _PDCLIB_EINVAL ) ); 21 | return NULL; 22 | } 23 | 24 | return _PDCLIB_localtime_tzset( timer, result, true ); 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | int main( void ) 34 | { 35 | #ifndef REGTEST 36 | time_t t; 37 | struct tm time; 38 | 39 | /* TODO: Constraint handling, System Clock DST */ 40 | 41 | t = -2147483648l; 42 | TESTCASE( localtime_s( &t, &time ) != NULL ); 43 | TESTCASE( time.tm_sec == 52 ); 44 | TESTCASE( time.tm_min == 45 ); 45 | TESTCASE( time.tm_hour == 21 ); 46 | TESTCASE( time.tm_mday == 13 ); 47 | TESTCASE( time.tm_mon == 11 ); 48 | TESTCASE( time.tm_year == 1 ); 49 | TESTCASE( time.tm_wday == 5 ); 50 | TESTCASE( time.tm_yday == 346 ); 51 | 52 | t = 2147483647l; 53 | TESTCASE( localtime_s( &t, &time ) != NULL ); 54 | TESTCASE( time.tm_sec == 7 ); 55 | TESTCASE( time.tm_min == 14 ); 56 | TESTCASE( time.tm_hour == 4 ); 57 | TESTCASE( time.tm_mday == 19 ); 58 | TESTCASE( time.tm_mon == 0 ); 59 | TESTCASE( time.tm_year == 138 ); 60 | TESTCASE( time.tm_wday == 2 ); 61 | TESTCASE( time.tm_yday == 18 ); 62 | #endif 63 | 64 | return TEST_RESULTS; 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /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 | #include "pdclib/_PDCLIB_tzcode.h" 12 | 13 | #ifndef __STDC_NO_THREADS__ 14 | #include 15 | extern mtx_t _PDCLIB_time_mtx; 16 | #endif 17 | 18 | time_t mktime( struct tm * timeptr ) 19 | { 20 | time_t t; 21 | _PDCLIB_LOCK( _PDCLIB_time_mtx ); 22 | _PDCLIB_tzset_unlocked(); 23 | t = _PDCLIB_mktime_tzname( &_PDCLIB_lclmem, timeptr, true ); 24 | _PDCLIB_UNLOCK( _PDCLIB_time_mtx ); 25 | return t; 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | #include 35 | 36 | int main( void ) 37 | { 38 | /* System Clock DST */ 39 | struct tm time; 40 | time_t t; 41 | 42 | MKTIME( time, 52, 45, 21, 13, 11, 1, 0, 0 ); 43 | t = mktime( &time ); 44 | TESTCASE( t == -2147483648l ); 45 | 46 | MKTIME( time, 7, 14, 4, 19, 0, 138, 0, 0 ); 47 | t = mktime( &time ); 48 | TESTCASE( t == 2147483647l ); 49 | 50 | return TEST_RESULTS; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | #include "pdclib/_PDCLIB_internal.h" 12 | 13 | #ifndef _PDCLIB_ASSERT_H 14 | #define _PDCLIB_ASSERT_H _PDCLIB_ASSERT_H 15 | _PDCLIB_PUBLIC void _PDCLIB_assert99( const char * const, const char * const, const char * const ); 16 | _PDCLIB_PUBLIC void _PDCLIB_assert89( const char * const ); 17 | #endif 18 | 19 | /* If NDEBUG is set, assert() is a null operation. */ 20 | #undef assert 21 | 22 | #ifdef NDEBUG 23 | #define assert( ignore ) ( (void) 0 ) 24 | #else 25 | #if __STDC_VERSION__ >= 199901L 26 | #define assert( expression ) ( ( expression ) ? (void) 0 \ 27 | : _PDCLIB_assert99( "Assertion failed: " #expression \ 28 | ", function ", __func__, \ 29 | ", file " __FILE__ \ 30 | ", line " _PDCLIB_value2string( __LINE__ ) \ 31 | ".\n" ) ) 32 | #else 33 | #define assert( expression ) ( ( expression ) ? (void) 0 \ 34 | : _PDCLIB_assert89( "Assertion failed: " #expression \ 35 | ", file " __FILE__ \ 36 | ", line " _PDCLIB_value2string( __LINE__ ) \ 37 | ".\n" ) ) 38 | #endif 39 | #endif 40 | 41 | /* Extension hook for downstream projects that want to have non-standard 42 | extensions to standard headers. 43 | */ 44 | #ifdef _PDCLIB_EXTEND_ASSERT_H 45 | #include _PDCLIB_EXTEND_ASSERT_H 46 | #endif 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | -------------------------------------------------------------------------------- /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_internal.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_internal.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_internal.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 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | extern _PDCLIB_Noreturn void _exit( int status ) _PDCLIB_NORETURN; 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | void _PDCLIB_Exit( int status ) 28 | { 29 | _exit( status ); 30 | } 31 | 32 | #endif 33 | 34 | #ifdef TEST 35 | 36 | #include "_PDCLIB_test.h" 37 | 38 | int main( void ) 39 | { 40 | #ifndef REGTEST 41 | int UNEXPECTED_RETURN = 0; 42 | _PDCLIB_Exit( 0 ); 43 | TESTCASE( UNEXPECTED_RETURN ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | extern int close( int fd ); 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | int _PDCLIB_close( int fd ) 28 | { 29 | return close( fd ); 30 | } 31 | 32 | #endif 33 | 34 | #ifdef TEST 35 | 36 | #include "_PDCLIB_test.h" 37 | 38 | int main( void ) 39 | { 40 | /* No testdriver; tested in driver for _PDCLIB_open(). */ 41 | return TEST_RESULTS; 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /platform/example/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 example implementation of _PDCLIB_fillbuffer() fit for 8 | use with POSIX kernels. 9 | */ 10 | 11 | #include 12 | 13 | #ifndef REGTEST 14 | 15 | #include "pdclib/_PDCLIB_glue.h" 16 | 17 | #include "/usr/include/errno.h" 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | typedef long ssize_t; 24 | extern ssize_t read( int fd, void * buf, size_t count ); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | int _PDCLIB_fillbuffer( struct _PDCLIB_file_t * stream ) 31 | { 32 | /* No need to handle buffers > INT_MAX, as PDCLib doesn't allow them */ 33 | ssize_t rc = read( stream->handle, stream->buffer, stream->bufsize ); 34 | 35 | if ( rc > 0 ) 36 | { 37 | /* Reading successful. */ 38 | if ( !( stream->status & _PDCLIB_FBIN ) ) 39 | { 40 | /* TODO: Text stream conversion here */ 41 | } 42 | 43 | stream->pos.offset += rc; 44 | stream->bufend = rc; 45 | stream->bufidx = 0; 46 | return 0; 47 | } 48 | 49 | if ( rc < 0 ) 50 | { 51 | /* The 1:1 mapping done in _PDCLIB_config.h ensures 52 | this works. 53 | */ 54 | *_PDCLIB_errno_func() = errno; 55 | /* Flag the stream */ 56 | stream->status |= _PDCLIB_ERRORFLAG; 57 | return EOF; 58 | } 59 | 60 | /* End-of-File */ 61 | stream->status |= _PDCLIB_EOFFLAG; 62 | return EOF; 63 | } 64 | 65 | #endif 66 | 67 | #ifdef TEST 68 | 69 | #include "_PDCLIB_test.h" 70 | 71 | int main( void ) 72 | { 73 | /* Testing covered by ftell.c */ 74 | return TEST_RESULTS; 75 | } 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /platform/example/functions/_PDCLIB/_PDCLIB_realpath.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_realpath( const char * path ) 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_glue.h" 10 | 11 | #include 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | extern char * realpath( const char * file_name, char * resolved_name ); 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | char * _PDCLIB_realpath( const char * path ) 24 | { 25 | return realpath( path, NULL ); 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | /* No test drivers. */ 37 | return TEST_RESULTS; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /platform/example/functions/_PDCLIB/_PDCLIB_remove.c: -------------------------------------------------------------------------------- 1 | /* _PDCLIB_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 _PDCLIB_remove() fit for use with 8 | POSIX kernels. 9 | */ 10 | 11 | #ifndef REGTEST 12 | 13 | #include "pdclib/_PDCLIB_glue.h" 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | 19 | extern int unlink( const char * ); 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | 25 | int _PDCLIB_remove( const char * pathname ) 26 | { 27 | return unlink( pathname ); 28 | } 29 | 30 | #endif 31 | 32 | #ifdef TEST 33 | 34 | #include "_PDCLIB_test.h" 35 | 36 | int main( void ) 37 | { 38 | /* Testing covered by ftell.c (and several others) */ 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /platform/example/functions/_PDCLIB/_PDCLIB_seek.c: -------------------------------------------------------------------------------- 1 | /* int_least64_t _PDCLIB_seek( FILE *, int_least64_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 example implementation of _PDCLIB_seek() fit for use with POSIX 8 | kernels. 9 | */ 10 | 11 | #ifndef REGTEST 12 | 13 | #include 14 | #include 15 | 16 | #include "pdclib/_PDCLIB_glue.h" 17 | 18 | #include "/usr/include/errno.h" 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | extern int64_t lseek64( int fd, _PDCLIB_int_least64_t offset, int whence ); 25 | extern long lseek( int fd, long offset, int whence ); 26 | 27 | #ifdef __cplusplus 28 | } 29 | #endif 30 | 31 | _PDCLIB_int_least64_t _PDCLIB_seek( struct _PDCLIB_file_t * stream, _PDCLIB_int_least64_t offset, int whence ) 32 | { 33 | _PDCLIB_int_least64_t rc; 34 | 35 | switch ( whence ) 36 | { 37 | case SEEK_SET: 38 | case SEEK_CUR: 39 | case SEEK_END: 40 | /* EMPTY - OK */ 41 | break; 42 | 43 | default: 44 | *_PDCLIB_errno_func() = _PDCLIB_EINVAL; 45 | return EOF; 46 | break; 47 | } 48 | 49 | #ifdef __CYGWIN__ 50 | rc = lseek( stream->handle, offset, whence ); 51 | #else 52 | rc = lseek64( stream->handle, offset, whence ); 53 | #endif 54 | 55 | if ( rc != EOF ) 56 | { 57 | stream->ungetidx = 0; 58 | stream->bufidx = 0; 59 | stream->bufend = 0; 60 | stream->pos.offset = rc; 61 | return rc; 62 | } 63 | 64 | /* The 1:1 mapping in _PDCLIB_config.h ensures that this works. */ 65 | *_PDCLIB_errno_func() = errno; 66 | return EOF; 67 | } 68 | 69 | #endif 70 | 71 | #ifdef TEST 72 | 73 | #include "_PDCLIB_test.h" 74 | 75 | int main( void ) 76 | { 77 | /* Testing covered by ftell.c */ 78 | return TEST_RESULTS; 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /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 | 29 | while ( environ[ index ] != NULL ) 30 | { 31 | if ( strncmp( environ[ index ], name, len ) == 0 ) 32 | { 33 | return environ[ index ] + len + 1; 34 | } 35 | 36 | index++; 37 | } 38 | 39 | return NULL; 40 | } 41 | 42 | #endif 43 | 44 | #ifdef TEST 45 | 46 | #include "_PDCLIB_test.h" 47 | 48 | int main( void ) 49 | { 50 | char * env = getenv( "SHELL" ); 51 | size_t len = strlen( env ); 52 | TESTCASE( strcmp( env + ( len - 2 ), "sh" ) == 0 ); 53 | return TEST_RESULTS; 54 | } 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /platform/example/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 | /* This is an example implementation of system() fit for use with POSIX kernels. 10 | */ 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | extern int fork( void ); 17 | extern int execve( const char * filename, char * const argv[], char * const envp[] ); 18 | extern int wait( int * status ); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | int system( const char * string ) 25 | { 26 | const char * argv[] = { "sh", "-c", NULL, NULL }; 27 | argv[2] = string; 28 | 29 | if ( string != NULL ) 30 | { 31 | int pid = fork(); 32 | 33 | if ( pid == 0 ) 34 | { 35 | execve( "/bin/sh", ( char * const *)argv, NULL ); 36 | } 37 | else if ( pid > 0 ) 38 | { 39 | while ( wait( NULL ) != pid ) 40 | { 41 | /* EMPTY */ 42 | } 43 | } 44 | } 45 | 46 | return -1; 47 | } 48 | 49 | #ifdef TEST 50 | 51 | #include "_PDCLIB_test.h" 52 | 53 | #define SHELLCOMMAND "echo 'SUCCESS testing system()'" 54 | 55 | int main( void ) 56 | { 57 | FILE * fh; 58 | char buffer[25]; 59 | buffer[24] = 'x'; 60 | TESTCASE( ( fh = freopen( testfile, "wb+", stdout ) ) != NULL ); 61 | TESTCASE( system( SHELLCOMMAND ) ); 62 | rewind( fh ); 63 | TESTCASE( fread( buffer, 1, 24, fh ) == 24 ); 64 | TESTCASE( memcmp( buffer, "SUCCESS testing system()", 24 ) == 0 ); 65 | TESTCASE( buffer[24] == 'x' ); 66 | TESTCASE( fclose( fh ) == 0 ); 67 | TESTCASE( remove( testfile ) == 0 ); 68 | return TEST_RESULTS; 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter. */ 16 | extern int pthread_once( once_flag *, void ( * )( void ) ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | void call_once( once_flag * flag, void ( *func )( void ) ) 23 | { 24 | pthread_once( flag, func ); 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/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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter. */ 16 | extern int pthread_cond_broadcast( cnd_t * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int cnd_broadcast( cnd_t * cond ) 23 | { 24 | if ( pthread_cond_broadcast( cond ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | TESTCASE( NO_TESTDRIVER ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter. */ 16 | extern int pthread_cond_destroy( cnd_t * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | void cnd_destroy( cnd_t * cond ) 23 | { 24 | /* Could return EBUsY */ 25 | pthread_cond_destroy( cond ); 26 | } 27 | 28 | #endif 29 | 30 | #ifdef TEST 31 | 32 | #include "_PDCLIB_test.h" 33 | 34 | int main( void ) 35 | { 36 | #ifndef REGTEST 37 | TESTCASE( NO_TESTDRIVER ); 38 | #endif 39 | return TEST_RESULTS; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameters. */ 16 | extern int pthread_cond_init( cnd_t *, _PDCLIB_cnd_attr_t * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int cnd_init( cnd_t * cond ) 23 | { 24 | if ( pthread_cond_init( cond, NULL ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | TESTCASE( NO_TESTDRIVER ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter. */ 16 | extern int pthread_cond_signal( cnd_t * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int cnd_signal( cnd_t * cond ) 23 | { 24 | if ( pthread_cond_signal( cond ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | TESTCASE( NO_TESTDRIVER ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | /* Implicitly casing the parameters. */ 18 | extern int pthread_cond_timedwait( cnd_t *, mtx_t *, const struct timespec * ); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | int cnd_timedwait( cnd_t * _PDCLIB_restrict cond, mtx_t * _PDCLIB_restrict mtx, const struct timespec * _PDCLIB_restrict ts ) 25 | { 26 | switch ( pthread_cond_timedwait( cond, mtx, ts ) ) 27 | { 28 | case 0: 29 | return thrd_success; 30 | 31 | case ETIMEDOUT: 32 | return thrd_timedout; 33 | 34 | default: 35 | return thrd_error; 36 | } 37 | } 38 | 39 | #endif 40 | 41 | #ifdef TEST 42 | 43 | #include "_PDCLIB_test.h" 44 | 45 | int main( void ) 46 | { 47 | #ifndef REGTEST 48 | TESTCASE( NO_TESTDRIVER ); 49 | #endif 50 | return TEST_RESULTS; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameters. */ 16 | extern int pthread_cond_wait( cnd_t *, mtx_t * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int cnd_wait( cnd_t * cond, mtx_t * mtx ) 23 | { 24 | if ( pthread_cond_wait( cond, mtx ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | TESTCASE( NO_TESTDRIVER ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter. */ 16 | extern int pthread_mutex_destroy( mtx_t * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | void mtx_destroy( mtx_t * mtx ) 23 | { 24 | pthread_mutex_destroy( mtx ); 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/mtx_init.c: -------------------------------------------------------------------------------- 1 | /* mtx_init( mtx_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 | int mtx_init( mtx_t * mtx, int type ) 12 | { 13 | /* As far as I could figure out from Pthread documentation, there is 14 | no special requirement for a Pthread mutex to support timeout (the 15 | default "fast" mutex already supports that). So the only thing we 16 | check for in type is whether the mutex shall be recursive or not. 17 | */ 18 | if ( type & mtx_recursive ) 19 | { 20 | _PDCLIB_mtx_t recursive_mutex = _PDCLIB_MTX_RECURSIVE_INIT; 21 | *mtx = recursive_mutex; 22 | } 23 | else 24 | { 25 | _PDCLIB_mtx_t plain_mutex = _PDCLIB_MTX_PLAIN_INIT; 26 | *mtx = plain_mutex; 27 | } 28 | 29 | return thrd_success; 30 | } 31 | 32 | #endif 33 | 34 | #ifdef TEST 35 | 36 | #include "_PDCLIB_test.h" 37 | 38 | #include 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | mtx_t plain; 44 | mtx_t plain_test = _PDCLIB_MTX_PLAIN_INIT; 45 | 46 | mtx_t recursive; 47 | mtx_t recursive_test = _PDCLIB_MTX_RECURSIVE_INIT; 48 | 49 | TESTCASE( mtx_init( &plain, mtx_plain ) == thrd_success ); 50 | TESTCASE( memcmp( &plain, &plain_test, sizeof( mtx_t ) ) == 0 ); 51 | 52 | TESTCASE( mtx_init( &recursive, mtx_recursive ) == thrd_success ); 53 | TESTCASE( memcmp( &recursive, &recursive_test, sizeof( mtx_t ) ) == 0 ); 54 | #endif 55 | return TEST_RESULTS; 56 | } 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter. */ 16 | extern int pthread_mutex_lock( mtx_t * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int mtx_lock( mtx_t * mtx ) 23 | { 24 | if ( pthread_mutex_lock( mtx ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | #ifndef REGTEST 41 | 42 | #define COUNT 10 43 | thrd_t g_thread[COUNT]; 44 | mtx_t g_mutex; 45 | 46 | static int func( void * arg ) 47 | { 48 | TESTCASE( mtx_lock( &g_mutex ) == thrd_success ); 49 | thrd_yield(); 50 | TESTCASE( mtx_unlock( &g_mutex ) == thrd_success ); 51 | return 0; 52 | } 53 | 54 | #endif 55 | 56 | int main( void ) 57 | { 58 | #ifndef REGTEST 59 | TESTCASE( mtx_init( &g_mutex, mtx_plain ) == thrd_success ); 60 | 61 | for ( unsigned i = 0; i < COUNT; ++i ) 62 | { 63 | TESTCASE( thrd_create( &g_thread[i], func, NULL ) == thrd_success ); 64 | } 65 | 66 | for ( unsigned i = 0; i < COUNT; ++i ) 67 | { 68 | TESTCASE( thrd_join( g_thread[i], NULL ) == thrd_success ); 69 | } 70 | 71 | #endif 72 | return TEST_RESULTS; 73 | } 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | /* Implicitly casting the first parameters. */ 18 | extern int pthread_mutex_timedlock( mtx_t *, const struct timespec * ); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | int mtx_timedlock( mtx_t * _PDCLIB_restrict mtx, const struct timespec * _PDCLIB_restrict ts ) 25 | { 26 | switch ( pthread_mutex_timedlock( mtx, ts ) ) 27 | { 28 | case 0: 29 | return thrd_success; 30 | 31 | case ETIMEDOUT: 32 | return thrd_timedout; 33 | 34 | default: 35 | return thrd_error; 36 | } 37 | } 38 | 39 | #endif 40 | 41 | #ifdef TEST 42 | 43 | #include "_PDCLIB_test.h" 44 | 45 | int main( void ) 46 | { 47 | #ifndef REGTEST 48 | TESTCASE( NO_TESTDRIVER ); 49 | #endif 50 | return TEST_RESULTS; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | /* Implicity casting the parameter. */ 18 | extern int pthread_mutex_trylock( mtx_t * ); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | int mtx_trylock( mtx_t * mtx ) 25 | { 26 | switch ( pthread_mutex_trylock( mtx ) ) 27 | { 28 | case 0: 29 | return thrd_success; 30 | 31 | case EBUSY: 32 | return thrd_busy; 33 | 34 | default: 35 | return thrd_error; 36 | } 37 | } 38 | 39 | #endif 40 | 41 | #ifdef TEST 42 | 43 | #include "_PDCLIB_test.h" 44 | 45 | int main( void ) 46 | { 47 | #ifndef REGTEST 48 | TESTCASE( NO_TESTDRIVER ); 49 | #endif 50 | return TEST_RESULTS; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicity casting the parameter. */ 16 | extern int pthread_mutex_unlock( mtx_t * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int mtx_unlock( mtx_t * mtx ) 23 | { 24 | if ( pthread_mutex_unlock( mtx ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | /* Tested by the mtx_lock test driver. */ 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the first parameters. */ 16 | extern int pthread_create( thrd_t *, const _PDCLIB_thrd_attr_t *, thrd_start_t, void * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int thrd_create( thrd_t * thr, thrd_start_t func, void * arg ) 23 | { 24 | if ( pthread_create( thr, NULL, func, arg ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | #ifndef REGTEST 41 | 42 | #define COUNT 10 43 | thrd_t g_thread[ COUNT ] = {0}; 44 | int g_count = 0; 45 | 46 | static int func( void * arg ) 47 | { 48 | ++g_count; 49 | return 0; 50 | } 51 | 52 | #endif 53 | 54 | int main( void ) 55 | { 56 | #ifndef REGTEST 57 | 58 | for ( unsigned i = 0; i < COUNT; ++i ) 59 | { 60 | TESTCASE( thrd_create( &g_thread[i], func, NULL ) == thrd_success ); 61 | } 62 | 63 | for ( unsigned i = 0; i < COUNT; ++i ) 64 | { 65 | TESTCASE( thrd_join( g_thread[i], NULL ) == thrd_success ); 66 | } 67 | 68 | TESTCASE( g_count == COUNT ); 69 | #endif 70 | return TEST_RESULTS; 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the return type. */ 16 | extern thrd_t pthread_self( void ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | thrd_t thrd_current( void ) 23 | { 24 | return ( thrd_t )pthread_self(); 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | #ifndef REGTEST 34 | 35 | #define COUNT 10 36 | thrd_t g_thread[COUNT]; 37 | unsigned g_index[COUNT] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u }; 38 | 39 | static int func( void * arg ) 40 | { 41 | TESTCASE( thrd_equal( g_thread[ *( unsigned * )arg ], thrd_current() ) != 0 ); 42 | return 0; 43 | } 44 | 45 | #endif 46 | 47 | int main( void ) 48 | { 49 | #ifndef REGTEST 50 | 51 | for ( unsigned i = 0; i < COUNT; ++i ) 52 | { 53 | TESTCASE( thrd_create( &g_thread[i], func, &g_index[i] ) == thrd_success ); 54 | } 55 | 56 | for ( unsigned i = 0; i < COUNT; ++i ) 57 | { 58 | TESTCASE( thrd_join( g_thread[i], NULL ) == thrd_success ); 59 | } 60 | 61 | #endif 62 | return TEST_RESULTS; 63 | } 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter */ 16 | extern int pthread_detach( thrd_t ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int thrd_detach( thrd_t thr ) 23 | { 24 | if ( pthread_detach( thr ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | TESTCASE( NO_TESTDRIVER ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameters. */ 16 | extern int pthread_equal( thrd_t, thrd_t ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int thrd_equal( thrd_t thr0, thrd_t thr1 ) 23 | { 24 | return pthread_equal( thr0, thr1 ); 25 | } 26 | 27 | #endif 28 | 29 | #ifdef TEST 30 | 31 | #include "_PDCLIB_test.h" 32 | 33 | int main( void ) 34 | { 35 | /* Tested by thrd_current test driver. */ 36 | return TEST_RESULTS; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /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 | #include 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | extern _PDCLIB_Noreturn void pthread_exit( void * ) _PDCLIB_NORETURN; 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | void thrd_exit( int res ) 23 | { 24 | pthread_exit( ( void * )( uintptr_t )res ); 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_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 | #include 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | /* Implicitly casting the first parameter. */ 17 | extern int pthread_join( thrd_t, void ** ); 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | int thrd_join( thrd_t thr, int * res ) 24 | { 25 | void * result; 26 | int rc = pthread_join( thr, &result ); 27 | 28 | if ( res != NULL ) 29 | { 30 | *res = ( int )( uintptr_t )result; 31 | } 32 | 33 | if ( rc == 0 ) 34 | { 35 | return thrd_success; 36 | } 37 | else 38 | { 39 | return thrd_error; 40 | } 41 | } 42 | 43 | #endif 44 | 45 | #ifdef TEST 46 | 47 | #include "_PDCLIB_test.h" 48 | 49 | int main( void ) 50 | { 51 | #ifndef REGTEST 52 | TESTCASE( NO_TESTDRIVER ); 53 | #endif 54 | return TEST_RESULTS; 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | /* Implicitly casting the parameters. */ 18 | extern int nanosleep( const struct timespec *, struct timespec * ); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | int thrd_sleep( const struct timespec * duration, struct timespec * remaining ) 25 | { 26 | errno = 0; 27 | 28 | if ( nanosleep( duration, remaining ) == 0 ) 29 | { 30 | return 0; 31 | } 32 | 33 | if ( errno == EINTR ) 34 | { 35 | return -1; 36 | } 37 | 38 | return -2; 39 | } 40 | 41 | #endif 42 | 43 | #ifdef TEST 44 | 45 | #include "_PDCLIB_test.h" 46 | 47 | int main( void ) 48 | { 49 | #ifndef REGTEST 50 | TESTCASE( NO_TESTDRIVER ); 51 | #endif 52 | return TEST_RESULTS; 53 | } 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | extern int sched_yield( void ); 16 | 17 | #ifdef __cplusplus 18 | } 19 | #endif 20 | 21 | void thrd_yield( void ) 22 | { 23 | sched_yield(); 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_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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameters. */ 16 | extern int pthread_key_create( tss_t *, tss_dtor_t ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int tss_create( tss_t * key, tss_dtor_t dtor ) 23 | { 24 | if ( pthread_key_create( key, dtor ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | TESTCASE( NO_TESTDRIVER ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter. */ 16 | extern int pthread_key_delete( tss_t ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | void tss_delete( tss_t key ) 23 | { 24 | pthread_key_delete( key ); 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/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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the parameter. */ 16 | extern void * pthread_getspecific( tss_t ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | void * tss_get( tss_t key ) 23 | { 24 | return pthread_getspecific( key ); 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/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 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Implicitly casting the first parameter. */ 16 | extern int pthread_setspecific( tss_t, const void * ); 17 | 18 | #ifdef __cplusplus 19 | } 20 | #endif 21 | 22 | int tss_set( tss_t key, void * val ) 23 | { 24 | if ( pthread_setspecific( key, val ) == 0 ) 25 | { 26 | return thrd_success; 27 | } 28 | else 29 | { 30 | return thrd_error; 31 | } 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | #ifndef REGTEST 43 | TESTCASE( NO_TESTDRIVER ); 44 | #endif 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 | 17 | if ( times( &buf ) != ( clock_t )-1 ) 18 | { 19 | return buf.tms_utime + buf.tms_stime; 20 | } 21 | 22 | return -1; 23 | } 24 | 25 | #endif 26 | 27 | #ifdef TEST 28 | 29 | #include "_PDCLIB_test.h" 30 | 31 | int main( void ) 32 | { 33 | TESTCASE( NO_TESTDRIVER ); 34 | return TEST_RESULTS; 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /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 "pdclib/_PDCLIB_defguard.h" 12 | 13 | #include "sys/time.h" 14 | 15 | /* See comments in _PDCLIB_config.h on the semantics of time_t. */ 16 | 17 | time_t time( time_t * timer ) 18 | { 19 | struct timeval tv; 20 | 21 | if ( gettimeofday( &tv, NULL ) == 0 ) 22 | { 23 | if ( timer != NULL ) 24 | { 25 | *timer = tv.tv_sec; 26 | } 27 | 28 | return tv.tv_sec; 29 | } 30 | 31 | return -1; 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | time_t t = time( NULL ); 43 | printf( "%d\n", (int)t ); 44 | TESTCASE( NO_TESTDRIVER ); 45 | return TEST_RESULTS; 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /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 "pdclib/_PDCLIB_defguard.h" 12 | 13 | #include "sys/time.h" 14 | 15 | int timespec_get( struct timespec * ts, int base ) 16 | { 17 | if ( base == TIME_UTC ) 18 | { 19 | /* We can make do with a really thin wrapper here. */ 20 | struct timeval tv; 21 | 22 | if ( gettimeofday( &tv, NULL ) == 0 ) 23 | { 24 | ts->tv_sec = tv.tv_sec; 25 | ts->tv_nsec = tv.tv_usec * 1000; 26 | return base; 27 | } 28 | } 29 | 30 | /* Not supporting any other time base than TIME_UTC for now. */ 31 | return 0; 32 | } 33 | 34 | #endif 35 | 36 | #ifdef TEST 37 | 38 | #include "_PDCLIB_test.h" 39 | 40 | int main( void ) 41 | { 42 | TESTCASE( NO_TESTDRIVER ); 43 | return TEST_RESULTS; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /platform/example/include/pdclib/_PDCLIB_defguard.h: -------------------------------------------------------------------------------- 1 | /* Definition guard <_PDCLIB_defguard.h> 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_DEFGUARD_H 8 | #define _PDCLIB_DEFGUARD_H _PDCLIB_DEFGUARD_H 9 | 10 | #if defined( __ANDROID__ ) 11 | /* typedef sigset_t */ 12 | #include "bits/signal_types.h" 13 | #endif 14 | 15 | /* Linux defines its own version of struct timespec (from ) in 16 | some internal header (depending on clib implementation), which leads 17 | to problems when accessing e.g. sys/time.h (type redefinition). 18 | The solution is to set the Linux header's include guard (to avoid 19 | Linux' definition), and to include PDCLib's to define the 20 | type unambiguously. 21 | */ 22 | 23 | #define _TIMESPEC_DEFINED 24 | #define _SYS__TIMESPEC_H_ 25 | #define _STRUCT_TIMESPEC 26 | 27 | #include 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /test_support/_PDCLIB_test.h: -------------------------------------------------------------------------------- 1 | /* PDCLib testing suite <_PDCLIB_test.h> 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 | /* -------------------------------------------------------------------------- */ 8 | /* Helper macros for test drivers */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | /* Some strings used for and testing. */ 17 | static const char abcde[] = "abcde"; 18 | static const char abcdx[] = "abcdx"; 19 | static const char teststring[] = "1234567890\nABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n"; 20 | 21 | /* Temporary file names */ 22 | static const char testfile[] = "test_support/testfile"; 23 | static const char testfile1[] = "test_support/testfile1"; 24 | static const char testfile2[] = "test_support/testfile2"; 25 | 26 | #ifndef NO_TESTDRIVER 27 | #define NO_TESTDRIVER 0 28 | #endif 29 | 30 | static int TEST_RESULTS = 0; 31 | 32 | /* TESTCASE() - generic test */ 33 | #define TESTCASE( x ) \ 34 | if ( x ) \ 35 | { \ 36 | /* EMPTY */ \ 37 | } \ 38 | else \ 39 | { \ 40 | TEST_RESULTS += 1; \ 41 | printf( "FAILED: " __FILE__ ", line %d - %s\n", __LINE__, #x ); \ 42 | } 43 | 44 | /* TESTCASE_NOREG() - PDCLib-only test */ 45 | #ifndef REGTEST 46 | #define TESTCASE_NOREG( x ) TESTCASE( x ) 47 | #else 48 | #define TESTCASE_NOREG( x ) 49 | #endif 50 | 51 | /* Include printf() / scanf() test macros if required */ 52 | 53 | #if defined( _PDCLIB_FILEIO ) || defined( _PDCLIB_STRINGIO ) 54 | #include "_PDCLIB_iotest.h" 55 | #endif 56 | 57 | /* Helper macro to fill a struct tm */ 58 | #define MKTIME( tm, sec, min, hour, day, month, year, wday, yday ) tm.tm_sec = sec; tm.tm_min = min; tm.tm_hour = hour; tm.tm_mday = day; tm.tm_mon = month; tm.tm_year = year; tm.tm_wday = wday; tm.tm_yday = yday; tm.tm_isdst = -1; 59 | 60 | --------------------------------------------------------------------------------