binary-io.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 _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. /* set_binary_mode (fd, mode)
  29. sets the binary/text I/O mode of file descriptor fd to the given mode
  30. (must be O_BINARY or O_TEXT) and returns the previous mode. */
  31. #if O_BINARY
  32. # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
  33. # include <io.h> /* declares setmode() */
  34. # define set_binary_mode setmode
  35. # else
  36. # define set_binary_mode _setmode
  37. # undef fileno
  38. # define fileno _fileno
  39. # endif
  40. #else
  41. /* On reasonable systems, binary I/O is the only choice. */
  42. /* Use a function rather than a macro, to avoid gcc warnings
  43. "warning: statement with no effect". */
  44. BINARY_IO_INLINE int
  45. set_binary_mode (int fd, int mode)
  46. {
  47. (void) fd;
  48. (void) mode;
  49. return O_BINARY;
  50. }
  51. #endif
  52. /* SET_BINARY (fd);
  53. changes the file descriptor fd to perform binary I/O. */
  54. #ifdef __DJGPP__
  55. # include <unistd.h> /* declares isatty() */
  56. /* Avoid putting stdin/stdout in binary mode if it is connected to
  57. the console, because that would make it impossible for the user
  58. to interrupt the program through Ctrl-C or Ctrl-Break. */
  59. # define SET_BINARY(fd) ((void) (!isatty (fd) ? (set_binary_mode (fd, O_BINARY), 0) : 0))
  60. #else
  61. # define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
  62. #endif
  63. _GL_INLINE_HEADER_END
  64. #endif /* _BINARY_H */