or1k: add clone() from old GNU libc implementation
[uclibc-ng.git] / libc / stdio / fgetc.c
blob56a284c02bdc0a9730bac742919abb1ade3b5742
1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
2 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
7 */
9 #include "_stdio.h"
11 #undef fgetc
12 #undef fgetc_unlocked
13 #undef getc
14 #undef getc_unlocked
17 #ifdef __DO_UNLOCKED
20 int __fgetc_unlocked(FILE *stream)
22 __STDIO_STREAM_VALIDATE(stream);
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);
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;
41 if (__STDIO_STREAM_BUFFER_RAVAIL(stream)) { /* Have buffered? */
42 return __STDIO_STREAM_BUFFER_GET(stream);
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;
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;
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);
64 } else {
65 unsigned char uc;
66 if (__stdio_READ(stream, &uc, 1)) {
67 return uc;
72 return EOF;
74 libc_hidden_def(__fgetc_unlocked)
76 strong_alias(__fgetc_unlocked,fgetc_unlocked)
77 libc_hidden_def(fgetc_unlocked)
79 strong_alias(__fgetc_unlocked,getc_unlocked)
80 libc_hidden_def(getc_unlocked)
82 #ifndef __UCLIBC_HAS_THREADS__
83 strong_alias(__fgetc_unlocked,fgetc)
84 libc_hidden_def(fgetc)
86 strong_alias(__fgetc_unlocked,getc)
87 #endif
89 #elif defined __UCLIBC_HAS_THREADS__
91 int fgetc(register FILE *stream)
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;
103 libc_hidden_def(fgetc)
105 strong_alias(fgetc,getc)
107 #endif