blob: 56a284c02bdc0a9730bac742919abb1ade3b5742 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
2 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3 *
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5 *
6 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
7 */
8
9#include "_stdio.h"
10
11#undef fgetc
12#undef fgetc_unlocked
13#undef getc
14#undef getc_unlocked
15
16
17#ifdef __DO_UNLOCKED
18
19
20int __fgetc_unlocked(FILE *stream)
21{
22 __STDIO_STREAM_VALIDATE(stream);
23
24 /* First the fast path. We're good to go if getc macro enabled. */
25 if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)) {
26 return __STDIO_STREAM_BUFFER_GET(stream);
27 }
28
29 /* Next quickest... reading and narrow oriented, but macro
30 * disabled and/or buffer is exhausted. */
31 if (__STDIO_STREAM_IS_NARROW_READING(stream)
32 || !__STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_NARROW)
33 ) {
34 if (stream->__modeflags & __FLAG_UNGOT) { /* Use ungots first. */
35 unsigned char uc = stream->__ungot[(stream->__modeflags--) & 1];
36 stream->__ungot[1] = 0;
37 __STDIO_STREAM_VALIDATE(stream);
38 return uc;
39 }
40
41 if (__STDIO_STREAM_BUFFER_RAVAIL(stream)) { /* Have buffered? */
42 return __STDIO_STREAM_BUFFER_GET(stream);
43 }
44
45 /* Is this a fake stream for *sscanf? */
46 if (__STDIO_STREAM_IS_FAKE_VSSCANF(stream)) {
47 __STDIO_STREAM_SET_EOF(stream);
48 return EOF;
49 }
50
51 /* We need to read from the host environment, so we must
52 * flush all line buffered streams if the stream is not
53 * fully buffered. */
54 if (!__STDIO_STREAM_IS_FBF(stream)) {
55 __STDIO_FLUSH_LBF_STREAMS;
56 }
57
58 if (__STDIO_STREAM_BUFFER_SIZE(stream)) { /* Do we have a buffer? */
59 __STDIO_STREAM_DISABLE_GETC(stream);
60 if(__STDIO_FILL_READ_BUFFER(stream)) { /* Refill succeeded? */
61 __STDIO_STREAM_ENABLE_GETC(stream); /* FBF or LBF */
62 return __STDIO_STREAM_BUFFER_GET(stream);
63 }
64 } else {
65 unsigned char uc;
66 if (__stdio_READ(stream, &uc, 1)) {
67 return uc;
68 }
69 }
70 }
71
72 return EOF;
73}
74libc_hidden_def(__fgetc_unlocked)
75
76strong_alias(__fgetc_unlocked,fgetc_unlocked)
77libc_hidden_def(fgetc_unlocked)
78
79strong_alias(__fgetc_unlocked,getc_unlocked)
80libc_hidden_def(getc_unlocked)
81
82#ifndef __UCLIBC_HAS_THREADS__
83strong_alias(__fgetc_unlocked,fgetc)
84libc_hidden_def(fgetc)
85
86strong_alias(__fgetc_unlocked,getc)
87#endif
88
89#elif defined __UCLIBC_HAS_THREADS__
90
91int fgetc(register FILE *stream)
92{
93 if (stream->__user_locking != 0) {
94 return __GETC_UNLOCKED_MACRO(stream);
95 } else {
96 int retval;
97 __STDIO_ALWAYS_THREADLOCK(stream);
98 retval = __GETC_UNLOCKED_MACRO(stream);
99 __STDIO_ALWAYS_THREADUNLOCK(stream);
100 return retval;
101 }
102}
103libc_hidden_def(fgetc)
104
105strong_alias(fgetc,getc)
106
107#endif