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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config TTY |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "tty (3.3 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: tty is used to print the name of the current terminal to |
| 14 | //config: standard output. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 15 | |
| 16 | //applet:IF_TTY(APPLET(tty, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_TTY) += tty.o |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 19 | |
Bernhard Reutner-Fischer | 95a036e | 2009-10-29 21:37:48 +0100 | [diff] [blame] | 20 | /* BB_AUDIT SUSv4 compliant */ |
| 21 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/tty.html */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 23 | //usage:#define tty_trivial_usage |
| 24 | //usage: "" |
| 25 | //usage:#define tty_full_usage "\n\n" |
| 26 | //usage: "Print file name of stdin's terminal" |
| 27 | //usage: IF_INCLUDE_SUSv2( "\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 28 | //usage: "\n -s Print nothing, only return exit status" |
| 29 | //usage: ) |
| 30 | //usage: |
| 31 | //usage:#define tty_example_usage |
| 32 | //usage: "$ tty\n" |
| 33 | //usage: "/dev/tty2\n" |
| 34 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 37 | int tty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | a355da0 | 2010-01-04 13:16:08 +0100 | [diff] [blame] | 38 | int tty_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | const char *s; |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 41 | IF_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | int retval; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 43 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 44 | xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 46 | IF_INCLUDE_SUSv2(silent = getopt32(argv, "s");) |
Denys Vlasenko | a355da0 | 2010-01-04 13:16:08 +0100 | [diff] [blame] | 47 | IF_INCLUDE_SUSv2(argv += optind;) |
| 48 | IF_NOT_INCLUDE_SUSv2(argv += 1;) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | |
| 50 | /* gnu tty outputs a warning that it is ignoring all args. */ |
Denys Vlasenko | a355da0 | 2010-01-04 13:16:08 +0100 | [diff] [blame] | 51 | bb_warn_ignoring_args(argv[0]); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | |
Bernhard Reutner-Fischer | 95a036e | 2009-10-29 21:37:48 +0100 | [diff] [blame] | 53 | retval = EXIT_SUCCESS; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | |
Bernhard Reutner-Fischer | 95a036e | 2009-10-29 21:37:48 +0100 | [diff] [blame] | 55 | s = xmalloc_ttyname(STDIN_FILENO); |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 56 | if (s == NULL) { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 57 | /* According to SUSv3, ttyname can fail with EBADF or ENOTTY. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | * We know the file descriptor is good, so failure means not a tty. */ |
| 59 | s = "not a tty"; |
Bernhard Reutner-Fischer | 95a036e | 2009-10-29 21:37:48 +0100 | [diff] [blame] | 60 | retval = EXIT_FAILURE; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 61 | } |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 62 | IF_INCLUDE_SUSv2(if (!silent) puts(s);) |
| 63 | IF_NOT_INCLUDE_SUSv2(puts(s);) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 65 | fflush_stdout_and_exit(retval); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 66 | } |