195b7b453SJohn Marino /* Binary mode I/O. 2*680a9cb8SJohn Marino Copyright (C) 2001, 2003, 2005, 2008-2014 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*680a9cb8SJohn Marino #ifndef _GL_INLINE_HEADER_BEGIN 29*680a9cb8SJohn Marino #error "Please include config.h first." 30*680a9cb8SJohn Marino #endif 31*680a9cb8SJohn Marino _GL_INLINE_HEADER_BEGIN 32*680a9cb8SJohn Marino #ifndef BINARY_IO_INLINE 33*680a9cb8SJohn Marino # define BINARY_IO_INLINE _GL_INLINE 34*680a9cb8SJohn Marino #endif 35*680a9cb8SJohn Marino 36a8597f6cSJohn Marino /* set_binary_mode (fd, mode) 37a8597f6cSJohn Marino sets the binary/text I/O mode of file descriptor fd to the given mode 38a8597f6cSJohn Marino (must be O_BINARY or O_TEXT) and returns the previous mode. */ 3995b7b453SJohn Marino #if O_BINARY 4095b7b453SJohn Marino # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__ 4195b7b453SJohn Marino # include <io.h> /* declares setmode() */ 42a8597f6cSJohn Marino # define set_binary_mode setmode 4395b7b453SJohn Marino # else 44a8597f6cSJohn Marino # define set_binary_mode _setmode 4595b7b453SJohn Marino # undef fileno 4695b7b453SJohn Marino # define fileno _fileno 4795b7b453SJohn Marino # endif 48a8597f6cSJohn Marino #else 49a8597f6cSJohn Marino /* On reasonable systems, binary I/O is the only choice. */ 50*680a9cb8SJohn Marino /* Use a function rather than a macro, to avoid gcc warnings 51a8597f6cSJohn Marino "warning: statement with no effect". */ 52*680a9cb8SJohn Marino BINARY_IO_INLINE int 53a8597f6cSJohn Marino set_binary_mode (int fd, int mode) 54a8597f6cSJohn Marino { 55a8597f6cSJohn Marino (void) fd; 56a8597f6cSJohn Marino (void) mode; 57a8597f6cSJohn Marino return O_BINARY; 58a8597f6cSJohn Marino } 59a8597f6cSJohn Marino #endif 60a8597f6cSJohn Marino 61a8597f6cSJohn Marino /* SET_BINARY (fd); 62a8597f6cSJohn Marino changes the file descriptor fd to perform binary I/O. */ 6395b7b453SJohn Marino #ifdef __DJGPP__ 6495b7b453SJohn Marino # include <unistd.h> /* declares isatty() */ 6595b7b453SJohn Marino /* Avoid putting stdin/stdout in binary mode if it is connected to 6695b7b453SJohn Marino the console, because that would make it impossible for the user 6795b7b453SJohn Marino to interrupt the program through Ctrl-C or Ctrl-Break. */ 68a8597f6cSJohn Marino # define SET_BINARY(fd) ((void) (!isatty (fd) ? (set_binary_mode (fd, O_BINARY), 0) : 0)) 6995b7b453SJohn Marino #else 70a8597f6cSJohn Marino # define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY)) 7195b7b453SJohn Marino #endif 7295b7b453SJohn Marino 73*680a9cb8SJohn Marino _GL_INLINE_HEADER_END 74*680a9cb8SJohn Marino 7595b7b453SJohn Marino #endif /* _BINARY_H */ 76