binary-io.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Binary mode I/O.
  2. Copyright (C) 2001, 2003, 2005, 2008-2013 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 <http://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. _GL_INLINE_HEADER_BEGIN
  28. #ifndef BINARY_IO_INLINE
  29. # define BINARY_IO_INLINE _GL_INLINE
  30. #endif
  31. /* set_binary_mode (fd, mode)
  32. sets the binary/text I/O mode of file descriptor fd to the given mode
  33. (must be O_BINARY or O_TEXT) and returns the previous mode. */
  34. #if O_BINARY
  35. # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
  36. # include <io.h> /* declares setmode() */
  37. # define set_binary_mode setmode
  38. # else
  39. # define set_binary_mode _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. set_binary_mode (int fd, int mode)
  49. {
  50. (void) fd;
  51. (void) mode;
  52. return O_BINARY;
  53. }
  54. #endif
  55. /* SET_BINARY (fd);
  56. changes the file descriptor fd to perform binary I/O. */
  57. #ifdef __DJGPP__
  58. # include <unistd.h> /* declares isatty() */
  59. /* Avoid putting stdin/stdout in binary mode if it is connected to
  60. the console, because that would make it impossible for the user
  61. to interrupt the program through Ctrl-C or Ctrl-Break. */
  62. # define SET_BINARY(fd) ((void) (!isatty (fd) ? (set_binary_mode (fd, O_BINARY), 0) : 0))
  63. #else
  64. # define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
  65. #endif
  66. _GL_INLINE_HEADER_END
  67. #endif /* _BINARY_H */