fread.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 >= -2);
  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. while ((avail = __stdio_READ(stream, buffer, todo)) > 0) {
  53. buffer += avail;
  54. if (!(todo -= avail)) {
  55. break;
  56. }
  57. }
  58. DONE:
  59. __STDIO_STREAM_VALIDATE(stream);
  60. return (bytes - todo) / size;
  61. }
  62. __STDIO_STREAM_SET_ERROR(stream);
  63. __set_errno(EINVAL);
  64. }
  65. __STDIO_STREAM_VALIDATE(stream);
  66. return 0;
  67. }
  68. libc_hidden_def(fread_unlocked)
  69. #ifndef __UCLIBC_HAS_THREADS__
  70. strong_alias(fread_unlocked,fread)
  71. libc_hidden_def(fread)
  72. #endif
  73. #elif defined __UCLIBC_HAS_THREADS__
  74. size_t fread(void * __restrict ptr, size_t size, size_t nmemb,
  75. register FILE * __restrict stream)
  76. {
  77. size_t retval;
  78. __STDIO_AUTO_THREADLOCK_VAR;
  79. __STDIO_AUTO_THREADLOCK(stream);
  80. retval = fread_unlocked(ptr, size, nmemb, stream);
  81. __STDIO_AUTO_THREADUNLOCK(stream);
  82. return retval;
  83. }
  84. libc_hidden_def(fread)
  85. #endif