You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/12/20 02:29:07 UTC

[incubator-nuttx-apps] branch master updated: examples/wget: Saving a file downloaded with wget

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 2aadb22  examples/wget: Saving a file downloaded with wget
2aadb22 is described below

commit 2aadb223010234ba105ebd4a77c70562dd4e0f53
Author: Fadil R. Berisha <fa...@gmail.com>
AuthorDate: Wed Dec 15 10:37:40 2021 -0500

    examples/wget: Saving a file downloaded with wget
    
    wget [-o <local-path>] <url>
---
 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 cb88f0a..d5ff0b9 100644
--- a/examples/wget/wget_main.c
+++ b/examples/wget/wget_main.c
@@ -26,6 +26,8 @@
 
 #include <stdint.h>
 #include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
 
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -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;