blob: e83289440b29c51f60c38d13aa2979a728f22c8a [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersen31638212000-01-15 22:28:50 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/tty.html */
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Erik Andersen31638212000-01-15 22:28:50 +000014
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000015int tty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000016int tty_main(int argc, char **argv SKIP_INCLUDE_SUSv2(UNUSED_PARAM))
Erik Andersene49d5ec2000-02-08 19:58:47 +000017{
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 const char *s;
Bernhard Reutner-Fischer1118a1d2007-01-20 21:32:20 +000019 USE_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 int retval;
Erik Andersen31638212000-01-15 22:28:50 +000021
Denis Vlasenko40920822006-10-03 20:28:06 +000022 xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000023
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000024 USE_INCLUDE_SUSv2(silent = getopt32(argv, "s");)
Denis Vlasenko68404f12008-03-17 09:00:54 +000025 USE_INCLUDE_SUSv2(argc -= optind;)
26 SKIP_INCLUDE_SUSv2(argc -= 1;)
Manuel Novoa III cad53642003-03-19 09:13:01 +000027
28 /* gnu tty outputs a warning that it is ignoring all args. */
Denis Vlasenko68404f12008-03-17 09:00:54 +000029 bb_warn_ignoring_args(argc);
Manuel Novoa III cad53642003-03-19 09:13:01 +000030
31 retval = 0;
32
Denis Vlasenko99912ca2007-04-10 15:43:37 +000033 s = ttyname(0);
34 if (s == NULL) {
Denis Vlasenko68404f12008-03-17 09:00:54 +000035 /* According to SUSv3, ttyname can fail with EBADF or ENOTTY.
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 * We know the file descriptor is good, so failure means not a tty. */
37 s = "not a tty";
38 retval = 1;
Erik Andersen31638212000-01-15 22:28:50 +000039 }
Bernhard Reutner-Fischer1118a1d2007-01-20 21:32:20 +000040 USE_INCLUDE_SUSv2(if (!silent) puts(s);)
41 SKIP_INCLUDE_SUSv2(puts(s);)
Manuel Novoa III cad53642003-03-19 09:13:01 +000042
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000043 fflush_stdout_and_exit(retval);
Erik Andersen31638212000-01-15 22:28:50 +000044}