_READ.c 1.8 KB

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