aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2016-10-31 01:52:18 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2016-10-31 01:52:18 +0100
commit7c3c92c533b65d4c29f2990915c9c424c3f6629d (patch)
treed9eb784d9c458c3d91fcf9a9e31e636f39d8a360 /libbb
parenta92a74961d838209f3468d10426bc945ba26070c (diff)
downloadbusybox-7c3c92c533b65d4c29f2990915c9c424c3f6629d.tar.gz
man: make width selection more thorough; explain how to override it
Fedora's "man CMD >file" still uses terminal width, not 80 (but disables formatting), this change mimics that. Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/xfuncs.c47
1 files changed, 38 insertions, 9 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 3f9a84ad4..45650edba 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -237,16 +237,27 @@ ssize_t FAST_FUNC full_write2_str(const char *str)
static int wh_helper(int value, int def_val, const char *env_name, int *err)
{
- if (value == 0) {
- char *s = getenv(env_name);
- if (s) {
- value = atoi(s);
- /* If LINES/COLUMNS are set, pretend that there is
- * no error getting w/h, this prevents some ugly
- * cursor tricks by our callers */
- *err = 0;
- }
+ /* Envvars override even if "value" from ioctl is valid (>0).
+ * Rationale: it's impossible to guess what user wants.
+ * For example: "man CMD | ...": should "man" format output
+ * to stdout's width? stdin's width? /dev/tty's width? 80 chars?
+ * We _cant_ know it. If "..." saves text for e.g. email,
+ * then it's probably 80 chars.
+ * If "..." is, say, "grep -v DISCARD | $PAGER", then user
+ * would prefer his tty's width to be used!
+ *
+ * Since we don't know, at least allow user to do this:
+ * "COLUMNS=80 man CMD | ..."
+ */
+ char *s = getenv(env_name);
+ if (s) {
+ value = atoi(s);
+ /* If LINES/COLUMNS are set, pretend that there is
+ * no error getting w/h, this prevents some ugly
+ * cursor tricks by our callers */
+ *err = 0;
}
+
if (value <= 1 || value >= 30000)
value = def_val;
return value;
@@ -258,6 +269,20 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh
{
struct winsize win;
int err;
+ int close_me = -1;
+
+ if (fd == -1) {
+ if (isatty(STDOUT_FILENO))
+ fd = STDOUT_FILENO;
+ else
+ if (isatty(STDERR_FILENO))
+ fd = STDERR_FILENO;
+ else
+ if (isatty(STDIN_FILENO))
+ fd = STDIN_FILENO;
+ else
+ close_me = fd = open("/dev/tty", O_RDONLY);
+ }
win.ws_row = 0;
win.ws_col = 0;
@@ -268,6 +293,10 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh
*height = wh_helper(win.ws_row, 24, "LINES", &err);
if (width)
*width = wh_helper(win.ws_col, 80, "COLUMNS", &err);
+
+ if (close_me >= 0)
+ close(close_me);
+
return err;
}
int FAST_FUNC get_terminal_width(int fd)