readr

Minimal RSS reader (WIP)
Log | Files | Refs | README | LICENSE

commit 82f2d105dc0851215e3cc279330a5a2802ed6d9c
parent eeb04711d0ddc2bdaf2cdb53b6b6bbee8f57eeb0
Author: citbl <citbl@citbl.org>
Date:   Fri, 10 Oct 2025 20:56:19 +1000

switch to ecalloc

Diffstat:
Mdb.c | 8++------
Mfeeds.c | 6++----
Mutils.c | 39+++++++++++++++++++++++++++++----------
Mutils.h | 4++++
4 files changed, 37 insertions(+), 20 deletions(-)

diff --git a/db.c b/db.c @@ -115,7 +115,7 @@ dup_col(sqlite3_stmt* st, int i) const unsigned char* t = sqlite3_column_text(st, i); if (!t) return NULL; size_t n = strlen((const char*)t); - char* s = (char*)malloc(n + 1); + char* s = (char*)ecalloc(n + 1, sizeof(char)); if (!s) return NULL; memcpy(s, t, n + 1); return s; @@ -145,11 +145,7 @@ db_fetch_posts(const char* feed_url) db_post_t** rows = NULL; while (sqlite3_step(st) == SQLITE_ROW) { - db_post_t* p = calloc(1, sizeof(db_post_t)); - if (p == NULL) { - fprintf(stderr, "could not allocate post out of db\n"); - break; - } + db_post_t* p = ecalloc(1, sizeof(db_post_t)); p->id = sqlite3_column_int(st, 0); p->title = dup_col(st, 1); p->link = dup_col(st, 2); diff --git a/feeds.c b/feeds.c @@ -18,16 +18,14 @@ load_app(char* contents) { app_t app = { .feeds_cap = FEEDS_CAP, - .feeds = calloc(FEEDS_CAP, sizeof(char*)), + .feeds = ecalloc(FEEDS_CAP, sizeof(char*)), }; remove_all_chars(contents, '\r'); char* line = strtok(contents, "\n"); while (line != NULL) { - feed_t* feed = (feed_t*)malloc(sizeof(feed_t)); - - if (feed == NULL) perror("could not alloc feed"); + feed_t* feed = (feed_t*)ecalloc(1, sizeof(feed_t)); fetch_feed(feed, line); // gets the content and loads it into db printf("."); diff --git a/utils.c b/utils.c @@ -1,11 +1,28 @@ #include <stdio.h> -#include <stdlib.h> #include <strings.h> #include <stdbool.h> +#include <errno.h> +#include <stdarg.h> #include "utils.h" void +panic(const char* fmt, ...) +{ + va_list args; + int saved_errno; + saved_errno = errno; + va_start(args, fmt); + fprintf(stderr, "Error: "); + vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + va_end(args); + if (fmt[0] && fmt[strlen(fmt) - 1] == ':') fprintf(stderr, " %s", strerror(saved_errno)); + fputc('\n', stderr); + exit(1); +} + +void util_fill(int* dst, size_t n, int value) { for (size_t i = 0; i < n; ++i) @@ -25,8 +42,7 @@ get_home_dir(void) const char* pth = getenv("HOMEPATH"); if (drv && pth) { size_t n = strlen(drv) + strlen(pth) + 1; - char* buf = (char*)malloc(n); - if (!buf) return NULL; + char* buf = (char*)ecalloc(n, sizeof(char)); snprintf(buf, n, "%s%s", drv, pth); return buf; /* caller frees if not an env ptr */ } @@ -62,7 +78,7 @@ expand_tilde(const char* path) for (const char* s = path; *s; ++s) out_cap += (*s == '~') ? hlen : 1; - char* out = (char*)malloc(out_cap); + char* out = (char*)ecalloc(out_cap, sizeof(char)); if (!out) return NULL; char* w = out; @@ -113,12 +129,7 @@ read_file(const char* file_path) return NULL; } - char* contents = (char*)calloc(1, (size_t)file_size + 1); - if (contents == NULL) { - printf("Failed to allocate memory to read file\n"); - fclose(fp); - return NULL; - } + char* contents = (char*)ecalloc(1, (size_t)file_size + 1); size_t bytes_read = fread(contents, 1, (size_t)file_size, fp); if (bytes_read != (size_t)file_size) { @@ -281,3 +292,11 @@ nonascii_replace(char* s, char to) } *w = '\0'; } + +void* +ecalloc(size_t c, size_t s) +{ + void* p; + if (!(p = calloc(c, s))) { panic("calloc:"); } + return p; +} diff --git a/utils.h b/utils.h @@ -1,5 +1,8 @@ #pragma once +#include <stdlib.h> + +void panic(const char*, ...); const char* get_home_dir(void); const char* expand_tilde(const char*); char* read_file(const char*); @@ -8,3 +11,4 @@ void remove_all_chars(char*, char); void remove_all_tags(char*); int open_url(const char*); void nonascii_replace(char*, char); +void* ecalloc(size_t, size_t);