]> pd.if.org Git - pdclib.old/blob - functions/stdio/perror.c
[gandr] s/__lp64__/__LP64__/ to match GCC define
[pdclib.old] / 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 #include <errno.h>
13 #include <_PDCLIB_locale.h>
14
15 /* TODO: Doing this via a static array is not the way to do it. */
16 void perror( const char * s )
17 {
18     if ( ( s != NULL ) && ( s[0] != '\n' ) )
19     {
20         fprintf( stderr, "%s: ", s );
21     }
22     if ( errno >= _PDCLIB_ERRNO_MAX )
23     {
24         fprintf( stderr, "Unknown error\n" );
25     }
26     else
27     {
28         fprintf( stderr, "%s\n", _PDCLIB_threadlocale()->_ErrnoStr[errno] );
29     }
30     return;
31 }
32
33 #endif
34
35 #ifdef TEST
36 #include <_PDCLIB_test.h>
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