]> pd.if.org Git - pdclib/blob - functions/stdio/gets.c
gets() not required anymore since C11. Disabled to avoid implicit declaration / type...
[pdclib] / functions / stdio / gets.c
1 /* gets( char * )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8
9 #ifndef REGTEST
10 #include <_PDCLIB_io.h>
11 #include <stdint.h>
12
13 char * gets( char * s )
14 {
15     _PDCLIB_flockfile( stdin );
16     if ( _PDCLIB_prepread( stdin ) == EOF )
17     {
18         _PDCLIB_funlockfile( stdin );
19         return NULL;
20     }
21     char * dest = s;
22     
23     dest += _PDCLIB_getchars( dest, SIZE_MAX, '\n', stdin );
24     _PDCLIB_funlockfile( stdin );
25
26     if(*(dest - 1) == '\n') {
27         *(--dest) = '\0';
28     } else {
29         *dest = '\0';
30     }
31
32     return ( dest == s ) ? NULL : s;
33 }
34
35 #endif
36
37 #ifdef TEST
38 #include <_PDCLIB_test.h>
39 #include <string.h>
40
41 int main( void )
42 {
43 #ifndef REGTEST
44     FILE * fh;
45     char buffer[10];
46     char const * gets_test = "foo\nbar\0baz\nweenie";
47     TESTCASE( ( fh = fopen( testfile, "wb" ) ) != NULL );
48     TESTCASE( fwrite( gets_test, 1, 18, fh ) == 18 );
49     TESTCASE( fclose( fh ) == 0 );
50     TESTCASE( ( fh = freopen( testfile, "rb", stdin ) ) != NULL );
51     TESTCASE( gets( buffer ) == buffer );
52     TESTCASE( strcmp( buffer, "foo" ) == 0 );
53     TESTCASE( gets( buffer ) == buffer );
54     TESTCASE( memcmp( buffer, "bar\0baz\0", 8 ) == 0 );
55     TESTCASE( gets( buffer ) == buffer );
56     TESTCASE( strcmp( buffer, "weenie" ) == 0 );
57     TESTCASE( feof( fh ) );
58     TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
59     TESTCASE( gets( buffer ) == buffer );
60     TESTCASE( strcmp( buffer, "e" ) == 0 );
61     TESTCASE( feof( fh ) );
62     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
63     TESTCASE( gets( buffer ) == NULL );
64     TESTCASE( fclose( fh ) == 0 );
65     TESTCASE( remove( testfile ) == 0 );
66 #endif
67     return TEST_RESULTS;
68 }
69
70 #endif
71