blob: c28aa33d7144c371994cf2e1aa22740186ab8e10 [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
Erik Andersen31638212000-01-15 22:28:50 +000013#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000014#include <stdlib.h>
15#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000016#include "busybox.h"
Erik Andersen31638212000-01-15 22:28:50 +000017
Denis Vlasenko06af2162007-02-03 17:28:39 +000018int tty_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000019int tty_main(int argc, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000020{
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 const char *s;
Bernhard Reutner-Fischer1118a1d2007-01-20 21:32:20 +000022 USE_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000023 int retval;
Erik Andersen31638212000-01-15 22:28:50 +000024
Denis Vlasenko40920822006-10-03 20:28:06 +000025 xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000026
Bernhard Reutner-Fischer1118a1d2007-01-20 21:32:20 +000027 USE_INCLUDE_SUSv2(silent = getopt32(argc, argv, "s");)
Manuel Novoa III cad53642003-03-19 09:13:01 +000028
29 /* gnu tty outputs a warning that it is ignoring all args. */
30 bb_warn_ignoring_args(argc - optind);
31
32 retval = 0;
33
34 if ((s = ttyname(0)) == NULL) {
35 /* According to SUSv3, ttyname can on fail with EBADF or ENOTTY.
36 * 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}