stdio: Don't break attribute 'scanf' on clang.
[gnulib.git] / lib / safe-read.c
blob613e056d71920a9d37f919e7676e50ccaeb7a4e9
1 /* An interface to read and write that retries after interrupts.
3 Copyright (C) 1993-1994, 1998, 2002-2006, 2009-2020 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 3 of the License, or
9 (at your option) 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, see <https://www.gnu.org/licenses/>. */
19 #include <config.h>
21 /* Specification. */
22 #ifdef SAFE_WRITE
23 # include "safe-write.h"
24 #else
25 # include "safe-read.h"
26 #endif
28 /* Get ssize_t. */
29 #include <sys/types.h>
30 #include <unistd.h>
32 #include <errno.h>
34 #ifdef EINTR
35 # define IS_EINTR(x) ((x) == EINTR)
36 #else
37 # define IS_EINTR(x) 0
38 #endif
40 #include "sys-limits.h"
42 #ifdef SAFE_WRITE
43 # define safe_rw safe_write
44 # define rw write
45 #else
46 # define safe_rw safe_read
47 # define rw read
48 # undef const
49 # define const /* empty */
50 #endif
52 /* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if
53 interrupted. Return the actual number of bytes read(written), zero for EOF,
54 or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error. */
55 size_t
56 safe_rw (int fd, void const *buf, size_t count)
58 for (;;)
60 ssize_t result = rw (fd, buf, count);
62 if (0 <= result)
63 return result;
64 else if (IS_EINTR (errno))
65 continue;
66 else if (errno == EINVAL && SYS_BUFSIZE_MAX < count)
67 count = SYS_BUFSIZE_MAX;
68 else
69 return result;