]> git.cameronkatri.com Git - opendircolors.git/commitdiff
Sane errors, Linux support(?), stdin support
authorCameron Katri <me@cameronkatri.com>
Tue, 27 Apr 2021 02:02:19 +0000 (22:02 -0400)
committerCameron Katri <me@cameronkatri.com>
Tue, 27 Apr 2021 02:02:19 +0000 (22:02 -0400)
dirconvert.c
opendircolors.c

index 5169a0566b45909495ffb7e1f35160e9b91555f1..0b93884f81295aec6fbd48c808ff9838eed37d6e 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <sysexits.h>
+#include <libgen.h>
 
 #include "common.h"
 
-void usage(void);
+void usage(const char *);
 char *tols_colors(char *);
 
 int
 main(int argc, char **argv)
 {
        if (argc != 2)
-               usage();
+               usage(argv[0]);
 
-       if (strchr(argv[1], '=')) {
-               printf("%s\n", tolscolors(argv[1]));
-               return (0);
-       } else {
-               printf("%s\n", tols_colors(argv[1]));
-               return (0);
-       }
+       char *input = NULL;
+       if (strcmp(argv[1], "-") == 0) {
+               char *buf = NULL;
+               size_t linecap = 0;
+               ssize_t linelen;
+               getline(&buf, &linecap, stdin);
+               if (buf[sizeof(buf) - 1] == '\n')
+                       buf[sizeof(buf) - 1] = '\0';
+               input = strdup(buf);
+               free(buf);
+       } else
+               input = argv[1];
+
+       if (strchr(input, '='))
+               fprintf(stdout, "%s\n", tolscolors(input));
+       else
+               fprintf(stdout, "%s\n", tols_colors(input));
 
-       /* NOTREACHED */
        return (0);
 }
 
@@ -83,8 +93,11 @@ tols_colors(char *lscolors)
 }
 
 void
-usage(void)
+usage(const char *progname)
 {
-       (void)fprintf(stderr, "usage: %s LSCOLORS|LS_COLORS\n", getprogname());
+       char *path;
+       path = strdup(progname);
+
+       (void)fprintf(stderr, "usage: %s LSCOLORS|LS_COLORS\n", basename(path));
        exit(EX_USAGE);
 }
index 726ad3f7259ee7c7053502859d8cd80f37863f69..6f91f0d970e1a5fe2aa9e06c3f53e87887dfd473 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <sysexits.h>
+#include <errno.h>
+#include <libgen.h>
+#include <err.h>
 
 #include "common.h"
 
-void usage(void);
+void usage(const char*);
 
 #define MAXKEYLEN 21
 
@@ -62,6 +65,7 @@ int
 main(int argc, char **argv)
 {
        int ch;
+       FILE *fd;
        char *prefix = "LS_COLORS='";
        char *suffix = "';\nexport LS_COLORS";
        char *lsprefix = "LSCOLORS='";
@@ -85,14 +89,22 @@ main(int argc, char **argv)
                case 'h':
                case '?':
                default:
-                       usage();
+                       usage(argv[0]);
                }
        }
        argc -= optind;
        argv += optind;
 
-       char *path = *argv != NULL ? *argv++ : "-";
-       FILE *fd = fopen(path, "r");
+       if (argc != 1)
+               usage(argv[1]);
+
+       char *path = *argv;
+       if (strcmp(path, "-") == 0)
+               fd = stdin;
+       else if ((fd = fopen(path, "r")) == NULL) {
+               warnx("%s: %s\n", path, strerror(errno));
+               return(errno);
+       }
 
        char *line = NULL;
        size_t linecap = 0;
@@ -119,18 +131,23 @@ main(int argc, char **argv)
                        }
                }
        }
+       fclose(fd);
+
+       fprintf(stdout, "%s%s%s\n", prefix, out, suffix);
+       fprintf(stdout, "%s%s%s\n", lsprefix, tolscolors(out), lssuffix);
+
        free(line);
-       printf("%s%s%s\n", prefix, out, suffix);
-       printf("%s%s%s\n", lsprefix, tolscolors(out), lssuffix);
        free(out);
 
-       /* NOTREACHED */
        return (0);
 }
 
 void
-usage(void)
+usage(const char *progname)
 {
-       (void)fprintf(stderr, "usage: %s [-bch] [FILE]\n", getprogname());
+       char *path;
+       path = strdup(progname);
+
+       (void)fprintf(stderr, "usage: %s [-bch] [FILE]\n", basename(path));
        exit(EX_USAGE);
 }