diff options
author | Eric Andersen | 2002-03-20 09:13:48 +0000 |
---|---|---|
committer | Eric Andersen | 2002-03-20 09:13:48 +0000 |
commit | 3ad0bd956385fa695c5f6c0bddb945d7f9fe6ab2 (patch) | |
tree | 61459316807cd79e366d14d77a768137fecd1c8f /coreutils | |
parent | 3ec5c692ecfe211b1ed217a704d00ae32d8bfed4 (diff) | |
download | busybox-3ad0bd956385fa695c5f6c0bddb945d7f9fe6ab2.zip busybox-3ad0bd956385fa695c5f6c0bddb945d7f9fe6ab2.tar.gz |
Patch from J.W.Janssen <JanWillem.Janssen@lxtreme.nl> to provide
color ls support, modifed by me to behave properly when not running
output to a terminal (i.e. 'ls | more')
-Erik
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/ls.c | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index f2512fb..bbc9114 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -145,6 +145,14 @@ static const int SPLIT_SUBDIR = 2; #ifdef CONFIG_FEATURE_LS_FILETYPES #define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)]) #endif +/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ +#ifdef CONFIG_FEATURE_LS_COLOR +static int show_color = 0; +#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ + "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) +#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ + "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)]) +#endif /* * a directory entry and its stat info are stored here @@ -222,6 +230,31 @@ static void newline(void) } /*----------------------------------------------------------------------*/ +#ifdef CONFIG_FEATURE_LS_COLOR +static char fgcolor(mode_t mode) +{ + /* Check wheter the file is existing (if so, color it red!) */ + if ( errno == ENOENT ) { + errno = 0; + return '\037'; + } + if ( LIST_EXEC && S_ISREG( mode ) + && ( mode & ( S_IXUSR | S_IXGRP | S_IXOTH ) ) ) + return COLOR(0xF000); /* File is executable ... */ + return COLOR(mode); +} + +/*----------------------------------------------------------------------*/ +static char bgcolor(mode_t mode) +{ + if ( LIST_EXEC && S_ISREG( mode ) + && ( mode & ( S_IXUSR | S_IXGRP | S_IXOTH ) ) ) + return ATTR(0xF000); /* File is executable ... */ + return ATTR(mode); +} +#endif + +/*----------------------------------------------------------------------*/ #ifdef CONFIG_FEATURE_LS_FILETYPES static char append_char(mode_t mode) { @@ -682,19 +715,42 @@ static int list_single(struct dnode *dn) break; #endif case LIST_FILENAME: +#ifdef CONFIG_FEATURE_LS_COLOR + if (show_color && !lstat(dn->fullname, &info)) { + printf( "\033[%d;%dm", bgcolor(info.st_mode), + fgcolor(info.st_mode) ); + } +#endif printf("%s", dn->name); +#ifdef CONFIG_FEATURE_LS_COLOR + if (show_color) { + printf( "\033[0m" ); + } +#endif column += strlen(dn->name); break; case LIST_SYMLINK: if (S_ISLNK(dn->dstat.st_mode)) { char *lpath = xreadlink(dn->fullname); if (lpath) { - printf(" -> %s", lpath); -#ifdef CONFIG_FEATURE_LS_FILETYPES + printf(" -> "); +#if defined(BB_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR) if (!stat(dn->fullname, &info)) { append = append_char(info.st_mode); } #endif +#ifdef CONFIG_FEATURE_LS_COLOR + if (show_color) { + printf( "\033[%d;%dm", bgcolor(info.st_mode), + fgcolor(info.st_mode) ); + } +#endif + printf("%s", lpath); +#ifdef CONFIG_FEATURE_LS_COLOR + if (show_color) { + printf( "\033[0m" ); + } +#endif column += strlen(lpath) + 4; free(lpath); } @@ -747,6 +803,11 @@ extern int ls_main(int argc, char **argv) #endif nfiles=0; +#ifdef CONFIG_FEATURE_LS_COLOR + if (isatty(fileno(stdout))) + show_color = 1; +#endif + /* process options */ while ((opt = getopt(argc, argv, "1AaCdgilnsx" #ifdef CONFIG_FEATURE_AUTOWIDTH |