$OpenBSD: patch-ls_c,v 1.4 2003/08/27 00:41:51 naddy Exp $
--- ls.c.orig	2003-08-27 00:37:00.000000000 +0200
+++ ls.c	2003-08-27 00:47:19.000000000 +0200
@@ -61,6 +61,10 @@ static char rcsid[] = "$OpenBSD: ls.c,v 
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#ifdef COLORLS
+#include <termcap.h>
+#include <signal.h>
+#endif
 
 #include "ls.h"
 #include "extern.h"
@@ -105,6 +109,15 @@ int f_dirname;			/* if precede with dire
 int f_type;			/* add type character for non-regular files */
 int f_typedir;			/* add type character for directories */
 int f_whiteout;			/* show whiteout entries */
+#ifdef COLORLS
+int f_color;			/* add type in color for non-regular files */
+
+char *ansi_bgcol;		/* ANSI sequence to set background color */
+char *ansi_fgcol;		/* ANSI sequence to set foreground color */
+char *ansi_coloff;		/* ANSI sequence to reset colors */
+char *attrs_off;		/* ANSI sequence to turn off attributes */
+char *enter_bold;		/* ANSI sequence to set color to bold mode */
+#endif
 
 int rval;
 
@@ -116,6 +129,11 @@ ls_main(int argc, char *argv[])
 	int ch, fts_options, notused;
 	int kflag = 0;
 	char *p;
+#ifdef COLORLS
+	char termcapbuf[1024];	/* termcap definition buffer */
+	char tcapbuf[512];	/* capability buffer */
+	char *bp = tcapbuf;
+#endif
 
 	/* Terminal defaults to -Cq, non-terminal defaults to -1. */
 	if (isatty(STDOUT_FILENO)) {
@@ -133,7 +151,7 @@ ls_main(int argc, char *argv[])
 		f_listdot = 1;
 
 	fts_options = FTS_PHYSICAL;
-	while ((ch = getopt(argc, argv, "1ACFLRSTWacdfghiklmnopqrstux")) != -1) {
+	while ((ch = getopt(argc, argv, "1ACFGLRSTWacdfghiklmnopqrstux")) != -1) {
 		switch (ch) {
 		/*
 		 * The -1, -C and -l, -m and -x options all override each
@@ -178,6 +196,9 @@ ls_main(int argc, char *argv[])
 		case 'F':
 			f_type = 1;
 			break;
+		case 'G':
+			setenv("CLICOLOR", "", 1);
+			break;
 		case 'L':
 			fts_options &= ~FTS_PHYSICAL;
 			fts_options |= FTS_LOGICAL;
@@ -245,11 +266,46 @@ ls_main(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
+	/* Enabling of colours is conditional on the environment. */
+	if (getenv("CLICOLOR") &&
+	    (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")))
+#ifdef COLORLS
+		if (tgetent(termcapbuf, getenv("TERM")) == 1) {
+			ansi_fgcol = tgetstr("AF", &bp);
+			ansi_bgcol = tgetstr("AB", &bp);
+			attrs_off = tgetstr("me", &bp);
+			enter_bold = tgetstr("md", &bp);
+
+			/* To switch colours off use 'op' if
+			 * available, otherwise use 'oc', or
+			 * don't do colours at all. */
+			ansi_coloff = tgetstr("op", &bp);
+			if (!ansi_coloff)
+				ansi_coloff = tgetstr("oc", &bp);
+			if (ansi_fgcol && ansi_bgcol && ansi_coloff)
+				f_color = 1;
+		}
+#else
+		warnx("color support not compiled in");
+#endif /*COLORLS*/
+ 
+#ifdef COLORLS
+	if (f_color) {
+		(void)signal(SIGINT, colorquit);
+		(void)signal(SIGQUIT, colorquit);
+		parsecolors(getenv("LSCOLORS"));
+	}
+#endif
+
 	/*
 	 * If not -F, -i, -l, -p, -S, -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_longform && !f_inode && !f_size && !f_type && !f_typedir &&
+#ifdef COLORLS
+	    !f_color &&
+#endif
 	    sortkey == BY_NAME)
 		fts_options |= FTS_NOSTAT;
 
