aboutsummaryrefslogtreecommitdiffstats
path: root/host
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2019-09-30 13:18:19 +0200
committerBrent Stapleton <brent.stapleton@ettus.com>2019-10-07 11:38:25 -0700
commit15b10f9e8e2e98be8717a347304202c7aa171125 (patch)
treed981d7aec8793421180bad620bbe39d47f87f4f6 /host
parentd33eb0efa8a90491a3f89e42c008f6602190714f (diff)
downloaduhd-15b10f9e8e2e98be8717a347304202c7aa171125.tar.gz
uhd-15b10f9e8e2e98be8717a347304202c7aa171125.tar.bz2
uhd-15b10f9e8e2e98be8717a347304202c7aa171125.zip
lib: utils: Add is_a_tty()
This is a portable version of POSIX's isatty(). Windows has its own version, called _isatty(). UHD thus gains its own, portable version. The underscores aren't beautiful, but they're necessary so we can distinguish the POSIX version from the UHD version.
Diffstat (limited to 'host')
-rw-r--r--host/lib/include/uhdlib/utils/isatty.hpp55
-rw-r--r--host/tests/CMakeLists.txt1
-rw-r--r--host/tests/isatty_test.cpp32
3 files changed, 88 insertions, 0 deletions
diff --git a/host/lib/include/uhdlib/utils/isatty.hpp b/host/lib/include/uhdlib/utils/isatty.hpp
new file mode 100644
index 000000000..1ae2c8de4
--- /dev/null
+++ b/host/lib/include/uhdlib/utils/isatty.hpp
@@ -0,0 +1,55 @@
+//
+// Copyright 2019 Ettus Research, a National Instruments Brand
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+
+#ifndef INCLUDED_UHDLIB_UTILS_ISATTY_HPP
+#define INCLUDED_UHDLIB_UTILS_ISATTY_HPP
+
+#include <uhd/config.hpp>
+
+namespace uhd {
+
+#ifdef UHD_PLATFORM_WIN32
+
+# include <io.h>
+
+ /*! Portable version of isatty()
+ *
+ * We call it is_a_tty() to distinguish from the from the POSIX version.
+ * Also, we simply return a Boolean since the Windows version doesn't set
+ * errno.
+ */
+ bool is_a_tty(const int fd)
+ {
+ return _isatty(fd);
+ }
+
+#elif _POSIX_C_SOURCE >= _200112L
+
+# include <unistd.h>
+
+ /*! Portable version of isatty()
+ *
+ * We call it is_a_tty() to distinguish from the from the POSIX version.
+ * Also, we simply return a Boolean since the Windows version doesn't set
+ * errno.
+ */
+ bool is_a_tty(const int fd)
+ {
+ return isatty(fd);
+ }
+
+#else
+
+ bool is_a_tty(const int fd)
+ {
+ return false;
+ }
+
+#endif
+
+} /* namespace uhd */
+
+#endif /* INCLUDED_UHDLIB_UTILS_ISATTY_HPP */
diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt
index a7d2120aa..769c09796 100644
--- a/host/tests/CMakeLists.txt
+++ b/host/tests/CMakeLists.txt
@@ -34,6 +34,7 @@ set(test_sources
fp_compare_delta_test.cpp
fp_compare_epsilon_test.cpp
gain_group_test.cpp
+ isatty_test.cpp
log_test.cpp
math_test.cpp
narrow_cast_test.cpp
diff --git a/host/tests/isatty_test.cpp b/host/tests/isatty_test.cpp
new file mode 100644
index 000000000..dfeebfd25
--- /dev/null
+++ b/host/tests/isatty_test.cpp
@@ -0,0 +1,32 @@
+//
+// Copyright 2019 Ettus Research, a National Instruments Brand
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+
+#include <uhdlib/utils/isatty.hpp>
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+#include <stdio.h>
+
+BOOST_AUTO_TEST_CASE(test_isatty)
+{
+ // We can't really pass or fail based on the result, because it depends on
+ // how the tests are executed. We'll just run it and see it doesn't crash.
+ if (uhd::is_a_tty(2)) {
+ std::cout << "stderr is a TTY" << std::endl;
+ } else {
+ std::cout << "stderr is not a TTY" << std::endl;
+ }
+
+ FILE* tmp_file = std::tmpfile();
+#ifdef UHD_PLATFORM_WIN32
+ BOOST_REQUIRE(!uhd::is_a_tty(_fileno(tmp_file)));
+#elif _POSIX_C_SOURCE >= _200112L
+ BOOST_REQUIRE(!uhd::is_a_tty(fileno(tmp_file)));
+#else
+ // I got 99 problems but dealing with portability ain't one
+ BOOST_REQUIRE(!uhd::is_a_tty(99));
+#endif
+}
+