~sircmpwn/gmni-devel

gmni: allow ~ as home alias in file path for downloads v2 PROPOSED

René Wagner: 1
 allow ~ as home alias in file path for downloads

 1 files changed, 19 insertions(+), 9 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/gmni-devel/patches/26120/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gmni v2] allow ~ as home alias in file path for downloads Export this patch

This works for gmni and gmnlm respectively.
---
 src/util.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/util.c b/src/util.c
index 1d44c07..ec497be 100644
--- 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) {
-- 
2.33.1
> This works for gmni and gmnlm respectively.
> ---
> src/util.c | 28 +++++++++++++++++++---------
> 1 file changed, 19 insertions(+), 9 deletions(-)
> 
> diff --git a/src/util.c b/src/util.c
> index 1d44c07..ec497be 100644
> --- 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;
This should just be strcpy for a statically allocated string buffer.
> + case '~':;
> + int n = snprintf(path_buf, PATH_MAX, "%s/%s", getenv("HOME"), &path[1]);
> + assert((size_t)n < PATH_MAX);
> + break;
Style: declare n outside of the switch rather than adding a semicolon to
the case.
> + default:
> + strncpy(path_buf, path, PATH_MAX);
Is it guaranteed that the path parameter will be less than PATH_MAX in
length? Can we assert this if so?
> }
> - 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);
Not sure an assertion is the best thing here. Would prefer a
user-friendly error message. Take FreeBSD, for example, where PATH_MAX
is just 1024 - equal to the maximum length of a gemini URL.