blob: fee4b8b9f09c0c22fb934b5e3a066d961520a156 [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
Rob Landleydfba7412006-03-06 20:47:33 +000018int tty_main(int argc, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000019{
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 const char *s;
Bernhard Reutner-Fischer1118a1d2007-01-20 21:32:20 +000021 USE_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 int retval;
Erik Andersen31638212000-01-15 22:28:50 +000023
Denis Vlasenko40920822006-10-03 20:28:06 +000024 xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000025
Bernhard Reutner-Fischer1118a1d2007-01-20 21:32:20 +000026 USE_INCLUDE_SUSv2(silent = getopt32(argc, argv, "s");)
Manuel Novoa III cad53642003-03-19 09:13:01 +000027
28 /* gnu tty outputs a warning that it is ignoring all args. */
29 bb_warn_ignoring_args(argc - optind);
30
31 retval = 0;
32
33 if ((s = ttyname(0)) == NULL) {
34 /* According to SUSv3, ttyname can on fail with EBADF or ENOTTY.
35 * We know the file descriptor is good, so failure means not a tty. */
36 s = "not a tty";
37 retval = 1;
Erik Andersen31638212000-01-15 22:28:50 +000038 }
Bernhard Reutner-Fischer1118a1d2007-01-20 21:32:20 +000039 USE_INCLUDE_SUSv2(if (!silent) puts(s);)
40 SKIP_INCLUDE_SUSv2(puts(s);)
Manuel Novoa III cad53642003-03-19 09:13:01 +000041
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000042 fflush_stdout_and_exit(retval);
Erik Andersen31638212000-01-15 22:28:50 +000043}