GNU file utilities
[gnulib.git] / lib / full-write.c
1 /* full-write.c -- an interface to write 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    Copied largely from GNU C's cccp.c.
19    */
20
21 #ifdef HAVE_CONFIG_H
22 #if defined (CONFIG_BROKETS)
23 /* We use <config.h> instead of "config.h" so that a compilation
24    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
25    (which it would do because it found this file in $srcdir).  */
26 #include <config.h>
27 #else
28 #include "config.h"
29 #endif
30 #endif
31
32 #include <sys/types.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <errno.h>
39 #ifndef STDC_HEADERS
40 extern int errno;
41 #endif
42
43 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
44    Return LEN upon success, write's (negative) error code otherwise.  */
45
46 int
47 full_write (desc, ptr, len)
48      int desc;
49      char *ptr;
50      int len;
51 {
52   int total_written;
53
54   total_written = 0;
55   while (len > 0)
56     {
57       int written = write (desc, ptr, len);
58       if (written < 0)
59         {
60 #ifdef EINTR
61           if (errno == EINTR)
62             continue;
63 #endif
64           return written;
65         }
66       total_written += written;
67       ptr += written;
68       len -= written;
69     }
70   return total_written;
71 }