Using less in environment without controlling tty

Harald Becker ralda at
Tue Jul 30 05:16:06 UTC 2013

What about solving the problem in the following manner:

/* fallback to cat if stdout not tty (as it currently is) */
if( !isatty(1) )
  behave as cat

/* allow passing in a tty without any extra options */
else if( isatty(3) )
  kb_fd = 3;

   /* try to use tty from stdout without duplicating fd */
   tty = ttyname(1);

   /* else use /dev/tty as it is currently done */
   kb_fd = open( tty ? tty : "/dev/tty", O_RDWR );

   /* and last resort fallback to stdout (which is tty) */
   if( kb_fd < 0 ) kb_fd = 1;

use kb_fd for message viewing

This adds just one isatty and one ttyname call and shall not
break (most) current use cases. The ttyname makes Busybox less
behave more like standard less without neglecting what Denys
explained. The fallback to using stdout is for cases without
controlling tty and no /proc & /sys mounted (-->ttyname fails)
and provides a usable alternative (with known restrictions).


More information about the busybox mailing list