summaryrefslogtreecommitdiff
path: root/coreutils/tty.c (plain)
blob: 359e5bc9314b1a8a416f967eb34ebfa8d192cf30
1/* vi: set sw=4 ts=4: */
2/*
3 * tty implementation for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9//config:config TTY
10//config: bool "tty"
11//config: default y
12//config: help
13//config: tty is used to print the name of the current terminal to
14//config: standard output.
15
16//applet:IF_TTY(APPLET(tty, BB_DIR_USR_BIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_TTY) += tty.o
19
20/* BB_AUDIT SUSv4 compliant */
21/* http://www.opengroup.org/onlinepubs/9699919799/utilities/tty.html */
22
23//usage:#define tty_trivial_usage
24//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"
29//usage: )
30//usage:
31//usage:#define tty_example_usage
32//usage: "$ tty\n"
33//usage: "/dev/tty2\n"
34
35#include "libbb.h"
36
37int tty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
38int tty_main(int argc UNUSED_PARAM, char **argv)
39{
40 const char *s;
41 IF_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */
42 int retval;
43
44 xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */
45
46 IF_INCLUDE_SUSv2(silent = getopt32(argv, "s");)
47 IF_INCLUDE_SUSv2(argv += optind;)
48 IF_NOT_INCLUDE_SUSv2(argv += 1;)
49
50 /* gnu tty outputs a warning that it is ignoring all args. */
51 bb_warn_ignoring_args(argv[0]);
52
53 retval = EXIT_SUCCESS;
54
55 s = xmalloc_ttyname(STDIN_FILENO);
56 if (s == NULL) {
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. */
59 s = "not a tty";
60 retval = EXIT_FAILURE;
61 }
62 IF_INCLUDE_SUSv2(if (!silent) puts(s);)
63 IF_NOT_INCLUDE_SUSv2(puts(s);)
64
65 fflush_stdout_and_exit(retval);
66}
67