xref: /netbsd-src/external/gpl2/diffutils/dist/lib/setmode.c (revision 75f6d617e282811cb173c2ccfbf5df0dd71f7045)
1*75f6d617Schristos /*	$NetBSD: setmode.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2*75f6d617Schristos 
3*75f6d617Schristos /* Set a file descriptor's mode to binary or to text.
4*75f6d617Schristos 
5*75f6d617Schristos    Copyright (C) 2001 Free Software Foundation, Inc.
6*75f6d617Schristos 
7*75f6d617Schristos    This program is free software; you can redistribute it and/or modify
8*75f6d617Schristos    it under the terms of the GNU General Public License as published by
9*75f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
10*75f6d617Schristos    any later version.
11*75f6d617Schristos 
12*75f6d617Schristos    This program is distributed in the hope that it will be useful,
13*75f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*75f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*75f6d617Schristos    GNU General Public License for more details.
16*75f6d617Schristos 
17*75f6d617Schristos    You should have received a copy of the GNU General Public License
18*75f6d617Schristos    along with this program; see the file COPYING.
19*75f6d617Schristos    If not, write to the Free Software Foundation,
20*75f6d617Schristos    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21*75f6d617Schristos 
22*75f6d617Schristos /* Written by Paul Eggert <eggert@twinsun.com>  */
23*75f6d617Schristos 
24*75f6d617Schristos #if HAVE_CONFIG_H
25*75f6d617Schristos # include <config.h>
26*75f6d617Schristos #endif
27*75f6d617Schristos 
28*75f6d617Schristos #if HAVE_STDBOOL_H
29*75f6d617Schristos # include <stdbool.h>
30*75f6d617Schristos #else
31*75f6d617Schristos typedef enum {false = 0, true = 1} bool;
32*75f6d617Schristos #endif
33*75f6d617Schristos 
34*75f6d617Schristos #if HAVE_SETMODE_DOS
35*75f6d617Schristos # include <io.h>
36*75f6d617Schristos # if HAVE_FCNTL_H
37*75f6d617Schristos #  include <fcntl.h>
38*75f6d617Schristos # endif
39*75f6d617Schristos # if HAVE_UNISTD_H
40*75f6d617Schristos #  include <unistd.h>
41*75f6d617Schristos # endif
42*75f6d617Schristos #endif
43*75f6d617Schristos 
44*75f6d617Schristos #include "setmode.h"
45*75f6d617Schristos #undef set_binary_mode
46*75f6d617Schristos 
47*75f6d617Schristos 
48*75f6d617Schristos /* Set the binary mode of FD to MODE, returning its previous mode.
49*75f6d617Schristos    MODE is 1 for binary and 0 for text.  If setting the mode might
50*75f6d617Schristos    cause problems, ignore the request and return MODE.  Always return
51*75f6d617Schristos    1 on POSIX platforms, which do not distinguish between text and
52*75f6d617Schristos    binary.  */
53*75f6d617Schristos 
54*75f6d617Schristos bool
set_binary_mode(int fd,bool mode)55*75f6d617Schristos set_binary_mode (int fd, bool mode)
56*75f6d617Schristos {
57*75f6d617Schristos #if HAVE_SETMODE_DOS
58*75f6d617Schristos   if (isatty (fd))
59*75f6d617Schristos     return mode;
60*75f6d617Schristos   return setmode (fd, mode ? O_BINARY : O_TEXT) != O_TEXT;
61*75f6d617Schristos #else
62*75f6d617Schristos   return 1;
63*75f6d617Schristos #endif
64*75f6d617Schristos }
65