]> pd.if.org Git - pdclib.old/blob - functions/stdio/gets.c
83a261efd6744001414b25dcf2f234afb32bccb9
[pdclib.old] / 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 #include <_PDCLIB_io.h>
13 #include <stdint.h>
14
15 char * gets( char * s )
16 {
17     if ( _PDCLIB_prepread( stdin ) == EOF )
18     {
19         return NULL;
20     }
21     char * dest = s;
22     
23     dest += _PDCLIB_getchars( dest, SIZE_MAX, '\n', stdin );
24
25     if(*(dest - 1) == '\n') {
26         *(--dest) = '\0';
27     } else {
28         *dest = '\0';
29     }
30
31     return ( dest == s ) ? NULL : s;
32 }
33
34 #endif
35
36 #ifdef TEST
37 #include <_PDCLIB_test.h>
38 #include <string.h>
39
40 int main( void )
41 {
42     FILE * fh;
43     char buffer[10];
44     char const * gets_test = "foo\nbar\0baz\nweenie";
45     TESTCASE( ( fh = fopen( testfile, "wb" ) ) != NULL );
46     TESTCASE( fwrite( gets_test, 1, 18, fh ) == 18 );
47     TESTCASE( fclose( fh ) == 0 );
48     TESTCASE( ( fh = freopen( testfile, "rb", stdin ) ) != NULL );
49     TESTCASE( gets( buffer ) == buffer );
50     TESTCASE( strcmp( buffer, "foo" ) == 0 );
51     TESTCASE( gets( buffer ) == buffer );
52     TESTCASE( memcmp( buffer, "bar\0baz\0", 8 ) == 0 );
53     TESTCASE( gets( buffer ) == buffer );
54     TESTCASE( strcmp( buffer, "weenie" ) == 0 );
55     TESTCASE( feof( fh ) );
56     TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
57     TESTCASE( gets( buffer ) == buffer );
58     TESTCASE( strcmp( buffer, "e" ) == 0 );
59     TESTCASE( feof( fh ) );
60     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
61     TESTCASE( gets( buffer ) == NULL );
62     TESTCASE( fclose( fh ) == 0 );
63     TESTCASE( remove( testfile ) == 0 );
64     return TEST_RESULTS;
65 }
66
67 #endif
68