summaryrefslogtreecommitdiff
path: root/lib/isatty.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2012-01-03 03:54:08 +0100
committerBruno Haible <bruno@clisp.org>2012-01-03 03:54:08 +0100
commitd7af94ea8b164efa0cc8e8618875a8e9c127ec3c (patch)
treeece14c44736450e2816b9d4ee9747ba3c11f2f6b /lib/isatty.c
parenta5fc828950c79ec061ffab67ea6c5c6862dfb0ec (diff)
downloadgnulib-d7af94ea8b164efa0cc8e8618875a8e9c127ec3c.tar.gz
New module 'isatty'.
* lib/unistd.in.h (isatty): New declaration. * lib/isatty.c: New file, based on an idea of Bastien Roucariès <roucaries.bastien@gmail.com>. * m4/isatty.m4: New file. * m4/unistd_h.m4 (gl_UNISTD_H): Test whether isatty is declared. (gl_UNISTD_H_DEFAULTS): Initialize GNULIB_ISATTY, REPLACE_ISATTY. * modules/unistd (Makefile.am): Substitute GNULIB_ISATTY, REPLACE_ISATTY. * modules/isatty: New file. * doc/posix-functions/isatty.texi: Mention the new module. Suggested by Paolo Bonzini.
Diffstat (limited to 'lib/isatty.c')
-rw-r--r--lib/isatty.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/isatty.c b/lib/isatty.c
new file mode 100644
index 0000000000..ed8be82eff
--- /dev/null
+++ b/lib/isatty.c
@@ -0,0 +1,44 @@
+/* isatty() replacement.
+ Copyright (C) 2012 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>
+
+/* Specification. */
+#include <unistd.h>
+
+/* This replacement is enabled on native Windows. */
+
+/* Get declarations of the Win32 API functions. */
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+/* Get _get_osfhandle(). */
+#include "msvc-nothrow.h"
+
+#define IsConsoleHandle(h) (((long) (h) & 3) == 3)
+
+int
+isatty (int fd)
+{
+ /* _isatty (fd) tests whether GetFileType of the handle is FILE_TYPE_CHAR. */
+ if (_isatty (fd))
+ {
+ HANDLE h = (HANDLE) _get_osfhandle (fd);
+ return IsConsoleHandle (h);
+ }
+ else
+ return 0;
+}