]> pd.if.org Git - pdclib/blobdiff - functions/stdio/perror.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / stdio / perror.c
index d94746cdb2c217399e61e82c45d583a280d9d511..97b727f253632fa08ddbfc3dda68f0e2dfd2456c 100644 (file)
@@ -1,26 +1,56 @@
-// ----------------------------------------------------------------------------
-// $Id$
-// ----------------------------------------------------------------------------
-// Public Domain C Library - http://pdclib.sourceforge.net
-// This code is Public Domain. Use, modify, and redistribute at will.
-// ----------------------------------------------------------------------------
+/* perror( const char * )
 
-void perror( const char * s ) { /* TODO */ };
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+#include <errno.h>
+#include "_PDCLIB_locale.h"
 
-/* PDPC code - unreviewed
+/* TODO: Doing this via a static array is not the way to do it. */
+void perror( const char * s )
 {
-    if ((s != NULL) && (*s != '\0'))
+    if ( ( s != NULL ) && ( s[0] != '\n' ) )
     {
-        printf("%s: ");
+        fprintf( stderr, "%s: ", s );
     }
-    if (errno == 0)
+    if ( errno >= _PDCLIB_ERRNO_MAX )
     {
-        printf("No error has occurred\n");
+        fprintf( stderr, "Unknown error\n" );
     }
     else
     {
-        printf("An error has occurred\n");
+        fprintf( stderr, "%s\n", _PDCLIB_threadlocale()->_ErrnoStr[errno] );
     }
     return;
 }
-*/
+
+#endif
+
+#ifdef TEST
+#include "_PDCLIB_test.h"
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+int main( void )
+{
+    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;
+}
+
+#endif