2003-01-07 22:03:15 +08:00
|
|
|
/* An interface to read and write that retries (if necessary) until complete.
|
2001-08-31 15:29:05 +08:00
|
|
|
|
2004-08-03 06:47:00 +08:00
|
|
|
Copyright (C) 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
|
|
|
|
2004 Free Software Foundation, Inc.
|
1993-12-29 14:13:37 +08:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1996-07-14 20:35:45 +08:00
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
2005-05-14 15:58:06 +08:00
|
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
1993-12-29 14:13:37 +08:00
|
|
|
|
2005-09-22 14:05:39 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
1996-07-14 20:35:45 +08:00
|
|
|
# include <config.h>
|
1993-12-29 14:13:37 +08:00
|
|
|
#endif
|
|
|
|
|
2003-08-18 17:44:49 +08:00
|
|
|
/* Specification. */
|
|
|
|
#ifdef FULL_READ
|
|
|
|
# include "full-read.h"
|
|
|
|
#else
|
|
|
|
# include "full-write.h"
|
|
|
|
#endif
|
|
|
|
|
2003-01-09 01:36:35 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2003-01-07 22:03:15 +08:00
|
|
|
#ifdef FULL_READ
|
|
|
|
# include "safe-read.h"
|
|
|
|
# define safe_rw safe_read
|
|
|
|
# define full_rw full_read
|
|
|
|
# undef const
|
|
|
|
# define const /* empty */
|
|
|
|
#else
|
|
|
|
# include "safe-write.h"
|
|
|
|
# define safe_rw safe_write
|
|
|
|
# define full_rw full_write
|
|
|
|
#endif
|
2001-08-31 15:29:05 +08:00
|
|
|
|
2003-01-07 22:03:15 +08:00
|
|
|
#ifdef FULL_READ
|
|
|
|
/* Set errno to zero upon EOF. */
|
|
|
|
# define ZERO_BYTE_TRANSFER_ERRNO 0
|
|
|
|
#else
|
|
|
|
/* Some buggy drivers return 0 when one tries to write beyond
|
|
|
|
a device's end. (Example: Linux 1.2.13 on /dev/fd0.)
|
|
|
|
Set errno to ENOSPC so they get a sensible diagnostic. */
|
|
|
|
# define ZERO_BYTE_TRANSFER_ERRNO ENOSPC
|
|
|
|
#endif
|
1993-12-29 14:13:37 +08:00
|
|
|
|
2003-01-07 22:03:15 +08:00
|
|
|
/* Write(read) COUNT bytes at BUF to(from) descriptor FD, retrying if
|
|
|
|
interrupted or if a partial write(read) occurs. Return the number
|
|
|
|
of bytes transferred.
|
|
|
|
When writing, set errno if fewer than COUNT bytes are written.
|
|
|
|
When reading, if fewer than COUNT bytes are read, you must examine
|
|
|
|
errno to distinguish failure from EOF (errno == 0). */
|
2001-08-31 15:29:05 +08:00
|
|
|
size_t
|
2003-01-07 22:03:15 +08:00
|
|
|
full_rw (int fd, const void *buf, size_t count)
|
1993-12-29 14:13:37 +08:00
|
|
|
{
|
2003-01-07 22:03:15 +08:00
|
|
|
size_t total = 0;
|
2002-12-06 22:24:22 +08:00
|
|
|
const char *ptr = buf;
|
1994-01-27 02:20:04 +08:00
|
|
|
|
2002-12-06 22:24:22 +08:00
|
|
|
while (count > 0)
|
1993-12-29 14:13:37 +08:00
|
|
|
{
|
2003-01-07 22:03:15 +08:00
|
|
|
size_t n_rw = safe_rw (fd, ptr, count);
|
|
|
|
if (n_rw == (size_t) -1)
|
2002-12-06 22:24:22 +08:00
|
|
|
break;
|
2003-01-07 22:03:15 +08:00
|
|
|
if (n_rw == 0)
|
1993-12-29 14:13:37 +08:00
|
|
|
{
|
2003-01-07 22:03:15 +08:00
|
|
|
errno = ZERO_BYTE_TRANSFER_ERRNO;
|
2001-08-31 15:29:05 +08:00
|
|
|
break;
|
1993-12-29 14:13:37 +08:00
|
|
|
}
|
2003-01-07 22:03:15 +08:00
|
|
|
total += n_rw;
|
|
|
|
ptr += n_rw;
|
|
|
|
count -= n_rw;
|
1993-12-29 14:13:37 +08:00
|
|
|
}
|
2002-12-06 22:24:22 +08:00
|
|
|
|
2003-01-07 22:03:15 +08:00
|
|
|
return total;
|
1993-12-29 14:13:37 +08:00
|
|
|
}
|