_READ.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /* Given a reading stream without its end-of-file indicator set and
  9. * with no buffered input or ungots, read at most 'bufsize' bytes
  10. * into 'buf' (which may be the stream's __bufstart).
  11. * If a read error occurs, set the stream's error indicator.
  12. * If EOF is encountered, set the stream's end-of-file indicator.
  13. *
  14. * Returns the number of bytes read, even in EOF and error cases.
  15. *
  16. * Notes:
  17. * Calling with bufsize == 0 is NOT permitted (unlike __stdio_WRITE).
  18. * NOT THREADSAFE! Assumes stream already locked if necessary.
  19. */
  20. size_t attribute_hidden __stdio_READ(register FILE *stream,
  21. unsigned char *buf, size_t bufsize)
  22. {
  23. ssize_t rv = 0;
  24. __STDIO_STREAM_VALIDATE(stream);
  25. assert(stream->__filedes >= -2);
  26. assert(__STDIO_STREAM_IS_READING(stream));
  27. assert(!__STDIO_STREAM_BUFFER_RAVAIL(stream)); /* Buffer must be empty. */
  28. assert(!(stream->__modeflags & __FLAG_UNGOT));
  29. assert(bufsize);
  30. if (!__FEOF_UNLOCKED(stream)) {
  31. if (bufsize > SSIZE_MAX) {
  32. bufsize = SSIZE_MAX;
  33. }
  34. /* RETRY: */
  35. if ((rv = __READ(stream, (char *) buf, bufsize)) <= 0) {
  36. if (rv == 0) {
  37. __STDIO_STREAM_SET_EOF(stream);
  38. } else {
  39. /* if (errno == EINTR) goto RETRY; */
  40. __STDIO_STREAM_SET_ERROR(stream);
  41. rv = 0;
  42. }
  43. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  44. } else {
  45. assert(rv <= bufsize);
  46. if (rv > bufsize) { /* Read more than bufsize! */
  47. abort();
  48. }
  49. #endif
  50. }
  51. }
  52. return rv;
  53. }