cgmnlm

Unnamed repository; edit this file 'description' to name the repository.
git clone git://code.clttr.info/cgmnlm.git
Log | Files | Refs | README | LICENSE

commit 2d7f75383cfca3b4076a889fc7c065e06d146609
parent 6e3c8e48f98c008abda2bbf7a2bbdb8ea3988b18
Author: René Wagner <rwa@clttr.info>
Date:   Mon,  1 Nov 2021 11:50:23 +0100

allow ~ as home alias in file path for downloads

This works for gmni and gmnlm respectively.

Diffstat:
Msrc/util.c | 28+++++++++++++++++++---------
1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/util.c b/src/util.c @@ -67,21 +67,31 @@ download_resp(FILE *out, struct gemini_response resp, const char *path, { char path_buf[PATH_MAX]; assert(path); - if (path[0] == '\0') { - path = "./"; + switch (path[0]) { + case '\0': + strncpy(path_buf, "./", 3); + break; + case '~':; + int n = snprintf(path_buf, PATH_MAX, "%s/%s", getenv("HOME"), &path[1]); + assert((size_t)n < PATH_MAX); + break; + default: + strncpy(path_buf, path, PATH_MAX); } - if (path[strlen(path)-1] == '/') { - int n = snprintf(path_buf, sizeof(path_buf), "%s%s", path, basename(url)); - assert((size_t)n < sizeof(path_buf)); - path = path_buf; + char path_res[PATH_MAX]; + if (path_buf[strlen(path_buf)-1] == '/') { + int n = snprintf(path_res, PATH_MAX, "%s%s", path_buf, basename(url)); + assert((size_t)n < PATH_MAX); + } else { + strncpy(path_res, path_buf, PATH_MAX); } - FILE *f = fopen(path, "w"); + FILE *f = fopen(path_res, "w"); if (f == NULL) { fprintf(stderr, "Could not open %s for writing: %s\n", - path, strerror(errno)); + path_res, strerror(errno)); return 1; } - fprintf(out, "Downloading %s to %s\n", url, path); + fprintf(out, "Downloading %s to %s\n", url, path_res); char buf[BUFSIZ]; for (int n = 1; n > 0;) { if (resp.sc) {