Stub out most of stdio on AltOS
[fw/pdclib] / platform / altos / 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 char * gets( char * s )
14 {
15     char *dest = s;
16     int c;
17
18     for (;;) {
19         c = getchar();
20         if (c == '\n' || c == EOF)
21             break;
22         *dest++ = c;
23     }
24     *dest = '\0';
25     return ( dest == s ) ? NULL : s;
26 }
27
28 #endif
29
30 #ifdef TEST
31 #include <_PDCLIB_test.h>
32 #include <string.h>
33
34 int main( void )
35 {
36     FILE * fh;
37     char buffer[10];
38     char const * gets_test = "foo\nbar\0baz\nweenie";
39     TESTCASE( ( fh = fopen( testfile, "wb" ) ) != NULL );
40     TESTCASE( fwrite( gets_test, 1, 18, fh ) == 18 );
41     TESTCASE( fclose( fh ) == 0 );
42     TESTCASE( ( fh = freopen( testfile, "rb", stdin ) ) != NULL );
43     TESTCASE( gets( buffer ) == buffer );
44     TESTCASE( strcmp( buffer, "foo" ) == 0 );
45     TESTCASE( gets( buffer ) == buffer );
46     TESTCASE( memcmp( buffer, "bar\0baz\0", 8 ) == 0 );
47     TESTCASE( gets( buffer ) == buffer );
48     TESTCASE( strcmp( buffer, "weenie" ) == 0 );
49     TESTCASE( feof( fh ) );
50     TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
51     TESTCASE( gets( buffer ) == buffer );
52     TESTCASE( strcmp( buffer, "e" ) == 0 );
53     TESTCASE( feof( fh ) );
54     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
55     TESTCASE( gets( buffer ) == NULL );
56     TESTCASE( fclose( fh ) == 0 );
57     TESTCASE( remove( testfile ) == 0 );
58     return TEST_RESULTS;
59 }
60
61 #endif
62