1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2025-08-08 17:22:05 +03:00

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.
This commit is contained in:
Bruno Haible
2012-05-13 22:54:49 +02:00
parent 81e2fe4047
commit 290f951644
5 changed files with 65 additions and 23 deletions

View File

@@ -1,3 +1,15 @@
2012-05-13 Bruno Haible <bruno@clisp.org>
Paul Eggert <eggert@cs.ucla.edu>
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.
2012-05-13 Stefano Lattarini <stefano.lattarini@gmail.com> 2012-05-13 Stefano Lattarini <stefano.lattarini@gmail.com>
bootstrap: take advantage of POSIX shell features bootstrap: take advantage of POSIX shell features

View File

@@ -25,28 +25,41 @@
so we include it here first. */ so we include it here first. */
#include <stdio.h> #include <stdio.h>
/* SET_BINARY (fd); /* set_binary_mode (fd, mode)
changes the file descriptor fd to perform binary I/O. */ sets the binary/text I/O mode of file descriptor fd to the given mode
(must be O_BINARY or O_TEXT) and returns the previous mode. */
#if O_BINARY #if O_BINARY
# if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__ # if defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
# include <io.h> /* declares setmode() */ # include <io.h> /* declares setmode() */
# define set_binary_mode setmode
# else # else
# define setmode _setmode # define set_binary_mode _setmode
# undef fileno # undef fileno
# define fileno _fileno # define fileno _fileno
# endif # endif
# ifdef __DJGPP__ #else
/* On reasonable systems, binary I/O is the only choice. */
/* Use an inline function rather than a macro, to avoid gcc warnings
"warning: statement with no effect". */
static inline int
set_binary_mode (int fd, int mode)
{
(void) fd;
(void) mode;
return O_BINARY;
}
#endif
/* SET_BINARY (fd);
changes the file descriptor fd to perform binary I/O. */
#ifdef __DJGPP__
# include <unistd.h> /* declares isatty() */ # include <unistd.h> /* declares isatty() */
/* Avoid putting stdin/stdout in binary mode if it is connected to /* Avoid putting stdin/stdout in binary mode if it is connected to
the console, because that would make it impossible for the user the console, because that would make it impossible for the user
to interrupt the program through Ctrl-C or Ctrl-Break. */ to interrupt the program through Ctrl-C or Ctrl-Break. */
# define SET_BINARY(fd) ((void) (!isatty (fd) ? (setmode (fd, O_BINARY), 0) : 0)) # define SET_BINARY(fd) ((void) (!isatty (fd) ? (set_binary_mode (fd, O_BINARY), 0) : 0))
# else
# define SET_BINARY(fd) ((void) setmode (fd, O_BINARY))
# endif
#else #else
/* On reasonable systems, binary I/O is the default. */ # define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
# define SET_BINARY(fd) /* do nothing */ ((void) 0)
#endif #endif
#endif /* _BINARY_H */ #endif /* _BINARY_H */

View File

@@ -8,6 +8,7 @@ Depends-on:
fcntl-h fcntl-h
configure.ac: configure.ac:
AC_REQUIRE([AC_C_INLINE])
Makefile.am: Makefile.am:
lib_SOURCES += binary-io.h lib_SOURCES += binary-io.h

View File

@@ -30,26 +30,40 @@
#include "macros.h" #include "macros.h"
int int
main () main (int argc, char *argv[])
{ {
/* Test the O_BINARY macro. */ /* Test the O_BINARY macro. */
{ {
int fd = int fd =
open ("t-bin-out2.tmp", O_CREAT | O_TRUNC | O_RDWR | O_BINARY, 0600); open ("t-bin-out0.tmp", O_CREAT | O_TRUNC | O_RDWR | O_BINARY, 0600);
if (write (fd, "Hello\n", 6) < 0) if (write (fd, "Hello\n", 6) < 0)
exit (1); exit (1);
close (fd); close (fd);
} }
{ {
struct stat statbuf; struct stat statbuf;
if (stat ("t-bin-out2.tmp", &statbuf) < 0) if (stat ("t-bin-out0.tmp", &statbuf) < 0)
exit (1); exit (1);
ASSERT (statbuf.st_size == 6); ASSERT (statbuf.st_size == 6);
} }
switch (argv[1][0])
{
case '1':
/* Test the set_binary_mode() function. */
set_binary_mode (1, O_BINARY);
fputs ("Hello\n", stdout);
break;
case '2':
/* Test the SET_BINARY macro. */ /* Test the SET_BINARY macro. */
SET_BINARY (1); SET_BINARY (1);
fputs ("Hello\n", stdout); fputs ("Hello\n", stdout);
break;
default:
break;
}
return 0; return 0;
} }

View File

@@ -3,9 +3,11 @@
tmpfiles="" tmpfiles=""
trap 'rm -fr $tmpfiles' 1 2 3 15 trap 'rm -fr $tmpfiles' 1 2 3 15
tmpfiles="$tmpfiles t-bin-out1.tmp t-bin-out2.tmp" tmpfiles="$tmpfiles t-bin-out0.tmp t-bin-out1.tmp t-bin-out2.tmp"
./test-binary-io${EXEEXT} > t-bin-out1.tmp || exit 1 ./test-binary-io${EXEEXT} 1 > t-bin-out1.tmp || exit 1
cmp t-bin-out1.tmp t-bin-out2.tmp > /dev/null || exit 1 cmp t-bin-out0.tmp t-bin-out1.tmp > /dev/null || exit 1
./test-binary-io${EXEEXT} 2 > t-bin-out2.tmp || exit 1
cmp t-bin-out0.tmp t-bin-out2.tmp > /dev/null || exit 1
rm -fr $tmpfiles rm -fr $tmpfiles