_READ.c 1.8 KB

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