aboutsummaryrefslogtreecommitdiff
path: root/coreutils/ls.c
diff options
context:
space:
mode:
authorpgf <pgf@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-08-01 19:33:30 +0000
committerpgf <pgf@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-08-01 19:33:30 +0000
commitef4aaa23ad977961c2b8abc53512164314bff15e (patch)
tree7c84655b2121802fc8e6af14156b606ec041a054 /coreutils/ls.c
parent93f11bf3aa015cccb82b3838d99b961436be4240 (diff)
downloadbusybox-w32-ef4aaa23ad977961c2b8abc53512164314bff15e.tar.gz
busybox-w32-ef4aaa23ad977961c2b8abc53512164314bff15e.tar.bz2
busybox-w32-ef4aaa23ad977961c2b8abc53512164314bff15e.zip
commiting patch from bug 71:
0000071: patch: implement "--color" option for ls coloring control git-svn-id: svn://busybox.net/trunk/busybox@11009 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils/ls.c')
-rw-r--r--coreutils/ls.c63
1 files changed, 61 insertions, 2 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c
index 92e150966..75d7b1f33 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -60,6 +60,7 @@ enum {
60#include <fcntl.h> 60#include <fcntl.h>
61#include <signal.h> 61#include <signal.h>
62#include <termios.h> 62#include <termios.h>
63#include <getopt.h>
63#include <sys/ioctl.h> 64#include <sys/ioctl.h>
64#include <sys/sysmacros.h> /* major() and minor() */ 65#include <sys/sysmacros.h> /* major() and minor() */
65#include "busybox.h" 66#include "busybox.h"
@@ -164,8 +165,18 @@ enum {
164 165
165/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */ 166/* colored LS support by JaWi, janwillem.janssen@lxtreme.nl */
166#ifdef CONFIG_FEATURE_LS_COLOR 167#ifdef CONFIG_FEATURE_LS_COLOR
168
167static int show_color = 0; 169static int show_color = 0;
168 170
171/* long option entry used only for --color, which has no short option
172 * equivalent. */
173static int got_color_opt;
174static struct option ls_color_opt[] =
175{
176 {"color", optional_argument, &got_color_opt, 1},
177 {NULL, 0, NULL, 0}
178};
179
169#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\ 180#define COLOR(mode) ("\000\043\043\043\042\000\043\043"\
170 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)]) 181 "\000\000\044\000\043\000\000\040" [TYPEINDEX(mode)])
171#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\ 182#define ATTR(mode) ("\00\00\01\00\01\00\01\00"\
@@ -984,8 +995,7 @@ extern int ls_main(int argc, char **argv)
984#endif 995#endif
985 996
986#ifdef CONFIG_FEATURE_LS_COLOR 997#ifdef CONFIG_FEATURE_LS_COLOR
987 if (isatty(STDOUT_FILENO)) 998 bb_applet_long_options = ls_color_opt;
988 show_color = 1;
989#endif 999#endif
990 1000
991 /* process options */ 1001 /* process options */
@@ -1034,6 +1044,55 @@ extern int ls_main(int argc, char **argv)
1034 } 1044 }
1035 } 1045 }
1036 1046
1047#ifdef CONFIG_FEATURE_LS_COLOR
1048 if (got_color_opt) {
1049 /* there is no way for bb_getopt_ulflags() to
1050 * return us the argument string for long options
1051 * which don't have a short option equivalent.
1052 * all we can find out is that the option was
1053 * present, and we have to rescan to find the
1054 * argument string.
1055 */
1056 got_color_opt=0;
1057 optind = 1;
1058 while ((i = getopt_long (argc, argv, ls_options,
1059 ls_color_opt, NULL)) >= 0) {
1060 if (i != 0) continue;
1061 if (got_color_opt) {
1062 if (!optarg || strcmp("always", optarg) == 0)
1063 show_color = 1;
1064 else if (strcmp("never", optarg) == 0)
1065 show_color = 0;
1066 else if (strcmp("auto", optarg) == 0 &&
1067 isatty(STDOUT_FILENO))
1068 show_color = 1;
1069
1070 /* don't break; want to a) pick up repeated
1071 * --color options, and b) leave optind
1072 * set correctly when we're done.
1073 */
1074 got_color_opt = 0;
1075 }
1076 }
1077#if CONFIG_FEATURE_LS_COLOR_IS_DEFAULT
1078 } else {
1079 /* if no option set by user, then this config option
1080 * forces "auto", which is what busybox 1.00 and previous
1081 * did. however, provide one more "out" for users that
1082 * don't want color: if LS_COLOR is set, and is null or
1083 * "none" -- then default coloring to "off".
1084 */
1085 char *p;
1086 if ((p = getenv ("LS_COLORS")) != NULL &&
1087 (*p == '\0' || (strcmp(p, "none") == 0))) {
1088 show_color = 0;
1089 } else if (isatty(STDOUT_FILENO)) {
1090 show_color = 1;
1091 }
1092#endif
1093 }
1094#endif
1095
1037 /* sort out which command line options take precedence */ 1096 /* sort out which command line options take precedence */
1038#ifdef CONFIG_FEATURE_LS_RECURSIVE 1097#ifdef CONFIG_FEATURE_LS_RECURSIVE
1039 if (all_fmt & DISP_NOLIST) 1098 if (all_fmt & DISP_NOLIST)