From: solar Date: Sun, 16 May 2010 07:00:06 +0000 (+0000) Subject: Ad-hoc solutions for perror() and strerror(). X-Git-Tag: v0.5~82 X-Git-Url: https://pd.if.org/git/?p=pdclib;a=commitdiff_plain;h=4116251e9bb6c386580d71d8c4afd2d0d08c6096 Ad-hoc solutions for perror() and strerror(). --- diff --git a/Readme.txt b/Readme.txt index 61767ed..315e0bb 100644 --- a/Readme.txt +++ b/Readme.txt @@ -184,8 +184,8 @@ a backport of bugfixes in the current development code. - #10 NULL redefinition warnings (fixed) v0.5 - unreleased -Implementations for , , and most parts of -. +Implementations for , , most parts of , +and strerror() from . Still no locale / wide-char support. Enabled all GCC compiler warnings I could find, and fixed everything that threw a warning. (You see this, maintainers of Open Source software? No warnings whatsoever. Stop telling diff --git a/functions/_PDCLIB/errno.c b/functions/_PDCLIB/errno.c index ba01c35..0c959b7 100644 --- a/functions/_PDCLIB/errno.c +++ b/functions/_PDCLIB/errno.c @@ -18,6 +18,17 @@ int * _PDCLIB_errno_func() return &_PDCLIB_errno; } +/* TODO: Doing this via a static array is not the way to do it. */ +char const * _PDCLIB_errno_texts[] = { + "", + "ERANGE (Range error)", + "EDOM (Domain error)", + "EIO (I/O error)", + "EUNKNOWN (Unknown error)", + "EINVAL (Invalid parameter value)", + "ERETRY (I/O retries exceeded)" +}; + #endif #ifdef TEST diff --git a/functions/stdio/perror.c b/functions/stdio/perror.c index c59ca0b..609562a 100644 --- a/functions/stdio/perror.c +++ b/functions/stdio/perror.c @@ -10,9 +10,16 @@ #ifndef REGTEST +#include + +/* TODO: Doing this via a static array is not the way to do it. */ void perror( const char * s ) { - /* TODO: Implement. */ + if ( ( s != NULL ) && ( s[0] != '\n' ) ) + { + fprintf( stderr, "%s: ", s ); + } + fprintf( stderr, "%s\n", _PDCLIB_errno_texts[ errno ] ); return; } diff --git a/functions/string/strerror.c b/functions/string/strerror.c new file mode 100644 index 0000000..89ebed0 --- /dev/null +++ b/functions/string/strerror.c @@ -0,0 +1,32 @@ +/* $Id$ */ + +/* strerror( int ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +#ifndef REGTEST + +/* TODO: Doing this via a static array is not the way to do it. */ +char * strerror( int errnum ) +{ + return (char *)_PDCLIB_errno_texts[ errnum ]; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +#include +#include + +int main( void ) +{ + TESTCASE( strerror( ERANGE ) != strerror( EDOM ) ); + return TEST_RESULTS; +} +#endif diff --git a/includes/string.h b/includes/string.h index 2aa7aae..ef70df0 100644 --- a/includes/string.h +++ b/includes/string.h @@ -180,9 +180,8 @@ void * memset( void * s, int c, size_t n ); /* Map an error number to a (locale-specific) error message string. Error numbers are typically errno values, but any number is mapped to a message. TODO: PDCLib does not yet support locales. - TODO: strerror() not yet implemented. -char * strerror( int errnum ); */ +char * strerror( int errnum ); /* Returns the length of the string s (excluding terminating '\0'). */ diff --git a/internals/_PDCLIB_int.h b/internals/_PDCLIB_int.h index b5ac49e..c6283b9 100644 --- a/internals/_PDCLIB_int.h +++ b/internals/_PDCLIB_int.h @@ -399,4 +399,8 @@ int * _PDCLIB_errno_func( void ); #define _PDCLIB_EINVAL 5 /* Used in the example implementation for "I/O retries exceeded". */ #define _PDCLIB_ERETRY 6 +/* One larger than the largest used errno */ +#define _PDCLIB_EMAX 7 +/* TODO: Doing this via a static array is not the way to do it. */ +char const * _PDCLIB_errno_texts[ _PDCLIB_EMAX ]; diff --git a/platform/example/functions/_PDCLIB/stdinit.c b/platform/example/functions/_PDCLIB/stdinit.c index af37f06..78c76e2 100644 --- a/platform/example/functions/_PDCLIB/stdinit.c +++ b/platform/example/functions/_PDCLIB/stdinit.c @@ -27,7 +27,6 @@ static unsigned char _PDCLIB_sin_ungetbuf[_PDCLIB_UNGETCBUFSIZE]; static unsigned char _PDCLIB_sout_ungetbuf[_PDCLIB_UNGETCBUFSIZE]; static unsigned char _PDCLIB_serr_ungetbuf[_PDCLIB_UNGETCBUFSIZE]; -/* FIXME: serr should handle one character. Buffering on out / in? */ static struct _PDCLIB_file_t _PDCLIB_serr = { 2, _PDCLIB_serr_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_serr_ungetbuf, _IONBF | _PDCLIB_FWRITE, NULL, NULL }; static struct _PDCLIB_file_t _PDCLIB_sout = { 1, _PDCLIB_sout_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_sout_ungetbuf, _IOLBF | _PDCLIB_FWRITE, NULL, &_PDCLIB_serr }; static struct _PDCLIB_file_t _PDCLIB_sin = { 0, _PDCLIB_sin_buffer, BUFSIZ, 0, 0, { 0, 0 }, 0, _PDCLIB_sin_ungetbuf, _IOLBF | _PDCLIB_FREAD, NULL, &_PDCLIB_sout };