summaryrefslogtreecommitdiffstats
path: root/bin/ls/ls.c
diff options
context:
space:
mode:
authorjoe <joe@FreeBSD.org>2000-06-02 14:53:42 +0000
committerjoe <joe@FreeBSD.org>2000-06-02 14:53:42 +0000
commit9ad4da2e704de18c0d98495a5cdb9f62cb2e51ad (patch)
tree02f28297b98ef50fecef93835542760c91059cb8 /bin/ls/ls.c
parenta92f3a6a0abe25f8ef1b9690fca480fd0328d65d (diff)
downloadFreeBSD-src-9ad4da2e704de18c0d98495a5cdb9f62cb2e51ad.zip
FreeBSD-src-9ad4da2e704de18c0d98495a5cdb9f62cb2e51ad.tar.gz
Add colour support to /bin/ls (at a cost of 1056 bytes on my system).
It is not switched on by default and must be enabled with the -G flag. When using ls -G the output behaviour is modified with ANSI colour sequences wrapped around filenames to help distinguish file types. (Colours can be redefined in the LSCOLORS environment variable as described in the manual page.) Colour support is silently disabled (if switched on) if stdout isn't a tty. Based on: asami's colorls port. PR: bin/18900 && ports/18616.
Diffstat (limited to 'bin/ls/ls.c')
-rw-r--r--bin/ls/ls.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/bin/ls/ls.c b/bin/ls/ls.c
index 754236b..e6b12f7 100644
--- a/bin/ls/ls.c
+++ b/bin/ls/ls.c
@@ -111,6 +111,7 @@ int f_statustime; /* use time of last mode change */
int f_timesort; /* sort by time vice name */
int f_type; /* add type character for non-regular files */
int f_whiteout; /* show whiteout entries */
+int f_color; /* add type in color for non-regular files */
int rval;
@@ -148,7 +149,7 @@ main(argc, argv)
f_listdot = 1;
fts_options = FTS_PHYSICAL;
- while ((ch = getopt(argc, argv, "1ABCFHLPRTWabcdfgiklnoqrstu")) != -1) {
+ while ((ch = getopt(argc, argv, "1ABCFGHLPRTWabcdfgiklnoqrstu")) != -1) {
switch (ch) {
/*
* The -1, -C and -l options all override each other so shell
@@ -186,6 +187,10 @@ main(argc, argv)
case 'H':
fts_options |= FTS_COMFOLLOW;
break;
+ case 'G':
+ if (isatty(STDOUT_FILENO))
+ f_color = 1;
+ break;
case 'L':
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
@@ -259,11 +264,16 @@ main(argc, argv)
argc -= optind;
argv += optind;
+ if (f_color)
+ parsecolors(getenv("LSCOLORS"));
+
/*
* If not -F, -i, -l, -s or -t options, don't require stat
- * information.
+ * information, unless in color mode in which case we do
+ * need this to determine which colors to display.
*/
- if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type)
+ if (!f_inode && !f_longform && !f_size && !f_timesort && !f_type
+ && !f_color)
fts_options |= FTS_NOSTAT;
/*
OpenPOWER on IntegriCloud