From 2aadb223010234ba105ebd4a77c70562dd4e0f53 Mon Sep 17 00:00:00 2001 From: "Fadil R. Berisha" Date: Wed, 15 Dec 2021 10:37:40 -0500 Subject: [PATCH] examples/wget: Saving a file downloaded with wget wget [-o ] --- examples/wget/wget_main.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/examples/wget/wget_main.c b/examples/wget/wget_main.c index cb88f0ab4..d5ff0b95b 100644 --- a/examples/wget/wget_main.c +++ b/examples/wget/wget_main.c @@ -26,6 +26,8 @@ #include #include +#include +#include #include #include @@ -70,6 +72,7 @@ ****************************************************************************/ static char g_iobuffer[512]; +static int g_fd; /**************************************************************************** * Private Functions @@ -82,7 +85,7 @@ static char g_iobuffer[512]; static int callback(FAR char **buffer, int offset, int datend, FAR int *buflen, FAR void *arg) { - ssize_t written = write(1, &((*buffer)[offset]), datend - offset); + ssize_t written = write(g_fd, &((*buffer)[offset]), datend - offset); if (written == -1) { return -errno; @@ -152,19 +155,38 @@ int main(int argc, FAR char *argv[]) ctx.buflen = 512; ctx.sink_callback = callback; ctx.sink_callback_arg = NULL; - if (argc > 1) + + if (argc >= 3) + { + ctx.url = argv[argc - 1]; + g_fd = open(argv[argc - 2], + O_WRONLY | O_CREAT | O_TRUNC, + S_IRWXU | S_IRWXG | S_IRWXO); + if (g_fd < 0) + { + printf("cannot create file %s \n", argv[argc - 2]); + } + } + else if (argc == 2) { ctx.url = argv[1]; + g_fd = 1; } else { ctx.url = CONFIG_EXAMPLES_WGET_URL; + g_fd = 1; } - int ret = webclient_perform(&ctx); - if (ret != 0) + if (g_fd >= 0) { - printf("webclient_perform failed with %d\n", ret); + int ret = webclient_perform(&ctx); + if (ret != 0) + { + printf("webclient_perform failed with %d\n", ret); + } + + close(g_fd); } return 0;