binary-io.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Binary mode I/O.
  2. Copyright (C) 2001, 2003, 2005, 2008-2012 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. /* SET_BINARY (fd);
  22. changes the file descriptor fd to perform binary I/O. */
  23. #if O_BINARY
  24. # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
  25. # include <io.h> /* declares setmode() */
  26. # else
  27. # define setmode _setmode
  28. # undef fileno
  29. # define fileno _fileno
  30. # endif
  31. # ifdef __DJGPP__
  32. # include <unistd.h> /* declares isatty() */
  33. /* Avoid putting stdin/stdout in binary mode if it is connected to
  34. the console, because that would make it impossible for the user
  35. to interrupt the program through Ctrl-C or Ctrl-Break. */
  36. # define SET_BINARY(fd) ((void) (!isatty (fd) ? (setmode (fd, O_BINARY), 0) : 0))
  37. # else
  38. # define SET_BINARY(fd) ((void) setmode (fd, O_BINARY))
  39. # endif
  40. #else
  41. /* On reasonable systems, binary I/O is the default. */
  42. # define SET_BINARY(fd) /* do nothing */ ((void) 0)
  43. #endif
  44. #endif /* _BINARY_H */