aboutsummaryrefslogtreecommitdiff
path: root/networking/tftp.c
diff options
context:
space:
mode:
author"Vladimir N. Oleynik" <dzo@simtreas.ru>2005-10-17 10:47:19 +0000
committer"Vladimir N. Oleynik" <dzo@simtreas.ru>2005-10-17 10:47:19 +0000
commit86ac072b4429512275352ad09358e86b655c8638 (patch)
tree465d62460917d106e436b16d2932d4f84fa4d303 /networking/tftp.c
parent485d7cbdf1e6c09cdd40d66edf6e4098cc0670be (diff)
downloadbusybox-w32-86ac072b4429512275352ad09358e86b655c8638.tar.gz
busybox-w32-86ac072b4429512275352ad09358e86b655c8638.tar.bz2
busybox-w32-86ac072b4429512275352ad09358e86b655c8638.zip
more const, use bb_getopt_ulflags, insert XXX for show place of problems
Diffstat (limited to 'networking/tftp.c')
-rw-r--r--networking/tftp.c85
1 files changed, 49 insertions, 36 deletions
diff --git a/networking/tftp.c b/networking/tftp.c
index 9b9747785..96d8f3b79 100644
--- a/networking/tftp.c
+++ b/networking/tftp.c
@@ -60,7 +60,7 @@
60#define TFTP_ERROR 5 60#define TFTP_ERROR 5
61#define TFTP_OACK 6 61#define TFTP_OACK 6
62 62
63static const char *tftp_bb_error_msg[] = { 63static const char * const tftp_bb_error_msg[] = {
64 "Undefined error", 64 "Undefined error",
65 "File not found", 65 "File not found",
66 "Access violation", 66 "Access violation",
@@ -71,8 +71,17 @@ static const char *tftp_bb_error_msg[] = {
71 "No such user" 71 "No such user"
72}; 72};
73 73
74static const int tftp_cmd_get = 1; 74#ifdef CONFIG_FEATURE_TFTP_GET
75static const int tftp_cmd_put = 2; 75# define tftp_cmd_get 1
76#else
77# define tftp_cmd_get 0
78#endif
79#ifdef CONFIG_FEATURE_TFTP_PUT
80# define tftp_cmd_put (tftp_cmd_get+1)
81#else
82# define tftp_cmd_put 0
83#endif
84
76 85
77#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE 86#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
78 87
@@ -375,7 +384,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
375#endif 384#endif
376 385
377 if (opcode == TFTP_ERROR) { 386 if (opcode == TFTP_ERROR) {
378 char *msg = NULL; 387 const char *msg = NULL;
379 388
380 if (buf[4] != '\0') { 389 if (buf[4] != '\0') {
381 msg = &buf[4]; 390 msg = &buf[4];
@@ -383,7 +392,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
383 } else if (tmp < (sizeof(tftp_bb_error_msg) 392 } else if (tmp < (sizeof(tftp_bb_error_msg)
384 / sizeof(char *))) { 393 / sizeof(char *))) {
385 394
386 msg = (char *) tftp_bb_error_msg[tmp]; 395 msg = tftp_bb_error_msg[tmp];
387 } 396 }
388 397
389 if (msg) { 398 if (msg) {
@@ -509,71 +518,75 @@ int tftp_main(int argc, char **argv)
509 /* figure out what to pass to getopt */ 518 /* figure out what to pass to getopt */
510 519
511#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE 520#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
521 char *sblocksize = NULL;
512#define BS "b:" 522#define BS "b:"
523#define BS_ARG , &sblocksize
513#else 524#else
514#define BS 525#define BS
526#define BS_ARG
515#endif 527#endif
516 528
517#ifdef CONFIG_FEATURE_TFTP_GET 529#ifdef CONFIG_FEATURE_TFTP_GET
518#define GET "g" 530#define GET "g"
531#define GET_COMPL ":g"
519#else 532#else
520#define GET 533#define GET
534#define GET_COMP
521#endif 535#endif
522 536
523#ifdef CONFIG_FEATURE_TFTP_PUT 537#ifdef CONFIG_FEATURE_TFTP_PUT
524#define PUT "p" 538#define PUT "p"
539#define PUT_COMPL ":p"
525#else 540#else
526#define PUT 541#define PUT
542#define PUT_COMPL
527#endif 543#endif
528 544
529 while ((opt = getopt(argc, argv, BS GET PUT "l:r:")) != -1) { 545#if defined(CONFIG_FEATURE_TFTP_GET) && defined(CONFIG_FEATURE_TFTP_PUT)
530 switch (opt) { 546 bb_opt_complementally = GET_COMPL PUT_COMPL ":?g--p:p--g";
547#elif defined(CONFIG_FEATURE_TFTP_GET) || defined(CONFIG_FEATURE_TFTP_PUT)
548 bb_opt_complementally = GET_COMPL PUT_COMPL;
549#else
550 /* XXX: may be should #error ? */
551#endif
552
553
554 opt = bb_getopt_ulflags(argc, argv, GET PUT "l:r:" BS,
555 &localfile, &remotefile BS_ARG);
531#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE 556#ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
532 case 'b': 557 if(sblocksize) {
533 blocksize = atoi(optarg); 558 blocksize = atoi(sblocksize);
534 if (!tftp_blocksize_check(blocksize, 0)) { 559 if (!tftp_blocksize_check(blocksize, 0)) {
535 return EXIT_FAILURE; 560 return EXIT_FAILURE;
536 } 561 }
537 break; 562 }
538#endif 563#endif
564
565 cmd &= (tftp_cmd_get & tftp_cmd_put);
539#ifdef CONFIG_FEATURE_TFTP_GET 566#ifdef CONFIG_FEATURE_TFTP_GET
540 case 'g': 567 if(cmd == tftp_cmd_get)
541 cmd = tftp_cmd_get; 568 flags = O_WRONLY | O_CREAT | O_TRUNC;
542 flags = O_WRONLY | O_CREAT | O_TRUNC;
543 break;
544#endif 569#endif
545#ifdef CONFIG_FEATURE_TFTP_PUT 570#ifdef CONFIG_FEATURE_TFTP_PUT
546 case 'p': 571 if(cmd == tftp_cmd_put)
547 cmd = tftp_cmd_put; 572 flags = O_RDONLY;
548 flags = O_RDONLY;
549 break;
550#endif 573#endif
551 case 'l':
552 localfile = optarg;
553 break;
554 case 'r':
555 remotefile = optarg;
556 break;
557 }
558 }
559 574
560 if ((cmd == 0) || (optind == argc)) {
561 bb_show_usage();
562 }
563 if(localfile && strcmp(localfile, "-") == 0) {
564 fd = fileno((cmd==tftp_cmd_get)? stdout : stdin);
565 }
566 if(localfile == NULL) 575 if(localfile == NULL)
567 localfile = remotefile; 576 localfile = remotefile;
568 if(remotefile == NULL) 577 if(remotefile == NULL)
569 remotefile = localfile; 578 remotefile = localfile;
570 if (fd==-1) { 579 /* XXX: I corrected this, but may be wrong too. vodz */
580 if(localfile==NULL || strcmp(localfile, "-") == 0) {
581 fd = fileno((cmd==tftp_cmd_get)? stdout : stdin);
582 } else if (fd==-1) {
571 fd = open(localfile, flags, 0644); 583 fd = open(localfile, flags, 0644);
572 } 584 }
573 if (fd < 0) { 585 if (fd < 0) {
574 bb_perror_msg_and_die("local file"); 586 bb_perror_msg_and_die("local file");
575 } 587 }
576 588
589 /* XXX: argv[optind] and/or argv[optind + 1] may be NULL! */
577 host = xgethostbyname(argv[optind]); 590 host = xgethostbyname(argv[optind]);
578 port = bb_lookup_port(argv[optind + 1], "udp", 69); 591 port = bb_lookup_port(argv[optind + 1], "udp", 69);
579 592