]> pd.if.org Git - pdclib/blob - functions/stdio/fgets.c
PDCLIB-16: Add _unlocked variations of all I/O routines; move work into these versions
[pdclib] / functions / stdio / fgets.c
1 /* $Id$ */
2
3 /* fgets( char *, int, FILE * )
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 <_PDCLIB_glue.h>
14
15 char * fgets_unlocked( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
16 {
17     if ( size == 0 )
18     {
19         return NULL;
20     }
21     if ( size == 1 )
22     {
23         *s = '\0';
24         return s;
25     }
26     if ( _PDCLIB_prepread( stream ) == EOF )
27     {
28         return NULL;
29     }
30     char * dest = s;
31     while ( ( ( *dest++ = stream->buffer[stream->bufidx++] ) != '\n' ) && --size > 0 )
32     {
33         if ( stream->bufidx == stream->bufend )
34         {
35             if ( _PDCLIB_fillbuffer( stream ) == EOF )
36             {
37                 /* In case of error / EOF before a character is read, this
38                    will lead to a \0 be written anyway. Since the results
39                    are "indeterminate" by definition, this does not hurt.
40                 */
41                 break;
42             }
43         }
44     }
45     *dest = '\0';
46     return ( dest == s ) ? NULL : s;
47 }
48
49 char * fgets( char * _PDCLIB_restrict s, int size, 
50               struct _PDCLIB_file_t * _PDCLIB_restrict stream )
51 {
52     flockfile( stream );
53     char* r = fgets_unlocked( s, size, stream );
54     funlockfile( stream );
55     return r;
56 }
57
58 #endif
59
60 #ifdef TEST
61 #include <_PDCLIB_test.h>
62 #include <string.h>
63
64 int main( void )
65 {
66     FILE * fh;
67     char buffer[10];
68     char const * fgets_test = "foo\nbar\0baz\nweenie";
69     TESTCASE( ( fh = fopen( testfile, "wb+" ) ) != NULL );
70     TESTCASE( fwrite( fgets_test, 1, 18, fh ) == 18 );
71     rewind( fh );
72     TESTCASE( fgets( buffer, 10, fh ) == buffer );
73     TESTCASE( strcmp( buffer, "foo\n" ) == 0 );
74     TESTCASE( fgets( buffer, 10, fh ) == buffer );
75     TESTCASE( memcmp( buffer, "bar\0baz\n", 8 ) == 0 );
76     TESTCASE( fgets( buffer, 10, fh ) == buffer );
77     TESTCASE( strcmp( buffer, "weenie" ) == 0 );
78     TESTCASE( feof( fh ) );
79     TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
80     TESTCASE( fgets( buffer, 1, fh ) == buffer );
81     TESTCASE( strcmp( buffer, "" ) == 0 );
82     TESTCASE( fgets( buffer, 0, fh ) == NULL );
83     TESTCASE( ! feof( fh ) );
84     TESTCASE( fgets( buffer, 1, fh ) == buffer );
85     TESTCASE( strcmp( buffer, "" ) == 0 );
86     TESTCASE( ! feof( fh ) );
87     TESTCASE( fgets( buffer, 2, fh ) == buffer );
88     TESTCASE( strcmp( buffer, "e" ) == 0 );
89     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
90     TESTCASE( fgets( buffer, 2, fh ) == NULL );
91     TESTCASE( feof( fh ) );
92     TESTCASE( fclose( fh ) == 0 );
93     TESTCASE( remove( testfile ) == 0 );
94     return TEST_RESULTS;
95 }
96
97 #endif
98