blob: e448c27ec7446f60a48303248d95d179101a1b41 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen31638212000-01-15 22:28:50 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * tty implementation for busybox
Erik Andersen31638212000-01-15 22:28:50 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Erik Andersen31638212000-01-15 22:28:50 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen31638212000-01-15 22:28:50 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config TTY
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "tty (3.6 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: tty is used to print the name of the current terminal to
14//config: standard output.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015
Denys Vlasenko819b47a2017-08-03 03:29:32 +020016//applet:IF_TTY(APPLET_NOFORK(tty, tty, BB_DIR_USR_BIN, BB_SUID_DROP, tty))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010017
18//kbuild:lib-$(CONFIG_TTY) += tty.o
Erik Andersen31638212000-01-15 22:28:50 +000019
Bernhard Reutner-Fischer95a036e2009-10-29 21:37:48 +010020/* BB_AUDIT SUSv4 compliant */
21/* http://www.opengroup.org/onlinepubs/9699919799/utilities/tty.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000022
Pere Orga34425382011-03-31 14:43:25 +020023//usage:#define tty_trivial_usage
Denys Vlasenko18492852021-06-13 03:12:09 +020024//usage: "" IF_INCLUDE_SUSv2("[-s]")
Pere Orga34425382011-03-31 14:43:25 +020025//usage:#define tty_full_usage "\n\n"
26//usage: "Print file name of stdin's terminal"
27//usage: IF_INCLUDE_SUSv2( "\n"
Pere Orga34425382011-03-31 14:43:25 +020028//usage: "\n -s Print nothing, only return exit status"
29//usage: )
30//usage:
31//usage:#define tty_example_usage
32//usage: "$ tty\n"
33//usage: "/dev/tty2\n"
34
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Erik Andersen31638212000-01-15 22:28:50 +000036
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000037int tty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoa355da02010-01-04 13:16:08 +010038int tty_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000039{
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 const char *s;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020041 IF_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 int retval;
Erik Andersen31638212000-01-15 22:28:50 +000043
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020044 xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000045
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000046 IF_INCLUDE_SUSv2(silent = getopt32(argv, "s");)
Denys Vlasenkoa355da02010-01-04 13:16:08 +010047 IF_INCLUDE_SUSv2(argv += optind;)
48 IF_NOT_INCLUDE_SUSv2(argv += 1;)
Manuel Novoa III cad53642003-03-19 09:13:01 +000049
50 /* gnu tty outputs a warning that it is ignoring all args. */
Denys Vlasenkoa355da02010-01-04 13:16:08 +010051 bb_warn_ignoring_args(argv[0]);
Manuel Novoa III cad53642003-03-19 09:13:01 +000052
Bernhard Reutner-Fischer95a036e2009-10-29 21:37:48 +010053 retval = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000054
Bernhard Reutner-Fischer95a036e2009-10-29 21:37:48 +010055 s = xmalloc_ttyname(STDIN_FILENO);
Denis Vlasenko99912ca2007-04-10 15:43:37 +000056 if (s == NULL) {
Denis Vlasenko68404f12008-03-17 09:00:54 +000057 /* According to SUSv3, ttyname can fail with EBADF or ENOTTY.
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 * We know the file descriptor is good, so failure means not a tty. */
59 s = "not a tty";
Bernhard Reutner-Fischer95a036e2009-10-29 21:37:48 +010060 retval = EXIT_FAILURE;
Erik Andersen31638212000-01-15 22:28:50 +000061 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000062 IF_INCLUDE_SUSv2(if (!silent) puts(s);)
63 IF_NOT_INCLUDE_SUSv2(puts(s);)
Manuel Novoa III cad53642003-03-19 09:13:01 +000064
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000065 fflush_stdout_and_exit(retval);
Erik Andersen31638212000-01-15 22:28:50 +000066}