add ext4,vfat and tar.bz2
[u-tools.git] / u-tools / apps / tar / gnu / safe-read.c
blobb30a3deaead0f0be1b60cd66bb39d737ff6f92d4
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* An interface to read and write that retries after interrupts.
5 Copyright (C) 1993-1994, 1998, 2002-2006, 2009-2011 Free Software
6 Foundation, Inc.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 /* Specification. */
24 #ifdef SAFE_WRITE
25 # include "safe-write.h"
26 #else
27 # include "safe-read.h"
28 #endif
30 /* Get ssize_t. */
31 #include <sys/types.h>
32 #include <unistd.h>
34 #include <errno.h>
36 #ifdef EINTR
37 # define IS_EINTR(x) ((x) == EINTR)
38 #else
39 # define IS_EINTR(x) 0
40 #endif
42 #include <limits.h>
44 #ifdef SAFE_WRITE
45 # define safe_rw safe_write
46 # define rw write
47 #else
48 # define safe_rw safe_read
49 # define rw read
50 # undef const
51 # define const /* empty */
52 #endif
54 /* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if
55 interrupted. Return the actual number of bytes read(written), zero for EOF,
56 or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error. */
57 size_t
58 safe_rw (int fd, void const *buf, size_t count)
60 /* Work around a bug in Tru64 5.1. Attempting to read more than
61 INT_MAX bytes fails with errno == EINVAL. See
62 <http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00010.html>.
63 When decreasing COUNT, keep it block-aligned. */
64 enum { BUGGY_READ_MAXIMUM = INT_MAX & ~8191 };
66 for (;;)
68 ssize_t result = rw (fd, buf, count);
70 if (0 <= result)
71 return result;
72 else if (IS_EINTR (errno))
73 continue;
74 else if (errno == EINVAL && BUGGY_READ_MAXIMUM < count)
75 count = BUGGY_READ_MAXIMUM;
76 else
77 return result;