Reworked scanf() testing. General cleanups.
[fw/pdclib] / functions / stdio / gets.c
1 /* $Id$ */
2
3 /* gets( 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 #define _PDCLIB_GLUE_H _PDCLIB_GLUE_H
14 #include <_PDCLIB_glue.h>
15
16 char * gets( char * s )
17 {
18     if ( _PDCLIB_prepread( stdin ) == EOF )
19     {
20         return NULL;
21     }
22     char * dest = s;
23     while ( ( *dest = stdin->buffer[stdin->bufidx++] ) != '\n' )
24     {
25         ++dest;
26         if ( stdin->bufidx == stdin->bufend )
27         {
28             if ( _PDCLIB_fillbuffer( stdin ) == EOF )
29             {
30                 break;
31             }
32         }
33     }
34     *dest = '\0';
35     return ( dest == s ) ? NULL : s;
36 }
37
38 #endif
39
40 #ifdef TEST
41 #include <_PDCLIB_test.h>
42 #include <string.h>
43
44 int main( void )
45 {
46     FILE * fh;
47     char buffer[10];
48     char const * gets_test = "foo\nbar\0baz\nweenie";
49     TESTCASE( ( fh = fopen( testfile, "wb" ) ) != NULL );
50     TESTCASE( fwrite( gets_test, 1, 18, fh ) == 18 );
51     TESTCASE( fclose( fh ) == 0 );
52     TESTCASE( ( fh = freopen( testfile, "rb", stdin ) ) != NULL );
53     TESTCASE( gets( buffer ) == buffer );
54     TESTCASE( strcmp( buffer, "foo" ) == 0 );
55     TESTCASE( gets( buffer ) == buffer );
56     TESTCASE( memcmp( buffer, "bar\0baz\0", 8 ) == 0 );
57     TESTCASE( gets( buffer ) == buffer );
58     TESTCASE( strcmp( buffer, "weenie" ) == 0 );
59     TESTCASE( feof( fh ) );
60     TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
61     TESTCASE( gets( buffer ) == buffer );
62     TESTCASE( strcmp( buffer, "e" ) == 0 );
63     TESTCASE( feof( fh ) );
64     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
65     TESTCASE( gets( buffer ) == NULL );
66     TESTCASE( fclose( fh ) == 0 );
67     TESTCASE( remove( testfile ) == 0 );
68     return TEST_RESULTS;
69 }
70
71 #endif
72