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