Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * tty implementation for busybox |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 compliant */ |
| 11 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/tty.html */ |
| 12 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame^] | 18 | int tty_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 19 | int tty_main(int argc, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 20 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | const char *s; |
Bernhard Reutner-Fischer | 1118a1d | 2007-01-20 21:32:20 +0000 | [diff] [blame] | 22 | USE_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | int retval; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 4092082 | 2006-10-03 20:28:06 +0000 | [diff] [blame] | 25 | xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | |
Bernhard Reutner-Fischer | 1118a1d | 2007-01-20 21:32:20 +0000 | [diff] [blame] | 27 | USE_INCLUDE_SUSv2(silent = getopt32(argc, argv, "s");) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | |
| 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 Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 39 | } |
Bernhard Reutner-Fischer | 1118a1d | 2007-01-20 21:32:20 +0000 | [diff] [blame] | 40 | USE_INCLUDE_SUSv2(if (!silent) puts(s);) |
| 41 | SKIP_INCLUDE_SUSv2(puts(s);) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 43 | fflush_stdout_and_exit(retval); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 44 | } |