]> pd.if.org Git - zpackage/commitdiff
add option to determine name from url
authorNathan Wagner <nw@hydaspes.if.org>
Sat, 16 Feb 2019 09:27:20 +0000 (09:27 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 16 Feb 2019 09:27:20 +0000 (09:27 +0000)
src/fetchurl.c

index 3a9436bddfbdd93cde3de6a3339c3258be54ff61..9d526961f32afbd9cee6f999605be2f96e9670ee 100644 (file)
@@ -508,6 +508,37 @@ static void fake_header(struct io *io, int fd) {
        tls_buffer_append(hdr, "\r\n", 2);
 }
 
        tls_buffer_append(hdr, "\r\n", 2);
 }
 
+char *pathlast(char *path) {
+       char *last = 0;
+       size_t len = 0;
+
+       if (path == 0) {
+               return 0;
+       }
+
+       while (*path == '/') {
+               path++;
+       }
+
+       do {
+               last = path;
+               len = 0;
+               while (*path && *path != '/') {
+                       path++;
+                       len++;
+               }
+               while (*path == '/') {
+                       path++;
+               }
+       } while (*path);
+
+       if (len == 0) {
+               return 0;
+       }
+
+       return strndup(last, len);
+}
+
 int main(int ac, char *av[]) {
        int sockfd, port = -1, rv;
        ssize_t ret;
 int main(int ac, char *av[]) {
        int sockfd, port = -1, rv;
        ssize_t ret;
@@ -536,13 +567,14 @@ int main(int ac, char *av[]) {
        size_t header_len;
        char *url = 0;
        int redirs = 0, redirlimit = 50, printstatus = 0;
        size_t header_len;
        char *url = 0;
        int redirs = 0, redirlimit = 50, printstatus = 0;
-       int verifypolicy = 1;
+       int verifypolicy = 1, calcoutfile = 0;
 
        ltc_mp = tfm_desc;
 
 
        ltc_mp = tfm_desc;
 
-       while ((option = getopt(ac, av, "o:rIfz:#R:SkK")) != -1) {
+       while ((option = getopt(ac, av, "o:OrIfz:#R:SkK")) != -1) {
                switch (option) {
                        case 'o': outfile = optarg; break;
                switch (option) {
                        case 'o': outfile = optarg; break;
+                       case 'O': calcoutfile = 1; break;
                        case 'S': printstatus = 1; head = 1; break;
                        case 'k': verifypolicy = 0; break;
                        case 'K': verifypolicy = 2; break;
                        case 'S': printstatus = 1; head = 1; break;
                        case 'k': verifypolicy = 0; break;
                        case 'K': verifypolicy = 2; break;
@@ -586,6 +618,19 @@ int main(int ac, char *av[]) {
                exit(EXIT_FAILURE);
        }
 
                exit(EXIT_FAILURE);
        }
 
+       if (calcoutfile && !outfile) {
+               tls_parse_uri(url, &uri);
+               outfile = pathlast(uri.path);
+               /* outfile leaks memory here, so if this
+                * were turned into a library function,
+                * we'd need to track it
+                */
+               if (!outfile) {
+                       fprintf(stderr, "unable to determine outfile\n");
+                       exit(EXIT_FAILURE);
+               }
+       }
+
        if (outfile) {
                out = open(outfile, O_WRONLY|O_CREAT, 0600);
                if (out == -1) {
        if (outfile) {
                out = open(outfile, O_WRONLY|O_CREAT, 0600);
                if (out == -1) {
@@ -609,6 +654,7 @@ int main(int ac, char *av[]) {
                port = atoi(uri.port);
                req_file = uri.path;
 
                port = atoi(uri.port);
                req_file = uri.path;
 
+
                /* construct request */
                if (head) {
                        tls_buffer_append(&request, "HEAD ", 5);
                /* construct request */
                if (head) {
                        tls_buffer_append(&request, "HEAD ", 5);