write to cache if cache is enabled, but not found

This commit is contained in:
Joshix 2021-10-11 00:15:46 +02:00
parent 4e13dfd1cc
commit 2f450de6b1

View file

@ -109,7 +109,7 @@ void print_ascii();
void print_unknown_ascii(); void print_unknown_ascii();
void print_info(); void print_info();
void write_cache(); void write_cache();
void read_cache(); int read_cache();
void print_cache(); void print_cache();
void print_image(); void print_image();
void usage(char *); void usage(char *);
@ -128,10 +128,14 @@ int main(int argc, char *argv[])
char buffer[128]; char buffer[128];
sscanf(cache_env, "%[TRUEtrue1]", buffer); sscanf(cache_env, "%[TRUEtrue1]", buffer);
cache_enabled = (strcmp(buffer, "true") == 0 || strcmp(buffer, "TRUE") == 0 || strcmp(buffer, "1") == 0); cache_enabled = (strcmp(buffer, "true") == 0 || strcmp(buffer, "TRUE") == 0 || strcmp(buffer, "1") == 0);
if (cache_enabled) if (cache_enabled) {
{ // if no cache file found write to it
print_cache(); if (!read_cache()) {
return 0; get_info();
write_cache();
}
print_cache();
return 0;
} }
} }
@ -177,6 +181,7 @@ int main(int argc, char *argv[])
return 0; return 0;
case 'w': case 'w':
write_cache(); write_cache();
print_cache();
return 0; return 0;
default: default:
break; break;
@ -451,13 +456,14 @@ void write_cache()
return; return;
} }
void read_cache() // return whether the cache file is found
int read_cache()
{ {
char cache_file[512]; char cache_file[512];
sprintf(cache_file, "%s/.cache/uwufetch.cache", getenv("HOME")); sprintf(cache_file, "%s/.cache/uwufetch.cache", getenv("HOME"));
FILE *cache_fp = fopen(cache_file, "r"); FILE *cache_fp = fopen(cache_file, "r");
if (cache_fp == NULL) if (cache_fp == NULL)
return; return 0;
char line[256]; char line[256];
@ -477,14 +483,12 @@ void read_cache()
} }
fclose(cache_fp); fclose(cache_fp);
return; return 1;
} }
void print_cache() void print_cache()
{ {
char line[256]; char line[256];
read_cache();
// ram // ram
#ifndef __APPLE__ #ifndef __APPLE__
#ifndef __CYGWIN__ #ifndef __CYGWIN__