full-read.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* full-read.c -- an interface to read that retries after interrupts
  2. Copyright (C) 1993, 1994, 1997 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <sys/types.h>
  19. #if HAVE_UNISTD_H
  20. # include <unistd.h>
  21. #endif
  22. #include <errno.h>
  23. #ifndef errno
  24. extern int errno;
  25. #endif
  26. /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
  27. Return the actual number of bytes read, zero for EOF, or negative
  28. for an error. */
  29. int
  30. full_read (desc, ptr, len)
  31. int desc;
  32. char *ptr;
  33. size_t len;
  34. {
  35. int n_chars;
  36. if (len <= 0)
  37. return len;
  38. #ifdef EINTR
  39. do
  40. {
  41. n_chars = read (desc, ptr, len);
  42. }
  43. while (n_chars < 0 && errno == EINTR);
  44. #else
  45. n_chars = read (desc, ptr, len);
  46. #endif
  47. return n_chars;
  48. }