safe_write.c 596 B

12345678910111213141516171819202122232425262728
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. ssize_t FAST_FUNC safe_write(int fd, const void *buf, size_t count)
  11. {
  12. ssize_t n;
  13. for (;;) {
  14. n = write(fd, buf, count);
  15. if (n >= 0 || errno != EINTR)
  16. break;
  17. /* Some callers set errno=0, are upset when they see EINTR.
  18. * Returning EINTR is wrong since we retry write(),
  19. * the "error" was transient.
  20. */
  21. errno = 0;
  22. /* repeat the write() */
  23. }
  24. return n;
  25. }