binary-io.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Binary mode I/O.
  2. Copyright (C) 2001, 2003, 2005, 2008-2022 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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 _GL_INLINE_HEADER_BEGIN
  22. #error "Please include config.h first."
  23. #endif
  24. _GL_INLINE_HEADER_BEGIN
  25. #ifndef BINARY_IO_INLINE
  26. # define BINARY_IO_INLINE _GL_INLINE
  27. #endif
  28. #if O_BINARY
  29. # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
  30. # include <io.h> /* declares setmode() */
  31. # define __gl_setmode setmode
  32. # else
  33. # define __gl_setmode _setmode
  34. # undef fileno
  35. # define fileno _fileno
  36. # endif
  37. #else
  38. /* On reasonable systems, binary I/O is the only choice. */
  39. /* Use a function rather than a macro, to avoid gcc warnings
  40. "warning: statement with no effect". */
  41. BINARY_IO_INLINE int
  42. __gl_setmode (_GL_UNUSED int fd, _GL_UNUSED int mode)
  43. {
  44. return O_BINARY;
  45. }
  46. #endif
  47. /* Set FD's mode to MODE, which should be either O_TEXT or O_BINARY.
  48. Return the old mode if successful, -1 (setting errno) on failure.
  49. Ordinarily this function would be called 'setmode', since that is
  50. its old name on MS-Windows, but it is called 'set_binary_mode' here
  51. to avoid colliding with a BSD function of another name. */
  52. #if defined __DJGPP__ || defined __EMX__
  53. extern int set_binary_mode (int fd, int mode);
  54. #else
  55. BINARY_IO_INLINE int
  56. set_binary_mode (int fd, int mode)
  57. {
  58. return __gl_setmode (fd, mode);
  59. }
  60. #endif
  61. /* This macro is obsolescent. */
  62. #define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
  63. _GL_INLINE_HEADER_END
  64. #endif /* _BINARY_H */