openat: don’t close (-1)
[gnulib.git] / lib / binary-io.h
blob1da018fad85669fda555a1d04929f5a7dfad375a
1 /* Binary mode I/O.
2 Copyright (C) 2001, 2003, 2005, 2008-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #ifndef _BINARY_H
18 #define _BINARY_H
20 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_UNUSED. */
21 #if !_GL_CONFIG_H_INCLUDED
22 #error "Please include config.h first."
23 #endif
25 /* For systems that distinguish between text and binary I/O.
26 O_BINARY is guaranteed by the gnulib <fcntl.h>. */
27 #include <fcntl.h>
29 /* The MSVC7 <stdio.h> doesn't like to be included after '#define fileno ...',
30 so we include it here first. */
31 #include <stdio.h>
33 _GL_INLINE_HEADER_BEGIN
34 #ifndef BINARY_IO_INLINE
35 # define BINARY_IO_INLINE _GL_INLINE
36 #endif
38 #if O_BINARY
39 # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
40 # include <io.h> /* declares setmode() */
41 # define __gl_setmode setmode
42 # else
43 # define __gl_setmode _setmode
44 # undef fileno
45 # define fileno _fileno
46 # endif
47 #else
48 /* On reasonable systems, binary I/O is the only choice. */
49 /* Use a function rather than a macro, to avoid gcc warnings
50 "warning: statement with no effect". */
51 BINARY_IO_INLINE int
52 __gl_setmode (_GL_UNUSED int fd, _GL_UNUSED int mode)
54 return O_BINARY;
56 #endif
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
63 /* Set FD's mode to MODE, which should be either O_TEXT or O_BINARY.
64 Return the old mode if successful, -1 (setting errno) on failure.
65 Ordinarily this function would be called 'setmode', since that is
66 its old name on MS-Windows, but it is called 'set_binary_mode' here
67 to avoid colliding with a BSD function of another name. */
69 #if defined __DJGPP__ || defined __EMX__
70 extern int set_binary_mode (int fd, int mode);
71 #else
72 BINARY_IO_INLINE int
73 set_binary_mode (int fd, int mode)
75 return __gl_setmode (fd, mode);
77 #endif
79 /* This macro is obsolescent. */
80 #define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
83 #ifdef __cplusplus
85 #endif
87 _GL_INLINE_HEADER_END
89 #endif /* _BINARY_H */