]> pd.if.org Git - pdclib.old/commitdiff
Ad-hoc solutions for perror() and strerror().
authorsolar <>
Sun, 16 May 2010 07:00:06 +0000 (07:00 +0000)
committersolar <>
Sun, 16 May 2010 07:00:06 +0000 (07:00 +0000)
Readme.txt
functions/_PDCLIB/errno.c
functions/stdio/perror.c
functions/string/strerror.c [new file with mode: 0644]
includes/string.h
internals/_PDCLIB_int.h
platform/example/functions/_PDCLIB/stdinit.c

index 61767edf6bc08ff91c6ff0b4e3a2c8995ac048b7..315e0bb37795b5983f33e8560a4f0a21f0b764e6 100644 (file)
@@ -184,8 +184,8 @@ a backport of bugfixes in the current development code.
 - #10 NULL redefinition warnings            (fixed)
 
 v0.5 - unreleased
-Implementations for <inttypes.h>, <errno.h>, and most parts of
-<stdio.h>.
+Implementations for <inttypes.h>, <errno.h>, most parts of <stdio.h>,
+and strerror() from <string.h>.
 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
index ba01c350102bf71f9ca7136f470db8343d70e0ca..0c959b7282bbbcc8d41c2da2d43bd96718eb1ac3 100644 (file)
@@ -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
index c59ca0b290a582934f50c3b4a467002915a4a527..609562a8e46a783a89420808117163d6d8ce5c5e 100644 (file)
 
 #ifndef REGTEST
 
+#include <errno.h>
+
+/* 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 (file)
index 0000000..89ebed0
--- /dev/null
@@ -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 <string.h>
+
+#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 <stdio.h>
+#include <errno.h>
+
+int main( void )
+{
+    TESTCASE( strerror( ERANGE ) != strerror( EDOM ) );
+    return TEST_RESULTS;
+}
+#endif
index 2aa7aaedb41206750ca1beac9a1c252b1e9c3fe5..ef70df00ce952cd3f6054a01450ba135d57086f8 100644 (file)
@@ -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').
 */
index b5ac49e962eba1e048f4f21758fcb532e52fad0e..c6283b9aee74acba2d350d749495e7ed58075d3c 100644 (file)
@@ -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 ];
index af37f069c2e74cea62b79bec20e028a5e93a9e73..78c76e259e45ab1ae62a0f310dcb8806d0e30cba 100644 (file)
@@ -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 };