]> pd.if.org Git - pdclib/blobdiff - functions/stdio/perror.c
Addressed ticket #40 (non-standard errno values).
[pdclib] / functions / stdio / perror.c
index 609562a8e46a783a89420808117163d6d8ce5c5e..23ce68bd356cc11e997dcdabf4873b32156b2204 100644 (file)
@@ -11,6 +11,7 @@
 #ifndef REGTEST
 
 #include <errno.h>
+#include <locale.h>
 
 /* TODO: Doing this via a static array is not the way to do it. */
 void perror( const char * s )
@@ -19,7 +20,14 @@ void perror( const char * s )
     {
         fprintf( stderr, "%s: ", s );
     }
-    fprintf( stderr, "%s\n", _PDCLIB_errno_texts[ errno ] );
+    if ( errno >= _PDCLIB_ERRNO_MAX )
+    {
+        fprintf( stderr, "Unknown error\n" );
+    }
+    else
+    {
+        fprintf( stderr, "%s\n", _PDCLIB_lconv._PDCLIB_errno_texts[errno] );
+    }
     return;
 }
 
@@ -27,10 +35,24 @@ void perror( const char * s )
 
 #ifdef TEST
 #include <_PDCLIB_test.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
 
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    FILE * fh;
+    unsigned long long max = ULLONG_MAX;
+    char buffer[100];
+    sprintf( buffer, "%llu", max );
+    TESTCASE( ( fh = freopen( testfile, "wb+", stderr ) ) != NULL );
+    TESTCASE( strtol( buffer, NULL, 10 ) == LONG_MAX );
+    perror( "Test" );
+    rewind( fh );
+    TESTCASE( fread( buffer, 1, 7, fh ) == 7 );
+    TESTCASE( memcmp( buffer, "Test: ", 6 ) == 0 );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( remove( testfile ) == 0 );
     return TEST_RESULTS;
 }