]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/cpio/lib/full-write.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / cpio / lib / full-write.c
1 /* An interface to read and write that retries (if necessary) until complete.
2
3    Copyright (C) 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4    2004 Free Software 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 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 /* Specification.  */
25 #ifdef FULL_READ
26 # include "full-read.h"
27 #else
28 # include "full-write.h"
29 #endif
30
31 #include <errno.h>
32
33 #ifdef FULL_READ
34 # include "safe-read.h"
35 # define safe_rw safe_read
36 # define full_rw full_read
37 # undef const
38 # define const /* empty */
39 #else
40 # include "safe-write.h"
41 # define safe_rw safe_write
42 # define full_rw full_write
43 #endif
44
45 #ifdef FULL_READ
46 /* Set errno to zero upon EOF.  */
47 # define ZERO_BYTE_TRANSFER_ERRNO 0
48 #else
49 /* Some buggy drivers return 0 when one tries to write beyond
50    a device's end.  (Example: Linux 1.2.13 on /dev/fd0.)
51    Set errno to ENOSPC so they get a sensible diagnostic.  */
52 # define ZERO_BYTE_TRANSFER_ERRNO ENOSPC
53 #endif
54
55 /* Write(read) COUNT bytes at BUF to(from) descriptor FD, retrying if
56    interrupted or if a partial write(read) occurs.  Return the number
57    of bytes transferred.
58    When writing, set errno if fewer than COUNT bytes are written.
59    When reading, if fewer than COUNT bytes are read, you must examine
60    errno to distinguish failure from EOF (errno == 0).  */
61 size_t
62 full_rw (int fd, const void *buf, size_t count)
63 {
64   size_t total = 0;
65   const char *ptr = buf;
66
67   while (count > 0)
68     {
69       size_t n_rw = safe_rw (fd, ptr, count);
70       if (n_rw == (size_t) -1)
71         break;
72       if (n_rw == 0)
73         {
74           errno = ZERO_BYTE_TRANSFER_ERRNO;
75           break;
76         }
77       total += n_rw;
78       ptr += n_rw;
79       count -= n_rw;
80     }
81
82   return total;
83 }