Merge from gnulib

This incorporates:
2017-02-16 xbinary-io: rename from xsetmode
2017-02-15 xsetmode: new module
* lib-src/etags.c (main):
* lib-src/hexl.c (main):
* src/emacs.c (main) [MSDOS]:
Prefer set_binary_mode to the obsolescent SET_BINARY.
* lib/binary-io.c, lib/binary-io.h: Copy from gnulib.
This commit is contained in:
Paul Eggert 2017-02-23 09:14:06 -08:00
parent 23e64facf9
commit 7204577bf9
5 changed files with 61 additions and 20 deletions

View file

@ -1255,7 +1255,7 @@ main (int argc, char **argv)
if (streq (tagfile, "-"))
{
tagf = stdout;
SET_BINARY (fileno (stdout));
set_binary_mode (STDOUT_FILENO, O_BINARY);
}
else
tagf = fopen (tagfile, append_to_tagfile ? "ab" : "wb");

View file

@ -76,7 +76,7 @@ main (int argc, char **argv)
else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
{
un_flag = true;
SET_BINARY (fileno (stdout));
set_binary_mode (fileno (stdout), O_BINARY);
}
else if (!strcmp (*argv, "-hex"))
/* Hex is the default and is only base supported. */;
@ -109,7 +109,7 @@ main (int argc, char **argv)
{
fp = stdin;
if (!un_flag)
SET_BINARY (fileno (stdin));
set_binary_mode (fileno (stdin), O_BINARY);
}
else
{

View file

@ -1,4 +1,37 @@
/* Binary mode I/O.
Copyright 2017 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#define BINARY_IO_INLINE _GL_EXTERN_INLINE
#include "binary-io.h"
typedef int dummy;
#if defined __DJGPP__ || defined __EMX__
# include <errno.h>
# include <unistd.h>
int
__gl_setmode_check (int fd)
{
if (isatty (fd))
{
errno = EINVAL;
return -1;
}
else
return 0;
}
#endif

View file

@ -33,15 +33,12 @@ _GL_INLINE_HEADER_BEGIN
# define BINARY_IO_INLINE _GL_INLINE
#endif
/* set_binary_mode (fd, mode)
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 defined __EMX__ || defined __DJGPP__ || defined __CYGWIN__
# include <io.h> /* declares setmode() */
# define set_binary_mode setmode
# define __gl_setmode setmode
# else
# define set_binary_mode _setmode
# define __gl_setmode _setmode
# undef fileno
# define fileno _fileno
# endif
@ -50,7 +47,7 @@ _GL_INLINE_HEADER_BEGIN
/* Use a function rather than a macro, to avoid gcc warnings
"warning: statement with no effect". */
BINARY_IO_INLINE int
set_binary_mode (int fd, int mode)
__gl_setmode (int fd, int mode)
{
(void) fd;
(void) mode;
@ -58,18 +55,29 @@ set_binary_mode (int fd, int mode)
}
#endif
/* SET_BINARY (fd);
changes the file descriptor fd to perform binary I/O. */
#if defined __DJGPP__ || defined __EMX__
# include <unistd.h> /* declares isatty() */
/* Avoid putting stdin/stdout in binary mode if it is connected to
the console, because that would make it impossible for the user
to interrupt the program through Ctrl-C or Ctrl-Break. */
# define SET_BINARY(fd) ((void) (!isatty (fd) ? (set_binary_mode (fd, O_BINARY), 0) : 0))
extern int __gl_setmode_check (int);
#else
# define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
BINARY_IO_INLINE int
__gl_setmode_check (int fd) { return 0; }
#endif
/* Set FD's mode to MODE, which should be either O_TEXT or O_BINARY.
Return the old mode if successful, -1 (setting errno) on failure.
Ordinarily this function would be called 'setmode', since that is
its name on MS-Windows, but it is called 'set_binary_mode' here
to avoid colliding with a BSD function of another name. */
BINARY_IO_INLINE int
set_binary_mode (int fd, int mode)
{
int r = __gl_setmode_check (fd);
return r != 0 ? r : __gl_setmode (fd, mode);
}
/* This macro is obsolescent. */
#define SET_BINARY(fd) ((void) set_binary_mode (fd, O_BINARY))
_GL_INLINE_HEADER_END
#endif /* _BINARY_H */

View file

@ -894,9 +894,9 @@ main (int argc, char **argv)
#endif /* not SYSTEM_MALLOC and not HYBRID_MALLOC */
#ifdef MSDOS
SET_BINARY (fileno (stdin));
set_binary_mode (STDIN_FILENO, O_BINARY);
fflush (stdout);
SET_BINARY (fileno (stdout));
set_binary_mode (STDOUT_FILENO, O_BINARY);
#endif /* MSDOS */
/* Skip initial setlocale if LC_ALL is "C", as it's not needed in that case.