aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-03-20 09:13:48 +0000
committerandersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277>2002-03-20 09:13:48 +0000
commitd1ad0628e10ec7442cb04fd3d007019694c73d53 (patch)
tree61459316807cd79e366d14d77a768137fecd1c8f
parentb4909dae43e481f740715a7220b906a4fdb27e6b (diff)
downloadbusybox-w32-d1ad0628e10ec7442cb04fd3d007019694c73d53.tar.gz
busybox-w32-d1ad0628e10ec7442cb04fd3d007019694c73d53.tar.bz2
busybox-w32-d1ad0628e10ec7442cb04fd3d007019694c73d53.zip
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 git-svn-id: svn://busybox.net/trunk/busybox@4431 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--coreutils/ls.c65
1 files changed, 63 insertions, 2 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c
index f2512fb9c..bbc9114d2 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -145,6 +145,14 @@ static const int SPLIT_SUBDIR = 2;
145#ifdef CONFIG_FEATURE_LS_FILETYPES 145#ifdef CONFIG_FEATURE_LS_FILETYPES
146#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)]) 146#define APPCHAR(mode) ("\0|\0\0/\0\0\0\0\0@\0=\0\0\0" [TYPEINDEX(mode)])
147#endif 147#endif
148/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
149#ifdef CONFIG_FEATURE_LS_COLOR
150static int show_color = 0;
151#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
152 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
153#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
154 "\00\00\01\00\01\00\00\01" [TYPEINDEX(mode)])
155#endif
148 156
149/* 157/*
150 * a directory entry and its stat info are stored here 158 * a directory entry and its stat info are stored here
@@ -222,6 +230,31 @@ static void newline(void)
222} 230}
223 231
224/*----------------------------------------------------------------------*/ 232/*----------------------------------------------------------------------*/
233#ifdef CONFIG_FEATURE_LS_COLOR
234static char fgcolor(mode_t mode)
235{
236 /* Check wheter the file is existing (if so, color it red!) */
237 if ( errno == ENOENT ) {
238 errno = 0;
239 return '\037';
240 }
241 if ( LIST_EXEC && S_ISREG( mode )
242 && ( mode & ( S_IXUSR | S_IXGRP | S_IXOTH ) ) )
243 return COLOR(0xF000); /* File is executable ... */
244 return COLOR(mode);
245}
246
247/*----------------------------------------------------------------------*/
248static char bgcolor(mode_t mode)
249{
250 if ( LIST_EXEC && S_ISREG( mode )
251 && ( mode & ( S_IXUSR | S_IXGRP | S_IXOTH ) ) )
252 return ATTR(0xF000); /* File is executable ... */
253 return ATTR(mode);
254}
255#endif
256
257/*----------------------------------------------------------------------*/
225#ifdef CONFIG_FEATURE_LS_FILETYPES 258#ifdef CONFIG_FEATURE_LS_FILETYPES
226static char append_char(mode_t mode) 259static char append_char(mode_t mode)
227{ 260{
@@ -682,19 +715,42 @@ static int list_single(struct dnode *dn)
682 break; 715 break;
683#endif 716#endif
684 case LIST_FILENAME: 717 case LIST_FILENAME:
718#ifdef CONFIG_FEATURE_LS_COLOR
719 if (show_color && !lstat(dn->fullname, &info)) {
720 printf( "\033[%d;%dm", bgcolor(info.st_mode),
721 fgcolor(info.st_mode) );
722 }
723#endif
685 printf("%s", dn->name); 724 printf("%s", dn->name);
725#ifdef CONFIG_FEATURE_LS_COLOR
726 if (show_color) {
727 printf( "\033[0m" );
728 }
729#endif
686 column += strlen(dn->name); 730 column += strlen(dn->name);
687 break; 731 break;
688 case LIST_SYMLINK: 732 case LIST_SYMLINK:
689 if (S_ISLNK(dn->dstat.st_mode)) { 733 if (S_ISLNK(dn->dstat.st_mode)) {
690 char *lpath = xreadlink(dn->fullname); 734 char *lpath = xreadlink(dn->fullname);
691 if (lpath) { 735 if (lpath) {
692 printf(" -> %s", lpath); 736 printf(" -> ");
693#ifdef CONFIG_FEATURE_LS_FILETYPES 737#if defined(BB_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
694 if (!stat(dn->fullname, &info)) { 738 if (!stat(dn->fullname, &info)) {
695 append = append_char(info.st_mode); 739 append = append_char(info.st_mode);
696 } 740 }
697#endif 741#endif
742#ifdef CONFIG_FEATURE_LS_COLOR
743 if (show_color) {
744 printf( "\033[%d;%dm", bgcolor(info.st_mode),
745 fgcolor(info.st_mode) );
746 }
747#endif
748 printf("%s", lpath);
749#ifdef CONFIG_FEATURE_LS_COLOR
750 if (show_color) {
751 printf( "\033[0m" );
752 }
753#endif
698 column += strlen(lpath) + 4; 754 column += strlen(lpath) + 4;
699 free(lpath); 755 free(lpath);
700 } 756 }
@@ -747,6 +803,11 @@ extern int ls_main(int argc, char **argv)
747#endif 803#endif
748 nfiles=0; 804 nfiles=0;
749 805
806#ifdef CONFIG_FEATURE_LS_COLOR
807 if (isatty(fileno(stdout)))
808 show_color = 1;
809#endif
810
750 /* process options */ 811 /* process options */
751 while ((opt = getopt(argc, argv, "1AaCdgilnsx" 812 while ((opt = getopt(argc, argv, "1AaCdgilnsx"
752#ifdef CONFIG_FEATURE_AUTOWIDTH 813#ifdef CONFIG_FEATURE_AUTOWIDTH