1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
|
/*
ftp -- ftp protocol functions
Copyright (C) 1996, 1997, 1998, 1999, 2000 Dieter Baron
This file is part of cftp, a fullscreen ftp client
The author can be contacted at <dillo@giga.or.at>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include "directory.h"
#include "display.h"
#include "sockets.h"
#include "ftp.h"
#include "readdir.h"
#include "options.h"
#include "util.h"
#include "bindings.h"
#include "status.h"
#include "signals.h"
extern char *prg;
char *ftp_lcwd;
char *ftp_pcwd;
char ftp_curmode; /* current transfer mode (' ' for unknown) */
char ftp_anon;
char *ftp_last_resp;
char *ftp_host, *ftp_prt, *ftp_user, *ftp_pass;
/* host, port, user, passwd (for reconnect) */
struct ftp_hist *ftp_history; /* command/response history */
struct ftp_hist *ftp_hist_last; /* tail of history */
int ftp_hist_cursize; /* size of history (for dynamic growth) */
static int _ftp_keptresp;
static int ftp_dosnames; /* server sends directory names in
DOS format */
char **ftp_response;
long ftp_response_size;
FILE *conin, *conout; /* control connection to server */
#ifdef HAVE_STRUCT_SOCKADDR_STORAGE
static struct sockaddr_storage _ftp_addr;
#else
static struct sockaddr _ftp_addr;
#endif
struct sockaddr *ftp_addr = (struct sockaddr *)&_ftp_addr;
/* local ip address (for port commands) */
int ftp_put(char *fmt, ...);
int ftp_abort(FILE *fin);
int ftp_resp(void);
void ftp_unresp(int resp);
int ftp_port(void);
FILE *ftp_accept(int fd, char *mode);
int ftp_mode(char m);
int ftp_cwd(char *path);
int ftp_gethostaddr(int fd);
void ftp_histf(char *fmt, ...);
void ftp_hist(char *line);
#ifdef ENABLE_TRANSFER_RATE
static void _ftp_update_transfer(char *fmt, long got, long *cur,
int old_sec, int new_sec);
#endif
void
ftp_init(void)
{
ftp_pcwd = NULL;
ftp_curmode = ' ';
ftp_anon = 0;
ftp_last_resp = NULL;
ftp_host = ftp_prt = ftp_pass = NULL;
ftp_history = NULL;
_ftp_keptresp = -1;
ftp_dosnames = -1;
ftp_response = NULL;
ftp_response_size = 0;
conin = conout = NULL;
}
int
ftp_open(char *host, char *port)
{
int fd;
if ((fd=sopen(host, port, AF_UNSPEC)) == -1)
return -1;
if (ftp_gethostaddr(fd) == -1) {
close(fd);
return -1;
}
conin = fdopen(fd, "r");
conout = fdopen(fd, "w");
if (!conin || !conout) {
close(fd);
return -1;
}
return 0;
}
int
ftp_login(char *host, char *user, char *pass)
{
int resp;
if (ftp_resp() != 220)
return -1;
if (strcmp(user, "ftp") != 0 && strcmp(user, "anonymous") != 0) {
status.host = (char *)malloc(strlen(user)+strlen(host)+2);
sprintf(status.host, "%s@%s", user, host);
ftp_anon = 0;
}
else {
status.host = strdup(host);
ftp_anon = 1;
}
ftp_put("user %s", user);
resp = ftp_resp();
if (resp == 331) {
ftp_put("pass %s", pass);
resp = ftp_resp();
}
if (resp != 230)
return -1;
free(status.remote.path);
status.remote.path = NULL;
status_do(bs_none);
return 0;
}
int
ftp_site(char *cmd)
{
ftp_put("%s", cmd);
return ftp_resp();
}
int
ftp_reconnect(void)
{
char *pass;
pass = ftp_pass;
if (ftp_host == NULL || ftp_prt == NULL || ftp_user == NULL)
return -1;
if (conin) {
fclose(conin);
conin = NULL;
}
if (conout) {
fclose(conout);
conout = NULL;
}
if (pass == NULL) {
char *b;
b = (char *)malloc(strlen(ftp_user)+strlen(ftp_host)+16);
sprintf(b, "Password (%s@%s): ", ftp_user, ftp_host);
pass = read_string(b, 0);
free(b);
}
disp_status("connecting. . .");
if (ftp_open(ftp_host, ftp_prt) == -1) {
/* Error printed in ftp_open or sopen. */
return -1;
}
if (ftp_login(ftp_host, ftp_user, pass) == -1) {
/* ftp response is error message */
return -1;
}
status.remote.path = strdup(ftp_lcwd);
status_do(bs_remote);
ftp_pcwd = NULL;
ftp_curmode = ' ';
return 0;
}
int
ftp_close(void)
{
int err = 0;
if (conin == NULL)
return 0;
ftp_put("quit");
if (ftp_resp() != 221)
err = 1;
fclose(conin);
fclose(conout);
conin = conout = NULL;
return err;
}
directory *
ftp_list(char *path)
{
directory *dir;
int fd;
FILE *f;
if (ftp_mode('a') == -1 || ftp_cwd(path) == -1)
return NULL;
free(status.remote.path);
status.remote.path = strdup(path);
status_do(bs_remote);
if ((fd=ftp_port()) == -1)
return NULL;
ftp_put("list");
if (ftp_resp() != 150) {
close(fd);
dir = (directory *)malloc(sizeof(directory));
dir->line = (direntry *)malloc(sizeof(direntry));
dir->path = strdup(path);
dir->len = 0;
dir->cur = dir->top = 0;
dir->size = sizeof(struct direntry);
dir->line->line = strdup("");
dir->line->type = 'x';
dir->line->name = strdup("");
dir->line->link = NULL;
return dir;
}
if ((f=ftp_accept(fd, "r")) == NULL)
return NULL;
dir = read_dir(f);
if (dir)
dir->path = strdup(path);
fclose(f);
ftp_resp();
return dir;
}
directory *
ftp_cd(char *wd, int force)
{
directory *dir;
char *nwd;
nwd = canonical(wd, NULL);
dir = get_dir(nwd, force);
if (dir != NULL) {
free(ftp_lcwd);
ftp_lcwd = nwd;
free(status.remote.path);
status.remote.path = strdup(ftp_lcwd);
status_do(bs_remote);
}
else
free(nwd);
return dir;
}
FILE *
ftp_retr(char *file, int mode, long *startp, long *sizep)
{
int fd;
char *dir, *name, *can, *p;
FILE *fin;
can = canonical(file, NULL);
dir = dirname(can);
name = (char *)basename(can);
if (ftp_mode(mode) == -1 || ftp_cwd(dir) == -1)
return NULL;
if ((fd=ftp_port()) == -1)
return NULL;
if (startp && *startp > 0) {
ftp_put("rest %ld", *startp);
if (ftp_resp() != 350)
*startp = 0;
}
ftp_put("retr %s", name);
if (ftp_resp() != 150) {
close(fd);
return NULL;
}
if (sizep != NULL) {
/* XXX: check how other servers format 150s */
if (strcmp(ftp_last_resp+strlen(ftp_last_resp)-8, " bytes).") == 0) {
if ((p=strrchr(ftp_last_resp, '(')) != NULL)
*sizep = strtol(p+1, NULL, 10);
}
}
if ((fin=ftp_accept(fd, "r")) == NULL) {
close(fd);
return NULL;
}
return fin;
}
FILE *
ftp_stor(char *file, int mode)
{
int fd, resp;
char *dir, *name, *can;
FILE *fin;
can = canonical(file, NULL);
dir = dirname(can);
name = (char *)basename(can);
if (ftp_mode(mode) == -1 || ftp_cwd(dir) == -1)
return NULL;
if ((fd=ftp_port()) == -1)
return NULL;
ftp_put("stor %s", name);
if ((resp=ftp_resp()) != 150 && resp != 125) {
close(fd);
return NULL;
}
if ((fin=ftp_accept(fd, "w")) == NULL) {
close(fd);
return NULL;
}
return fin;
}
int
ftp_fclose(FILE *f)
{
int err;
err = fclose(f);
if ((ftp_resp() != 226))
return -1;
return err;
}
int
ftp_mkdir(char *path)
{
ftp_put("mkd %s", path);
if (ftp_resp() != 257)
return -1;
return 0;
}
int
ftp_noop(void)
{
ftp_put("noop");
if (ftp_resp() != 200)
return -1;
return 0;
}
char *
ftp_pwd(void)
{
char *s, *e, *dir;
ftp_put("pwd");
if (ftp_resp() != 257)
return NULL;
if ((s=strchr(ftp_last_resp, '"')) == NULL
|| (e=strchr(s+1, '"')) == NULL)
return NULL;
if ((dir=(char *)malloc(e-s+1)) == NULL)
return NULL;
strncpy(dir, s+1, e-s-1);
dir[e-s-1] = '\0';
if (ftp_dosnames == -1) {
if ((isalpha(dir[0]) && dir[1] == ':' && dir[2] == '\\'))
ftp_dosnames = 1;
else if (dir[0] == '\\')
ftp_dosnames = 2;
else
ftp_dosnames = 0;
}
if (ftp_dosnames == 1 || ftp_dosnames == 2) {
if (ftp_dosnames == 1) {
strncpy(dir+1, s+1, e-s-1);
dir[e-s] = '\0';
dir[0] = '/';
}
for (s=dir; *s; s++)
if (*s == '\\')
*s = '/';
}
free(ftp_pcwd);
ftp_pcwd = strdup(dir);
return dir;
}
char *
ftp_gets(FILE *f)
{
char buf[8192], *line;
int l;
if (fgets(buf, 8192, f) == NULL)
return NULL;
line = strdup(buf);
l = strlen(line);
while (line[l-1] != '\n') {
if (fgets(buf, 8192, f) == NULL) {
free(line);
return NULL;
}
l += strlen(buf);
if ((line=realloc(line, l+1)) == NULL) {
disp_status("malloc failure");
return NULL;
}
strcpy(line+l, buf);
}
if (line[l-2] == '\r')
line[l-2] = '\0';
else
line[l-1] = '\0';
return line;
}
int
ftp_put(char *fmt, ...)
{
char buf[8192];
va_list argp;
if (conout == NULL) {
disp_status("not connected");
return -1;
}
va_start(argp, fmt);
vsprintf(buf, fmt, argp);
va_end(argp);
if (strncmp(buf, "pass ", 5) == 0 && ftp_anon == 0) {
disp_status("-> pass ********");
ftp_hist(strdup("-> pass ********"));
}
else {
disp_status("-> %s", buf);
ftp_histf("-> %s", buf);
}
fprintf(conout, "%s\r\n", buf);
if (fflush(conout) || ferror(conout)) {
disp_status("error writing to server: %s", strerror(errno));
return -1;
}
return 0;
}
int
ftp_abort(FILE *fin)
{
int resp;
char buf[4096];
fd_set ready;
struct timeval poll;
/* check wether abort is necessary (no data on control connection) */
poll.tv_sec = poll.tv_usec = 0;
FD_ZERO(&ready);
FD_SET(fileno(conin), &ready);
/* XXX: error ignored */
if (select(fileno(conin)+1, &ready, NULL, NULL, &poll) == 1)
return 0;
/* do abort */
disp_status("-> <attention>");
ftp_hist(strdup("-> <attention>"));
if (send(fileno(conout), "\377\364\377" /* IAC IP IAC */, 3,
MSG_OOB) != 3)
return -1;
if (fputc('\362' /* DM */, conout) == EOF)
return -1;
ftp_put("abor");
/* read remaining bytes from data connection */
while (fread(buf, 4096, 1, fin) > 0)
;
/* hanlde server response */
resp = ftp_resp();
if (resp == 226) {
ftp_unresp(226);
sleep(1);
/* check wether 226 is for us */
poll.tv_sec = poll.tv_usec = 0;
FD_ZERO(&ready);
FD_SET(fileno(conin), &ready);
/* XXX: error ignored */
if (select(fileno(conin)+1, &ready, NULL, NULL, &poll) == 1)
ftp_resp();
return 0;
}
else if (resp == 426) {
resp = ftp_resp();
ftp_unresp(426);
disp_status("426 Transfer aborted.");
return resp == 226;
}
else
return 1;
}
int
ftp_resp(void)
{
char *line;
int resp;
if (_ftp_keptresp != -1) {
resp = _ftp_keptresp;
_ftp_keptresp = -1;
return resp;
}
if (conin == NULL) {
disp_status("not connectecd");
return -1;
}
clearerr(conin);
if ((line=ftp_gets(conin)) == NULL) {
if (ferror(conin))
disp_status("read error from server: %s", strerror(errno));
else
disp_status("connection to server lost");
return -1;
}
resp = atoi(line);
disp_status("%s", line);
free(ftp_last_resp);
ftp_last_resp = strdup(line);
while (!(isdigit(line[0]) && isdigit(line[1]) &&
isdigit(line[2]) && line[3] == ' ')) {
ftp_hist(line);
if ((line=ftp_gets(conin)) == NULL) {
disp_status("read error from server: %s", strerror(errno));
return -1;
}
}
ftp_hist(line);
return resp;
}
void ftp_unresp(int resp)
{
_ftp_keptresp = resp;
}
int
ftp_port(void)
{
int fd, port, val, i, delim;
int len;
unsigned int iaddr;
unsigned char addr[4];
char baddr[16], bport[6], *s, *e;
char *saddr, *sport;
#ifdef HAVE_STRUCT_SOCKADDR_STORAGE
struct sockaddr_storage sa;
#else
struct sockaddr sa;
#endif
struct sockaddr_in *sin;
if (!opt_pasv) {
sin = (struct sockaddr_in *)ftp_addr;
len = sizeof(sa);
if ((fd=spassive(ftp_addr->sa_family,
(struct sockaddr *)&sa, &len)) == -1)
return -1;
port = htons(((struct sockaddr_in *)&sa)->sin_port);
if (ftp_addr->sa_family == AF_INET) {
iaddr = htonl(sin->sin_addr.s_addr);
ftp_put("port %d,%d,%d,%d,%d,%d",
(iaddr>>24) & 0xff, (iaddr>>16) & 0xff,
(iaddr>>8) & 0xff, iaddr & 0xff,
(port>>8) & 0xff, port & 0xff);
}
else {
ftp_put("eprt |%d|%s|%d|",
/* proto no */ 2,
sockaddr_ntop(ftp_addr), port);
}
if (ftp_resp() != 200) {
close(fd);
return -1;
}
}
else {
if (ftp_addr->sa_family == AF_INET) {
ftp_put("pasv");
if (ftp_resp() != 227)
return -1;
if ((s=strchr(ftp_last_resp, ',')) == NULL)
return -1;
while (isdigit(*(--s)))
;
s++;
for (i=0; i<4; i++) {
val = strtol(s, &e, 10);
if (val < 0 || val > 255 || *e != ',')
return -1;
addr[i] = val;
s = e+1;
}
port = strtol(s, &e, 10);
if (port < 0 || port > 255 || *e != ',')
return -1;
s = e+1;
val = strtol(s, &e, 10);
if (val < 0 || val > 255)
return -1;
port = port*256+val;
sprintf(baddr, "%d.%d.%d.%d",
addr[0], addr[1], addr[2], addr[3]);
sprintf(bport, "%d", port);
saddr = baddr;
sport = bport;
}
else { /* inet 6 passive */
ftp_put("epsv");
if (ftp_resp() != 229)
return -1;
if ((s=strchr(ftp_last_resp, '(')) == NULL)
return -1;
delim = s[1];
if ((e=strchr(s+2, delim)) == NULL
|| (e=strchr(e+1, delim)) == NULL)
return -1;
s = e+1;
if ((e=strchr(s, delim)) == NULL)
return -1;
if (e-s>5)
return -1;
strncpy(bport, s, e-s);
bport[e-s] = '\0';
sport = bport;
saddr = ftp_host;
}
fd = sopen(saddr, sport, ftp_addr->sa_family);
}
return fd;
}
FILE *
ftp_accept(int fd, char *mode)
{
int len, ns;
struct sockaddr addr;
if (!opt_pasv) {
len = sizeof(addr);
if ((ns=accept(fd, &addr, &len)) == -1)
return NULL;
close(fd);
fd = ns;
}
fcntl(fd, F_SETFD, 1); /* XXX: error check */
return fdopen(fd, mode);
}
int
ftp_mode(char m)
{
if (m == ftp_curmode)
return 0;
ftp_put("type %c", toupper(m));
if (ftp_resp() != 200)
return -1;
ftp_curmode = m;
return 0;
}
int
ftp_cwd(char *path)
{
char *s, *e;
int off;
if (ftp_pcwd && strcmp(path, ftp_pcwd) == 0)
return 0;
if (ftp_dosnames == 1 || ftp_dosnames == 2) {
off = (ftp_dosnames == 1) ? 1 : 0;
for (s=path+off; *s; s++)
if (*s == '/')
*s = '\\';
ftp_put("cwd %s", path+off);
for (s=path+off; *s; s++)
if (*s == '\\')
*s = '/';
}
else
ftp_put("cwd %s", path);
if (ftp_resp() != 250)
return -1;
if (ftp_dosnames == -1) {
if ((s=strchr(ftp_last_resp, '"')) != NULL
&& (e=strchr(s+1, '"')) != NULL) {
if (isalpha(s[1]) && s[2] == ':' && s[3] == '\\')
ftp_dosnames = 1;
else if (s[1] == '\\')
ftp_dosnames = 2;
else
ftp_dosnames = 0;
}
else
ftp_dosnames = 0;
}
free(ftp_pcwd);
ftp_pcwd = strdup(path);
return 0;
}
int
ftp_cat(FILE *fin, FILE *fout, long start, long size)
{
char buf[4096], fmt[4096];
int c, n, err;
long got;
#ifdef ENABLE_TRANSFER_RATE
struct itimerval itv;
long cur[4];
int old_alarm;
#else
time_t oldt, newt;
#endif
if (size >= 0) {
#ifdef ENABLE_TRANSFER_RATE
sprintf(fmt, "transferred %%ld/%ld "
"(total: %%.2fkb/s, current: %%.2fkb/s)",
size);
#else
sprintf(fmt, "transferred %%ld/%ld", size);
#endif
}
else {
#ifdef ENABLE_TRANSFER_RATE
strcpy(fmt, "transferred %ld (total: %%.2fkb/s, current: %%.2fkb/s)");
#else
strcpy(fmt, "transferred %ld");
#endif
}
got = start;
signal(SIGINT, sig_remember);
#ifdef ENABLE_TRANSFER_RATE
cur[0] = cur[1] = cur[2] = cur[3] = 0;
old_alarm = sig_alarm = sig_intr = 0;
itv.it_value.tv_sec = itv.it_interval.tv_sec = 1;
itv.it_value.tv_usec = itv.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &itv, NULL);
#else
oldt = 0;
#endif
if (ftp_curmode == 'i')
while ((n=fread(buf, 1, 4096, fin)) > 0) {
if (sig_intr)
break;
if (fwrite(buf, 1, n, fout) != n) {
break;
}
got += n;
#ifdef ENABLE_TRANSFER_RATE
if (old_alarm != sig_alarm) {
_ftp_update_transfer(fmt, got-start, cur,
old_alarm, sig_alarm);
old_alarm = sig_alarm;
}
#else
if ((newt=time(NULL)) != oldt) {
disp_status(fmt, got);
oldt = newt;
}
#endif
}
else
while ((c=getc(fin)) != EOF) {
if (sig_intr)
break;
got++;
if (c == '\r') {
if ((c=getc(fin)) != '\n')
putc('\r', fout);
ungetc(c, fin);
}
else {
putc(c, fout);
}
if (ferror(fout))
break;
#ifdef ENABLE_TRANSFER_RATE
if (old_alarm != sig_alarm) {
_ftp_update_transfer(fmt, got-start, cur,
old_alarm, sig_alarm);
old_alarm = sig_alarm;
}
#else
if (got%512 == 0 && (newt=time(NULL)) != oldt) {
disp_status(fmt, got);
oldt = newt;
}
#endif
}
signal(SIGINT, sig_end);
#ifdef ENABLE_TRANSFER_RATE
itv.it_value.tv_sec = itv.it_interval.tv_sec = 0;
itv.it_value.tv_usec = itv.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &itv, NULL);
#endif
if (ferror(fin) || sig_intr) {
errno = 0;
sig_intr = 0;
ftp_abort(fin);
return -1;
}
else if (ferror(fout)) {
err = errno;
errno = 0;
ftp_abort(fin);
disp_status("write error: %s", strerror(err));
return -1;
}
return 0;
}
#ifdef ENABLE_TRANSFER_RATE
static void
_ftp_update_transfer(char *fmt, long got, long *cur, int old_sec, int new_sec)
{
long step;
int nsec;
nsec = new_sec-old_sec;
switch (nsec) {
case 0:
break;
case 1:
cur[0] = cur[1];
cur[1] = cur[2];
cur[2] = cur[3];
break;
case 2:
cur[0] = cur[2];
cur[1] = cur[3];
cur[2] = cur[1] + (got-cur[1])/2;
break;
default:
step = (got-cur[3]) / nsec;
cur[0] = cur[3]+step*(nsec-3);
cur[1] = cur[3]+step*(nsec-2);
cur[2] = cur[3]+step*(nsec-1);
break;
}
cur[3] = got;
disp_status(fmt, got, got/(float)(new_sec*1024),
(cur[3]-cur[0])/((float)3*1024));
}
#endif /* ENABLE_TRANSFER_RATE */
int
ftp_gethostaddr(int fd)
{
int len;
len = sizeof(_ftp_addr);
if (getsockname(fd, ftp_addr, &len) == -1) {
if (disp_active)
disp_status("can't get host address: %s",
strerror(errno));
else
fprintf(stderr, "%s: can't get host addres: %s\n",
prg, strerror(errno));
return -1;
}
return 0;
}
void
ftp_histf(char *fmt, ...)
{
char buf[1024];
va_list argp;
if (ftp_hist_size == 0)
return;
va_start(argp, fmt);
vsprintf(buf, fmt, argp);
va_end(argp);
ftp_hist(strdup(buf));
}
void ftp_hist(char *line)
{
struct ftp_hist *p;
if (ftp_hist_size == 0)
return;
if (ftp_history == NULL) {
if ((ftp_history=(struct ftp_hist *)
malloc(sizeof(struct ftp_hist))) == NULL) {
free(line);
return;
}
ftp_hist_last = ftp_history;
ftp_history->next = NULL;
ftp_history->line = line;
ftp_hist_cursize = 1;
return;
}
if ((ftp_hist_last->next=(struct ftp_hist *)
malloc(sizeof(struct ftp_hist))) == NULL) {
free(line);
return;
}
ftp_hist_last = ftp_hist_last->next;
ftp_hist_last->next = NULL;
ftp_hist_last->line = line;
if (++ftp_hist_cursize > ftp_hist_size) {
--ftp_hist_cursize;
p = ftp_history;
ftp_history = ftp_history->next;
free(p->line);
free(p);
}
}
void
ftp_set_hist_size(int size, int *sizep)
{
struct ftp_hist *p;
if (size < 0)
return;
while (ftp_hist_cursize > size) {
--ftp_hist_cursize;
p = ftp_history;
ftp_history = ftp_history->next;
free(p->line);
free(p);
}
ftp_hist_size = size;
}
void
opts_mode(int c, int *cp)
{
switch (c) {
case 'a':
opt_mode = 'a';
break;
case 'i':
case 'b':
opt_mode = 'i';
}
}
|