xref: /dflybsd-src/contrib/grep/lib/safe-read.h (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
1680a9cb8SJohn Marino /* An interface to read() that retries after interrupts.
2*09d4459fSDaniel Fojt    Copyright (C) 2002, 2006, 2009-2020 Free Software Foundation, Inc.
3680a9cb8SJohn Marino 
4680a9cb8SJohn Marino    This program is free software: you can redistribute it and/or modify
5680a9cb8SJohn Marino    it under the terms of the GNU General Public License as published by
6680a9cb8SJohn Marino    the Free Software Foundation; either version 3 of the License, or
7680a9cb8SJohn Marino    (at your option) any later version.
8680a9cb8SJohn Marino 
9680a9cb8SJohn Marino    This program is distributed in the hope that it will be useful,
10680a9cb8SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
11680a9cb8SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12680a9cb8SJohn Marino    GNU General Public License for more details.
13680a9cb8SJohn Marino 
14680a9cb8SJohn Marino    You should have received a copy of the GNU General Public License
15*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16680a9cb8SJohn Marino 
17680a9cb8SJohn Marino /* Some system calls may be interrupted and fail with errno = EINTR in the
18680a9cb8SJohn Marino    following situations:
19680a9cb8SJohn Marino      - The process is stopped and restarted (signal SIGSTOP and SIGCONT, user
20680a9cb8SJohn Marino        types Ctrl-Z) on some platforms: Mac OS X.
21680a9cb8SJohn Marino      - The process receives a signal for which a signal handler was installed
22680a9cb8SJohn Marino        with sigaction() with an sa_flags field that does not contain
23680a9cb8SJohn Marino        SA_RESTART.
24680a9cb8SJohn Marino      - The process receives a signal for which a signal handler was installed
25680a9cb8SJohn Marino        with signal() and for which no call to siginterrupt(sig,0) was done,
26680a9cb8SJohn Marino        on some platforms: AIX, HP-UX, IRIX, OSF/1, Solaris.
27680a9cb8SJohn Marino 
28680a9cb8SJohn Marino    This module provides a wrapper around read() that handles EINTR.  */
29680a9cb8SJohn Marino 
30680a9cb8SJohn Marino #include <stddef.h>
31680a9cb8SJohn Marino 
32680a9cb8SJohn Marino #ifdef __cplusplus
33680a9cb8SJohn Marino extern "C" {
34680a9cb8SJohn Marino #endif
35680a9cb8SJohn Marino 
36680a9cb8SJohn Marino 
37680a9cb8SJohn Marino #define SAFE_READ_ERROR ((size_t) -1)
38680a9cb8SJohn Marino 
39680a9cb8SJohn Marino /* Read up to COUNT bytes at BUF from descriptor FD, retrying if interrupted.
40680a9cb8SJohn Marino    Return the actual number of bytes read, zero for EOF, or SAFE_READ_ERROR
41680a9cb8SJohn Marino    upon error.  */
42680a9cb8SJohn Marino extern size_t safe_read (int fd, void *buf, size_t count);
43680a9cb8SJohn Marino 
44680a9cb8SJohn Marino 
45680a9cb8SJohn Marino #ifdef __cplusplus
46680a9cb8SJohn Marino }
47680a9cb8SJohn Marino #endif
48