~sircmpwn/gmni-devel

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

~rwa: 1
 allow ~ as home alias in file path for downloads

 1 files changed, 35 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/26137/mbox | git am -3
Learn more about email & git
View this thread in the archives

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

From: René Wagner <rwa@clttr.info>

This works for gmni and gmnlm respectively.

The asserts for paths exceeding PATH_MAX limits
have been replaced with user-friendly messages.
---
 src/util.c | 44 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 9 deletions(-)
diff --git a/src/util.c b/src/util.c
index 1d44c07..e064418 100644
--- a/src/util.c
+++ b/src/util.c
@@ -66,22 +66,48 @@ download_resp(FILE *out, struct gemini_response resp, const char *path,
		char *url)
{
	char path_buf[PATH_MAX];
	int n = 0;
	assert(path);
	if (path[0] == '\0') {
		path = "./";
	switch (path[0]) {
	case '\0':
		strcpy(path_buf, "./");
		break;
	case '~':
		n = snprintf(path_buf, PATH_MAX, "%s/%s", getenv("HOME"), &path[1]);
		if (n > PATH_MAX) {
			fprintf(stderr, 
				"Path %s exceeds limit of %d bytes and has been truncated\n", 
				path_buf, PATH_MAX);
			return 1;
		}
		break;
	default:
		if (strlen(path_buf) > PATH_MAX) {
			fprintf(stderr, "Path %s exceeds limit of %d bytes\n", 
				path_buf, PATH_MAX);
			return 1;
		}
		strcpy(path_buf, path);
	}
	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] == '/') {
		n = snprintf(path_res, PATH_MAX, "%s%s", path_buf, basename(url));
		if (n > PATH_MAX) {
			fprintf(stderr, 
				"Path %s exceeds limit of %d bytes and has been truncated\n", 
				path_res, PATH_MAX);
			return 1;
		}
	} else {
		strcpy(path_res, path_buf);
	}
	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.32.0