aboutsummaryrefslogtreecommitdiffstats
path: root/opendircolors.c
diff options
context:
space:
mode:
authorCameron Katri <me@cameronkatri.com>2021-04-26 22:02:19 -0400
committerCameron Katri <me@cameronkatri.com>2021-04-26 22:02:19 -0400
commite8627d18b48f68d825976f839fd5b1b35173bf40 (patch)
tree6a69cfc715ed0979b0ce40b2107b77a25c6d21ca /opendircolors.c
parent219b0151ae954faa63cdc0abed49bb4ddb53d75f (diff)
downloadopendircolors-e8627d18b48f68d825976f839fd5b1b35173bf40.tar.gz
opendircolors-e8627d18b48f68d825976f839fd5b1b35173bf40.tar.zst
opendircolors-e8627d18b48f68d825976f839fd5b1b35173bf40.zip
Sane errors, Linux support(?), stdin support
Diffstat (limited to 'opendircolors.c')
-rw-r--r--opendircolors.c35
1 files changed, 26 insertions, 9 deletions
diff --git a/opendircolors.c b/opendircolors.c
index 726ad3f..6f91f0d 100644
--- a/opendircolors.c
+++ b/opendircolors.c
@@ -31,10 +31,13 @@
#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);
}