X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fuclient.git;a=blobdiff_plain;f=uclient-fetch.c;h=38c9c53e35c7f20c4f36d059f5f25673a4e81176;hp=3c0c36b9e1cf4c119418621b53a7189e3ba130e6;hb=HEAD;hpb=c444957f5589049932f62cb901669ceb86ba48c1 diff --git a/uclient-fetch.c b/uclient-fetch.c index 3c0c36b..38c9c53 100644 --- a/uclient-fetch.c +++ b/uclient-fetch.c @@ -50,7 +50,7 @@ static bool verify = true; static bool proxy = true; static bool default_certs = false; static bool no_output; -static const char *output_file; +static const char *opt_output_file; static int output_fd = -1; static int error_ret; static off_t out_offset; @@ -97,6 +97,7 @@ get_proxy_url(char *url) static int open_output_file(const char *path, uint64_t resume_offset) { + const char *output_file = opt_output_file; char *filename = NULL; int flags; int ret; @@ -121,6 +122,11 @@ static int open_output_file(const char *path, uint64_t resume_offset) } } else { filename = uclient_get_url_filename(path, "index.html"); + if (!filename) { + ret = -ENOMEM; + goto out; + } + output_file = filename; } @@ -150,6 +156,7 @@ done: free: free(filename); +out: return ret; } @@ -254,6 +261,7 @@ static void header_done_cb(struct uclient *cl) static void read_data_cb(struct uclient *cl) { char buf[256]; + ssize_t n; int len; if (!no_output && output_fd < 0) @@ -261,12 +269,15 @@ static void read_data_cb(struct uclient *cl) while (1) { len = uclient_read(cl, buf, sizeof(buf)); - if (!len) + if (len <= 0) return; out_bytes += len; - if (!no_output) - write(output_fd, buf, len); + if (!no_output) { + n = write(output_fd, buf, len); + if (n < 0) + return; + } } } @@ -363,7 +374,7 @@ static void request_done(struct uclient *cl) return; } - if (output_fd >= 0 && !output_file) { + if (output_fd >= 0 && !opt_output_file) { close(output_fd); output_fd = -1; } @@ -489,7 +500,7 @@ static int no_ssl(const char *progname) { fprintf(stderr, "%s: SSL support not available, please install one of the " - "libustream-ssl-* libraries as well as the ca-bundle and ", + "libustream-.*[ssl|tls] packages as well as the ca-bundle and " "ca-certificates packages.\n", progname); @@ -611,7 +622,7 @@ int main(int argc, char **argv) user_agent = optarg; break; case 'O': - output_file = optarg; + opt_output_file = optarg; break; case 'P': if (chdir(optarg)) { @@ -660,9 +671,11 @@ int main(int argc, char **argv) uloop_init(); if (username) { - if (password) - asprintf(&auth_str, "%s:%s", username, password); - else + if (password) { + rc = asprintf(&auth_str, "%s:%s", username, password); + if (rc < 0) + return rc; + } else auth_str = username; }