xref: /dflybsd-src/contrib/grep/lib/binary-io.h (revision 95b7b45377d922df3f9cbf9a1f160c77aa3555fa)
1*95b7b453SJohn Marino /* Binary mode I/O.
2*95b7b453SJohn Marino    Copyright (C) 2001, 2003, 2005, 2008, 2009, 2010 Free Software Foundation,
3*95b7b453SJohn Marino    Inc.
4*95b7b453SJohn Marino 
5*95b7b453SJohn Marino    This program is free software: you can redistribute it and/or modify
6*95b7b453SJohn Marino    it under the terms of the GNU General Public License as published by
7*95b7b453SJohn Marino    the Free Software Foundation; either version 3 of the License, or
8*95b7b453SJohn Marino    (at your option) any later version.
9*95b7b453SJohn Marino 
10*95b7b453SJohn Marino    This program is distributed in the hope that it will be useful,
11*95b7b453SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*95b7b453SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*95b7b453SJohn Marino    GNU General Public License for more details.
14*95b7b453SJohn Marino 
15*95b7b453SJohn Marino    You should have received a copy of the GNU General Public License
16*95b7b453SJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17*95b7b453SJohn Marino 
18*95b7b453SJohn Marino #ifndef _BINARY_H
19*95b7b453SJohn Marino #define _BINARY_H
20*95b7b453SJohn Marino 
21*95b7b453SJohn Marino /* For systems that distinguish between text and binary I/O.
22*95b7b453SJohn Marino    O_BINARY is usually declared in <fcntl.h>. */
23*95b7b453SJohn Marino #include <fcntl.h>
24*95b7b453SJohn Marino 
25*95b7b453SJohn Marino /* The MSVC7 <stdio.h> doesn't like to be included after '#define fileno ...',
26*95b7b453SJohn Marino    so we include it here first.  */
27*95b7b453SJohn Marino #include <stdio.h>
28*95b7b453SJohn Marino 
29*95b7b453SJohn Marino #if !defined O_BINARY && defined _O_BINARY
30*95b7b453SJohn Marino   /* For MSC-compatible compilers.  */
31*95b7b453SJohn Marino # define O_BINARY _O_BINARY
32*95b7b453SJohn Marino # define O_TEXT _O_TEXT
33*95b7b453SJohn Marino #endif
34*95b7b453SJohn Marino #if defined __BEOS__ || defined __HAIKU__
35*95b7b453SJohn Marino   /* BeOS 5 and Haiku have O_BINARY and O_TEXT, but they have no effect.  */
36*95b7b453SJohn Marino # undef O_BINARY
37*95b7b453SJohn Marino # undef O_TEXT
38*95b7b453SJohn Marino #endif
39*95b7b453SJohn Marino 
40*95b7b453SJohn Marino /* SET_BINARY (fd);
41*95b7b453SJohn Marino    changes the file descriptor fd to perform binary I/O.  */
42*95b7b453SJohn Marino #if O_BINARY
43*95b7b453SJohn Marino # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
44*95b7b453SJohn Marino #  include <io.h> /* declares setmode() */
45*95b7b453SJohn Marino # else
46*95b7b453SJohn Marino #  define setmode _setmode
47*95b7b453SJohn Marino #  undef fileno
48*95b7b453SJohn Marino #  define fileno _fileno
49*95b7b453SJohn Marino # endif
50*95b7b453SJohn Marino # ifdef __DJGPP__
51*95b7b453SJohn Marino #  include <unistd.h> /* declares isatty() */
52*95b7b453SJohn Marino    /* Avoid putting stdin/stdout in binary mode if it is connected to
53*95b7b453SJohn Marino       the console, because that would make it impossible for the user
54*95b7b453SJohn Marino       to interrupt the program through Ctrl-C or Ctrl-Break.  */
55*95b7b453SJohn Marino #  define SET_BINARY(fd) ((void) (!isatty (fd) ? (setmode (fd, O_BINARY), 0) : 0))
56*95b7b453SJohn Marino # else
57*95b7b453SJohn Marino #  define SET_BINARY(fd) ((void) setmode (fd, O_BINARY))
58*95b7b453SJohn Marino # endif
59*95b7b453SJohn Marino #else
60*95b7b453SJohn Marino   /* On reasonable systems, binary I/O is the default.  */
61*95b7b453SJohn Marino # undef O_BINARY
62*95b7b453SJohn Marino # define O_BINARY 0
63*95b7b453SJohn Marino # define SET_BINARY(fd) /* do nothing */ ((void) 0)
64*95b7b453SJohn Marino #endif
65*95b7b453SJohn Marino 
66*95b7b453SJohn Marino #endif /* _BINARY_H */
67