gs

Unnamed repository; edit this file 'description' to name the repository.
git clone git://edryd.org/gs
Log | Files | Refs | LICENSE

commit 1c3dd4826a76be4fd1cecd70000aead78fd21fd6
parent d626089d4fb66822f8ede5fbd87f1249b2bce3ff
Author: Ed van Bruggen <edvb54@gmail.com>
Date:   Fri, 28 Jul 2017 23:00:04 -0700

Fix memory leaks, improve error messages

Diffstat:
config.def.h | 4++--
gs.c | 51++++++++++++++++++++++++++++++++-------------------
util.c | 13+++++++------
util.h | 2+-
4 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -1,4 +1,4 @@ -char *desc = ""; /* default gist description */ +char *desc = ""; /* default gist description */ char *fname = NULL; /* default gist filename when reading from stdin */ -char *user = NULL; /* default user, password can be given after colon */ +char *user = NULL; /* default user, password can be given after colon */ int pub = 1; /* 0: private need link to view, 1: public anyone can find */ diff --git a/gs.c b/gs.c @@ -49,6 +49,7 @@ str_write(void *ptr, size_t size, size_t nmemb, Str *s) static Str http_post(char *content, long okcode) { + char *resmsg; long code; CURL *curl; CURLcode res; @@ -58,8 +59,14 @@ http_post(char *content, long okcode) resstr.ptr = emalloc(resstr.len+1); resstr.ptr[0] = '\0'; - if (!(curl = curl_easy_init())) - die("%s: cURL: could not init", argv0); + /* init cURL */ + curl_global_init(CURL_GLOBAL_ALL); + if (!(curl = curl_easy_init())) { + curl_global_cleanup(); + die(1, "%s: cURL: could not init", argv0); + } + + /* set cURL options */ curl_easy_setopt(curl, CURLOPT_URL, GIST_URL); curl_easy_setopt(curl, CURLOPT_USERAGENT, "gs/"VERSION); if (user) { @@ -73,12 +80,24 @@ http_post(char *content, long okcode) curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, str_write); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resstr); - if ((res = curl_easy_perform(curl)) != CURLE_OK) - die("%s: cURL: %s", argv0, curl_easy_strerror(res)); + + /* run cURL */ + if ((res = curl_easy_perform(curl)) != CURLE_OK) { + curl_global_cleanup(); + die(1, "%s: cURL: %s", argv0, curl_easy_strerror(res)); + } + + /* response checking and cleanup */ curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); curl_easy_cleanup(curl); - if (code != okcode) - die("%s: could not create Gist: %s", argv0, resstr.ptr); + curl_global_cleanup(); + if (code != okcode) { + json_scanf(resstr.ptr, resstr.len, "{message: %Q}", &resmsg); + die(-1, "%s: could not create Gist: %s", argv0, resmsg); + free(resmsg); + exit(1); + } + return resstr; } @@ -111,7 +130,7 @@ files_js(char *files[], int filec) struct json_out jout = JSON_OUT_BUF(js, BUF_SIZE); if (!desc) - die("%s: description not given", argv0); + die(1, "%s: description not given", argv0); json_printf(&jout, "{ description: %Q, public: %B, files: {", desc, pub); /* add each file */ @@ -119,19 +138,18 @@ files_js(char *files[], int filec) if (i) /* insert comma if this is another file */ json_printf(&jout, ","); if (filec && !(fp = fopen(files[i], "r"))) - die("%s: %s: could not load file", argv0, files[i]); + die(1, "%s: %s: could not load file", argv0, files[i]); if (filec) /* set file name if given */ fname = files[i]; if (!fname) /* check for file name when using stdin */ - die("%s: file name not given", argv0); + die(1, "%s: file name not given", argv0); fbuf = file_str(fp); json_printf(&jout, "%Q: { content: %Q }", basename(fname), fbuf); + efree(fbuf); } json_printf(&jout, "} }"); - efree(fbuf); - return js; } @@ -155,11 +173,10 @@ gs_new(char *files[], int filec) } static void -usage(const int e) +usage(const int eval) { - fprintf(stderr, "usage: %s [-pPhv] [-d DESCRIPTION] [-f FILENAME]\n" - " [-u USER[:PASSWORD] | -U] [FILES ...]\n", argv0); - exit(e); + die(eval, "usage: %s [-pPhv] [-d DESCRIPTION] [-f FILENAME]\n" + " [-u USER[:PASSWORD] | -U] [FILES ...]", argv0); } int @@ -193,11 +210,7 @@ main(int argc, char *argv[]) usage(1); } ARGEND; - curl_global_init(CURL_GLOBAL_ALL); - gs_new(argv, argc); - curl_global_cleanup(); - return 0; } diff --git a/util.c b/util.c @@ -13,7 +13,7 @@ ecalloc(size_t nmemb, size_t size) void *p; if (!(p = calloc(nmemb, size))) - die("calloc: out of memory"); + die(1, "calloc:"); return p; } @@ -24,7 +24,7 @@ emalloc(size_t size) void *p; if (!(p = malloc(size))) - die("malloc: out of memory"); + die(1, "malloc:"); return p; } @@ -33,7 +33,7 @@ void * erealloc(void *p, size_t size) { if (!(p = realloc(p, size))) - die("realloc: out of memory"); + die(1, "realloc:"); return p; } @@ -42,7 +42,7 @@ char * estrdup(char *s) { if (!(s = strdup(s))) - die("strdup: out of memory"); + die(1, "strdup:"); return s; } @@ -55,7 +55,7 @@ efree(void *p) } void -die(const char *fmt, ...) +die(int eval, const char *fmt, ...) { va_list ap; @@ -70,5 +70,6 @@ die(const char *fmt, ...) fputc('\n', stderr); } - exit(1); + if (eval > -1) + exit(eval); } diff --git a/util.h b/util.h @@ -11,4 +11,4 @@ void *erealloc(void *p, size_t size); char *estrdup(char *s); void efree(void *p); -void die(const char *errstr, ...); +void die(int eval, const char *fmt, ...);