httpd: trivial simplification
[oweals/busybox.git] / networking / ftpgetput.c
index 9e64ff98a309ea087eb2c4f49cc26ceee815ddb0..58847a9f2b7a48ed396bf37fd3889265473ab492 100644 (file)
@@ -5,7 +5,7 @@
  * Mini implementation of FTP to retrieve a remote file.
  *
  * Copyright (C) 2002 Jeff Angielski, The PTR Group <jeff@theptrgroup.com>
- * Copyright (C) 2002 Glenn McGrath <bug1@iinet.net.au>
+ * Copyright (C) 2002 Glenn McGrath
  *
  * Based on wget.c by Chip Rosenthal Covad Communications
  * <chip@laserlink.net>
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include "busybox.h"
 #include <getopt.h>
+#include "libbb.h"
 
 typedef struct ftp_host_info_s {
-       char *user;
-       char *password;
-       struct sockaddr_in *s_in;
+       const char *user;
+       const char *password;
+       struct len_and_sockaddr *lsa;
 } ftp_host_info_t;
 
-static char verbose_flag;
-static char do_continue;
+static smallint verbose_flag;
+static smallint do_continue;
 
 static void ftp_die(const char *msg, const char *remote) ATTRIBUTE_NORETURN;
 static void ftp_die(const char *msg, const char *remote)
@@ -41,7 +41,7 @@ static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
 {
        unsigned n;
        if (verbose_flag) {
-               bb_error_msg("cmd %s%s", s1, s2);
+               bb_error_msg("cmd %s %s", s1, s2);
        }
 
        if (s1) {
@@ -88,8 +88,8 @@ static int xconnect_ftpdata(ftp_host_info_t *server, char *buf)
        *buf_ptr = '\0';
        port_num += xatoul_range(buf_ptr + 1, 0, 255) * 256;
 
-       server->s_in->sin_port = htons(port_num);
-       return xconnect_tcp_v4(server->s_in);
+       set_nport(server->lsa, htons(port_num));
+       return xconnect_stream(server->lsa);
 }
 
 static FILE *ftp_login(ftp_host_info_t *server)
@@ -98,9 +98,9 @@ static FILE *ftp_login(ftp_host_info_t *server)
        char buf[512];
 
        /* Connect to the command socket */
-       control_stream = fdopen(xconnect_tcp_v4(server->s_in), "r+");
+       control_stream = fdopen(xconnect_stream(server->lsa), "r+");
        if (control_stream == NULL) {
-               /* Extremely unlikely */
+               /* fdopen failed - extremely unlikely */
                bb_perror_nomsg_and_die();
        }
 
@@ -287,16 +287,16 @@ int ftp_send(ftp_host_info_t *server, FILE *control_stream,
 #define FTPGETPUT_OPT_PORT     16
 
 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
-static const struct option ftpgetput_long_options[] = {
-       { "continue", 1, NULL, 'c' },
-       { "verbose", 0, NULL, 'v' },
-       { "username", 1, NULL, 'u' },
-       { "password", 1, NULL, 'p' },
-       { "port", 1, NULL, 'P' },
-       { 0, 0, 0, 0 }
-};
+static const char ftpgetput_longopts[] ALIGN1 =
+       "continue\0" Required_argument "c"
+       "verbose\0"  No_argument       "v"
+       "username\0" Required_argument "u"
+       "password\0" Required_argument "p"
+       "port\0"     Required_argument "P"
+       ;
 #endif
 
+int ftpgetput_main(int argc, char **argv);
 int ftpgetput_main(int argc, char **argv)
 {
        /* content-length of the file */
@@ -304,7 +304,6 @@ int ftpgetput_main(int argc, char **argv)
        const char *port = "ftp";
        /* socket to ftp server */
        FILE *control_stream;
-       struct sockaddr_in s_in;
        /* continue previous transfer (-c) */
        ftp_host_info_t *server;
 
@@ -321,19 +320,18 @@ int ftpgetput_main(int argc, char **argv)
 #endif
 
        /* Set default values */
-       server = xmalloc(sizeof(ftp_host_info_t));
+       server = xmalloc(sizeof(*server));
        server->user = "anonymous";
        server->password = "busybox@";
-       verbose_flag = 0;
 
        /*
         * Decipher the command line
         */
 #if ENABLE_FEATURE_FTPGETPUT_LONG_OPTIONS
-       applet_long_options = ftpgetput_long_options;
+       applet_long_options = ftpgetput_longopts;
 #endif
        opt_complementary = "=3"; /* must have 3 params */
-       opt = getopt32(argc, argv, "cvu:p:P:", &server->user, &server->password, &port);
+       opt = getopt32(argv, "cvu:p:P:", &server->user, &server->password, &port);
        argv += optind;
 
        /* Process the non-option command line arguments */
@@ -347,12 +345,10 @@ int ftpgetput_main(int argc, char **argv)
        /* We want to do exactly _one_ DNS lookup, since some
         * sites (i.e. ftp.us.debian.org) use round-robin DNS
         * and we want to connect to only one IP... */
-       server->s_in = &s_in;
-       bb_lookup_host(&s_in, argv[0]);
-       s_in.sin_port = bb_lookup_port(port, "tcp", 21);
+       server->lsa = xhost2sockaddr(argv[0], bb_lookup_port(port, "tcp", 21));
        if (verbose_flag) {
-               printf("Connecting to %s[%s]:%d\n",
-                       argv[0], inet_ntoa(s_in.sin_addr), ntohs(s_in.sin_port));
+               printf("Connecting to %s (%s)\n", argv[0],
+                       xmalloc_sockaddr2dotted(&server->lsa->sa));
        }
 
        /*  Connect/Setup/Configure the FTP session */