aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2016-11-02 10:19:18 +0000
committerRon Yorston <rmy@pobox.com>2016-11-02 10:19:18 +0000
commite45ff573dc9707b1ea71f490ef16cd9d08feaaf2 (patch)
tree09980b02b9c8efc8d772c7379abdcd4eef4ddad8 /libbb
parent7bc3efa4f0c7608723d301bda5ee006a0f6141fe (diff)
parent2e6af549715f5d7b4c2ab204e46c8b8f6f057045 (diff)
downloadbusybox-w32-e45ff573dc9707b1ea71f490ef16cd9d08feaaf2.tar.gz
busybox-w32-e45ff573dc9707b1ea71f490ef16cd9d08feaaf2.tar.bz2
busybox-w32-e45ff573dc9707b1ea71f490ef16cd9d08feaaf2.zip
Merge branch 'busybox' into merge
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)
237 237
238static int wh_helper(int value, int def_val, const char *env_name, int *err) 238static int wh_helper(int value, int def_val, const char *env_name, int *err)
239{ 239{
240 if (value == 0) { 240 /* Envvars override even if "value" from ioctl is valid (>0).
241 char *s = getenv(env_name); 241 * Rationale: it's impossible to guess what user wants.
242 if (s) { 242 * For example: "man CMD | ...": should "man" format output
243 value = atoi(s); 243 * to stdout's width? stdin's width? /dev/tty's width? 80 chars?
244 /* If LINES/COLUMNS are set, pretend that there is 244 * We _cant_ know it. If "..." saves text for e.g. email,
245 * no error getting w/h, this prevents some ugly 245 * then it's probably 80 chars.
246 * cursor tricks by our callers */ 246 * If "..." is, say, "grep -v DISCARD | $PAGER", then user
247 *err = 0; 247 * would prefer his tty's width to be used!
248 } 248 *
249 * Since we don't know, at least allow user to do this:
250 * "COLUMNS=80 man CMD | ..."
251 */
252 char *s = getenv(env_name);
253 if (s) {
254 value = atoi(s);
255 /* If LINES/COLUMNS are set, pretend that there is
256 * no error getting w/h, this prevents some ugly
257 * cursor tricks by our callers */
258 *err = 0;
249 } 259 }
260
250 if (value <= 1 || value >= 30000) 261 if (value <= 1 || value >= 30000)
251 value = def_val; 262 value = def_val;
252 return value; 263 return value;
@@ -258,6 +269,20 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh
258{ 269{
259 struct winsize win; 270 struct winsize win;
260 int err; 271 int err;
272 int close_me = -1;
273
274 if (fd == -1) {
275 if (isatty(STDOUT_FILENO))
276 fd = STDOUT_FILENO;
277 else
278 if (isatty(STDERR_FILENO))
279 fd = STDERR_FILENO;
280 else
281 if (isatty(STDIN_FILENO))
282 fd = STDIN_FILENO;
283 else
284 close_me = fd = open("/dev/tty", O_RDONLY);
285 }
261 286
262 win.ws_row = 0; 287 win.ws_row = 0;
263 win.ws_col = 0; 288 win.ws_col = 0;
@@ -268,6 +293,10 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh
268 *height = wh_helper(win.ws_row, 24, "LINES", &err); 293 *height = wh_helper(win.ws_row, 24, "LINES", &err);
269 if (width) 294 if (width)
270 *width = wh_helper(win.ws_col, 80, "COLUMNS", &err); 295 *width = wh_helper(win.ws_col, 80, "COLUMNS", &err);
296
297 if (close_me >= 0)
298 close(close_me);
299
271 return err; 300 return err;
272} 301}
273int FAST_FUNC get_terminal_width(int fd) 302int FAST_FUNC get_terminal_width(int fd)