unistd_ext.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* unistd.h related helpers.
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _UNISTD_EXT_H
  16. #define _UNISTD_EXT_H
  17. #include <error.h>
  18. #include <errno.h>
  19. #include <libintl.h>
  20. #include <unistd.h>
  21. /* Helpers used in catgets/gencat.c and malloc/memusage*.c */
  22. static inline void
  23. write_all (int fd, const void *buffer, size_t length)
  24. {
  25. const char *p = buffer;
  26. const char *end = p + length;
  27. while (p < end)
  28. {
  29. ssize_t ret = write (fd, p, end - p);
  30. if (ret < 0)
  31. error (EXIT_FAILURE, errno,
  32. gettext ("write of %zu bytes failed after %td: %m"),
  33. length, p - (const char *) buffer);
  34. if (ret == 0)
  35. error (EXIT_FAILURE, 0,
  36. gettext ("write returned 0 after writing %td bytes of %zu"),
  37. p - (const char *) buffer, length);
  38. p += ret;
  39. }
  40. }
  41. static inline void
  42. read_all (int fd, void *buffer, size_t length)
  43. {
  44. char *p = buffer;
  45. char *end = p + length;
  46. while (p < end)
  47. {
  48. ssize_t ret = read (fd, p, end - p);
  49. if (ret < 0)
  50. error (EXIT_FAILURE, errno,
  51. gettext ("read of %zu bytes failed after %td: %m"),
  52. length, p - (char *) buffer);
  53. p += ret;
  54. }
  55. }
  56. #endif