]> pd.if.org Git - pdclib/blob - functions/stdio/perror.c
Whitespace cleanups.
[pdclib] / functions / stdio / perror.c
1 /* perror( const char * )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8 #include <errno.h>
9 #include <locale.h>
10
11 #ifndef REGTEST
12
13 /* TODO: Doing this via a static array is not the way to do it. */
14 void perror( const char * s )
15 {
16     if ( ( s != NULL ) && ( s[0] != '\n' ) )
17     {
18         fprintf( stderr, "%s: ", s );
19     }
20     if ( errno >= _PDCLIB_ERRNO_MAX )
21     {
22         fprintf( stderr, "Unknown error\n" );
23     }
24     else
25     {
26         fprintf( stderr, "%s\n", _PDCLIB_lconv._PDCLIB_errno_texts[errno] );
27     }
28     return;
29 }
30
31 #endif
32
33 #ifdef TEST
34
35 #include "_PDCLIB_test.h"
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <limits.h>
40
41 int main( void )
42 {
43     FILE * fh;
44     unsigned long long max = ULLONG_MAX;
45     char buffer[100];
46     sprintf( buffer, "%llu", max );
47     TESTCASE( ( fh = freopen( testfile, "wb+", stderr ) ) != NULL );
48     TESTCASE( strtol( buffer, NULL, 10 ) == LONG_MAX );
49     perror( "Test" );
50     rewind( fh );
51     TESTCASE( fread( buffer, 1, 7, fh ) == 7 );
52     TESTCASE( memcmp( buffer, "Test: ", 6 ) == 0 );
53     TESTCASE( fclose( fh ) == 0 );
54     TESTCASE( remove( testfile ) == 0 );
55     return TEST_RESULTS;
56 }
57
58 #endif