binary-io.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 Lesser 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 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 <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. _GL_INLINE_HEADER_BEGIN
  22. #ifndef BINARY_IO_INLINE
  23. # define BINARY_IO_INLINE _GL_INLINE
  24. #endif
  25. /* set_binary_mode (fd, mode)
  26. sets the binary/text I/O mode of file descriptor fd to the given mode
  27. (must be O_BINARY or O_TEXT) and returns the previous mode. */
  28. #if O_BINARY
  29. # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
  30. # include <io.h> /* declares setmode() */
  31. # define set_binary_mode setmode
  32. # else
  33. # define set_binary_mode _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. set_binary_mode (int fd, int mode)
  43. {
  44. (void) fd;
  45. (void) mode;
  46. return O_BINARY;
  47. }
  48. #endif
  49. /* SET_BINARY (fd);
  50. changes the file descriptor fd to perform binary I/O. */
  51. #ifdef __DJGPP__
  52. # include <unistd.h> /* declares isatty() */
  53. /* Avoid putting stdin/stdout in binary mode if it is connected to
  54. the console, because that would make it impossible for the user
  55. to interrupt the program through Ctrl-C or Ctrl-Break. */
  56. # define SET_BINARY(fd) ((void) (!isatty (fd) ? (set_binary_mode (fd, O_BINARY), 0) : 0))
  57. #else
  58. # define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
  59. #endif
  60. _GL_INLINE_HEADER_END
  61. #endif /* _BINARY_H */