1 /* An interface to read and write that retries after interrupts.
3 Copyright (C) 1993, 1994, 1998, 2002, 2003, 2004, 2005 Free Software
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)
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 # include "safe-write.h"
28 # include "safe-read.h"
32 #include <sys/types.h>
38 # define IS_EINTR(x) ((x) == EINTR)
40 # define IS_EINTR(x) 0
46 # define safe_rw safe_write
49 # define safe_rw safe_read
52 # define const /* empty */
55 /* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if
56 interrupted. Return the actual number of bytes read(written), zero for EOF,
57 or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error. */
59 safe_rw (int fd, void const *buf, size_t count)
61 /* Work around a bug in Tru64 5.1. Attempting to read more than
62 INT_MAX bytes fails with errno == EINVAL. See
63 <http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00010.html>.
64 When decreasing COUNT, keep it block-aligned. */
65 enum { BUGGY_READ_MAXIMUM = INT_MAX & ~8191 };
69 ssize_t result = rw (fd, buf, count);
73 else if (IS_EINTR (errno))
75 else if (errno == EINVAL && BUGGY_READ_MAXIMUM < count)
76 count = BUGGY_READ_MAXIMUM;