From 3a61c3f4a8820def50e7fbe68e31a07648102eca Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Tue, 6 May 2014 15:03:21 +0200 Subject: [PATCH 1/1] add uclient_get_url_filename helper function Signed-off-by: Felix Fietkau --- uclient-fetch.c | 28 ++++++---------------------- uclient-utils.c | 22 ++++++++++++++++++++++ uclient-utils.h | 2 ++ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/uclient-fetch.c b/uclient-fetch.c index 9672e07..8dcb97b 100644 --- a/uclient-fetch.c +++ b/uclient-fetch.c @@ -25,6 +25,7 @@ #include #include "uclient.h" +#include "uclient-utils.h" #ifdef __APPLE__ #define LIB_EXT "dylib" @@ -42,10 +43,9 @@ static int error_ret; static int open_output_file(const char *path, bool create) { - const char *str; char *filename; - int len; int flags = O_WRONLY; + int ret; if (create) flags |= O_CREAT; @@ -61,27 +61,11 @@ static int open_output_file(const char *path, bool create) if (create) flags |= O_EXCL; - len = strcspn(path, ";&"); - while (len > 0 && path[len - 1] == '/') - len--; - - for (str = path + len - 1; str >= path; str--) { - if (*str == '/') - break; - } - - str++; - len -= str - path; - - if (len > 0) { - filename = alloca(len + 1); - strncpy(filename, str, len); - filename[len] = 0; - } else { - filename = "index.html"; - } + filename = uclient_get_url_filename(path, "index.html"); + ret = open(filename, flags, 0644); + free(filename); - return open(filename, flags, 0644); + return ret; } static void request_done(struct uclient *cl) diff --git a/uclient-utils.c b/uclient-utils.c index 93a6880..a375eea 100644 --- a/uclient-utils.c +++ b/uclient-utils.c @@ -160,3 +160,25 @@ void http_digest_calculate_response(char *dest, const struct http_digest_data *d http_create_hash(dest, h_a2_strings, ARRAY_SIZE(h_a2_strings)); http_create_hash(dest, resp_strings, ARRAY_SIZE(resp_strings)); } + +char *uclient_get_url_filename(const char *url, const char *default_name) +{ + const char *str; + int len = strcspn(url, ";&"); + + while (len > 0 && url[len - 1] == '/') + len--; + + for (str = url + len - 1; str >= url; str--) { + if (*str == '/') + break; + } + + str++; + len -= str - url; + + if (len > 0) + return strncpy(calloc(1, len + 1), str, len); + + return strdup(default_name); +} diff --git a/uclient-utils.h b/uclient-utils.h index 3c52eff..a4f975d 100644 --- a/uclient-utils.h +++ b/uclient-utils.h @@ -44,4 +44,6 @@ int uclient_urldecode(const char *in, char *out, bool decode_plus); void http_digest_calculate_auth_hash(char *dest, const char *user, const char *realm, const char *password); void http_digest_calculate_response(char *dest, const struct http_digest_data *data); +char *uclient_get_url_filename(const char *url, const char *default_name); + #endif -- 2.11.0