cpc

Cryptocurrency price checker
git clone git://git.wimdupont.com/cpc.git
Log | Files | Refs | LICENSE

commit 47d1014cae55d56478765e3200b1fb3aea521e80
parent 7e7a7bfbcf65672ede40e874a8aa293969dec35e
Author: Wim Dupont <wim@wimdupont.com>
Date:   Thu, 19 Dec 2024 16:49:00 +0100

cleanup

Diffstat:
MMakefile | 6+++---
Mcpc.c | 69+++++++++++++++++++++++++++++++++------------------------------------
2 files changed, 36 insertions(+), 39 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -CC = gcc -g +CC = gcc NAME = cpc VERSION = 0.1 @@ -8,7 +8,7 @@ MANPREFIX = ${PREFIX}/share/man BIN = cpc SRC = cpc.c -OBJ = ${SRC:.c=.o} +OBJ = ${SRC:.c=.o} config.h MAN1 = ${BIN:=.1} all: ${BIN} @@ -18,7 +18,7 @@ LDFLAGS=-lcjson -lcurl .c.o: $(CC) -g -c $(CFLAGS) $< -cex: cpc.o +cex: ${OBJ} ${CC} -g -o $@ ${OBJ} ${LDFLAGS} clean: diff --git a/cpc.c b/cpc.c @@ -19,7 +19,7 @@ typedef struct } Memory; static size_t writefunc(char *data, size_t size, size_t nmemb, Memory *mem); -static void parse_array(cJSON *array); +static void wfile(cJSON *array); static void parse_json_object(const char *json_string); static char * replace_home(char *buf, const char *path); static void do_action(Action action, const double value); @@ -67,10 +67,10 @@ main(int argc, char **argv) size_t writefunc(char *data, size_t size, size_t nmemb, Memory *mem) { + char *ptr; size_t realsize = size * nmemb; - char *ptr = realloc(mem->response, mem->size + realsize + 1); - if (!ptr) + if ((ptr = realloc(mem->response, mem->size + realsize + 1)) == NULL) fatal("Realloc failed\n"); mem->response = ptr; @@ -82,13 +82,13 @@ writefunc(char *data, size_t size, size_t nmemb, Memory *mem) } void -parse_array(cJSON *array) +wfile(cJSON *array) { cJSON *item = array ? array->child : 0; - FILE *fp = fopen(file, "w+"); + FILE *fp; - if (!fp) - fatal("opening file failed\n"); + if ((fp = fopen(file, "w+")) == NULL) + fatal("Opening file failed: %s\n", file); while (item) { cJSON *name = cJSON_GetObjectItem(item, "name"); @@ -104,22 +104,22 @@ void parse_json_object(const char *json_string) { cJSON *json = cJSON_Parse(json_string); + if (json == NULL) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) - fprintf(stderr, "Error before: %s\n", error_ptr); + fatal("Error before: %s\n", error_ptr); return; } - parse_array(json); + wfile(json); cJSON_Delete(json); } char * replace_home(char *buf, const char *path) { - char *envv = "$HOME"; + char *userhome, *envv = "$HOME"; struct passwd userinf; - char *userhome; userinf = *getpwuid(getuid()); userhome = strdup(userinf.pw_dir); @@ -138,15 +138,13 @@ replace_home(char *buf, const char *path) void do_action(Action action, const double value) { - char *line_buf = NULL; + char name[50], *line_buf = NULL; size_t line_buf_size = 0; ssize_t line_size; - char name[50]; double price; - - FILE *fp = fopen(file, "r"); - - if (!fp) + FILE *fp; +; + if ((fp = fopen(file, "r")) == NULL) fatal("Error opening file '%s'\n", file); while ((getline(&line_buf, &line_buf_size, fp)) >= 0) { @@ -171,23 +169,23 @@ do_action(Action action, const double value) void call_api() { - char request[strlen(URL) + strlen(FIAT) + strlen(CURRENCIES) + 1]; - char libversion[20]; + char libversion[20], request[strlen(URL) + strlen(FIAT) + strlen(CURRENCIES) + 1]; struct curl_slist *headers = NULL; Memory mem = {0}; - headers = curl_slist_append(headers, "Content-Type: application/json"); - headers = curl_slist_append(headers, "charset: utf-8"); CURL *curl = curl_easy_init(); - curl_version_info_data *ver = curl_version_info(CURLVERSION_NOW); - sprintf(libversion, "curl/%u.%u.%u", - (ver->version_num >> 16) & 0xff, - (ver->version_num >> 8) & 0xff, - ver->version_num & 0xff); - sprintf(request, URL, CURRENCIES, FIAT); - if (curl) { + headers = curl_slist_append(headers, "Content-Type: application/json"); + headers = curl_slist_append(headers, "charset: utf-8"); + + curl_version_info_data *ver = curl_version_info(CURLVERSION_NOW); + sprintf(libversion, "curl/%u.%u.%u", + (ver->version_num >> 16) & 0xff, + (ver->version_num >> 8) & 0xff, + ver->version_num & 0xff); + sprintf(request, URL, CURRENCIES, FIAT); + CURLcode res; curl_easy_setopt(curl, CURLOPT_URL, request); curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); @@ -201,14 +199,15 @@ call_api() if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); - - parse_json_object(mem.response); - free(mem.response); - + else { + parse_json_object(mem.response); + printf("\n=== Updated prices ===\n"); + free(mem.response); + } curl_slist_free_all(headers); - curl_easy_cleanup(curl); } - printf("\n=== Updated prices ===\n"); + + curl_easy_cleanup(curl); } char * @@ -229,7 +228,5 @@ fatal(const char *fmt, ...) vfprintf(stderr, fmt, ap); va_end(ap); - //clean(); - exit(EXIT_FAILURE); }