fgetc.c 2.6 KB

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