aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2013-09-01 08:00:41 -0500
committerRob Landley <rob@landley.net>2013-09-01 08:00:41 -0500
commit408d4f2670df910180988747f11b6290551ddaa7 (patch)
tree745642cdcd47d4e9aa04cec21effa4d200b69856
parentb7162a47e71a69e30bb9103b5caae72e5097f68f (diff)
downloadtoybox-408d4f2670df910180988747f11b6290551ddaa7.tar.gz
Implement ls --color=auto, suggested by Rich Felker.
-rwxr-xr-xscripts/make.sh2
-rw-r--r--toys/posix/ls.c16
2 files changed, 12 insertions, 6 deletions
diff --git a/scripts/make.sh b/scripts/make.sh
index 6266dbf6..ecc92686 100755
--- a/scripts/make.sh
+++ b/scripts/make.sh
@@ -68,7 +68,7 @@ function getflags()
sed -n -e "s/.*TOY($FLX"',[ \t]*"\([^"]*\)"[ \t]*,.*)/\1/' \
-e 't keep;d;:keep' -e 's/^[<>=][0-9]//' -e 's/[?&^]//' \
-e 't keep' -e 's/[><=][0-9][0-9]*//g' -e 's/+.//g' \
- -e 's/\[[^]]*\]//g' -e 's/[-?^:&#|@* ]//g' "$@" -e 'p'
+ -e 's/\[[^]]*\]//g' -e 's/[-?^:&#|@* ;]//g' "$@" -e 'p'
}
# Extract global structure definitions and flag definitions from toys/*/*.c
diff --git a/toys/posix/ls.c b/toys/posix/ls.c
index b1807c25..3bad9e3d 100644
--- a/toys/posix/ls.c
+++ b/toys/posix/ls.c
@@ -5,7 +5,7 @@
*
* See http://opengroup.org/onlinepubs/9699919799/utilities/ls.html
-USE_LS(NEWTOY(ls, USE_LS_COLOR("(color)")"goACFHLRSacdfiklmnpqrstux1[-1Cglmnox][-cu][-ftS][-HL]", TOYFLAG_BIN))
+USE_LS(NEWTOY(ls, USE_LS_COLOR("(color):;")"goACFHLRSacdfiklmnpqrstux1[-1Cglmnox][-cu][-ftS][-HL]", TOYFLAG_BIN))
config LS
bool "ls"
@@ -37,10 +37,11 @@ config LS_COLOR
default y
depends on LS
help
- usage: ls --color
+ usage: ls --color[=auto]
--color device=yellow symlink=turquoise/red dir=blue socket=purple
files: exe=green suid=red suidfile=redback stickydir=greenback
+ =auto means detect if output is a tty.
*/
#define FOR_ls
@@ -51,6 +52,8 @@ config LS_COLOR
// ls -lR starts .: then ./subdir:
GLOBALS(
+ char *color;
+
struct dirtree *files;
unsigned screen_width;
@@ -381,7 +384,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
xprintf(" %s ", thyme);
}
- if ((flags & FLAG_color) && TT.screen_width) {
+ if (flags & FLAG_color) {
color = color_from_mode(st->st_mode);
if (color) printf("\033[%d;%dm", color>>8, color&255);
}
@@ -394,7 +397,7 @@ static void listfiles(int dirfd, struct dirtree *indir)
if ((flags & (FLAG_l|FLAG_o|FLAG_n|FLAG_g)) && S_ISLNK(mode)) {
printf(" -> ");
- if ((flags & FLAG_color) && TT.screen_width) {
+ if (flags & FLAG_color) {
struct stat st2;
if (fstatat(dirfd, sort[next]->symlink, &st2, 0)) color = 256+31;
@@ -442,7 +445,10 @@ void ls_main(void)
if (TT.screen_width<2) TT.screen_width = 2;
// Do we have an implied -1
- if (!isatty(1) || (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g)))
+ if (!isatty(1)) {
+ toys.optflags |= FLAG_1;
+ if (TT.color) toys.optflags ^= FLAG_color;
+ } else if (toys.optflags&(FLAG_l|FLAG_o|FLAG_n|FLAG_g))
toys.optflags |= FLAG_1;
else if (!(toys.optflags&(FLAG_1|FLAG_x|FLAG_m))) toys.optflags |= FLAG_C;
// The optflags parsing infrastructure should really do this for us,