.
[gnulib.git] / lib / safe-read.c
1 /* Read LEN bytes at PTR from descriptor DESC, retrying if necessary.
2    Return the actual number of bytes read, zero for EOF, or negative
3    for an error.  */
4
5 int
6 safe_read (desc, ptr, len)
7      int desc;
8      char *ptr;
9      int len;
10 {
11   int n_remaining;
12
13   n_remaining = len;
14   while (n_remaining > 0)
15     {
16       int n_chars = read (desc, ptr, n_remaining);
17       if (n_chars < 0)
18         {
19 #ifdef EINTR
20           if (errno == EINTR)
21             continue;
22 #endif
23           return n_chars;
24         }
25       if (n_chars == 0)
26         break;
27       ptr += n_chars;
28       n_remaining -= n_chars;
29     }
30   return len - n_remaining;
31 }