]> pd.if.org Git - pdclib/blob - functions/stdio/perror.c
bf3b3afee1c1bf0028218f657263853c0ffcc7a6
[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
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     fprintf( stderr, "%s\n", _PDCLIB_errno_texts[ errno ] );
23     return;
24 }
25
26 #endif
27
28 #ifdef TEST
29 #include <_PDCLIB_test.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <limits.h>
33
34 int main( void )
35 {
36     FILE * fh;
37     unsigned long long max = ULLONG_MAX;
38     char buffer[100];
39     sprintf( buffer, "%llu", max );
40     TESTCASE( ( fh = freopen( testfile, "wb+", stderr ) ) != NULL );
41     TESTCASE( strtol( buffer, NULL, 10 ) == LONG_MAX );
42     perror( "Test" );
43     rewind( fh );
44     TESTCASE( fread( buffer, 1, 7, fh ) == 7 );
45     TESTCASE( memcmp( buffer, "Test: ", 6 ) == 0 );
46     TESTCASE( fclose( fh ) == 0 );
47     TESTCASE( remove( testfile ) == 0 );
48     return TEST_RESULTS;
49 }
50
51 #endif