$OpenBSD: patch-ls_c,v 1.9 2007/08/11 17:52:11 naddy Exp $ --- ls.c.orig Mon May 7 20:39:28 2007 +++ ls.c Sat Aug 11 19:28:48 2007 @@ -62,6 +62,10 @@ static char rcsid[] = "$OpenBSD: ls.c,v 1.29 2007/05/0 #include #include #include +#ifdef COLORLS +#include +#include +#endif #include "ls.h" #include "extern.h" @@ -104,6 +108,15 @@ int f_statustime; /* use time of last mode change */ int f_stream; /* stream format */ int f_type; /* add type character for non-regular files */ int f_typedir; /* add type character for directories */ +#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; @@ -115,6 +128,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)) { @@ -132,7 +150,7 @@ ls_main(int argc, char *argv[]) f_listdot = 1; fts_options = FTS_PHYSICAL; - while ((ch = getopt(argc, argv, "1ACFLRSTacdfghiklmnopqrstux")) != -1) { + while ((ch = getopt(argc, argv, "1ACFGLRSTacdfghiklmnopqrstux")) != -1) { switch (ch) { /* * The -1, -C and -l, -m and -x options all override each @@ -185,6 +203,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; @@ -254,11 +275,46 @@ ls_main(int argc, char *argv[]) if (f_grouponly == -1) f_grouponly = 0; + /* 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;