.
[gnulib.git] / lib / safe-read.c
1 /* safe-read.c -- an interface to read that retries after interrupts
2    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17    */
18
19 #ifdef HAVE_CONFIG_H
20 #if defined (CONFIG_BROKETS)
21 /* We use <config.h> instead of "config.h" so that a compilation
22    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
23    (which it would do because it found this file in $srcdir).  */
24 #include <config.h>
25 #else
26 #include "config.h"
27 #endif
28 #endif
29
30 #include <sys/types.h>
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 #include <errno.h>
37 #ifndef STDC_HEADERS
38 extern int errno;
39 #endif
40
41 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
42    Return the actual number of bytes read, zero for EOF, or negative
43    for an error.  */
44
45 int
46 safe_read (desc, ptr, len)
47      int desc;
48      char *ptr;
49      int len;
50 {
51   int n_chars;
52
53   if (len <= 0)
54     return len;
55
56 #ifdef EINTR
57   do
58     {
59       n_chars = read (desc, ptr, len);
60     }
61   while (n_chars < 0 && errno == EINTR);
62 #else
63   n_chars = read (desc, ptr, len);
64 #endif
65
66   return n_chars;
67 }