diff options
author | joerg <joerg@pkgsrc.org> | 2008-04-21 17:15:31 +0000 |
---|---|---|
committer | joerg <joerg@pkgsrc.org> | 2008-04-21 17:15:31 +0000 |
commit | 6dff82271482af7b17922b3edd35097ddda24f20 (patch) | |
tree | 55d23602099c4ddd85e99d90e8fa7766a697144c /net/libfetch/files | |
parent | cc5fc0695e19fbd9f7e38e0bbf928c19ef502a57 (diff) | |
download | pkgsrc-6dff82271482af7b17922b3edd35097ddda24f20.tar.gz |
libfetch-2.9:
Add fetch_extract_filename to extract the unquoted filename of a URL.
Diffstat (limited to 'net/libfetch/files')
-rw-r--r-- | net/libfetch/files/fetch.c | 50 | ||||
-rw-r--r-- | net/libfetch/files/fetch.h | 3 |
2 files changed, 51 insertions, 2 deletions
diff --git a/net/libfetch/files/fetch.c b/net/libfetch/files/fetch.c index f1d52dbf9a5..4e89429dac1 100644 --- a/net/libfetch/files/fetch.c +++ b/net/libfetch/files/fetch.c @@ -1,4 +1,4 @@ -/* $NetBSD: fetch.c,v 1.5 2008/04/20 15:29:26 joerg Exp $ */ +/* $NetBSD: fetch.c,v 1.6 2008/04/21 17:15:31 joerg Exp $ */ /*- * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -451,3 +451,51 @@ fetchFreeURL(struct url *u) free(u->doc); free(u); } + +static char +xdigit2digit(char digit) +{ + digit = tolower((unsigned char)digit); + if (digit >= 'a' && digit <= 'f') + digit = digit - 'a' + 10; + else + digit = digit - '0'; + + return digit; +} + +/* + * Extract the file name component of a URL. + */ +char * +fetch_extract_filename(struct url *url) +{ + char *name, *name_iter; + const char *last_slash, *iter; + + last_slash = url->doc; + if (*last_slash == '\0') + return strdup(""); + for (iter = last_slash + 1; *iter; ++iter) { + if (*iter == '#' || *iter == '?') + break; + if (*iter == '/') + last_slash = iter; + } + if (last_slash + 1 == iter) + return strdup(""); + name_iter = name = malloc(iter - last_slash); + while (++last_slash < iter) { + if (*last_slash != '%' || + !isxdigit((unsigned char)last_slash[1]) || + !isxdigit((unsigned char)last_slash[2])) { + *name_iter++ = *last_slash; + continue; + } + *name_iter++ = xdigit2digit(last_slash[1]) * 16 + + xdigit2digit(last_slash[2]); + last_slash += 2; + } + *name_iter = '\0'; + return name; +} diff --git a/net/libfetch/files/fetch.h b/net/libfetch/files/fetch.h index d9f106776f0..e03d4f71de2 100644 --- a/net/libfetch/files/fetch.h +++ b/net/libfetch/files/fetch.h @@ -1,4 +1,4 @@ -/* $NetBSD: fetch.h,v 1.7 2008/04/20 15:29:26 joerg Exp $ */ +/* $NetBSD: fetch.h,v 1.8 2008/04/21 17:15:31 joerg Exp $ */ /*- * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -151,6 +151,7 @@ void fetchFreeURL(struct url *); /* URL listening */ void fetch_init_url_list(struct url_list *); void fetch_free_url_list(struct url_list *); +char *fetch_extract_filename(struct url *); /* Authentication */ typedef int (*auth_t)(struct url *); |