floor, ceil, trunc, truncf, truncl: Defeat GCC optimizations.
[gnulib.git] / lib / isatty.c
blobb8c8ebb9b4d3bb40215f373b65afdee56aaecdbe
1 /* isatty() replacement.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <unistd.h>
22 /* This replacement is enabled on native Windows. */
24 #include <errno.h>
26 /* Get declarations of the Win32 API functions. */
27 #define WIN32_LEAN_AND_MEAN
28 #include <windows.h>
30 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
31 # include "msvc-inval.h"
32 #endif
34 /* Get _get_osfhandle(). */
35 #if GNULIB_MSVC_NOTHROW
36 # include "msvc-nothrow.h"
37 #else
38 # include <io.h>
39 #endif
41 static BOOL IsConsoleHandle (HANDLE h)
43 DWORD mode;
44 return GetConsoleMode (h, &mode) != 0;
47 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
48 static int
49 _isatty_nothrow (int fd)
51 int result;
53 TRY_MSVC_INVAL
55 result = _isatty (fd);
57 CATCH_MSVC_INVAL
59 result = 0;
61 DONE_MSVC_INVAL;
63 return result;
65 #else
66 # define _isatty_nothrow _isatty
67 #endif
69 /* Determine whether FD refers to a console device. Return 1 if yes.
70 Return 0 and set errno if no. (ptsname_r relies on the errno value.) */
71 int
72 isatty (int fd)
74 HANDLE h = (HANDLE) _get_osfhandle (fd);
75 if (h == INVALID_HANDLE_VALUE)
77 errno = EBADF;
78 return 0;
80 /* _isatty (fd) tests whether GetFileType of the handle is FILE_TYPE_CHAR.
81 But it does not set errno when it returns 0. */
82 if (_isatty_nothrow (fd))
84 if (IsConsoleHandle (h))
85 return 1;
87 errno = ENOTTY;
88 return 0;