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