xref: /dflybsd-src/contrib/grep/lib/binary-io.h (revision a8597f6c2834b9c79a5491bc214978eb0de0c0ee)
195b7b453SJohn Marino /* Binary mode I/O.
2cf28ed85SJohn Marino    Copyright (C) 2001, 2003, 2005, 2008-2012 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.
21200fbe8dSJohn 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 
28*a8597f6cSJohn Marino /* set_binary_mode (fd, mode)
29*a8597f6cSJohn Marino    sets the binary/text I/O mode of file descriptor fd to the given mode
30*a8597f6cSJohn Marino    (must be O_BINARY or O_TEXT) and returns the previous mode.  */
3195b7b453SJohn Marino #if O_BINARY
3295b7b453SJohn Marino # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
3395b7b453SJohn Marino #  include <io.h> /* declares setmode() */
34*a8597f6cSJohn Marino #  define set_binary_mode setmode
3595b7b453SJohn Marino # else
36*a8597f6cSJohn Marino #  define set_binary_mode _setmode
3795b7b453SJohn Marino #  undef fileno
3895b7b453SJohn Marino #  define fileno _fileno
3995b7b453SJohn Marino # endif
40*a8597f6cSJohn Marino #else
41*a8597f6cSJohn Marino   /* On reasonable systems, binary I/O is the only choice.  */
42*a8597f6cSJohn Marino   /* Use an inline function rather than a macro, to avoid gcc warnings
43*a8597f6cSJohn Marino      "warning: statement with no effect".  */
44*a8597f6cSJohn Marino static inline int
45*a8597f6cSJohn Marino set_binary_mode (int fd, int mode)
46*a8597f6cSJohn Marino {
47*a8597f6cSJohn Marino   (void) fd;
48*a8597f6cSJohn Marino   (void) mode;
49*a8597f6cSJohn Marino   return O_BINARY;
50*a8597f6cSJohn Marino }
51*a8597f6cSJohn Marino #endif
52*a8597f6cSJohn Marino 
53*a8597f6cSJohn Marino /* SET_BINARY (fd);
54*a8597f6cSJohn Marino    changes the file descriptor fd to perform binary I/O.  */
5595b7b453SJohn Marino #ifdef __DJGPP__
5695b7b453SJohn Marino # include <unistd.h> /* declares isatty() */
5795b7b453SJohn Marino   /* Avoid putting stdin/stdout in binary mode if it is connected to
5895b7b453SJohn Marino      the console, because that would make it impossible for the user
5995b7b453SJohn Marino      to interrupt the program through Ctrl-C or Ctrl-Break.  */
60*a8597f6cSJohn Marino # define SET_BINARY(fd) ((void) (!isatty (fd) ? (set_binary_mode (fd, O_BINARY), 0) : 0))
6195b7b453SJohn Marino #else
62*a8597f6cSJohn Marino # define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
6395b7b453SJohn Marino #endif
6495b7b453SJohn Marino 
6595b7b453SJohn Marino #endif /* _BINARY_H */
66