comparison lib/binary-io.h @ 16851:2ef869fa79b4

binary-io: Define set_binary_mode function. * lib/binary-io.h (set_binary_mode): New function. (SET_BINARY): Define in terms of set_binary_mode. * modules/binary-io (configure.ac): Require AC_C_INLINE. * tests/test-binary-io.c (main): Accept an argument, and test either set_binary_mode or SET_BINARY depending on the argument. * tests/test-binary-io.sh: Invoke test-binary-io twice, with an argument. Clean up also t-bin-out0.tmp.
author Bruno Haible <bruno@clisp.org>
date Sun, 13 May 2012 22:54:49 +0200
parents 8250f2777afc
children 67cb7096523b
comparison
equal deleted inserted replaced
16850:f87aab0c02ac 16851:2ef869fa79b4
23 23
24 /* The MSVC7 <stdio.h> doesn't like to be included after '#define fileno ...', 24 /* The MSVC7 <stdio.h> doesn't like to be included after '#define fileno ...',
25 so we include it here first. */ 25 so we include it here first. */
26 #include <stdio.h> 26 #include <stdio.h>
27 27
28 /* SET_BINARY (fd); 28 /* set_binary_mode (fd, mode)
29 changes the file descriptor fd to perform binary I/O. */ 29 sets the binary/text I/O mode of file descriptor fd to the given mode
30 (must be O_BINARY or O_TEXT) and returns the previous mode. */
30 #if O_BINARY 31 #if O_BINARY
31 # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__ 32 # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
32 # include <io.h> /* declares setmode() */ 33 # include <io.h> /* declares setmode() */
34 # define set_binary_mode setmode
33 # else 35 # else
34 # define setmode _setmode 36 # define set_binary_mode _setmode
35 # undef fileno 37 # undef fileno
36 # define fileno _fileno 38 # define fileno _fileno
37 # endif 39 # endif
38 # ifdef __DJGPP__
39 # include <unistd.h> /* declares isatty() */
40 /* Avoid putting stdin/stdout in binary mode if it is connected to
41 the console, because that would make it impossible for the user
42 to interrupt the program through Ctrl-C or Ctrl-Break. */
43 # define SET_BINARY(fd) ((void) (!isatty (fd) ? (setmode (fd, O_BINARY), 0) : 0))
44 # else
45 # define SET_BINARY(fd) ((void) setmode (fd, O_BINARY))
46 # endif
47 #else 40 #else
48 /* On reasonable systems, binary I/O is the default. */ 41 /* On reasonable systems, binary I/O is the only choice. */
49 # define SET_BINARY(fd) /* do nothing */ ((void) 0) 42 /* Use an inline function rather than a macro, to avoid gcc warnings
43 "warning: statement with no effect". */
44 static inline int
45 set_binary_mode (int fd, int mode)
46 {
47 (void) fd;
48 (void) mode;
49 return O_BINARY;
50 }
51 #endif
52
53 /* SET_BINARY (fd);
54 changes the file descriptor fd to perform binary I/O. */
55 #ifdef __DJGPP__
56 # include <unistd.h> /* declares isatty() */
57 /* Avoid putting stdin/stdout in binary mode if it is connected to
58 the console, because that would make it impossible for the user
59 to interrupt the program through Ctrl-C or Ctrl-Break. */
60 # define SET_BINARY(fd) ((void) (!isatty (fd) ? (set_binary_mode (fd, O_BINARY), 0) : 0))
61 #else
62 # define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
50 #endif 63 #endif
51 64
52 #endif /* _BINARY_H */ 65 #endif /* _BINARY_H */