fread.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifdef __DO_UNLOCKED
  9. size_t __fread_unlocked(void * __restrict ptr, size_t size, size_t nmemb,
  10. FILE * __restrict stream)
  11. {
  12. __STDIO_STREAM_VALIDATE(stream);
  13. assert(stream->__filedes >= -1);
  14. /* Note: If nmbem * size > SIZE_MAX then there is an application
  15. * bug since no array can be larger than SIZE_MAX in size. */
  16. if ((__STDIO_STREAM_IS_NARROW_READING(stream)
  17. || !__STDIO_STREAM_TRANS_TO_READ(stream, __FLAG_NARROW))
  18. && size && nmemb
  19. ) {
  20. if (nmemb <= (SIZE_MAX / size)) {
  21. unsigned char *buffer = (unsigned char *) ptr;
  22. size_t todo, bytes, avail;
  23. todo = bytes = size * nmemb;
  24. /* Check for ungots... */
  25. while (stream->__modeflags & __FLAG_UNGOT) {
  26. *buffer++ = stream->__ungot[(stream->__modeflags--) & 1];
  27. stream->__ungot[1] = 0;
  28. if (!--todo) {
  29. goto DONE;
  30. }
  31. }
  32. #ifdef __STDIO_BUFFERS
  33. /* Next check for available buffered... */
  34. if ((avail = stream->__bufread - stream->__bufpos) > 0) {
  35. if (avail > todo) {
  36. avail = todo;
  37. }
  38. memcpy(buffer, stream->__bufpos, avail);
  39. buffer += avail;
  40. stream->__bufpos += avail;
  41. if (!(todo -= avail)) {
  42. goto DONE;
  43. }
  44. }
  45. /* We need to read from the host environment, so we must
  46. * flush all line buffered streams if the stream is not
  47. * fully buffered. */
  48. if (!__STDIO_STREAM_IS_FBF(stream)) {
  49. __STDIO_FLUSH_LBF_STREAMS;
  50. }
  51. #endif
  52. #ifdef __UCLIBC_MJN3_ONLY__
  53. #warning CONSIDER: should we refill and read from the buffer sometimes?
  54. #endif
  55. while ((avail = __stdio_READ(stream, buffer, todo)) > 0) {
  56. buffer += avail;
  57. if (!(todo -= avail)) {
  58. break;
  59. }
  60. }
  61. DONE:
  62. __STDIO_STREAM_VALIDATE(stream);
  63. return (bytes - todo) / size;
  64. }
  65. __STDIO_STREAM_SET_ERROR(stream);
  66. __set_errno(EINVAL);
  67. }
  68. __STDIO_STREAM_VALIDATE(stream);
  69. return 0;
  70. }
  71. weak_alias(__fread_unlocked,fread_unlocked);
  72. #ifndef __UCLIBC_HAS_THREADS__
  73. weak_alias(__fread_unlocked,fread);
  74. #endif
  75. #elif defined __UCLIBC_HAS_THREADS__
  76. size_t fread(void * __restrict ptr, size_t size, size_t nmemb,
  77. register FILE * __restrict stream)
  78. {
  79. size_t retval;
  80. __STDIO_AUTO_THREADLOCK_VAR;
  81. __STDIO_AUTO_THREADLOCK(stream);
  82. retval = __fread_unlocked(ptr, size, nmemb, stream);
  83. __STDIO_AUTO_THREADUNLOCK(stream);
  84. return retval;
  85. }
  86. #endif