blob: 924aff2d54c52fea688f0010be1a92c72c6fbaa7 [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;
21 int silent; /* Note: No longer relevant in SUSv3. */
22 int retval;
Erik Andersen31638212000-01-15 22:28:50 +000023
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 bb_default_error_retval = 2; /* SUSv3 requires > 1 for error. */
25
26 silent = bb_getopt_ulflags(argc, argv, "s");
27
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 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000039
40 if (!silent) {
41 puts(s);
42 }
43
44 bb_fflush_stdout_and_exit(retval);
Erik Andersen31638212000-01-15 22:28:50 +000045}