coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-06-26 12:16:43
Commit9e5b4df5c22c4891fd7a27badc4f539c14d8d3ea
Parent8f5291e336bf0d8c3a246d6102353ae025351496

tty: Add POSIX tty

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/tty.1 | 25 +++++++++++++++++++++++++
A src/tty.c | 28 ++++++++++++++++++++++++++++

4 files changed, 55 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 74f1971..0551795 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,5 +24,6 @@ ADD_EXECUTABLE(sleep    ${CMAKE_SOURCE_DIR}/src/sleep.c)
 ADD_EXECUTABLE(sync     ${CMAKE_SOURCE_DIR}/src/sync.c)
 ADD_EXECUTABLE(tee      ${CMAKE_SOURCE_DIR}/src/tee.c)
 ADD_EXECUTABLE(true     ${CMAKE_SOURCE_DIR}/src/true.c)
+ADD_EXECUTABLE(tty      ${CMAKE_SOURCE_DIR}/src/tty.c)
 ADD_EXECUTABLE(unlink   ${CMAKE_SOURCE_DIR}/src/unlink.c)
 ADD_EXECUTABLE(yes      ${CMAKE_SOURCE_DIR}/src/yes.c)
diff --git a/README.md b/README.md
index c4779f2..f1e7811 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,7 @@ UNIX-like systems.
 | sync             | Synchronise file system caches to disk   |          |
 | tee              | Duplicate standard input                 | POSIX    |
 | true             | Return true value                        | POSIX    |
+| tty              | Return user's terminal name              | POSIX    |
 | unlink           | Remove a file using the unlink function  | POSIX    |
 | yes              | Output a string repeatedly               |          |
 
diff --git a/man/tty.1 b/man/tty.1
new file mode 100644
index 0000000..6f2f6b5
--- /dev/null
+++ b/man/tty.1
@@ -0,0 +1,25 @@
+.TH TTY 1 2020-06-26 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+tty \(em return user's terminal name
+.SH SYNOPSYS
+\fBtty\fR
+.SH DESCRIPTION
+Print the name of the terminal that is connected to standard input, if standard
+input is not a terminal, "not a tty" will be printed instead.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+0
+Standard input is a terminal.
+.TP
+1
+Standard input is not a terminal.
+.SH STANDARDS
+The \fItty\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2")
+specification.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Public Domain Licence (https://www.omkov.net/OLPD)
+.fi
diff --git a/src/tty.c b/src/tty.c
new file mode 100644
index 0000000..e41e8c7
--- /dev/null
+++ b/src/tty.c
@@ -0,0 +1,28 @@
+// tty.c, version 1.0.0
+// OMKOV coreutils implementation of POSIX tty
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Public Domain Licence, version 1.0
+
+Permission is hereby granted to deal with this software and its associated
+documentation files without restriction.
+
+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 COPYRIGHT
+HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE.
+*/
+
+#include "error.h"
+
+#include <unistd.h>
+
+int main(void) {
+	register char *tty = ttyname(fileno(stdin));
+	if (!tty) { error(1, "not a tty"); }
+	puts(tty); return 0;
+}