fd-read.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Copyright (C) 1993-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include <hurd.h>
  17. #include <hurd/fd.h>
  18. #include <string.h>
  19. error_t
  20. _hurd_fd_read (struct hurd_fd *fd, void *buf, size_t *nbytes, loff_t offset)
  21. {
  22. error_t err;
  23. char *data;
  24. mach_msg_type_number_t nread;
  25. error_t readfd (io_t port)
  26. {
  27. return __io_read (port, &data, &nread, offset, *nbytes);
  28. }
  29. data = buf;
  30. nread = *nbytes;
  31. if (err = HURD_FD_PORT_USE_CANCEL (fd, _hurd_ctty_input (port, ctty, readfd)))
  32. return err;
  33. if (__glibc_unlikely (nread > *nbytes)) /* Sanity check for bogus server. */
  34. {
  35. if (data != buf)
  36. __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
  37. return EGRATUITOUS;
  38. }
  39. if (data != buf)
  40. {
  41. memcpy (buf, data, nread);
  42. __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
  43. }
  44. *nbytes = nread;
  45. return 0;
  46. }