Merge from vendor branch GDB:
[dragonfly.git] / contrib / tar / lib / full-write.c
1 /* full-write.c -- an interface to write that retries after interrupts
2
3    Copyright 1993, 1994, 1997, 1998, 1999, 2000, 2001 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    Written by Paul Eggert.  */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <sys/types.h>
27
28 #include "full-write.h"
29
30 #if HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33
34 #include <errno.h>
35 #ifndef errno
36 extern int errno;
37 #endif
38
39 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted
40    or if partial writes occur.  Return the number of bytes successfully
41    written, setting errno if that is less than LEN.  */
42
43 size_t
44 full_write (int desc, const char *ptr, size_t len)
45 {
46   size_t total_written = 0;
47
48   while (len > 0)
49     {
50       ssize_t written = write (desc, ptr, len);
51       if (written <= 0)
52         {
53           /* Some buggy drivers return 0 when you fall off a device's end.  */
54           if (written == 0)
55             errno = ENOSPC;
56 #ifdef EINTR
57           if (errno == EINTR)
58             continue;
59 #endif
60           break;
61         }
62       total_written += written;
63       ptr += written;
64       len -= written;
65     }
66   return total_written;
67 }