fgetc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* 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. #include "_stdio.h"
  9. #undef fgetc
  10. #undef fgetc_unlocked
  11. #undef getc
  12. #undef getc_unlocked
  13. #ifdef __DO_UNLOCKED
  14. libc_hidden_proto(fflush_unlocked)
  15. int __fgetc_unlocked(FILE *stream)
  16. {
  17. __STDIO_STREAM_VALIDATE(stream);
  18. /* First the fast path. We're good to go if getc macro enabled. */
  19. if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)) {
  20. return __STDIO_STREAM_BUFFER_GET(stream);
  21. }
  22. /* Next quickest... reading and narrow oriented, but macro
  23. * disabled and/or buffer is exhausted. */
  24. if (__STDIO_STREAM_IS_NARROW_READING(stream)
  25. || !__STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_NARROW)
  26. ) {
  27. if (stream->__modeflags & __FLAG_UNGOT) { /* Use ungots first. */
  28. unsigned char uc = stream->__ungot[(stream->__modeflags--) & 1];
  29. stream->__ungot[1] = 0;
  30. __STDIO_STREAM_VALIDATE(stream);
  31. return uc;
  32. }
  33. if (__STDIO_STREAM_BUFFER_RAVAIL(stream)) { /* Have buffered? */
  34. return __STDIO_STREAM_BUFFER_GET(stream);
  35. }
  36. /* Is this a fake stream for *sscanf? */
  37. if (__STDIO_STREAM_IS_FAKE_VSSCANF(stream)) {
  38. __STDIO_STREAM_SET_EOF(stream);
  39. return EOF;
  40. }
  41. /* We need to read from the host environment, so we must
  42. * flush all line buffered streams if the stream is not
  43. * fully buffered. */
  44. if (!__STDIO_STREAM_IS_FBF(stream)) {
  45. __STDIO_FLUSH_LBF_STREAMS;
  46. }
  47. if (__STDIO_STREAM_BUFFER_SIZE(stream)) { /* Do we have a buffer? */
  48. __STDIO_STREAM_DISABLE_GETC(stream);
  49. if(__STDIO_FILL_READ_BUFFER(stream)) { /* Refill succeeded? */
  50. __STDIO_STREAM_ENABLE_GETC(stream); /* FBF or LBF */
  51. return __STDIO_STREAM_BUFFER_GET(stream);
  52. }
  53. } else {
  54. unsigned char uc;
  55. if (__stdio_READ(stream, &uc, 1)) {
  56. return uc;
  57. }
  58. }
  59. }
  60. return EOF;
  61. }
  62. libc_hidden_proto(__fgetc_unlocked)
  63. libc_hidden_def(__fgetc_unlocked)
  64. strong_alias(__fgetc_unlocked,fgetc_unlocked)
  65. libc_hidden_proto(fgetc_unlocked)
  66. libc_hidden_def(fgetc_unlocked)
  67. //strong_alias(__fgetc_unlocked,__getc_unlocked)
  68. //libc_hidden_proto(__getc_unlocked)
  69. //libc_hidden_def(__getc_unlocked)
  70. strong_alias(__fgetc_unlocked,getc_unlocked)
  71. libc_hidden_proto(getc_unlocked)
  72. libc_hidden_def(getc_unlocked)
  73. #ifndef __UCLIBC_HAS_THREADS__
  74. strong_alias(__fgetc_unlocked,fgetc)
  75. libc_hidden_proto(fgetc)
  76. libc_hidden_def(fgetc)
  77. strong_alias(__fgetc_unlocked,getc)
  78. #endif
  79. #elif defined __UCLIBC_HAS_THREADS__
  80. libc_hidden_proto(__fgetc_unlocked)
  81. int fgetc(register FILE *stream)
  82. {
  83. if (stream->__user_locking != 0) {
  84. return __GETC_UNLOCKED_MACRO(stream);
  85. } else {
  86. int retval;
  87. __STDIO_ALWAYS_THREADLOCK(stream);
  88. retval = __GETC_UNLOCKED_MACRO(stream);
  89. __STDIO_ALWAYS_THREADUNLOCK(stream);
  90. return retval;
  91. }
  92. }
  93. libc_hidden_proto(fgetc)
  94. libc_hidden_def(fgetc)
  95. strong_alias(fgetc,getc)
  96. #endif