summaryrefslogtreecommitdiff
path: root/miscutils/less.c
diff options
context:
space:
mode:
authorDenys Vlasenko2013-08-04 17:41:19 +0200
committerDenys Vlasenko2013-08-04 17:41:19 +0200
commitcc1c9ca6f8117a28adf73b83f39b8dc3ba05fca4 (patch)
tree080b5fbb129b46382c85e48846a3f216eb98f726 /miscutils/less.c
parent93696341672cb8e086a55ecdef4c5617675c1da2 (diff)
downloadbusybox-cc1c9ca6f8117a28adf73b83f39b8dc3ba05fca4.zip
busybox-cc1c9ca6f8117a28adf73b83f39b8dc3ba05fca4.tar.gz
less: support "less 1<>TTY"
function old new delta less_main 2466 2507 +41 xmalloc_ttyname 46 42 -4 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils/less.c')
-rw-r--r--miscutils/less.c29
1 files changed, 25 insertions, 4 deletions
diff --git a/miscutils/less.c b/miscutils/less.c
index 5ce0a12..60105f4 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -1608,6 +1608,9 @@ static void sigwinch_handler(int sig UNUSED_PARAM)
int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int less_main(int argc, char **argv)
{
+ char *tty_name;
+ int tty_fd;
+
INIT_G();
/* TODO: -x: do not interpret backspace, -xx: tab also */
@@ -1637,10 +1640,28 @@ int less_main(int argc, char **argv)
if (option_mask32 & FLAG_TILDE)
empty_line_marker = "";
- kbd_fd = open(CURRENT_TTY, O_RDONLY);
- if (kbd_fd < 0)
- return bb_cat(argv);
- ndelay_on(kbd_fd);
+ /* Some versions of less can survive w/o controlling tty,
+ * try to do the same. This also allows to specify an alternative
+ * tty via "less 1<>TTY".
+ * We don't try to use STDOUT_FILENO directly,
+ * since we want to set this fd to non-blocking mode,
+ * and not bother with restoring it on exit.
+ */
+ tty_name = xmalloc_ttyname(STDOUT_FILENO);
+ if (tty_name) {
+ tty_fd = open(tty_name, O_RDONLY);
+ free(tty_name);
+ if (tty_fd < 0)
+ goto try_ctty;
+ } else {
+ /* Try controlling tty */
+ try_ctty:
+ tty_fd = open(CURRENT_TTY, O_RDONLY);
+ if (tty_fd < 0)
+ return bb_cat(argv);
+ }
+ ndelay_on(tty_fd);
+ kbd_fd = tty_fd; /* save in a global */
tcgetattr(kbd_fd, &term_orig);
term_less = term_orig;