fread.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include "_stdio.h"
  8. /* libc_hidden_proto(fread_unlocked) */
  9. #ifdef __DO_UNLOCKED
  10. /* Experimentally off - libc_hidden_proto(memcpy) */
  11. /* libc_hidden_proto(fflush_unlocked) */
  12. size_t fread_unlocked(void * __restrict ptr, size_t size, size_t nmemb,
  13. FILE * __restrict stream)
  14. {
  15. __STDIO_STREAM_VALIDATE(stream);
  16. assert(stream->__filedes >= -1);
  17. /* Note: If nmbem * size > SIZE_MAX then there is an application
  18. * bug since no array can be larger than SIZE_MAX in size. */
  19. if ((__STDIO_STREAM_IS_NARROW_READING(stream)
  20. || !__STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_NARROW))
  21. && size && nmemb
  22. ) {
  23. if (nmemb <= (SIZE_MAX / size)) {
  24. unsigned char *buffer = (unsigned char *) ptr;
  25. size_t todo, bytes, avail;
  26. todo = bytes = size * nmemb;
  27. /* Check for ungots... */
  28. while (stream->__modeflags & __FLAG_UNGOT) {
  29. *buffer++ = stream->__ungot[(stream->__modeflags--) & 1];
  30. stream->__ungot[1] = 0;
  31. if (!--todo) {
  32. goto DONE;
  33. }
  34. }
  35. #ifdef __STDIO_BUFFERS
  36. /* Next check for available buffered... */
  37. if ((avail = stream->__bufread - stream->__bufpos) > 0) {
  38. if (avail > todo) {
  39. avail = todo;
  40. }
  41. memcpy(buffer, stream->__bufpos, avail);
  42. buffer += avail;
  43. stream->__bufpos += avail;
  44. if (!(todo -= avail)) {
  45. goto DONE;
  46. }
  47. }
  48. /* We need to read from the host environment, so we must
  49. * flush all line buffered streams if the stream is not
  50. * fully buffered. */
  51. if (!__STDIO_STREAM_IS_FBF(stream)) {
  52. __STDIO_FLUSH_LBF_STREAMS;
  53. }
  54. #endif
  55. #ifdef __UCLIBC_MJN3_ONLY__
  56. #warning CONSIDER: should we refill and read from the buffer sometimes?
  57. #endif
  58. while ((avail = __stdio_READ(stream, buffer, todo)) > 0) {
  59. buffer += avail;
  60. if (!(todo -= avail)) {
  61. break;
  62. }
  63. }
  64. DONE:
  65. __STDIO_STREAM_VALIDATE(stream);
  66. return (bytes - todo) / size;
  67. }
  68. __STDIO_STREAM_SET_ERROR(stream);
  69. __set_errno(EINVAL);
  70. }
  71. __STDIO_STREAM_VALIDATE(stream);
  72. return 0;
  73. }
  74. libc_hidden_def(fread_unlocked)
  75. #ifndef __UCLIBC_HAS_THREADS__
  76. /* libc_hidden_proto(fread) */
  77. strong_alias(fread_unlocked,fread)
  78. libc_hidden_def(fread)
  79. #endif
  80. #elif defined __UCLIBC_HAS_THREADS__
  81. /* libc_hidden_proto(fread) */
  82. size_t fread(void * __restrict ptr, size_t size, size_t nmemb,
  83. register FILE * __restrict stream)
  84. {
  85. size_t retval;
  86. __STDIO_AUTO_THREADLOCK_VAR;
  87. __STDIO_AUTO_THREADLOCK(stream);
  88. retval = fread_unlocked(ptr, size, nmemb, stream);
  89. __STDIO_AUTO_THREADUNLOCK(stream);
  90. return retval;
  91. }
  92. libc_hidden_def(fread)
  93. #endif