re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / gnu / isatty.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* isatty() replacement.
4    Copyright (C) 2012-2014 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <unistd.h>
23
24 /* This replacement is enabled on native Windows.  */
25
26 #include <errno.h>
27
28 /* Get declarations of the Win32 API functions.  */
29 #define WIN32_LEAN_AND_MEAN
30 #include <windows.h>
31
32 #include "msvc-inval.h"
33
34 /* Get _get_osfhandle().  */
35 #include "msvc-nothrow.h"
36
37 /* Optimized test whether a HANDLE refers to a console.
38    See <http://lists.gnu.org/archive/html/bug-gnulib/2009-08/msg00065.html>.  */
39 #define IsConsoleHandle(h) (((intptr_t) (h) & 3) == 3)
40
41 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
42 static int
43 _isatty_nothrow (int fd)
44 {
45   int result;
46
47   TRY_MSVC_INVAL
48     {
49       result = _isatty (fd);
50     }
51   CATCH_MSVC_INVAL
52     {
53       result = 0;
54     }
55   DONE_MSVC_INVAL;
56
57   return result;
58 }
59 #else
60 # define _isatty_nothrow _isatty
61 #endif
62
63 /* Determine whether FD refers to a console device.  Return 1 if yes.
64    Return 0 and set errno if no. (ptsname_r relies on the errno value.)  */
65 int
66 isatty (int fd)
67 {
68   HANDLE h = (HANDLE) _get_osfhandle (fd);
69   if (h == INVALID_HANDLE_VALUE)
70     {
71       errno = EBADF;
72       return 0;
73     }
74   /* _isatty (fd) tests whether GetFileType of the handle is FILE_TYPE_CHAR.
75      But it does not set errno when it returns 0.  */
76   if (_isatty_nothrow (fd))
77     {
78       if (IsConsoleHandle (h))
79         return 1;
80     }
81   errno = ENOTTY;
82   return 0;
83 }