xref: /minix3/lib/libc/stdio/ungetc.c (revision b706112487045bc1efd01e3d4d53d9a6b04a0bca)
1 /*
2  * ungetc.c - push a character back onto an input stream
3  */
4 /* $Header$ */
5 
6 #include	<stdio.h>
7 #include	"loc_incl.h"
8 
9 int
10 ungetc(int ch, FILE *stream)
11 {
12 	unsigned char *p;
13 
14 	if (ch == EOF  || !io_testflag(stream,_IOREADING))
15 		return EOF;
16 	if (stream->_ptr == stream->_buf) {
17 		if (stream->_count != 0) return EOF;
18 		stream->_ptr++;
19 	}
20 	stream->_count++;
21 	p = --(stream->_ptr);		/* ??? Bloody vax assembler !!! */
22 	/* ungetc() in sscanf() shouldn't write in rom */
23 	if (*p != (unsigned char) ch)
24 		*p = (unsigned char) ch;
25 	return ch;
26 }
27