summaryrefslogtreecommitdiff
path: root/libc/stdio/fgetc.c
blob: 3df25c39a29d1816697601fe674a05a309a3c9e9 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
 *
 * GNU Library General Public License (LGPL) version 2 or later.
 *
 * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
 */

#include "_stdio.h"

#undef fgetc
#undef fgetc_unlocked
#undef getc
#undef getc_unlocked

#ifdef __DO_UNLOCKED

int attribute_hidden __fgetc_unlocked(FILE *stream)
{
	__STDIO_STREAM_VALIDATE(stream);

	/* First the fast path.  We're good to go if getc macro enabled. */
	if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)) {
		return __STDIO_STREAM_BUFFER_GET(stream);
	}

	/* Next quickest... reading and narrow oriented, but macro
	 * disabled and/or buffer is exhausted. */
	if (__STDIO_STREAM_IS_NARROW_READING(stream)
		|| !__STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_NARROW)
		) {
		if (stream->__modeflags & __FLAG_UNGOT) { /* Use ungots first. */
			unsigned char uc = stream->__ungot[(stream->__modeflags--) & 1];
			stream->__ungot[1] = 0;
			__STDIO_STREAM_VALIDATE(stream);
			return uc;
		}

		if (__STDIO_STREAM_BUFFER_RAVAIL(stream)) {	/* Have buffered? */
			return __STDIO_STREAM_BUFFER_GET(stream);
		}

		/* Is this a fake stream for *sscanf? */
		if (__STDIO_STREAM_IS_FAKE_VSSCANF(stream)) {
			__STDIO_STREAM_SET_EOF(stream);
			return EOF;
		}

		/* We need to read from the host environment, so we must
		 * flush all line buffered streams if the stream is not
		 * fully buffered. */
		if (!__STDIO_STREAM_IS_FBF(stream)) {
			__STDIO_FLUSH_LBF_STREAMS;
		}

		if (__STDIO_STREAM_BUFFER_SIZE(stream)) { /* Do we have a buffer? */
			__STDIO_STREAM_DISABLE_GETC(stream);
			if(__STDIO_FILL_READ_BUFFER(stream)) {	/* Refill succeeded? */
				__STDIO_STREAM_ENABLE_GETC(stream);	/* FBF or LBF */
				return __STDIO_STREAM_BUFFER_GET(stream);
			}
		} else {
			unsigned char uc;
			if (__stdio_READ(stream, &uc, 1)) {
				return uc;
			}
		}
	}

	return EOF;
}

weak_alias(__fgetc_unlocked,fgetc_unlocked);
weak_alias(__fgetc_unlocked,getc_unlocked);
#ifndef __UCLIBC_HAS_THREADS__
weak_alias(__fgetc_unlocked,fgetc);
weak_alias(__fgetc_unlocked,getc);
#endif

#elif defined __UCLIBC_HAS_THREADS__

int fgetc(register FILE *stream)
{
	if (stream->__user_locking != 0) {
		return __GETC_UNLOCKED_MACRO(stream);
	} else {
		int retval;
		__STDIO_ALWAYS_THREADLOCK(stream);
		retval = __GETC_UNLOCKED_MACRO(stream);
		__STDIO_ALWAYS_THREADUNLOCK(stream);
		return retval;
	}
}

weak_alias(fgetc,getc);

#endif