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.
12 #include <_PDCLIB_io.h>
14 int ungetc_unlocked( int c, FILE * stream )
16 if ( c == EOF || stream->ungetidx == _PDCLIB_UNGETCBUFSIZE )
20 return stream->ungetbuf[stream->ungetidx++] = (unsigned char) c;
23 int ungetc( int c, FILE * stream )
26 int r = ungetc_unlocked( c, stream );
34 #include <_PDCLIB_test.h>
37 const char* hellostr = "Hello, world!";
41 // Also see ftell() for some testing
43 // PDCLIB-18: fread ignores ungetc
44 size_t bufsz = strlen( hellostr ) + 1;
45 char * buf = malloc( bufsz );
49 TESTCASE( ( fh = tmpfile() ) != NULL );
50 TESTCASE( fputs(hellostr, fh) == 0 );
52 TESTCASE( fgetc( fh ) == 'H' );
53 TESTCASE( ungetc( 'H', fh ) == 'H' );
54 TESTCASE( fgets( buf, bufsz, fh ) != NULL );
55 TESTCASE( strcmp( buf, hellostr ) == 0 );
59 TESTCASE( fgetc( fh ) == 'H' );
60 TESTCASE( ungetc( 'H', fh ) == 'H' );
61 TESTCASE( fread( buf, bufsz - 1, 1, fh ) == 1 );
62 TESTCASE( strncmp( buf, hellostr, bufsz - 1 ) == 0 );