commit 7094f99aca68cdc7f02c0378e231932ec7e13e37
parent 874085430dd9e3d1a9fdb2316f00eed42114f375
Author: sam-barr <samfbarr@outlook.com>
Date: Fri, 24 Apr 2020 13:51:08 -0500
Merge pull request #37 from allisio/cache-miss
Handle cache misses gracefully
Diffstat:
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/paleofetch.c b/paleofetch.c
@@ -4,6 +4,7 @@
#include <stdbool.h>
#include <string.h>
#include <dirent.h>
+#include <errno.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
@@ -18,6 +19,14 @@
#define BUF_SIZE 150
#define COUNT(x) (int)(sizeof x / sizeof *x)
+#define halt_and_catch_fire(fmt, ...) \
+ do { \
+ if(status != 0) { \
+ fprintf(stderr, "paleofetch: " fmt "\n", ##__VA_ARGS__); \
+ exit(status); \
+ } \
+ } while(0)
+
struct conf {
char *label, *(*function)();
bool cached;
@@ -37,13 +46,6 @@ struct sysinfo my_sysinfo;
int title_length;
int status;
-void halt_and_catch_fire(const char *message) {
- if(status != 0) {
- printf("paleofetch: %s\n", message);
- exit(status);
- }
-}
-
/*
* Replaces the first newline character with null terminator
*/
@@ -514,9 +516,13 @@ char *get_cache_file() {
* we might get in trouble would be if the user decided not to have any
* sort of sigil (like ':') after their labels. */
char *search_cache(char *cache_data, char *label) {
- char *start = strstr(cache_data, label) + strlen(label);
+ char *start = strstr(cache_data, label);
+ if(start == NULL) {
+ status = ENODATA;
+ halt_and_catch_fire("cache miss on key '%s'; need to --recache?", label);
+ }
+ start += strlen(label);
char *end = strchr(start, ';');
-
char *buf = calloc(1, BUF_SIZE);
// skip past the '=' and stop just before the ';'
strncpy(buf, start + 1, end - start - 1);