xref: /dflybsd-src/contrib/grep/lib/binary-io.h (revision 200fbe8d4864acebd870d2b49e6432d73728581d)
195b7b453SJohn Marino /* Binary mode I/O.
2*200fbe8dSJohn Marino    Copyright (C) 2001, 2003, 2005, 2008-2011 Free Software Foundation, Inc.
395b7b453SJohn Marino 
495b7b453SJohn Marino    This program is free software: you can redistribute it and/or modify
595b7b453SJohn Marino    it under the terms of the GNU General Public License as published by
695b7b453SJohn Marino    the Free Software Foundation; either version 3 of the License, or
795b7b453SJohn Marino    (at your option) any later version.
895b7b453SJohn Marino 
995b7b453SJohn Marino    This program is distributed in the hope that it will be useful,
1095b7b453SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1195b7b453SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1295b7b453SJohn Marino    GNU General Public License for more details.
1395b7b453SJohn Marino 
1495b7b453SJohn Marino    You should have received a copy of the GNU General Public License
1595b7b453SJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1695b7b453SJohn Marino 
1795b7b453SJohn Marino #ifndef _BINARY_H
1895b7b453SJohn Marino #define _BINARY_H
1995b7b453SJohn Marino 
2095b7b453SJohn Marino /* For systems that distinguish between text and binary I/O.
21*200fbe8dSJohn Marino    O_BINARY is guaranteed by the gnulib <fcntl.h>. */
2295b7b453SJohn Marino #include <fcntl.h>
2395b7b453SJohn Marino 
2495b7b453SJohn Marino /* The MSVC7 <stdio.h> doesn't like to be included after '#define fileno ...',
2595b7b453SJohn Marino    so we include it here first.  */
2695b7b453SJohn Marino #include <stdio.h>
2795b7b453SJohn Marino 
2895b7b453SJohn Marino /* SET_BINARY (fd);
2995b7b453SJohn Marino    changes the file descriptor fd to perform binary I/O.  */
3095b7b453SJohn Marino #if O_BINARY
3195b7b453SJohn Marino # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
3295b7b453SJohn Marino #  include <io.h> /* declares setmode() */
3395b7b453SJohn Marino # else
3495b7b453SJohn Marino #  define setmode _setmode
3595b7b453SJohn Marino #  undef fileno
3695b7b453SJohn Marino #  define fileno _fileno
3795b7b453SJohn Marino # endif
3895b7b453SJohn Marino # ifdef __DJGPP__
3995b7b453SJohn Marino #  include <unistd.h> /* declares isatty() */
4095b7b453SJohn Marino    /* Avoid putting stdin/stdout in binary mode if it is connected to
4195b7b453SJohn Marino       the console, because that would make it impossible for the user
4295b7b453SJohn Marino       to interrupt the program through Ctrl-C or Ctrl-Break.  */
4395b7b453SJohn Marino #  define SET_BINARY(fd) ((void) (!isatty (fd) ? (setmode (fd, O_BINARY), 0) : 0))
4495b7b453SJohn Marino # else
4595b7b453SJohn Marino #  define SET_BINARY(fd) ((void) setmode (fd, O_BINARY))
4695b7b453SJohn Marino # endif
4795b7b453SJohn Marino #else
4895b7b453SJohn Marino   /* On reasonable systems, binary I/O is the default.  */
4995b7b453SJohn Marino # define SET_BINARY(fd) /* do nothing */ ((void) 0)
5095b7b453SJohn Marino #endif
5195b7b453SJohn Marino 
5295b7b453SJohn Marino #endif /* _BINARY_H */
53