From f798a99058e815396aa5ef4314d8b28544f02c99 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Fri, 26 Apr 2024 17:14:42 +0900 Subject: [PATCH] =?utf8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E5=90=8D?= =?utf8?q?=E3=81=AF=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC=E3=83=88=E3=83=AB?= =?utf8?q?=E3=82=92=E4=BB=98=E3=81=84=E3=81=9F=E3=82=89=E3=80=81=E3=83=95?= =?utf8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E5=90=8D=E3=81=AF=E6=8B=A1=E5=BC=B5?= =?utf8?q?=E5=AD=90=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +++ main.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00911c4..2185549 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,5 @@ +# 0.2.0 +* ファイル名はパラメートルを付いたら、ファイル名は拡張子に + # 0.1.0 * 最初リリース diff --git a/main.c b/main.c index af195ff..2415780 100644 --- a/main.c +++ b/main.c @@ -1,13 +1,51 @@ #include #include #include +#include #include const char* sofname = "odl"; -const char* version = "0.1.0"; +const char* version = "0.2.0"; char* filename; +char* get_filename(const char* url) { + char* fn_start = strrchr(url, '/'); + if (fn_start == NULL) { + return NULL; + } + fn_start++; + + char* query = strchr(fn_start, '?'); + char* anchor = strchr(fn_start, '#'); + char* fn_end = NULL; + + if (query != NULL && anchor != NULL) { + fn_end = (query < anchor) ? query : anchor; + } else if (query != NULL) { + fn_end = query; + } else if (anchor != NULL) { + fn_end = anchor; + } + + // URLでパラメートルがなければ、そのままファイル名をコピーして + if (fn_end == NULL) { + fn_end = strchr(fn_start, '\0'); + } + + size_t length = fn_end - fn_start; + + char* extfn = malloc(length + 1); + if (extfn == NULL) { + return NULL; + } + + strncpy(extfn, fn_start, length); + extfn[length] = '\0'; + + return extfn; +} + int progress_callback(void *cp, double dt, double dn, double ut, double un) { (void)cp; (void)ut; @@ -35,7 +73,7 @@ int progress_callback(void *cp, double dt, double dn, double ut, double un) { int main(int argc, char* argv[]) { if (argc < 2) { - printf("usage: %s [url ...]\n", sofname); + printf("usage: %s-%s [url ...]\n", sofname, version); return 1; } @@ -47,7 +85,11 @@ int main(int argc, char* argv[]) { for (int i = 1; i < argc; i++) { const char* url = argv[i]; - filename = basename((char*)url); + filename = get_filename(url); + if (filename == NULL) { + fprintf(stderr, "URLからファイル名を抽出出来ませんでした。\n"); + continue; + } curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); -- 2.43.0