Move #define MAXLINE so this compiles without circ buffers.
[oweals/busybox.git] / networking / tftp.c
index 530b3d134660e3c5b8dbbf2e86d89857b859667e..ec30725d20217278015c2e07014523b2230b8e68 100644 (file)
@@ -46,7 +46,7 @@
 
 #include "busybox.h"
 
-//#define BB_FEATURE_TFTP_DEBUG
+//#define CONFIG_FEATURE_TFTP_DEBUG
 
 #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
 #define TFTP_TIMEOUT 5             /* seconds */
@@ -74,7 +74,7 @@ static const char *tftp_error_msg[] = {
 const int tftp_cmd_get = 1;
 const int tftp_cmd_put = 2;
 
-#ifdef BB_FEATURE_TFTP_BLOCKSIZE
+#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
 
 static int tftp_blocksize_check(int blocksize, int bufsize)  
 {
@@ -158,11 +158,13 @@ static inline int tftp(const int cmd, const struct hostent *host,
        int timeout = bb_tftp_num_retries;
        int block_nr = 1;
 
-#ifdef BB_FEATURE_TFTP_BLOCKSIZE
+#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
        int want_option_ack = 0;
 #endif
 
-       RESERVE_BB_BUFFER(buf, tftp_bufsize + 4); /* Opcode + Block # + Data */
+       /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
+        * size varies meaning BUFFERS_GO_ON_STACK would fail */
+       char *buf=xmalloc(tftp_bufsize + 4);
 
        tftp_bufsize += 4;
 
@@ -230,7 +232,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
                        memcpy(cp, "octet", 6);
                        cp += 6;
 
-#ifdef BB_FEATURE_TFTP_BLOCKSIZE
+#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
 
                        len = tftp_bufsize - 4; /* data block size */
 
@@ -290,7 +292,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
 
                        len = cp - buf;
 
-#ifdef BB_FEATURE_TFTP_DEBUG
+#ifdef CONFIG_FEATURE_TFTP_DEBUG
                        printf("sending %u bytes\n", len);
                        for (cp = buf; cp < &buf[len]; cp++)
                                printf("%02x ", *cp);
@@ -367,7 +369,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
                opcode = ntohs(*((unsigned short *) buf));
                tmp = ntohs(*((unsigned short *) &buf[2]));
 
-#ifdef BB_FEATURE_TFTP_DEBUG
+#ifdef CONFIG_FEATURE_TFTP_DEBUG
                printf("received %d bytes: %04x %04x\n", len, opcode, tmp);
 #endif
 
@@ -390,7 +392,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
                        break;
                }
 
-#ifdef BB_FEATURE_TFTP_BLOCKSIZE
+#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
                if (want_option_ack) {
 
                         want_option_ack = 0;
@@ -416,7 +418,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
                                                 else {
                                                         opcode = TFTP_ACK;
                                                 }
-#ifdef BB_FEATURE_TFTP_DEBUG
+#ifdef CONFIG_FEATURE_TFTP_DEBUG
                                                 printf("using blksize %u\n");
 #endif
                                                 tftp_bufsize = foo + 4;
@@ -470,10 +472,10 @@ static inline int tftp(const int cmd, const struct hostent *host,
                }
        }
 
-#ifdef BB_FEATURE_CLEAN_UP
+#ifdef CONFIG_FEATURE_CLEAN_UP
        close(socketfd);
 
-        RELEASE_BB_BUFFER(buf);
+        RELEASE_CONFIG_BUFFER(buf);
 #endif
 
        return finished ? EXIT_SUCCESS : EXIT_FAILURE;
@@ -494,19 +496,19 @@ int tftp_main(int argc, char **argv)
 
        /* figure out what to pass to getopt */
 
-#ifdef BB_FEATURE_TFTP_BLOCKSIZE
+#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
 #define BS "b:"
 #else
 #define BS
 #endif
 
-#ifdef BB_FEATURE_TFTP_GET
+#ifdef CONFIG_FEATURE_TFTP_GET
 #define GET "g"
 #else
 #define GET 
 #endif
 
-#ifdef BB_FEATURE_TFTP_PUT
+#ifdef CONFIG_FEATURE_TFTP_PUT
 #define PUT "p"
 #else
 #define PUT 
@@ -514,7 +516,7 @@ int tftp_main(int argc, char **argv)
 
        while ((opt = getopt(argc, argv, BS GET PUT "l:r:")) != -1) {
                switch (opt) {
-#ifdef BB_FEATURE_TFTP_BLOCKSIZE
+#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
                case 'b':
                        blocksize = atoi(optarg);
                        if (!tftp_blocksize_check(blocksize, 0)) {
@@ -522,13 +524,13 @@ int tftp_main(int argc, char **argv)
                        }
                        break;
 #endif
-#ifdef BB_FEATURE_TFTP_GET
+#ifdef CONFIG_FEATURE_TFTP_GET
                case 'g':
                        cmd = tftp_cmd_get;
                        flags = O_WRONLY | O_CREAT;
                        break;
 #endif
-#ifdef BB_FEATURE_TFTP_PUT
+#ifdef CONFIG_FEATURE_TFTP_PUT
                case 'p':
                        cmd = tftp_cmd_put;
                        flags = O_RDONLY;
@@ -546,8 +548,16 @@ int tftp_main(int argc, char **argv)
        if ((cmd == 0) || (optind == argc)) {
                show_usage();
        }
-
-       fd = open(localfile, flags, 0644);
+       if(localfile && strcmp(localfile, "-") == 0) {
+           fd = fileno((cmd==tftp_cmd_get)? stdin : stdout);
+       }
+       if(localfile == NULL)
+           localfile = remotefile;
+       if(remotefile == NULL)
+           remotefile = localfile;
+       if (fd==-1) {
+           fd = open(localfile, flags, 0644);
+       }
        if (fd < 0) {
                perror_msg_and_die("local file");
        }
@@ -558,7 +568,7 @@ int tftp_main(int argc, char **argv)
                port = atoi(argv[optind + 1]);
        }
 
-#ifdef BB_FEATURE_TFTP_DEBUG
+#ifdef CONFIG_FEATURE_TFTP_DEBUG
        printf("using server \"%s\", remotefile \"%s\", "
                "localfile \"%s\".\n",
                inet_ntoa(*((struct in_addr *) host->h_addr)),
@@ -567,8 +577,10 @@ int tftp_main(int argc, char **argv)
 
        result = tftp(cmd, host, remotefile, fd, port, blocksize);
 
-#ifdef BB_FEATURE_CLEAN_UP
-       close(fd);
+#ifdef CONFIG_FEATURE_CLEAN_UP
+       if (!(fd == fileno(stdout) || fd == fileno(stdin))) {
+           close(fd);
+       }
 #endif
        return(result);
 }