1 /* vi: set sw=4 ts=4: */
3 * tty implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "tty (3.6 kb)"
13 //config: tty is used to print the name of the current terminal to
14 //config: standard output.
16 //applet:IF_TTY(APPLET_NOFORK(tty, tty, BB_DIR_USR_BIN, BB_SUID_DROP, tty))
18 //kbuild:lib-$(CONFIG_TTY) += tty.o
20 /* BB_AUDIT SUSv4 compliant */
21 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/tty.html */
23 //usage:#define tty_trivial_usage
25 //usage:#define tty_full_usage "\n\n"
26 //usage: "Print file name of stdin's terminal"
27 //usage: IF_INCLUDE_SUSv2( "\n"
28 //usage: "\n -s Print nothing, only return exit status"
31 //usage:#define tty_example_usage
33 //usage: "/dev/tty2\n"
37 int tty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
38 int tty_main(int argc UNUSED_PARAM, char **argv)
41 IF_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */
44 xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */
46 IF_INCLUDE_SUSv2(silent = getopt32(argv, "s");)
47 IF_INCLUDE_SUSv2(argv += optind;)
48 IF_NOT_INCLUDE_SUSv2(argv += 1;)
50 /* gnu tty outputs a warning that it is ignoring all args. */
51 bb_warn_ignoring_args(argv[0]);
53 retval = EXIT_SUCCESS;
55 s = xmalloc_ttyname(STDIN_FILENO);
57 /* According to SUSv3, ttyname can fail with EBADF or ENOTTY.
58 * We know the file descriptor is good, so failure means not a tty. */
60 retval = EXIT_FAILURE;
62 IF_INCLUDE_SUSv2(if (!silent) puts(s);)
63 IF_NOT_INCLUDE_SUSv2(puts(s);)
65 fflush_stdout_and_exit(retval);