diff options
author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-06-14 17:29:10 +0000 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-06-14 17:29:10 +0000 |
commit | 32bf1f9d42b5f04cfa7295bca67f7fc6f6dd176b (patch) | |
tree | 59228e1945641be46124e58c6b594cc408259b0d /networking/tftp.c | |
parent | f1bd42a305d1bb30e7dcafbc907a1c7f6e5d5cbf (diff) | |
download | busybox-w32-32bf1f9d42b5f04cfa7295bca67f7fc6f6dd176b.tar.gz busybox-w32-32bf1f9d42b5f04cfa7295bca67f7fc6f6dd176b.tar.bz2 busybox-w32-32bf1f9d42b5f04cfa7295bca67f7fc6f6dd176b.zip |
- rename CONFIG_FEATURE_TFTP_DEBUG to CONFIG_DEBUG_TFTP so it's off in defconfig
- expand the cmd_get/cmd_put macros
- Jason Schoon writes: unlink only if non-stdio
Diffstat (limited to 'networking/tftp.c')
-rw-r--r-- | networking/tftp.c | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/networking/tftp.c b/networking/tftp.c index 1c719f0d4..237609fad 100644 --- a/networking/tftp.c +++ b/networking/tftp.c | |||
@@ -95,7 +95,7 @@ static int tftp_blocksize_check(int blocksize, int bufsize) | |||
95 | return blocksize; | 95 | return blocksize; |
96 | } | 96 | } |
97 | 97 | ||
98 | static char *tftp_option_get(char *buf, int len, const char const *option) | 98 | static char *tftp_option_get(char *buf, int len, const char * const option) |
99 | { | 99 | { |
100 | int opt_val = 0; | 100 | int opt_val = 0; |
101 | int opt_found = 0; | 101 | int opt_found = 0; |
@@ -142,8 +142,6 @@ static int tftp(const int cmd, const struct hostent *host, | |||
142 | const char *remotefile, const int localfd, | 142 | const char *remotefile, const int localfd, |
143 | const unsigned short port, int tftp_bufsize) | 143 | const unsigned short port, int tftp_bufsize) |
144 | { | 144 | { |
145 | #define cmd_get cmd & tftp_cmd_get | ||
146 | #define cmd_put cmd & tftp_cmd_put | ||
147 | struct sockaddr_in sa; | 145 | struct sockaddr_in sa; |
148 | struct sockaddr_in from; | 146 | struct sockaddr_in from; |
149 | struct timeval tv; | 147 | struct timeval tv; |
@@ -181,10 +179,10 @@ static int tftp(const int cmd, const struct hostent *host, | |||
181 | sizeof(sa.sin_addr)); | 179 | sizeof(sa.sin_addr)); |
182 | 180 | ||
183 | /* build opcode */ | 181 | /* build opcode */ |
184 | if (cmd_get) { | 182 | if (cmd & tftp_cmd_get) { |
185 | opcode = TFTP_RRQ; | 183 | opcode = TFTP_RRQ; |
186 | } | 184 | } |
187 | if (cmd_put) { | 185 | if (cmd & tftp_cmd_put) { |
188 | opcode = TFTP_WRQ; | 186 | opcode = TFTP_WRQ; |
189 | } | 187 | } |
190 | 188 | ||
@@ -197,8 +195,8 @@ static int tftp(const int cmd, const struct hostent *host, | |||
197 | cp += 2; | 195 | cp += 2; |
198 | 196 | ||
199 | /* add filename and mode */ | 197 | /* add filename and mode */ |
200 | if (((cmd_get) && (opcode == TFTP_RRQ)) || | 198 | if (((cmd & tftp_cmd_get) && (opcode == TFTP_RRQ)) || |
201 | ((cmd_put) && (opcode == TFTP_WRQ))) | 199 | ((cmd & tftp_cmd_put) && (opcode == TFTP_WRQ))) |
202 | { | 200 | { |
203 | int too_long = 0; | 201 | int too_long = 0; |
204 | 202 | ||
@@ -245,8 +243,8 @@ static int tftp(const int cmd, const struct hostent *host, | |||
245 | 243 | ||
246 | /* add ack and data */ | 244 | /* add ack and data */ |
247 | 245 | ||
248 | if (((cmd_get) && (opcode == TFTP_ACK)) || | 246 | if (((cmd & tftp_cmd_get) && (opcode == TFTP_ACK)) || |
249 | ((cmd_put) && (opcode == TFTP_DATA))) { | 247 | ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA))) { |
250 | 248 | ||
251 | *((unsigned short *) cp) = htons(block_nr); | 249 | *((unsigned short *) cp) = htons(block_nr); |
252 | 250 | ||
@@ -254,7 +252,7 @@ static int tftp(const int cmd, const struct hostent *host, | |||
254 | 252 | ||
255 | block_nr++; | 253 | block_nr++; |
256 | 254 | ||
257 | if ((cmd_put) && (opcode == TFTP_DATA)) { | 255 | if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) { |
258 | len = bb_full_read(localfd, cp, tftp_bufsize - 4); | 256 | len = bb_full_read(localfd, cp, tftp_bufsize - 4); |
259 | 257 | ||
260 | if (len < 0) { | 258 | if (len < 0) { |
@@ -279,7 +277,7 @@ static int tftp(const int cmd, const struct hostent *host, | |||
279 | 277 | ||
280 | len = cp - buf; | 278 | len = cp - buf; |
281 | 279 | ||
282 | #ifdef CONFIG_FEATURE_TFTP_DEBUG | 280 | #ifdef CONFIG_DEBUG_TFTP |
283 | fprintf(stderr, "sending %u bytes\n", len); | 281 | fprintf(stderr, "sending %u bytes\n", len); |
284 | for (cp = buf; cp < &buf[len]; cp++) | 282 | for (cp = buf; cp < &buf[len]; cp++) |
285 | fprintf(stderr, "%02x ", (unsigned char) *cp); | 283 | fprintf(stderr, "%02x ", (unsigned char) *cp); |
@@ -355,7 +353,7 @@ static int tftp(const int cmd, const struct hostent *host, | |||
355 | opcode = ntohs(*((unsigned short *) buf)); | 353 | opcode = ntohs(*((unsigned short *) buf)); |
356 | tmp = ntohs(*((unsigned short *) &buf[2])); | 354 | tmp = ntohs(*((unsigned short *) &buf[2])); |
357 | 355 | ||
358 | #ifdef CONFIG_FEATURE_TFTP_DEBUG | 356 | #ifdef CONFIG_DEBUG_TFTP |
359 | fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp); | 357 | fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp); |
360 | #endif | 358 | #endif |
361 | 359 | ||
@@ -395,12 +393,12 @@ static int tftp(const int cmd, const struct hostent *host, | |||
395 | 393 | ||
396 | if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) { | 394 | if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) { |
397 | 395 | ||
398 | if (cmd_put) { | 396 | if (cmd & tftp_cmd_put) { |
399 | opcode = TFTP_DATA; | 397 | opcode = TFTP_DATA; |
400 | } else { | 398 | } else { |
401 | opcode = TFTP_ACK; | 399 | opcode = TFTP_ACK; |
402 | } | 400 | } |
403 | #ifdef CONFIG_FEATURE_TFTP_DEBUG | 401 | #ifdef CONFIG_DEBUG_TFTP |
404 | fprintf(stderr, "using %s %u\n", OPTION_BLOCKSIZE, | 402 | fprintf(stderr, "using %s %u\n", OPTION_BLOCKSIZE, |
405 | blksize); | 403 | blksize); |
406 | #endif | 404 | #endif |
@@ -422,7 +420,7 @@ static int tftp(const int cmd, const struct hostent *host, | |||
422 | } | 420 | } |
423 | #endif | 421 | #endif |
424 | 422 | ||
425 | if ((cmd_get) && (opcode == TFTP_DATA)) { | 423 | if ((cmd & tftp_cmd_get) && (opcode == TFTP_DATA)) { |
426 | 424 | ||
427 | if (tmp == block_nr) { | 425 | if (tmp == block_nr) { |
428 | 426 | ||
@@ -453,7 +451,7 @@ static int tftp(const int cmd, const struct hostent *host, | |||
453 | } | 451 | } |
454 | } | 452 | } |
455 | 453 | ||
456 | if ((cmd_put) && (opcode == TFTP_ACK)) { | 454 | if ((cmd & tftp_cmd_put) && (opcode == TFTP_ACK)) { |
457 | 455 | ||
458 | if (tmp == (unsigned short) (block_nr - 1)) { | 456 | if (tmp == (unsigned short) (block_nr - 1)) { |
459 | if (finished) { | 457 | if (finished) { |
@@ -562,7 +560,7 @@ int tftp_main(int argc, char **argv) | |||
562 | host = xgethostbyname(argv[optind]); | 560 | host = xgethostbyname(argv[optind]); |
563 | port = bb_lookup_port(argv[optind + 1], "udp", 69); | 561 | port = bb_lookup_port(argv[optind + 1], "udp", 69); |
564 | 562 | ||
565 | #ifdef CONFIG_FEATURE_TFTP_DEBUG | 563 | #ifdef CONFIG_DEBUG_TFTP |
566 | fprintf(stderr, "using server \"%s\", remotefile \"%s\", " | 564 | fprintf(stderr, "using server \"%s\", remotefile \"%s\", " |
567 | "localfile \"%s\".\n", | 565 | "localfile \"%s\".\n", |
568 | inet_ntoa(*((struct in_addr *) host->h_addr)), | 566 | inet_ntoa(*((struct in_addr *) host->h_addr)), |
@@ -571,12 +569,11 @@ int tftp_main(int argc, char **argv) | |||
571 | 569 | ||
572 | result = tftp(cmd, host, remotefile, fd, port, blocksize); | 570 | result = tftp(cmd, host, remotefile, fd, port, blocksize); |
573 | 571 | ||
574 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
575 | if (!(fd == STDOUT_FILENO || fd == STDIN_FILENO)) { | 572 | if (!(fd == STDOUT_FILENO || fd == STDIN_FILENO)) { |
576 | close(fd); | 573 | if (ENABLE_FEATURE_CLEAN_UP) |
574 | close(fd); | ||
575 | if (cmd == tftp_cmd_get && result != EXIT_SUCCESS) | ||
576 | unlink(localfile); | ||
577 | } | 577 | } |
578 | #endif | ||
579 | if (cmd == tftp_cmd_get && result != EXIT_SUCCESS) | ||
580 | unlink(localfile); | ||
581 | return (result); | 578 | return (result); |
582 | } | 579 | } |