X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=config%2Futil.c;h=0e76042473ccd3633aa0870da3cce23e0b1788c6;hb=c9ae0d06ab99e64ccbd1f2f6548818e6e860e384;hp=d0b8b2318e489577fd82713fd79b1759c3ff170b;hpb=d1dbdf395b7ee5330b22008ba7c2cc704e157229;p=carl9170fw.git diff --git a/config/util.c b/config/util.c index d0b8b23..0e76042 100644 --- a/config/util.c +++ b/config/util.c @@ -23,7 +23,7 @@ struct file *file_lookup(const char *name) } } - file = malloc(sizeof(*file)); + file = xmalloc(sizeof(*file)); memset(file, 0, sizeof(*file)); file->name = file_name; file->next = file_list; @@ -81,23 +81,13 @@ int file_write_dep(const char *name) struct gstr str_new(void) { struct gstr gs; - gs.s = malloc(sizeof(char) * 64); + gs.s = xmalloc(sizeof(char) * 64); gs.len = 64; gs.max_width = 0; strcpy(gs.s, "\0"); return gs; } -/* Allocate and assign growable string */ -struct gstr str_assign(const char *s) -{ - struct gstr gs; - gs.s = strdup(s); - gs.len = strlen(s) + 1; - gs.max_width = 0; - return gs; -} - /* Free storage for growable string */ void str_free(struct gstr *gs) { @@ -138,3 +128,20 @@ const char *str_get(struct gstr *gs) return gs->s; } +void *xmalloc(size_t size) +{ + void *p = malloc(size); + if (p) + return p; + fprintf(stderr, "Out of memory.\n"); + exit(1); +} + +void *xcalloc(size_t nmemb, size_t size) +{ + void *p = calloc(nmemb, size); + if (p) + return p; + fprintf(stderr, "Out of memory.\n"); + exit(1); +}