commit: ab23be019c4193254ca28365693a33c6604f42fa
parent: ce37bd73868a86e943e196e4daffa248401417c0
author: Chris Noxz <chris@noxz.tech>
date: Fri, 17 May 2024 21:29:24 +0200
change file format for downloads
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/browser.c b/browser.c
@@ -1048,7 +1048,7 @@ prepare_download(WebKitDownload *d,
/* check for a free suffix */
for (i = 1; g_file_test(f, G_FILE_TEST_EXISTS) && i < SUFFIX_MAX; i++) {
g_free(f);
- f = g_strdup_printf("%s.%d", p, i);
+ f = rebuild_filename(p, i);
}
if (i < SUFFIX_MAX) {
@@ -1090,6 +1090,32 @@ quit(void)
gtk_main_quit();
}
+gchar *
+rebuild_filename(
+ gchar *p,
+ int n)
+{
+ int i, l = strlen(p);
+ gchar *f;
+
+ for (i = l; i >= 0; i--)
+ if (p[i] == '.' || (p[i] == '/' && (i = l) >= 0))
+ break;
+
+ if (i == -1)
+ die(__NAME__ ": fatal: invalid download path\n");
+
+ if (i < l) {
+ p[i] = '\0';
+ f = g_strdup_printf("%s.%d.%s", p, n, &p[i + 1]);
+ p[i] = '.';
+ } else {
+ f = g_strdup_printf("%s.%d", p, n);
+ }
+
+ return f;
+}
+
void
render_tls_error(struct Client *c,
gchar *uri,
diff --git a/browser.h b/browser.h
@@ -244,6 +244,7 @@ static void main_window_setup(void);
static void open_external(struct Client *, const gchar);
static void prepare_download(WebKitDownload *, gchar *);
static void quit(void);
+static gchar *rebuild_filename(gchar *, int);
static void render_tls_error(struct Client *, gchar *, GTlsCertificate *,
GTlsCertificateFlags);
static void run_user_scripts(WebKitWebView *);