binary-io.h 2.5 KB

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