aboutsummaryrefslogtreecommitdiffstats
path: root/opendircolors.c
diff options
context:
space:
mode:
authorCameron Katri <me@cameronkatri.com>2021-04-26 16:40:28 -0400
committerCameron Katri <me@cameronkatri.com>2021-04-26 16:40:28 -0400
commit219b0151ae954faa63cdc0abed49bb4ddb53d75f (patch)
treef5ce6975b39b449146b1c327974f39b3430aedf3 /opendircolors.c
parent2574ea03da54335dc1641696aa3bbf1cbe02f766 (diff)
downloadopendircolors-219b0151ae954faa63cdc0abed49bb4ddb53d75f.tar.gz
opendircolors-219b0151ae954faa63cdc0abed49bb4ddb53d75f.tar.zst
opendircolors-219b0151ae954faa63cdc0abed49bb4ddb53d75f.zip
All works
Diffstat (limited to 'opendircolors.c')
-rw-r--r--opendircolors.c97
1 files changed, 87 insertions, 10 deletions
diff --git a/opendircolors.c b/opendircolors.c
index f6ec9c2..726ad3f 100644
--- a/opendircolors.c
+++ b/opendircolors.c
@@ -32,28 +32,105 @@
#include <string.h>
#include <sysexits.h>
+#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);
}