1N/A/* An interface to read and write that retries after interrupts.
1N/A
1N/A Copyright (C) 1993-1994, 1998, 2002-2006, 2009-2010 Free Software
1N/A Foundation, Inc.
1N/A
1N/A This program is free software: you can redistribute it and/or modify
1N/A it under the terms of the GNU General Public License as published by
1N/A the Free Software Foundation; either version 3 of the License, or
1N/A (at your option) any later version.
1N/A
1N/A This program is distributed in the hope that it will be useful,
1N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A GNU General Public License for more details.
1N/A
1N/A You should have received a copy of the GNU General Public License
1N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
1N/A
1N/A#include <config.h>
1N/A
1N/A/* Specification. */
1N/A#ifdef SAFE_WRITE
1N/A# include "safe-write.h"
1N/A#else
1N/A# include "safe-read.h"
1N/A#endif
1N/A
1N/A/* Get ssize_t. */
1N/A#include <sys/types.h>
1N/A#include <unistd.h>
1N/A
1N/A#include <errno.h>
1N/A
1N/A#ifdef EINTR
1N/A# define IS_EINTR(x) ((x) == EINTR)
1N/A#else
1N/A# define IS_EINTR(x) 0
1N/A#endif
1N/A
1N/A#include <limits.h>
1N/A
1N/A#ifdef SAFE_WRITE
1N/A# define safe_rw safe_write
1N/A# define rw write
1N/A#else
1N/A# define safe_rw safe_read
1N/A# define rw read
1N/A# undef const
1N/A# define const /* empty */
1N/A#endif
1N/A
1N/A/* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if
1N/A interrupted. Return the actual number of bytes read(written), zero for EOF,
1N/A or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error. */
1N/Asize_t
1N/Asafe_rw (int fd, void const *buf, size_t count)
1N/A{
1N/A /* Work around a bug in Tru64 5.1. Attempting to read more than
1N/A INT_MAX bytes fails with errno == EINVAL. See
1N/A <http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00010.html>.
1N/A When decreasing COUNT, keep it block-aligned. */
1N/A enum { BUGGY_READ_MAXIMUM = INT_MAX & ~8191 };
1N/A
1N/A for (;;)
1N/A {
1N/A ssize_t result = rw (fd, buf, count);
1N/A
1N/A if (0 <= result)
1N/A return result;
1N/A else if (IS_EINTR (errno))
1N/A continue;
1N/A else if (errno == EINVAL && BUGGY_READ_MAXIMUM < count)
1N/A count = BUGGY_READ_MAXIMUM;
1N/A else
1N/A return result;
1N/A }
1N/A}