commit 557b4bde4697e576f5fa09e9573ef519c3b54c40
parent c1fd28003760a319d79a79104bc210700b692dda
Author: hhvn <dev@hhvn.uk>
Date: Tue, 5 Jul 2022 17:06:40 +0100
Find cache file automatically
Diffstat:
1 file changed, 24 insertions(+), 7 deletions(-)
diff --git a/cmus2tsv.c b/cmus2tsv.c
@@ -25,9 +25,12 @@
#include <libgen.h>
#include <stdint.h>
#include <string.h>
+#include <assert.h>
#include <sys/stat.h>
#include <sys/mman.h>
+#define CACHE_PATH ".config/cmus/cache"
+
#define ALIGN(size) (((size) + sizeof(long) - 1) & ~(sizeof(long) - 1))
#define CACHE_VERSION 0x0d
@@ -192,15 +195,29 @@ close:
int
main(int argc, char *argv[]) {
- char *base;
-
- base = basename(argv[0]);
-
- if (argc != 2) {
- fprintf(stderr, "usage: %s <cachefile>\n", base);
+ char *prog;
+ char *home, *file;
+ size_t len;
+
+ prog = basename(argv[0]);
+
+ if (argc < 2) {
+ home = getenv("HOME");
+ assert(home);
+ len = strlen(home) + strlen(CACHE_PATH) + 2;
+ file = malloc(len);
+ assert(file);
+ snprintf(file, len, "%s/%s", home, CACHE_PATH);
+ } else if (argc == 2) {
+ file = argv[1];
+ } else {
+ fprintf(stderr, "usage: %s [cachefile]\n", prog);
return 2;
}
display_header();
- read_cache(argv[1]);
+ read_cache(file);
+
+ if (file != argv[1])
+ free(file);
}