summaryrefslogtreecommitdiff
path: root/src/stdio/ungetc.c
blob: 0718168427603259924da29e38e4fb8d30b49302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "stdio_impl.h"

int ungetc(int c, FILE *f)
{
	if (c == EOF) return c;

	FLOCK(f);

	/* Fail if unreadable or writing and unable to flush */
	if ((f->flags & (F_ERR|F_NORD)) || (f->wpos && __oflow(f))) {
		FUNLOCK(f);
		return EOF;
	}

	/* Clear write mode */
	f->wbase = f->wpos = f->wstop = f->wend = 0;

	/* Put the file in read mode */
	if (!f->rpos) f->rpos = f->rend = f->buf;

	/* If unget buffer is already full, fail. */
	if (f->rpos <= f->buf - UNGET) {
		FUNLOCK(f);
		return EOF;
	}

	/* Put a byte back into the buffer */
	*--f->rpos = c;
	f->flags &= ~F_EOF;

	FUNLOCK(f);
	return c;
}