X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Fperror.c;h=97b727f253632fa08ddbfc3dda68f0e2dfd2456c;hp=f93a53b522fa18ba3c8a00e9fa2a19973146cf8d;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=34893ecc2200dc7017c36a54cb6c5f4c2378b5ec diff --git a/functions/stdio/perror.c b/functions/stdio/perror.c index f93a53b..97b727f 100644 --- a/functions/stdio/perror.c +++ b/functions/stdio/perror.c @@ -1,8 +1,56 @@ -// ---------------------------------------------------------------------------- -// $Id$ -// ---------------------------------------------------------------------------- -// Public Domain C Library - http://pdclib.sourceforge.net -// This code is Public Domain. Use, modify, and redistribute at will. -// ---------------------------------------------------------------------------- - -void perror( const char * s ) { /* TODO */ }; +/* perror( const char * ) + + 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 +#include +#include "_PDCLIB_locale.h" + +/* TODO: Doing this via a static array is not the way to do it. */ +void perror( const char * s ) +{ + if ( ( s != NULL ) && ( s[0] != '\n' ) ) + { + fprintf( stderr, "%s: ", s ); + } + if ( errno >= _PDCLIB_ERRNO_MAX ) + { + fprintf( stderr, "Unknown error\n" ); + } + else + { + fprintf( stderr, "%s\n", _PDCLIB_threadlocale()->_ErrnoStr[errno] ); + } + return; +} + +#endif + +#ifdef TEST +#include "_PDCLIB_test.h" +#include +#include +#include + +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