_READ.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 >= -1);
  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. #ifdef __UCLIBC_MJN3_ONLY__
  35. #warning EINTR?
  36. #endif
  37. /* RETRY: */
  38. if ((rv = __READ(stream, (char *) buf, bufsize)) <= 0) {
  39. if (rv == 0) {
  40. __STDIO_STREAM_SET_EOF(stream);
  41. } else {
  42. /* if (errno == EINTR) goto RETRY; */
  43. __STDIO_STREAM_SET_ERROR(stream);
  44. rv = 0;
  45. }
  46. #ifdef __UCLIBC_MJN3_ONLY__
  47. #warning TODO: Make custom stream read return check optional.
  48. #endif
  49. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  50. } else {
  51. assert(rv <= bufsize);
  52. if (rv > bufsize) { /* Read more than bufsize! */
  53. abort();
  54. }
  55. #endif
  56. }
  57. }
  58. return rv;
  59. }