3 /* ungetc( int, FILE * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 int ungetc_unlocked( int c, struct _PDCLIB_file_t * stream )
15 if ( c == EOF || stream->ungetidx == _PDCLIB_UNGETCBUFSIZE )
19 return stream->ungetbuf[stream->ungetidx++] = (unsigned char) c;
22 int ungetc( int c, struct _PDCLIB_file_t * stream )
25 int r = ungetc_unlocked( c, stream );
33 #include <_PDCLIB_test.h>
35 const char* hellostr = "Hello, world!";
39 // Also see ftell() for some testing
41 // PDCLIB-18: fread ignores ungetc
42 size_t bufsz = strlen( hellostr ) + 1;
43 char * buf = malloc( bufsz );
47 TESTCASE( ( fh = tmpfile() ) != NULL );
48 TESTCASE( fputs(hellostr, fh) == 0 );
50 TESTCASE( fgetc( fh ) == 'H' );
51 TESTCASE( ungetc( 'H', fh ) == 'H' );
52 TESTCASE( fgets( buf, bufsz, fh ) != NULL );
53 TESTCASE( strcmp( buf, hellostr ) == 0 );
57 TESTCASE( fgetc( fh ) == 'H' );
58 TESTCASE( ungetc( 'H', fh ) == 'H' );
59 TESTCASE( fread( buf, bufsz - 1, 1, fh ) == 1 );
60 TESTCASE( strncmp( buf, hellostr, bufsz - 1 ) == 0 );