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