Bug 20483

Summary: allowing old colors+paging combination with ls(1) again
Product: Base System Reporter: Ralf S. Engelschall <rse>
Component: binAssignee: joe <joe>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.1-STABLE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Ralf S. Engelschall 2000-08-08 14:00:01 UTC
Since years I used the following stuff in my shell to get three directory
listing commands which both use color and (if any only if the listing is too
large) also the pager.

    ls () { colorls -G -C "$@"    | less -E -r }
    ll () { colorls -G -l "$@"    | less -E -r }
    lx () { colorls -G -l -a "$@" | less -E -r }

As you now, colorls and less were the beast from our ports, -G switched on the
colors and less' -E -r options did the magic for display: -E caused less to
automatically exit the first time it reached end-of-file, and -r forced less
to display the color sequences.

Now with FreeBSD 4.1 we got the nice color support built into our /bin/ls.
Fine, except for the problem that the great colors+paging combination is no
longer possible:

    ls () { /bin/ls -G -C "$@"    | less -E -r }
    ll () { /bin/ls -G -l "$@"    | less -E -r }
    lx () { /bin/ls -G -l -a "$@" | less -E -r }

The result is that all colors are gone. The reason is a simple isatty(3) call
in src/bin/ls/ls.c, line 205, which simple lets /bin/ls honor -G only if
stdout is connected to a tty. Hmmm... obviously a useful check, because things
like "ls *.c >filelist", etc. should not contain any color sequences.  But
OTOH this check seems a little-bit too restrictive and magic, because if I add
-G I really mean -G. And if I don't want any colors in "ls *.c

Fix: I propose either this patch:

or alternatively the following patch:



With the second patch, compatibility to the original intention ("be magic in
case of non-tty") is preserved and one is at least able to use the following
to achieve the old colors+paging combination:

    ls () { LSCOLORS_FORCED=1 /bin/ls -G -C "$@"    | less -E -r }
    ll () { LSCOLORS_FORCED=1 /bin/ls -G -l "$@"    | less -E -r }
    lx () { LSCOLORS_FORCED=1 /bin/ls -G -l -a "$@" | less -E -r }--NxvB5nIcZ4yQhZhBrkFFKA47TuG2U9m3knhYHdH93zZtx5Cu
Content-Type: text/plain; name="file.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="file.diff"

Index: ls.c
===================================================================
RCS file: /home/ncvs/src/bin/ls/ls.c,v
retrieving revision 1.42
diff -u -d -r1.42 ls.c
--- ls.c	2000/07/04 23:09:23	1.42
+++ ls.c	2000/08/08 12:44:55
@@ -202,7 +202,6 @@
 		        fts_options |= FTS_COMFOLLOW;
 			break;
 		case 'G':
-			if (isatty(STDOUT_FILENO))
 #ifdef COLORLS
 				if (tgetent(termcapbuf, getenv("TERM")) == 1) {
 					ansi_fgcol = tgetstr("AF", &bp);
How-To-Repeat: 
Run "/bin/ls -G -C | less -E -r" and recognize that colors are gone,
because the pipe means ls' stdout is no longer connected to a tty.
Comment 1 joe freebsd_committer freebsd_triage 2000-08-08 14:52:02 UTC
Responsible Changed
From-To: freebsd-bugs->joe

I'll take this one
Comment 2 Sheldon Hearn 2000-08-08 15:44:50 UTC
On Tue, 08 Aug 2000 06:52:23 MST, joe@FreeBSD.ORG wrote:

> Synopsis: allowing old colors+paging combination with ls(1) again
> 
> Responsible-Changed-From-To: freebsd-bugs->joe
> Responsible-Changed-By: joe
> Responsible-Changed-When: Tue Aug 8 06:52:02 PDT 2000
> Responsible-Changed-Why: 
> I'll take this one

Looks closely related to PR 20291, which also includes a patch.

Ciao,
Sheldon.
Comment 3 joe freebsd_committer freebsd_triage 2000-08-12 23:42:30 UTC
State Changed
From-To: open->suspended

This is now fixed in -current. 

Colour support is enabled using the CLICOLOR variable instead of 
the -G flag (which has been depricated).  In addition you can 
force ls to output colour sequences if the output isn't directed 
to a tty by defining CLICOLOR_FORCE. 

Your fix is therefore: 

CLICOLOR=1; export CLICOLOR 
ls () { CLICOLOR_FORCE=1 /bin/ls -C "$@"    | less -E -r } 
ll () { CLICOLOR_FORCE=1 /bin/ls -l "$@"    | less -E -r } 
lx () { CLICOLOR_FORCE=1 /bin/ls -l -a "$@" | less -E -r } 

... or at least it will be when I backport the change to 4.x. 

This PR will remain suspended until the MFC has been done.
Comment 4 joe freebsd_committer freebsd_triage 2000-08-18 10:17:55 UTC
State Changed
From-To: suspended->closed

The CLICOLOR and CLICOLOR_FORCE environment changes have 
been backported to RELENG_4.