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