From 219b0151ae954faa63cdc0abed49bb4ddb53d75f Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Mon, 26 Apr 2021 16:40:28 -0400 Subject: All works --- opendircolors.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 10 deletions(-) (limited to 'opendircolors.c') diff --git a/opendircolors.c b/opendircolors.c index f6ec9c2..726ad3f 100644 --- a/opendircolors.c +++ b/opendircolors.c @@ -32,28 +32,105 @@ #include #include +#include "common.h" + void usage(void); -static const struct option long_options[] = { { "help", no_argument, NULL, - 'h' }, - { "bourne-shell", no_argument, NULL, 'b' }, - { "sh", no_argument, NULL, 'b' }, - { "print-database", no_argument, NULL, 'p' }, - { NULL, no_argument, NULL, 0 } }; +#define MAXKEYLEN 21 + +static const struct option long_options[] = { /* no clang-format */ + { "help", no_argument, NULL, 'h' }, /**/ + { "bourne-shell", no_argument, NULL, 'b' }, /**/ + { "sh", no_argument, NULL, 'b' }, /**/ + { "csh", no_argument, NULL, 'c' }, /**/ + { NULL, no_argument, NULL, 0 } +}; + +static const char *long_types[38] = { "NORMAL", "NORM", "FILE", "RESET", "DIR", + "LNK", "LINK", "SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK", + "BLK", "BLOCK", "CHR", "CHAR", "DOOR", "EXEC", "LEFT", "LEFTCODE", + "RIGHT", "RIGHTCODE", "END", "ENDCODE", "SUID", "SETUID", "SGID", + "SETGID", "STICKY", "OTHER_WRITABLE", "OWR", "STICKY_OTHER_WRITABLE", + "OWT", "CAPABILITY", "MULTIHARDLINK", "CLRTOEOL", NULL }; + +static const char *short_types[38] = { "no", "no", "fi", "rs", "di", "ln", "ln", + "ln", "or", "mi", "pi", "pi", "so", "bd", "bd", "cd", "cd", "do", "ex", + "lc", "lc", "rc", "rc", "ec", "ec", "su", "su", "sg", "sg", "st", "ow", + "ow", "tw", "tw", "ca", "mh", "cl", NULL }; int main(int argc, char **argv) { - /* TODO */ - usage(); + int ch; + char *prefix = "LS_COLORS='"; + char *suffix = "';\nexport LS_COLORS"; + char *lsprefix = "LSCOLORS='"; + char *lssuffix = "';\nexport LSCOLORS"; + + while ( + (ch = getopt_long(argc, argv, "hbc", long_options, NULL)) != -1) { + switch (ch) { + case 'b': + prefix = "LS_COLORS='"; + suffix = "';\nexport LS_COLORS"; + lsprefix = "LSCOLORS='"; + lssuffix = "';\nexport LSCOLORS"; + break; + case 'c': + prefix = "setenv LS_COLORS '"; + suffix = "'"; + lsprefix = "setenv LSCOLORS '"; + lssuffix = "'"; + break; + case 'h': + case '?': + default: + usage(); + } + } + argc -= optind; + argv += optind; + + char *path = *argv != NULL ? *argv++ : "-"; + FILE *fd = fopen(path, "r"); + + char *line = NULL; + size_t linecap = 0; + ssize_t linelen; + char *out = strdup(""); + while ((linelen = getline(&line, &linecap, fd)) > 0) { + if (*line == '#' || *line == '\n') + continue; + char fmttype[MAXKEYLEN] = "", val[MAXKEYLEN] = ""; + sscanf(line, "%s %s\n", fmttype, val); + if (*line == '.') { + sprintf(out + strlen(out), "*%s=%s:", fmttype, val); + continue; + ; + } else if (*line == '*') { + sprintf(out + strlen(out), "%s=%s:", fmttype, val); + continue; + } + for (int i = 0; i < 37; i++) { + if (strcmp(fmttype, long_types[i]) == 0) { + sprintf(out + strlen(out), + "%s=%s:", short_types[i], val); + break; + } + } + } + free(line); + printf("%s%s%s\n", prefix, out, suffix); + printf("%s%s%s\n", lsprefix, tolscolors(out), lssuffix); + free(out); /* NOTREACHED */ - return 0; + return (0); } void usage(void) { - (void)fprintf(stderr, "usage: %s [-bchp] [FILE]\n", getprogname()); + (void)fprintf(stderr, "usage: %s [-bch] [FILE]\n", getprogname()); exit(EX_USAGE); } -- cgit v1.2.3-56-ge451