fgetc.c 2.9 KB

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