From 8ee6de6dd8376a3c087e94fe32d1b6e97a44a862 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 17 Mar 2019 15:03:40 -0600 Subject: color: Match the behaviour of GNU ls more closely --- color.c | 42 ++++++++++++-------------- tests.sh | 61 ++++++++++++++++++++++++++++++++++++-- tests/test_color.out | 4 +-- tests/test_color_L.out | 6 ++-- tests/test_color_ext.out | 4 +-- tests/test_color_ext0.out | 4 +-- tests/test_color_ext_override.out | 4 +-- tests/test_color_ext_underride.out | 4 +-- tests/test_color_mh.out | 4 +-- tests/test_color_mh0.out | 4 +-- tests/test_color_mi.out | 4 +-- tests/test_color_missing_colon.out | 4 +-- tests/test_color_or.out | 2 +- tests/test_color_or0_mi.out | 2 +- tests/test_color_or0_mi0.out | 17 +++++++++++ tests/test_color_or_mi.out | 2 +- tests/test_color_or_mi0.out | 2 +- tests/test_color_st0_tw0_ow.out | 17 +++++++++++ tests/test_color_st0_tw0_ow0.out | 17 +++++++++++ tests/test_color_st0_tw_ow.out | 17 +++++++++++ tests/test_color_st0_tw_ow0.out | 17 +++++++++++ tests/test_color_st_tw0_ow.out | 17 +++++++++++ tests/test_color_st_tw0_ow0.out | 17 +++++++++++ tests/test_color_st_tw_ow0.out | 17 +++++++++++ tests/test_color_su0_sg.out | 17 +++++++++++ tests/test_color_su0_sg0.out | 17 +++++++++++ tests/test_color_su_sg0.out | 17 +++++++++++ 27 files changed, 289 insertions(+), 51 deletions(-) create mode 100644 tests/test_color_or0_mi0.out create mode 100644 tests/test_color_st0_tw0_ow.out create mode 100644 tests/test_color_st0_tw0_ow0.out create mode 100644 tests/test_color_st0_tw_ow.out create mode 100644 tests/test_color_st0_tw_ow0.out create mode 100644 tests/test_color_st_tw0_ow.out create mode 100644 tests/test_color_st_tw0_ow0.out create mode 100644 tests/test_color_st_tw_ow0.out create mode 100644 tests/test_color_su0_sg.out create mode 100644 tests/test_color_su0_sg0.out create mode 100644 tests/test_color_su_sg0.out diff --git a/color.c b/color.c index e8f37ab..e7c2958 100644 --- a/color.c +++ b/color.c @@ -236,12 +236,13 @@ struct colors *parse_colors(const char *ls_colors) { colors->dir = "01;34"; colors->link = "01;36"; colors->multi_hard = NULL; - colors->pipe = "40;33"; + colors->pipe = "33"; colors->socket = "01;35"; colors->door = "01;35"; - colors->block = "40;33;01"; - colors->chardev = "40;33;01"; - colors->orphan = "40;31;01"; + colors->block = "01;33"; + colors->chardev = "01;33"; + colors->orphan = NULL; + colors->missing = NULL; colors->setuid = "37;41"; colors->setgid = "30;43"; colors->capable = "30;41"; @@ -249,8 +250,8 @@ struct colors *parse_colors(const char *ls_colors) { colors->ow = "34;42"; colors->sticky = "37;44"; colors->exec = "01;32"; - colors->warning = "40;33;01"; - colors->error = "40;31;01"; + colors->warning = "01;33"; + colors->error = "01;31"; colors->data = NULL; trie_init(&colors->ext_colors); @@ -369,17 +370,15 @@ static const char *file_color(const struct colors *colors, const char *filename, switch (sb->mode & S_IFMT) { case S_IFREG: - if (sb->mode & S_ISUID) { + if (colors->setuid && (sb->mode & 04000)) { color = colors->setuid; - } else if (sb->mode & S_ISGID) { + } else if (colors->setgid && (sb->mode & 02000)) { color = colors->setgid; } else if (colors->capable && bfs_check_capabilities(ftwbuf)) { color = colors->capable; - } else if (sb->mode & 0111) { + } else if (colors->exec && (sb->mode & 00111)) { color = colors->exec; - } - - if (!color && sb->nlink > 1) { + } else if (colors->multi_hard && sb->nlink > 1) { color = colors->multi_hard; } @@ -394,26 +393,23 @@ static const char *file_color(const struct colors *colors, const char *filename, break; case S_IFDIR: - if (sb->mode & S_ISVTX) { - if (sb->mode & S_IWOTH) { - color = colors->sticky_ow; - } else { - color = colors->sticky; - } - } else if (sb->mode & S_IWOTH) { + if (colors->sticky_ow && (sb->mode & 01002) == 01002) { + color = colors->sticky_ow; + } else if (colors->ow && (sb->mode & 00002)) { color = colors->ow; - } - - if (!color) { + } else if (colors->sticky && (sb->mode & 01000)) { + color = colors->sticky; + } else { color = colors->dir; } break; case S_IFLNK: - color = colors->link; if (colors->orphan && xfaccessat(ftwbuf->at_fd, ftwbuf->at_path, F_OK) != 0) { color = colors->orphan; + } else { + color = colors->link; } break; diff --git a/tests.sh b/tests.sh index 61e1273..05a3457 100755 --- a/tests.sh +++ b/tests.sh @@ -627,8 +627,19 @@ bfs_tests=( test_color_or test_color_mi test_color_or_mi - test_color_or0_mi test_color_or_mi0 + test_color_or0_mi + test_color_or0_mi0 + test_color_su_sg0 + test_color_su0_sg + test_color_su0_sg0 + test_color_st_tw_ow0 + test_color_st_tw0_ow + test_color_st_tw0_ow0 + test_color_st0_tw_ow + test_color_st0_tw_ow0 + test_color_st0_tw0_ow + test_color_st0_tw0_ow0 test_color_ext test_color_ext0 test_color_ext_override @@ -1852,12 +1863,56 @@ function test_color_or_mi() { LS_COLORS="or=01;31:mi=01;33:" bfs_diff rainbow -color } +function test_color_or_mi0() { + LS_COLORS="or=01;31:mi=00:" bfs_diff rainbow -color +} + function test_color_or0_mi() { LS_COLORS="or=00:mi=01;33:" bfs_diff rainbow -color } -function test_color_or_mi0() { - LS_COLORS="or=01;31:mi=00:" bfs_diff rainbow -color +function test_color_or0_mi0() { + LS_COLORS="or=00:mi=00:" bfs_diff rainbow -color +} + +function test_color_su_sg0() { + LS_COLORS="su=37;41:sg=00:" bfs_diff rainbow -color +} + +function test_color_su0_sg() { + LS_COLORS="su=00:sg=30;43:" bfs_diff rainbow -color +} + +function test_color_su0_sg0() { + LS_COLORS="su=00:sg=00:" bfs_diff rainbow -color +} + +function test_color_st_tw_ow0() { + LS_COLORS="st=37;44:tw=40;32:ow=00:" bfs_diff rainbow -color +} + +function test_color_st_tw0_ow() { + LS_COLORS="st=37;44:tw=00:ow=34;42:" bfs_diff rainbow -color +} + +function test_color_st_tw0_ow0() { + LS_COLORS="st=37;44:tw=00:ow=00:" bfs_diff rainbow -color +} + +function test_color_st0_tw_ow() { + LS_COLORS="st=00:tw=40;32:ow=34;42:" bfs_diff rainbow -color +} + +function test_color_st0_tw_ow0() { + LS_COLORS="st=00:tw=40;32:ow=00:" bfs_diff rainbow -color +} + +function test_color_st0_tw0_ow() { + LS_COLORS="st=00:tw=00:ow=34;42:" bfs_diff rainbow -color +} + +function test_color_st0_tw0_ow0() { + LS_COLORS="st=00:tw=00:ow=00:" bfs_diff rainbow -color } function test_color_ext() { diff --git a/tests/test_color.out b/tests/test_color.out index 0acd9f0..98d459f 100644 --- a/tests/test_color.out +++ b/tests/test_color.out @@ -1,16 +1,16 @@ rainbow rainbow/exec.sh rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/file.dat rainbow/file.txt rainbow/mh1 diff --git a/tests/test_color_L.out b/tests/test_color_L.out index 9e17614..b93716f 100644 --- a/tests/test_color_L.out +++ b/tests/test_color_L.out @@ -1,15 +1,15 @@ rainbow rainbow/exec.sh +rainbow/chardev_link rainbow/socket +rainbow/broken rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/chardev_link -rainbow/pipe rainbow/file.dat rainbow/file.txt rainbow/link.txt diff --git a/tests/test_color_ext.out b/tests/test_color_ext.out index c0bfe44..fff32ef 100644 --- a/tests/test_color_ext.out +++ b/tests/test_color_ext.out @@ -1,17 +1,17 @@ rainbow rainbow/exec.sh rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/file.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/file.dat rainbow/mh1 rainbow/mh2 diff --git a/tests/test_color_ext0.out b/tests/test_color_ext0.out index aa4c582..c9bc052 100644 --- a/tests/test_color_ext0.out +++ b/tests/test_color_ext0.out @@ -2,16 +2,16 @@ rainbow/file.txt rainbow/exec.sh rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/file.dat rainbow/mh1 rainbow/mh2 diff --git a/tests/test_color_ext_override.out b/tests/test_color_ext_override.out index ba895ec..cd3231d 100644 --- a/tests/test_color_ext_override.out +++ b/tests/test_color_ext_override.out @@ -3,15 +3,15 @@ rainbow/file.dat rainbow/file.txt rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/mh1 rainbow/mh2 diff --git a/tests/test_color_ext_underride.out b/tests/test_color_ext_underride.out index c9e21f2..127cc66 100644 --- a/tests/test_color_ext_underride.out +++ b/tests/test_color_ext_underride.out @@ -3,15 +3,15 @@ rainbow/exec.sh rainbow/file.dat rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/mh1 rainbow/mh2 diff --git a/tests/test_color_mh.out b/tests/test_color_mh.out index e10ee10..4651cf1 100644 --- a/tests/test_color_mh.out +++ b/tests/test_color_mh.out @@ -1,17 +1,17 @@ rainbow rainbow/exec.sh rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/mh1 rainbow/mh2 rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/file.dat rainbow/file.txt diff --git a/tests/test_color_mh0.out b/tests/test_color_mh0.out index 0acd9f0..98d459f 100644 --- a/tests/test_color_mh0.out +++ b/tests/test_color_mh0.out @@ -1,16 +1,16 @@ rainbow rainbow/exec.sh rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/file.dat rainbow/file.txt rainbow/mh1 diff --git a/tests/test_color_mi.out b/tests/test_color_mi.out index 0acd9f0..98d459f 100644 --- a/tests/test_color_mi.out +++ b/tests/test_color_mi.out @@ -1,16 +1,16 @@ rainbow rainbow/exec.sh rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/file.dat rainbow/file.txt rainbow/mh1 diff --git a/tests/test_color_missing_colon.out b/tests/test_color_missing_colon.out index c0bfe44..fff32ef 100644 --- a/tests/test_color_missing_colon.out +++ b/tests/test_color_missing_colon.out @@ -1,17 +1,17 @@ rainbow rainbow/exec.sh rainbow/socket +rainbow/broken rainbow/chardev_link rainbow/link.txt rainbow/file.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/broken -rainbow/pipe rainbow/file.dat rainbow/mh1 rainbow/mh2 diff --git a/tests/test_color_or.out b/tests/test_color_or.out index 5103be1..4668cc0 100644 --- a/tests/test_color_or.out +++ b/tests/test_color_or.out @@ -6,11 +6,11 @@ rainbow/broken rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/pipe rainbow/file.dat rainbow/file.txt rainbow/mh1 diff --git a/tests/test_color_or0_mi.out b/tests/test_color_or0_mi.out index 37b54c5..98d459f 100644 --- a/tests/test_color_or0_mi.out +++ b/tests/test_color_or0_mi.out @@ -6,11 +6,11 @@ rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/pipe rainbow/file.dat rainbow/file.txt rainbow/mh1 diff --git a/tests/test_color_or0_mi0.out b/tests/test_color_or0_mi0.out new file mode 100644 index 0000000..98d459f --- /dev/null +++ b/tests/test_color_or0_mi0.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sticky_ow +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/test_color_or_mi.out b/tests/test_color_or_mi.out index 6727548..8bf605e 100644 --- a/tests/test_color_or_mi.out +++ b/tests/test_color_or_mi.out @@ -6,11 +6,11 @@ rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/pipe rainbow/file.dat rainbow/file.txt rainbow/mh1 diff --git a/tests/test_color_or_mi0.out b/tests/test_color_or_mi0.out index 6727548..8bf605e 100644 --- a/tests/test_color_or_mi0.out +++ b/tests/test_color_or_mi0.out @@ -6,11 +6,11 @@ rainbow/link.txt rainbow/sticky_ow rainbow/sgid +rainbow/pipe rainbow/ow rainbow/sugid rainbow/suid rainbow/sticky -rainbow/pipe rainbow/file.dat rainbow/file.txt rainbow/mh1 diff --git a/tests/test_color_st0_tw0_ow.out b/tests/test_color_st0_tw0_ow.out new file mode 100644 index 0000000..51ca9d2 --- /dev/null +++ b/tests/test_color_st0_tw0_ow.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/sticky +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sticky_ow +rainbow/sugid +rainbow/suid +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/test_color_st0_tw0_ow0.out b/tests/test_color_st0_tw0_ow0.out new file mode 100644 index 0000000..e86ae35 --- /dev/null +++ b/tests/test_color_st0_tw0_ow0.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/ow +rainbow/sticky +rainbow/sticky_ow +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sgid +rainbow/pipe +rainbow/sugid +rainbow/suid +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/test_color_st0_tw_ow.out b/tests/test_color_st0_tw_ow.out new file mode 100644 index 0000000..efb7b39 --- /dev/null +++ b/tests/test_color_st0_tw_ow.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/sticky +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky_ow +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/test_color_st0_tw_ow0.out b/tests/test_color_st0_tw_ow0.out new file mode 100644 index 0000000..16c6f53 --- /dev/null +++ b/tests/test_color_st0_tw_ow0.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/ow +rainbow/sticky +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sgid +rainbow/pipe +rainbow/sugid +rainbow/suid +rainbow/sticky_ow +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/test_color_st_tw0_ow.out b/tests/test_color_st_tw0_ow.out new file mode 100644 index 0000000..6c2346c --- /dev/null +++ b/tests/test_color_st_tw0_ow.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sticky_ow +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/test_color_st_tw0_ow0.out b/tests/test_color_st_tw0_ow0.out new file mode 100644 index 0000000..242622d --- /dev/null +++ b/tests/test_color_st_tw0_ow0.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/ow +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sgid +rainbow/pipe +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/sticky_ow +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/test_color_st_tw_ow0.out b/tests/test_color_st_tw_ow0.out new file mode 100644 index 0000000..c3b5af1 --- /dev/null +++ b/tests/test_color_st_tw_ow0.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/ow +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sgid +rainbow/pipe +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/sticky_ow +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/test_color_su0_sg.out b/tests/test_color_su0_sg.out new file mode 100644 index 0000000..510c431 --- /dev/null +++ b/tests/test_color_su0_sg.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sticky_ow +rainbow/sgid +rainbow/sugid +rainbow/pipe +rainbow/ow +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 +rainbow/suid diff --git a/tests/test_color_su0_sg0.out b/tests/test_color_su0_sg0.out new file mode 100644 index 0000000..d018c2a --- /dev/null +++ b/tests/test_color_su0_sg0.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sticky_ow +rainbow/pipe +rainbow/ow +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 +rainbow/sgid +rainbow/sugid +rainbow/suid diff --git a/tests/test_color_su_sg0.out b/tests/test_color_su_sg0.out new file mode 100644 index 0000000..dffb3c5 --- /dev/null +++ b/tests/test_color_su_sg0.out @@ -0,0 +1,17 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sticky_ow +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 +rainbow/sgid -- cgit v1.2.3