aboutsummaryrefslogtreecommitdiff
path: root/tty.c
diff options
context:
space:
mode:
Diffstat (limited to 'tty.c')
-rw-r--r--tty.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/tty.c b/tty.c
new file mode 100644
index 0000000..e0c0580
--- /dev/null
+++ b/tty.c
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
+ */
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+
+#define REQ_ERRPRINT /* Require errprint() from common.h */
+#include "common.h"
+
+#ifdef FASESBOX
+int tty_main(int argc, char *argv[]) {
+#else
+int main(int argc, char *argv[]) {
+#endif
+ char *terminalname = ttyname(STDIN_FILENO);
+ if (argc != 1) {} /* workaround... */
+ 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 errprint(argv[0], NULL, errno);
+}