aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFerass El Hafidi <vitali64pmemail@protonmail.com>2023-02-19 11:56:31 +0100
committerFerass El Hafidi <vitali64pmemail@protonmail.com>2023-02-19 11:56:31 +0100
commite36a5982bd7d73ad5623fb3b91c41ab6f7bd6ff8 (patch)
tree4b05063ec4d4da44acf40eda09f1241596f3df14
parent273e5a0cc35a16aaa0652da651785e72dd25d10d (diff)
downloadfases-e36a5982bd7d73ad5623fb3b91c41ab6f7bd6ff8.tar.gz
fases-e36a5982bd7d73ad5623fb3b91c41ab6f7bd6ff8.zip
core/: tty
Signed-off-by: Ferass El Hafidi <vitali64pmemail@protonmail.com>
-rw-r--r--config.mk1
-rw-r--r--core/tty.c21
2 files changed, 22 insertions, 0 deletions
diff --git a/config.mk b/config.mk
index ec4901a..1585b79 100644
--- a/config.mk
+++ b/config.mk
@@ -32,6 +32,7 @@ CORE=\
tail\
test\
true\
+ tty\
uname\
unlink\
wc
diff --git a/core/tty.c b/core/tty.c
new file mode 100644
index 0000000..2e663e1
--- /dev/null
+++ b/core/tty.c
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+
+#define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
+#include "../common/common.h"
+
+int main(int, char *argv[]) {
+ char *terminalname = ttyname(STDIN_FILENO);
+ if (errno == ENOTTY) {
+ /* POSIX says that if the stdin isn't a tty then the error shall be
+ * written to stdout, not stderr.
+ */
+ printf("not a tty\n");
+ return 1;
+ } else if (errno) return errprint(argv[0], NULL, errno);
+ printf("%s\n", terminalname);
+ return 0;
+}