diff --git a/include/uv.h b/include/uv.h index bf7e88ad..47416165 100644 --- a/include/uv.h +++ b/include/uv.h @@ -600,6 +600,11 @@ struct uv_tty_s { UV_TTY_PRIVATE_FIELDS }; +/* + * Returns 1 if file is associated with a Console/TTY 0 otherwise. + */ +int uv_is_tty(uv_file file); + int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd); /* diff --git a/src/unix/tty.c b/src/unix/tty.c index 3ceeb5f1..9de7dd9f 100644 --- a/src/unix/tty.c +++ b/src/unix/tty.c @@ -67,3 +67,7 @@ fatal: return -1; } + +int uv_is_tty(uv_file file) { + return isatty(file); +} diff --git a/src/win/tty.c b/src/win/tty.c index 5498eec9..c451a99d 100644 --- a/src/win/tty.c +++ b/src/win/tty.c @@ -35,3 +35,10 @@ int uv_tty_set_mode(uv_tty_t* tty, int mode) { assert(0 && "implement me"); return -1; } + + +int uv_is_tty(uv_file file) { + DWORD result; + int r = GetConsoleMode((HANDLE)_get_osfhandle(file), &result); + return r ? 1 : 0; +} diff --git a/test/test-list.h b/test/test-list.h index bf84c6a9..429773f2 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -19,6 +19,7 @@ * IN THE SOFTWARE. */ +TEST_DECLARE (tty) TEST_DECLARE (tcp_ping_pong) TEST_DECLARE (tcp_ping_pong_v6) TEST_DECLARE (pipe_ping_pong) @@ -98,6 +99,8 @@ HELPER_DECLARE (pipe_echo_server) TASK_LIST_START + TEST_ENTRY (tty) + TEST_ENTRY (tcp_ping_pong) TEST_HELPER (tcp_ping_pong, tcp4_echo_server) diff --git a/test/test-tty.c b/test/test-tty.c new file mode 100644 index 00000000..4f9fd546 --- /dev/null +++ b/test/test-tty.c @@ -0,0 +1,33 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "task.h" + +TEST_IMPL(tty) { + uv_loop_t* loop = uv_default_loop(); + + ASSERT(uv_is_tty(0) == 1); + + uv_run(loop); + + return 0; +}