From 8fcd372cac9eb2e1f5b77be5c644c16050a14ff5 Mon Sep 17 00:00:00 2001 From: "Anthony J. Bentley" Date: Thu, 17 Mar 2016 05:27:11 +0000 Subject: Switch (non-curses, non-ksh) programs that use COLUMNS to a single idiom. Previously behaviors were all over the map. This changes them to use COLUMNS first, and either terminal width or a hardcoded value (typically 80) as appropriate. ok deraadt@; man bits ok jmc@ --- bin/ls/ls.1 | 14 ++++++++------ bin/ls/ls.c | 26 +++++++++++--------------- 2 files changed, 19 insertions(+), 21 deletions(-) (limited to 'bin/ls') diff --git a/bin/ls/ls.1 b/bin/ls/ls.1 index 0d9cc32b157..5c35c1bf95b 100644 --- a/bin/ls/ls.1 +++ b/bin/ls/ls.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ls.1,v 1.74 2016/03/11 02:35:57 bentley Exp $ +.\" $OpenBSD: ls.1,v 1.75 2016/03/17 05:27:10 bentley Exp $ .\" $NetBSD: ls.1,v 1.14 1995/12/05 02:44:01 jtc Exp $ .\" .\" Copyright (c) 1980, 1990, 1991, 1993, 1994 @@ -33,7 +33,7 @@ .\" .\" @(#)ls.1 8.7 (Berkeley) 7/29/94 .\" -.Dd $Mdocdate: March 11 2016 $ +.Dd $Mdocdate: March 17 2016 $ .Dt LS 1 .Os .Sh NAME @@ -436,10 +436,12 @@ option is not specified, the block counts .Fl s ) will be displayed in units of that size block. .It Ev COLUMNS -If this variable contains a string representing a -decimal integer, it is used as the -column position width for displaying -multiple-text-column output. +If set to a positive integer, +.Nm Ns 's +output is formatted to the given width in columns. +Otherwise, +.Nm +defaults to the terminal width, or 80 columns if the output is not a terminal. .It Ev LC_CTYPE If set to a string ending in .Qq .UTF-8 , diff --git a/bin/ls/ls.c b/bin/ls/ls.c index 6341bfc6fa2..b16e82759aa 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ls.c,v 1.44 2015/12/01 18:36:13 schwarze Exp $ */ +/* $OpenBSD: ls.c,v 1.45 2016/03/17 05:27:10 bentley Exp $ */ /* $NetBSD: ls.c,v 1.18 1996/07/09 09:16:29 mycroft Exp $ */ /* @@ -66,7 +66,7 @@ static int (*sortfcn)(const FTSENT *, const FTSENT *); #define BY_TIME 2 long blocksize; /* block size units */ -int termwidth = 80; /* default terminal width */ +int termwidth; /* default terminal width */ int sortkey = BY_NAME; /* flags */ @@ -110,24 +110,20 @@ ls_main(int argc, char *argv[]) /* Terminal defaults to -Cq, non-terminal defaults to -1. */ if (isatty(STDOUT_FILENO)) { - if ((p = getenv("COLUMNS")) != NULL) - width = strtonum(p, 1, INT_MAX, NULL); - if (width == 0 && - ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && - win.ws_col > 0) - width = win.ws_col; - if (width) - termwidth = width; f_column = f_nonprint = 1; } else { f_singlecol = 1; - /* retrieve environment variable, in case of explicit -C */ - if ((p = getenv("COLUMNS")) != NULL) - width = strtonum(p, 0, INT_MAX, NULL); - if (width) - termwidth = width; } + termwidth = 0; + if ((p = getenv("COLUMNS")) != NULL) + termwidth = strtonum(p, 1, INT_MAX, NULL); + if (termwidth == 0 && ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && + win.ws_col > 0) + termwidth = win.ws_col; + if (termwidth == 0) + termwidth = 80; + if (pledge("stdio rpath getpw", NULL) == -1) err(1, "pledge"); -- cgit v1.2.3