Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / safe-read.c
blob6f8bd780ed01a25da8452bb0b77a95eb302fbbc6
1 /* An interface to read and write that retries after interrupts.
3 Copyright (C) 1993, 1994, 1998, 2002, 2003, 2004 Free Software
4 Foundation, Inc.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 /* Specification. */
25 #ifdef SAFE_WRITE
26 # include "safe-write.h"
27 #else
28 # include "safe-read.h"
29 #endif
31 /* Get ssize_t. */
32 #include <sys/types.h>
33 #if HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
37 #include <errno.h>
39 #ifdef EINTR
40 # define IS_EINTR(x) ((x) == EINTR)
41 #else
42 # define IS_EINTR(x) 0
43 #endif
45 #include <limits.h>
47 #ifdef SAFE_WRITE
48 # define safe_rw safe_write
49 # define rw write
50 #else
51 # define safe_rw safe_read
52 # define rw read
53 # undef const
54 # define const /* empty */
55 #endif
57 /* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if
58 interrupted. Return the actual number of bytes read(written), zero for EOF,
59 or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error. */
60 size_t
61 safe_rw (int fd, void const *buf, size_t count)
63 /* Work around a bug in Tru64 5.1. Attempting to read more than
64 INT_MAX bytes fails with errno == EINVAL. See
65 <http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00010.html>.
66 When decreasing COUNT, keep it block-aligned. */
67 enum { BUGGY_READ_MAXIMUM = INT_MAX & ~8191 };
69 for (;;)
71 ssize_t result = rw (fd, buf, count);
73 if (0 <= result)
74 return result;
75 else if (IS_EINTR (errno))
76 continue;
77 else if (errno == EINVAL && BUGGY_READ_MAXIMUM < count)
78 count = BUGGY_READ_MAXIMUM;
79 else
80 return result;