1b39628e7SDag-Erling Smørgrav /*- 2b39628e7SDag-Erling Smørgrav * Copyright (c) 2000 Dag-Erling Co�dan Sm�rgrav 3b39628e7SDag-Erling Smørgrav * All rights reserved. 4b39628e7SDag-Erling Smørgrav * 5b39628e7SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 6b39628e7SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 7b39628e7SDag-Erling Smørgrav * are met: 8b39628e7SDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright 9b39628e7SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer 10b39628e7SDag-Erling Smørgrav * in this position and unchanged. 11b39628e7SDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright 12b39628e7SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the 13b39628e7SDag-Erling Smørgrav * documentation and/or other materials provided with the distribution. 14b39628e7SDag-Erling Smørgrav * 3. The name of the author may not be used to endorse or promote products 15b39628e7SDag-Erling Smørgrav * derived from this software without specific prior written permission 16b39628e7SDag-Erling Smørgrav * 17b39628e7SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18b39628e7SDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19b39628e7SDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20b39628e7SDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21b39628e7SDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22b39628e7SDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23b39628e7SDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24b39628e7SDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25b39628e7SDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26b39628e7SDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27b39628e7SDag-Erling Smørgrav */ 28b39628e7SDag-Erling Smørgrav 2915baa31aSPhilippe Charnier #include <sys/cdefs.h> 3015baa31aSPhilippe Charnier __FBSDID("$FreeBSD$"); 3115baa31aSPhilippe Charnier 32b39628e7SDag-Erling Smørgrav #include <sys/param.h> 33b39628e7SDag-Erling Smørgrav #include <sys/socket.h> 34469a4570SBruce Evans #include <sys/stat.h> 3505eb0358SBruce Evans #include <sys/time.h> 36b39628e7SDag-Erling Smørgrav 37b39628e7SDag-Erling Smørgrav #include <ctype.h> 38b39628e7SDag-Erling Smørgrav #include <err.h> 39b39628e7SDag-Erling Smørgrav #include <errno.h> 4078394463SDag-Erling Smørgrav #include <signal.h> 41b39628e7SDag-Erling Smørgrav #include <stdio.h> 42b39628e7SDag-Erling Smørgrav #include <stdlib.h> 43b39628e7SDag-Erling Smørgrav #include <string.h> 44b39628e7SDag-Erling Smørgrav #include <sysexits.h> 450d60c709SDag-Erling Smørgrav #include <termios.h> 46b39628e7SDag-Erling Smørgrav #include <unistd.h> 47b39628e7SDag-Erling Smørgrav 48b39628e7SDag-Erling Smørgrav #include <fetch.h> 49b39628e7SDag-Erling Smørgrav 50b39628e7SDag-Erling Smørgrav #define MINBUFSIZE 4096 51b39628e7SDag-Erling Smørgrav 52b39628e7SDag-Erling Smørgrav /* Option flags */ 53b39628e7SDag-Erling Smørgrav int A_flag; /* -A: do not follow 302 redirects */ 54b39628e7SDag-Erling Smørgrav int a_flag; /* -a: auto retry */ 5591404f38SDag-Erling Smørgrav off_t B_size; /* -B: buffer size */ 56b39628e7SDag-Erling Smørgrav int b_flag; /*! -b: workaround TCP bug */ 57a8369cd9SDag-Erling Smørgrav char *c_dirname; /* -c: remote directory */ 58b39628e7SDag-Erling Smørgrav int d_flag; /* -d: direct connection */ 59b39628e7SDag-Erling Smørgrav int F_flag; /* -F: restart without checking mtime */ 60b39628e7SDag-Erling Smørgrav char *f_filename; /* -f: file to fetch */ 61b39628e7SDag-Erling Smørgrav char *h_hostname; /* -h: host to fetch from */ 62b39628e7SDag-Erling Smørgrav int l_flag; /* -l: link rather than copy file: URLs */ 63bb11a878SDag-Erling Smørgrav int m_flag; /* -[Mm]: mirror mode */ 6413da7d99SDag-Erling Smørgrav char *N_filename; /* -N: netrc file name */ 65bb11a878SDag-Erling Smørgrav int n_flag; /* -n: do not preserve modification time */ 66b39628e7SDag-Erling Smørgrav int o_flag; /* -o: specify output file */ 67b39628e7SDag-Erling Smørgrav int o_directory; /* output file is a directory */ 68b39628e7SDag-Erling Smørgrav char *o_filename; /* name of output file */ 69b39628e7SDag-Erling Smørgrav int o_stdout; /* output file is stdout */ 70b39628e7SDag-Erling Smørgrav int once_flag; /* -1: stop at first successful file */ 71a5e3ae21SDag-Erling Smørgrav int p_flag; /* -[Pp]: use passive FTP */ 72b39628e7SDag-Erling Smørgrav int R_flag; /* -R: don't delete partially transferred files */ 73b39628e7SDag-Erling Smørgrav int r_flag; /* -r: restart previously interrupted transfer */ 74b39628e7SDag-Erling Smørgrav off_t S_size; /* -S: require size to match */ 757c480c6cSDag-Erling Smørgrav int s_flag; /* -s: show size, don't fetch */ 7632411a1bSDag-Erling Smørgrav long T_secs = 120; /* -T: transfer timeout in seconds */ 77b39628e7SDag-Erling Smørgrav int t_flag; /*! -t: workaround TCP bug */ 787c480c6cSDag-Erling Smørgrav int U_flag; /* -U: do not use high ports */ 79b39628e7SDag-Erling Smørgrav int v_level = 1; /* -v: verbosity level */ 80b39628e7SDag-Erling Smørgrav int v_tty; /* stdout is a tty */ 81dbcc1983SDag-Erling Smørgrav pid_t pgrp; /* our process group */ 8232411a1bSDag-Erling Smørgrav long w_secs; /* -w: retry delay */ 83b39628e7SDag-Erling Smørgrav int family = PF_UNSPEC; /* -[46]: address family to use */ 84b39628e7SDag-Erling Smørgrav 85ef50a72cSDag-Erling Smørgrav int sigalrm; /* SIGALRM received */ 86cd400b67SDag-Erling Smørgrav int siginfo; /* SIGINFO received */ 87ef50a72cSDag-Erling Smørgrav int sigint; /* SIGINT received */ 88b39628e7SDag-Erling Smørgrav 8932411a1bSDag-Erling Smørgrav long ftp_timeout; /* default timeout for FTP transfers */ 9032411a1bSDag-Erling Smørgrav long http_timeout; /* default timeout for HTTP transfers */ 91b39628e7SDag-Erling Smørgrav u_char *buf; /* transfer buffer */ 92b39628e7SDag-Erling Smørgrav 93b39628e7SDag-Erling Smørgrav 94db695db7SDag-Erling Smørgrav /* 95db695db7SDag-Erling Smørgrav * Signal handler 96db695db7SDag-Erling Smørgrav */ 9791404f38SDag-Erling Smørgrav static void 98b39628e7SDag-Erling Smørgrav sig_handler(int sig) 99b39628e7SDag-Erling Smørgrav { 100ef50a72cSDag-Erling Smørgrav switch (sig) { 101ef50a72cSDag-Erling Smørgrav case SIGALRM: 102ef50a72cSDag-Erling Smørgrav sigalrm = 1; 103ef50a72cSDag-Erling Smørgrav break; 104cd400b67SDag-Erling Smørgrav case SIGINFO: 105cd400b67SDag-Erling Smørgrav siginfo = 1; 106cd400b67SDag-Erling Smørgrav break; 107ef50a72cSDag-Erling Smørgrav case SIGINT: 108ef50a72cSDag-Erling Smørgrav sigint = 1; 109ef50a72cSDag-Erling Smørgrav break; 110ef50a72cSDag-Erling Smørgrav } 111b39628e7SDag-Erling Smørgrav } 112b39628e7SDag-Erling Smørgrav 113b39628e7SDag-Erling Smørgrav struct xferstat { 114b39628e7SDag-Erling Smørgrav char name[40]; 115b39628e7SDag-Erling Smørgrav struct timeval start; 116b39628e7SDag-Erling Smørgrav struct timeval last; 117b39628e7SDag-Erling Smørgrav off_t size; 118b39628e7SDag-Erling Smørgrav off_t offset; 119b39628e7SDag-Erling Smørgrav off_t rcvd; 120b39628e7SDag-Erling Smørgrav }; 121b39628e7SDag-Erling Smørgrav 122db695db7SDag-Erling Smørgrav /* 12313da7d99SDag-Erling Smørgrav * Compute and display ETA 12413da7d99SDag-Erling Smørgrav */ 12513da7d99SDag-Erling Smørgrav static void 12613da7d99SDag-Erling Smørgrav stat_eta(struct xferstat *xs) 12713da7d99SDag-Erling Smørgrav { 12832c8cd29SDag-Erling Smørgrav long elapsed, remaining; 12913da7d99SDag-Erling Smørgrav 13013da7d99SDag-Erling Smørgrav elapsed = xs->last.tv_sec - xs->start.tv_sec; 13113da7d99SDag-Erling Smørgrav remaining = ((xs->size * elapsed) / xs->rcvd) - elapsed; 13213da7d99SDag-Erling Smørgrav if (remaining > 3600) { 13313da7d99SDag-Erling Smørgrav fprintf(stderr, "%02ld:", remaining / 3600); 13413da7d99SDag-Erling Smørgrav remaining %= 3600; 13513da7d99SDag-Erling Smørgrav } 13613da7d99SDag-Erling Smørgrav fprintf(stderr, "%02ld:%02ld", 13713da7d99SDag-Erling Smørgrav remaining / 60, remaining % 60); 13813da7d99SDag-Erling Smørgrav } 13913da7d99SDag-Erling Smørgrav 14013da7d99SDag-Erling Smørgrav /* 14113da7d99SDag-Erling Smørgrav * Compute and display transfer rate 14213da7d99SDag-Erling Smørgrav */ 14313da7d99SDag-Erling Smørgrav static void 14413da7d99SDag-Erling Smørgrav stat_bps(struct xferstat *xs) 14513da7d99SDag-Erling Smørgrav { 14632c8cd29SDag-Erling Smørgrav double delta, bps; 14713da7d99SDag-Erling Smørgrav 14832c8cd29SDag-Erling Smørgrav delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6)) 14932c8cd29SDag-Erling Smørgrav - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6)); 15032c8cd29SDag-Erling Smørgrav if (delta == 0.0) { 15132c8cd29SDag-Erling Smørgrav fprintf(stderr, "?? Bps"); 15232c8cd29SDag-Erling Smørgrav return; 15332c8cd29SDag-Erling Smørgrav } 15432c8cd29SDag-Erling Smørgrav bps = (xs->rcvd - xs->offset) / delta; 15513da7d99SDag-Erling Smørgrav if (bps > 1024*1024) 15613da7d99SDag-Erling Smørgrav fprintf(stderr, "%.2f MBps", bps / (1024*1024)); 15713da7d99SDag-Erling Smørgrav else if (bps > 1024) 15813da7d99SDag-Erling Smørgrav fprintf(stderr, "%.2f kBps", bps / 1024); 15913da7d99SDag-Erling Smørgrav else 16013da7d99SDag-Erling Smørgrav fprintf(stderr, "%.2f Bps", bps); 16113da7d99SDag-Erling Smørgrav } 16213da7d99SDag-Erling Smørgrav 16313da7d99SDag-Erling Smørgrav /* 164db695db7SDag-Erling Smørgrav * Update the stats display 165db695db7SDag-Erling Smørgrav */ 16691404f38SDag-Erling Smørgrav static void 16749e62d8fSDag-Erling Smørgrav stat_display(struct xferstat *xs, int force) 16849e62d8fSDag-Erling Smørgrav { 16949e62d8fSDag-Erling Smørgrav struct timeval now; 170dbcc1983SDag-Erling Smørgrav int ctty_pgrp; 17149e62d8fSDag-Erling Smørgrav 172d939bf77SDag-Erling Smørgrav if (!v_tty || !v_level) 17349e62d8fSDag-Erling Smørgrav return; 17449e62d8fSDag-Erling Smørgrav 175dbcc1983SDag-Erling Smørgrav /* check if we're the foreground process */ 176dbcc1983SDag-Erling Smørgrav if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 || 177dbcc1983SDag-Erling Smørgrav (pid_t)ctty_pgrp != pgrp) 178dbcc1983SDag-Erling Smørgrav return; 179dbcc1983SDag-Erling Smørgrav 18049e62d8fSDag-Erling Smørgrav gettimeofday(&now, NULL); 18149e62d8fSDag-Erling Smørgrav if (!force && now.tv_sec <= xs->last.tv_sec) 18249e62d8fSDag-Erling Smørgrav return; 18349e62d8fSDag-Erling Smørgrav xs->last = now; 18449e62d8fSDag-Erling Smørgrav 18549e62d8fSDag-Erling Smørgrav fprintf(stderr, "\rReceiving %s", xs->name); 186dc161d55SDag-Erling Smørgrav if (xs->size <= 0) { 18791404f38SDag-Erling Smørgrav fprintf(stderr, ": %lld bytes", (long long)xs->rcvd); 188dc161d55SDag-Erling Smørgrav } else { 18991404f38SDag-Erling Smørgrav fprintf(stderr, " (%lld bytes): %d%%", (long long)xs->size, 190b3c141fdSDag-Erling Smørgrav (int)((100.0 * xs->rcvd) / xs->size)); 19132c8cd29SDag-Erling Smørgrav if (xs->rcvd > 0 && xs->last.tv_sec >= xs->start.tv_sec + 30) { 192dc161d55SDag-Erling Smørgrav fprintf(stderr, " (ETA "); 19313da7d99SDag-Erling Smørgrav stat_eta(xs); 19413da7d99SDag-Erling Smørgrav if (v_level > 1) { 19513da7d99SDag-Erling Smørgrav fprintf(stderr, " at "); 19613da7d99SDag-Erling Smørgrav stat_bps(xs); 197dc161d55SDag-Erling Smørgrav } 19813da7d99SDag-Erling Smørgrav fprintf(stderr, ") "); 199dc161d55SDag-Erling Smørgrav } 200dc161d55SDag-Erling Smørgrav } 20149e62d8fSDag-Erling Smørgrav } 202aa4b3574SDag-Erling Smørgrav 203db695db7SDag-Erling Smørgrav /* 204db695db7SDag-Erling Smørgrav * Initialize the transfer statistics 205db695db7SDag-Erling Smørgrav */ 20691404f38SDag-Erling Smørgrav static void 20791404f38SDag-Erling Smørgrav stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset) 208b39628e7SDag-Erling Smørgrav { 209b39628e7SDag-Erling Smørgrav snprintf(xs->name, sizeof xs->name, "%s", name); 210aa4b3574SDag-Erling Smørgrav gettimeofday(&xs->start, NULL); 211aa4b3574SDag-Erling Smørgrav xs->last.tv_sec = xs->last.tv_usec = 0; 212b39628e7SDag-Erling Smørgrav xs->size = size; 213b39628e7SDag-Erling Smørgrav xs->offset = offset; 214b3c141fdSDag-Erling Smørgrav xs->rcvd = offset; 21549e62d8fSDag-Erling Smørgrav stat_display(xs, 1); 216b39628e7SDag-Erling Smørgrav } 217b39628e7SDag-Erling Smørgrav 218db695db7SDag-Erling Smørgrav /* 219db695db7SDag-Erling Smørgrav * Update the transfer statistics 220db695db7SDag-Erling Smørgrav */ 22191404f38SDag-Erling Smørgrav static void 22291404f38SDag-Erling Smørgrav stat_update(struct xferstat *xs, off_t rcvd) 223b39628e7SDag-Erling Smørgrav { 224b39628e7SDag-Erling Smørgrav xs->rcvd = rcvd; 22549e62d8fSDag-Erling Smørgrav stat_display(xs, 0); 226b39628e7SDag-Erling Smørgrav } 227b39628e7SDag-Erling Smørgrav 228db695db7SDag-Erling Smørgrav /* 229db695db7SDag-Erling Smørgrav * Finalize the transfer statistics 230db695db7SDag-Erling Smørgrav */ 23191404f38SDag-Erling Smørgrav static void 232b39628e7SDag-Erling Smørgrav stat_end(struct xferstat *xs) 233b39628e7SDag-Erling Smørgrav { 234b39628e7SDag-Erling Smørgrav double delta; 23532190ef5SDag-Erling Smørgrav 23632190ef5SDag-Erling Smørgrav if (!v_level) 23732190ef5SDag-Erling Smørgrav return; 238b39628e7SDag-Erling Smørgrav 23932c8cd29SDag-Erling Smørgrav gettimeofday(&xs->last, NULL); 240b39628e7SDag-Erling Smørgrav 24149e62d8fSDag-Erling Smørgrav stat_display(xs, 1); 242b39628e7SDag-Erling Smørgrav fputc('\n', stderr); 24332c8cd29SDag-Erling Smørgrav delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6)) 244b39628e7SDag-Erling Smørgrav - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6)); 24513da7d99SDag-Erling Smørgrav fprintf(stderr, "%lld bytes transferred in %.1f seconds (", 24691404f38SDag-Erling Smørgrav (long long)(xs->rcvd - xs->offset), delta); 24713da7d99SDag-Erling Smørgrav stat_bps(xs); 24813da7d99SDag-Erling Smørgrav fprintf(stderr, ")\n"); 249b39628e7SDag-Erling Smørgrav } 250b39628e7SDag-Erling Smørgrav 251db695db7SDag-Erling Smørgrav /* 252db695db7SDag-Erling Smørgrav * Ask the user for authentication details 253db695db7SDag-Erling Smørgrav */ 25491404f38SDag-Erling Smørgrav static int 2550d60c709SDag-Erling Smørgrav query_auth(struct url *URL) 2560d60c709SDag-Erling Smørgrav { 2570d60c709SDag-Erling Smørgrav struct termios tios; 2580d60c709SDag-Erling Smørgrav tcflag_t saved_flags; 2590d60c709SDag-Erling Smørgrav int i, nopwd; 2600d60c709SDag-Erling Smørgrav 2610d60c709SDag-Erling Smørgrav 2620d60c709SDag-Erling Smørgrav fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n", 263650b9e0eSIan Dowse URL->scheme, URL->host, URL->port); 2640d60c709SDag-Erling Smørgrav 2650d60c709SDag-Erling Smørgrav fprintf(stderr, "Login: "); 2660d60c709SDag-Erling Smørgrav if (fgets(URL->user, sizeof URL->user, stdin) == NULL) 2670d60c709SDag-Erling Smørgrav return -1; 2680d60c709SDag-Erling Smørgrav for (i = 0; URL->user[i]; ++i) 2690d60c709SDag-Erling Smørgrav if (isspace(URL->user[i])) 2700d60c709SDag-Erling Smørgrav URL->user[i] = '\0'; 2710d60c709SDag-Erling Smørgrav 2720d60c709SDag-Erling Smørgrav fprintf(stderr, "Password: "); 2730d60c709SDag-Erling Smørgrav if (tcgetattr(STDIN_FILENO, &tios) == 0) { 2740d60c709SDag-Erling Smørgrav saved_flags = tios.c_lflag; 2750d60c709SDag-Erling Smørgrav tios.c_lflag &= ~ECHO; 2760d60c709SDag-Erling Smørgrav tios.c_lflag |= ECHONL|ICANON; 2770d60c709SDag-Erling Smørgrav tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios); 2780d60c709SDag-Erling Smørgrav nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL); 2790d60c709SDag-Erling Smørgrav tios.c_lflag = saved_flags; 2800d60c709SDag-Erling Smørgrav tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios); 2810d60c709SDag-Erling Smørgrav } else { 2820d60c709SDag-Erling Smørgrav nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL); 2830d60c709SDag-Erling Smørgrav } 2840d60c709SDag-Erling Smørgrav if (nopwd) 2850d60c709SDag-Erling Smørgrav return -1; 2860d60c709SDag-Erling Smørgrav 2870d60c709SDag-Erling Smørgrav for (i = 0; URL->pwd[i]; ++i) 2880d60c709SDag-Erling Smørgrav if (isspace(URL->pwd[i])) 2890d60c709SDag-Erling Smørgrav URL->pwd[i] = '\0'; 2900d60c709SDag-Erling Smørgrav return 0; 2910d60c709SDag-Erling Smørgrav } 2920d60c709SDag-Erling Smørgrav 293db695db7SDag-Erling Smørgrav /* 294db695db7SDag-Erling Smørgrav * Fetch a file 295db695db7SDag-Erling Smørgrav */ 29691404f38SDag-Erling Smørgrav static int 29791404f38SDag-Erling Smørgrav fetch(char *URL, const char *path) 298b39628e7SDag-Erling Smørgrav { 299b39628e7SDag-Erling Smørgrav struct url *url; 300b39628e7SDag-Erling Smørgrav struct url_stat us; 301e9dc4f3bSDag-Erling Smørgrav struct stat sb, nsb; 302b39628e7SDag-Erling Smørgrav struct xferstat xs; 303b39628e7SDag-Erling Smørgrav FILE *f, *of; 304cd400b67SDag-Erling Smørgrav size_t size, wr; 305b39628e7SDag-Erling Smørgrav off_t count; 306b39628e7SDag-Erling Smørgrav char flags[8]; 307e9dc4f3bSDag-Erling Smørgrav const char *slash; 308e9dc4f3bSDag-Erling Smørgrav char *tmppath; 3090d60c709SDag-Erling Smørgrav int r; 310b39628e7SDag-Erling Smørgrav u_int timeout; 311cd400b67SDag-Erling Smørgrav u_char *ptr; 312b39628e7SDag-Erling Smørgrav 313b39628e7SDag-Erling Smørgrav f = of = NULL; 314e9dc4f3bSDag-Erling Smørgrav tmppath = NULL; 315b39628e7SDag-Erling Smørgrav 31613da7d99SDag-Erling Smørgrav timeout = 0; 31713da7d99SDag-Erling Smørgrav *flags = 0; 31813da7d99SDag-Erling Smørgrav count = 0; 31913da7d99SDag-Erling Smørgrav 32013da7d99SDag-Erling Smørgrav /* set verbosity level */ 32113da7d99SDag-Erling Smørgrav if (v_level > 1) 32213da7d99SDag-Erling Smørgrav strcat(flags, "v"); 32313da7d99SDag-Erling Smørgrav if (v_level > 2) 32413da7d99SDag-Erling Smørgrav fetchDebug = 1; 32513da7d99SDag-Erling Smørgrav 326b39628e7SDag-Erling Smørgrav /* parse URL */ 327b39628e7SDag-Erling Smørgrav if ((url = fetchParseURL(URL)) == NULL) { 328b39628e7SDag-Erling Smørgrav warnx("%s: parse error", URL); 329b39628e7SDag-Erling Smørgrav goto failure; 330b39628e7SDag-Erling Smørgrav } 331b39628e7SDag-Erling Smørgrav 33264638f67SDag-Erling Smørgrav /* if no scheme was specified, take a guess */ 33364638f67SDag-Erling Smørgrav if (!*url->scheme) { 33464638f67SDag-Erling Smørgrav if (!*url->host) 33564638f67SDag-Erling Smørgrav strcpy(url->scheme, SCHEME_FILE); 33674bd3d76SDag-Erling Smørgrav else if (strncasecmp(url->host, "ftp.", 4) == 0) 33764638f67SDag-Erling Smørgrav strcpy(url->scheme, SCHEME_FTP); 33874bd3d76SDag-Erling Smørgrav else if (strncasecmp(url->host, "www.", 4) == 0) 33964638f67SDag-Erling Smørgrav strcpy(url->scheme, SCHEME_HTTP); 34064638f67SDag-Erling Smørgrav } 34164638f67SDag-Erling Smørgrav 342b39628e7SDag-Erling Smørgrav /* common flags */ 343b39628e7SDag-Erling Smørgrav switch (family) { 344b39628e7SDag-Erling Smørgrav case PF_INET: 345b39628e7SDag-Erling Smørgrav strcat(flags, "4"); 346b39628e7SDag-Erling Smørgrav break; 347b39628e7SDag-Erling Smørgrav case PF_INET6: 348b39628e7SDag-Erling Smørgrav strcat(flags, "6"); 349b39628e7SDag-Erling Smørgrav break; 350b39628e7SDag-Erling Smørgrav } 351b39628e7SDag-Erling Smørgrav 352b39628e7SDag-Erling Smørgrav /* FTP specific flags */ 353b39628e7SDag-Erling Smørgrav if (strcmp(url->scheme, "ftp") == 0) { 354b39628e7SDag-Erling Smørgrav if (p_flag) 355b39628e7SDag-Erling Smørgrav strcat(flags, "p"); 356b39628e7SDag-Erling Smørgrav if (d_flag) 357b39628e7SDag-Erling Smørgrav strcat(flags, "d"); 3587c480c6cSDag-Erling Smørgrav if (U_flag) 3597c480c6cSDag-Erling Smørgrav strcat(flags, "l"); 360b39628e7SDag-Erling Smørgrav timeout = T_secs ? T_secs : ftp_timeout; 361b39628e7SDag-Erling Smørgrav } 362b39628e7SDag-Erling Smørgrav 363b39628e7SDag-Erling Smørgrav /* HTTP specific flags */ 364b39628e7SDag-Erling Smørgrav if (strcmp(url->scheme, "http") == 0) { 365b39628e7SDag-Erling Smørgrav if (d_flag) 366b39628e7SDag-Erling Smørgrav strcat(flags, "d"); 367b39628e7SDag-Erling Smørgrav if (A_flag) 368b39628e7SDag-Erling Smørgrav strcat(flags, "A"); 369b39628e7SDag-Erling Smørgrav timeout = T_secs ? T_secs : http_timeout; 370b39628e7SDag-Erling Smørgrav } 371b39628e7SDag-Erling Smørgrav 372ef50a72cSDag-Erling Smørgrav /* set the protocol timeout. */ 373b39628e7SDag-Erling Smørgrav fetchTimeout = timeout; 374b39628e7SDag-Erling Smørgrav 375b39628e7SDag-Erling Smørgrav /* just print size */ 376b39628e7SDag-Erling Smørgrav if (s_flag) { 377dc161d55SDag-Erling Smørgrav if (timeout) 378dc161d55SDag-Erling Smørgrav alarm(timeout); 379dc161d55SDag-Erling Smørgrav r = fetchStat(url, &us, flags); 380dc161d55SDag-Erling Smørgrav if (timeout) 381dc161d55SDag-Erling Smørgrav alarm(0); 382dc161d55SDag-Erling Smørgrav if (sigalrm || sigint) 383dc161d55SDag-Erling Smørgrav goto signal; 384dc161d55SDag-Erling Smørgrav if (r == -1) { 385dc161d55SDag-Erling Smørgrav warnx("%s", fetchLastErrString); 386e9a039c2SDag-Erling Smørgrav goto failure; 387dc161d55SDag-Erling Smørgrav } 388b39628e7SDag-Erling Smørgrav if (us.size == -1) 389b39628e7SDag-Erling Smørgrav printf("Unknown\n"); 390b39628e7SDag-Erling Smørgrav else 39191404f38SDag-Erling Smørgrav printf("%lld\n", (long long)us.size); 392b39628e7SDag-Erling Smørgrav goto success; 393b39628e7SDag-Erling Smørgrav } 394b39628e7SDag-Erling Smørgrav 395e9a039c2SDag-Erling Smørgrav /* 39691404f38SDag-Erling Smørgrav * If the -r flag was specified, we have to compare the local 39791404f38SDag-Erling Smørgrav * and remote files, so we should really do a fetchStat() 39891404f38SDag-Erling Smørgrav * first, but I know of at least one HTTP server that only 39991404f38SDag-Erling Smørgrav * sends the content size in response to GET requests, and 40091404f38SDag-Erling Smørgrav * leaves it out of replies to HEAD requests. Also, in the 40191404f38SDag-Erling Smørgrav * (frequent) case that the local and remote files match but 40291404f38SDag-Erling Smørgrav * the local file is truncated, we have sufficient information 40391404f38SDag-Erling Smørgrav * before the compare to issue a correct request. Therefore, 40491404f38SDag-Erling Smørgrav * we always issue a GET request as if we were sure the local 40591404f38SDag-Erling Smørgrav * file was a truncated copy of the remote file; we can drop 40691404f38SDag-Erling Smørgrav * the connection later if we change our minds. 407e9a039c2SDag-Erling Smørgrav */ 408a0c6ec97SDag-Erling Smørgrav sb.st_size = -1; 409e9dc4f3bSDag-Erling Smørgrav if (!o_stdout && stat(path, &sb) == -1 && errno != ENOENT) { 410e9dc4f3bSDag-Erling Smørgrav warnx("%s: stat()", path); 411e9dc4f3bSDag-Erling Smørgrav goto failure; 412a0c6ec97SDag-Erling Smørgrav } 413e9dc4f3bSDag-Erling Smørgrav if (!o_stdout && r_flag && S_ISREG(sb.st_mode)) 414e9dc4f3bSDag-Erling Smørgrav url->offset = sb.st_size; 415e9a039c2SDag-Erling Smørgrav 416e9a039c2SDag-Erling Smørgrav /* start the transfer */ 417dc161d55SDag-Erling Smørgrav if (timeout) 418dc161d55SDag-Erling Smørgrav alarm(timeout); 419dc161d55SDag-Erling Smørgrav f = fetchXGet(url, &us, flags); 4200c6d34afSDag-Erling Smørgrav if (timeout) 4210c6d34afSDag-Erling Smørgrav alarm(0); 422dc161d55SDag-Erling Smørgrav if (sigalrm || sigint) 423dc161d55SDag-Erling Smørgrav goto signal; 424dc161d55SDag-Erling Smørgrav if (f == NULL) { 4253d7c408cSDag-Erling Smørgrav warnx("%s: %s", URL, fetchLastErrString); 426e9a039c2SDag-Erling Smørgrav goto failure; 427e9a039c2SDag-Erling Smørgrav } 428e9a039c2SDag-Erling Smørgrav if (sigint) 429e9a039c2SDag-Erling Smørgrav goto signal; 430e9a039c2SDag-Erling Smørgrav 431b39628e7SDag-Erling Smørgrav /* check that size is as expected */ 432e9a039c2SDag-Erling Smørgrav if (S_size) { 433e9a039c2SDag-Erling Smørgrav if (us.size == -1) { 4343d7c408cSDag-Erling Smørgrav warnx("%s: size unknown", URL); 435e9a039c2SDag-Erling Smørgrav goto failure; 436e9a039c2SDag-Erling Smørgrav } else if (us.size != S_size) { 437b39628e7SDag-Erling Smørgrav warnx("%s: size mismatch: expected %lld, actual %lld", 4383d7c408cSDag-Erling Smørgrav URL, (long long)S_size, (long long)us.size); 439b39628e7SDag-Erling Smørgrav goto failure; 440b39628e7SDag-Erling Smørgrav } 441e9a039c2SDag-Erling Smørgrav } 442b39628e7SDag-Erling Smørgrav 443b39628e7SDag-Erling Smørgrav /* symlink instead of copy */ 444b39628e7SDag-Erling Smørgrav if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) { 445b39628e7SDag-Erling Smørgrav if (symlink(url->doc, path) == -1) { 446b39628e7SDag-Erling Smørgrav warn("%s: symlink()", path); 447b39628e7SDag-Erling Smørgrav goto failure; 448b39628e7SDag-Erling Smørgrav } 449b39628e7SDag-Erling Smørgrav goto success; 450b39628e7SDag-Erling Smørgrav } 451b39628e7SDag-Erling Smørgrav 452eb87c5a7SDag-Erling Smørgrav if (us.size == -1 && !o_stdout && v_level > 0) 4533d7c408cSDag-Erling Smørgrav warnx("%s: size of remote file is not known", URL); 454d939bf77SDag-Erling Smørgrav if (v_level > 1) { 455a0c6ec97SDag-Erling Smørgrav if (sb.st_size != -1) 456a0c6ec97SDag-Erling Smørgrav fprintf(stderr, "local size / mtime: %lld / %ld\n", 45791404f38SDag-Erling Smørgrav (long long)sb.st_size, (long)sb.st_mtime); 45889a70fbeSDag-Erling Smørgrav if (us.size != -1) 459a0c6ec97SDag-Erling Smørgrav fprintf(stderr, "remote size / mtime: %lld / %ld\n", 46091404f38SDag-Erling Smørgrav (long long)us.size, (long)us.mtime); 461d939bf77SDag-Erling Smørgrav } 462d939bf77SDag-Erling Smørgrav 463e9a039c2SDag-Erling Smørgrav /* open output file */ 464b39628e7SDag-Erling Smørgrav if (o_stdout) { 465b39628e7SDag-Erling Smørgrav /* output to stdout */ 466b39628e7SDag-Erling Smørgrav of = stdout; 467e9dc4f3bSDag-Erling Smørgrav } else if (r_flag && sb.st_size != -1) { 468e9a039c2SDag-Erling Smørgrav /* resume mode, local file exists */ 469e9a039c2SDag-Erling Smørgrav if (!F_flag && us.mtime && sb.st_mtime != us.mtime) { 470e9a039c2SDag-Erling Smørgrav /* no match! have to refetch */ 471e9a039c2SDag-Erling Smørgrav fclose(f); 47289a70fbeSDag-Erling Smørgrav /* if precious, warn the user and give up */ 47389a70fbeSDag-Erling Smørgrav if (R_flag) { 47491404f38SDag-Erling Smørgrav warnx("%s: local modification time " 47591404f38SDag-Erling Smørgrav "does not match remote", path); 47689a70fbeSDag-Erling Smørgrav goto failure_keep; 47789a70fbeSDag-Erling Smørgrav } 478e9a039c2SDag-Erling Smørgrav } else { 479e9a039c2SDag-Erling Smørgrav if (us.size == sb.st_size) 480e9a039c2SDag-Erling Smørgrav /* nothing to do */ 481e9a039c2SDag-Erling Smørgrav goto success; 482e9a039c2SDag-Erling Smørgrav if (sb.st_size > us.size) { 483e9a039c2SDag-Erling Smørgrav /* local file too long! */ 484e9a039c2SDag-Erling Smørgrav warnx("%s: local file (%lld bytes) is longer " 48591404f38SDag-Erling Smørgrav "than remote file (%lld bytes)", path, 48691404f38SDag-Erling Smørgrav (long long)sb.st_size, (long long)us.size); 487e9a039c2SDag-Erling Smørgrav goto failure; 488e9a039c2SDag-Erling Smørgrav } 489e9dc4f3bSDag-Erling Smørgrav /* we got it, open local file */ 490b39628e7SDag-Erling Smørgrav if ((of = fopen(path, "a")) == NULL) { 49110e3b1c7SDag-Erling Smørgrav warn("%s: fopen()", path); 492b39628e7SDag-Erling Smørgrav goto failure; 493b39628e7SDag-Erling Smørgrav } 494e9dc4f3bSDag-Erling Smørgrav /* check that it didn't move under our feet */ 495e9dc4f3bSDag-Erling Smørgrav if (fstat(fileno(of), &nsb) == -1) { 496e9dc4f3bSDag-Erling Smørgrav /* can't happen! */ 497e9dc4f3bSDag-Erling Smørgrav warn("%s: fstat()", path); 49810e3b1c7SDag-Erling Smørgrav goto failure; 49910e3b1c7SDag-Erling Smørgrav } 500e9dc4f3bSDag-Erling Smørgrav if (nsb.st_dev != sb.st_dev || 501e9dc4f3bSDag-Erling Smørgrav nsb.st_ino != nsb.st_ino || 502e9dc4f3bSDag-Erling Smørgrav nsb.st_size != sb.st_size) { 5033d7c408cSDag-Erling Smørgrav warnx("%s: file has changed", URL); 504e9dc4f3bSDag-Erling Smørgrav fclose(of); 505e9dc4f3bSDag-Erling Smørgrav of = NULL; 506e9dc4f3bSDag-Erling Smørgrav sb = nsb; 50710e3b1c7SDag-Erling Smørgrav } 508b39628e7SDag-Erling Smørgrav } 509e9dc4f3bSDag-Erling Smørgrav } else if (m_flag && sb.st_size != -1) { 510e9a039c2SDag-Erling Smørgrav /* mirror mode, local file exists */ 511e9a039c2SDag-Erling Smørgrav if (sb.st_size == us.size && sb.st_mtime == us.mtime) 512e9a039c2SDag-Erling Smørgrav goto success; 513e9a039c2SDag-Erling Smørgrav } 514e9dc4f3bSDag-Erling Smørgrav 515e9dc4f3bSDag-Erling Smørgrav if (of == NULL) { 516e9a039c2SDag-Erling Smørgrav /* 51791404f38SDag-Erling Smørgrav * We don't yet have an output file; either this is a 51891404f38SDag-Erling Smørgrav * vanilla run with no special flags, or the local and 51991404f38SDag-Erling Smørgrav * remote files didn't match. 520e9a039c2SDag-Erling Smørgrav */ 521e9dc4f3bSDag-Erling Smørgrav 52213da7d99SDag-Erling Smørgrav if (url->offset > 0) { 523e9dc4f3bSDag-Erling Smørgrav /* 524e9dc4f3bSDag-Erling Smørgrav * We tried to restart a transfer, but for 525e9dc4f3bSDag-Erling Smørgrav * some reason gave up - so we have to restart 526e9dc4f3bSDag-Erling Smørgrav * from scratch if we want the whole file 527e9dc4f3bSDag-Erling Smørgrav */ 528e9dc4f3bSDag-Erling Smørgrav url->offset = 0; 529e9dc4f3bSDag-Erling Smørgrav if ((f = fetchXGet(url, &us, flags)) == NULL) { 5303d7c408cSDag-Erling Smørgrav warnx("%s: %s", URL, fetchLastErrString); 531e9dc4f3bSDag-Erling Smørgrav goto failure; 532e9dc4f3bSDag-Erling Smørgrav } 533e9dc4f3bSDag-Erling Smørgrav if (sigint) 534e9dc4f3bSDag-Erling Smørgrav goto signal; 535e9dc4f3bSDag-Erling Smørgrav } 536e9dc4f3bSDag-Erling Smørgrav 537e9dc4f3bSDag-Erling Smørgrav /* construct a temp file name */ 538e9dc4f3bSDag-Erling Smørgrav if (sb.st_size != -1 && S_ISREG(sb.st_mode)) { 539e9dc4f3bSDag-Erling Smørgrav if ((slash = strrchr(path, '/')) == NULL) 540e9dc4f3bSDag-Erling Smørgrav slash = path; 541e9dc4f3bSDag-Erling Smørgrav else 542e9dc4f3bSDag-Erling Smørgrav ++slash; 543e9dc4f3bSDag-Erling Smørgrav asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s", 5441c93d61fSMike Barcroft (int)(slash - path), path, slash); 545e9dc4f3bSDag-Erling Smørgrav if (tmppath != NULL) { 546e9dc4f3bSDag-Erling Smørgrav mkstemps(tmppath, strlen(slash) + 1); 547e9dc4f3bSDag-Erling Smørgrav of = fopen(tmppath, "w"); 548e9dc4f3bSDag-Erling Smørgrav } 54932411a1bSDag-Erling Smørgrav } 55032411a1bSDag-Erling Smørgrav if (of == NULL) 55132411a1bSDag-Erling Smørgrav of = fopen(path, "w"); 552e9dc4f3bSDag-Erling Smørgrav if (of == NULL) { 553b39628e7SDag-Erling Smørgrav warn("%s: open()", path); 554b39628e7SDag-Erling Smørgrav goto failure; 555b39628e7SDag-Erling Smørgrav } 556b39628e7SDag-Erling Smørgrav } 557b39628e7SDag-Erling Smørgrav count = url->offset; 558b39628e7SDag-Erling Smørgrav 559b39628e7SDag-Erling Smørgrav /* start the counter */ 560b39628e7SDag-Erling Smørgrav stat_start(&xs, path, us.size, count); 561b39628e7SDag-Erling Smørgrav 562cd400b67SDag-Erling Smørgrav sigalrm = siginfo = sigint = 0; 56349e62d8fSDag-Erling Smørgrav 56449e62d8fSDag-Erling Smørgrav /* suck in the data */ 565cd400b67SDag-Erling Smørgrav signal(SIGINFO, sig_handler); 566dc161d55SDag-Erling Smørgrav while (!sigint) { 56749e62d8fSDag-Erling Smørgrav if (us.size != -1 && us.size - count < B_size) 568b39628e7SDag-Erling Smørgrav size = us.size - count; 56949e62d8fSDag-Erling Smørgrav else 57049e62d8fSDag-Erling Smørgrav size = B_size; 5710d60c709SDag-Erling Smørgrav if (siginfo) { 5720d60c709SDag-Erling Smørgrav stat_end(&xs); 5730d60c709SDag-Erling Smørgrav siginfo = 0; 5740d60c709SDag-Erling Smørgrav } 575cd400b67SDag-Erling Smørgrav if ((size = fread(buf, 1, size, f)) == 0) { 576dc161d55SDag-Erling Smørgrav if (ferror(f) && errno == EINTR && !sigint) 577cd400b67SDag-Erling Smørgrav clearerr(f); 578cd400b67SDag-Erling Smørgrav else 579aa4b3574SDag-Erling Smørgrav break; 580b39628e7SDag-Erling Smørgrav } 58191404f38SDag-Erling Smørgrav stat_update(&xs, count += size); 582cd400b67SDag-Erling Smørgrav for (ptr = buf; size > 0; ptr += wr, size -= wr) 583cd400b67SDag-Erling Smørgrav if ((wr = fwrite(ptr, 1, size, of)) < size) { 584dc161d55SDag-Erling Smørgrav if (ferror(of) && errno == EINTR && !sigint) 585cd400b67SDag-Erling Smørgrav clearerr(of); 586cd400b67SDag-Erling Smørgrav else 587cd400b67SDag-Erling Smørgrav break; 588cd400b67SDag-Erling Smørgrav } 589cd400b67SDag-Erling Smørgrav if (size != 0) 590cd400b67SDag-Erling Smørgrav break; 591cd400b67SDag-Erling Smørgrav } 592dc161d55SDag-Erling Smørgrav if (!sigalrm) 593dc161d55SDag-Erling Smørgrav sigalrm = ferror(f) && errno == ETIMEDOUT; 594cd400b67SDag-Erling Smørgrav signal(SIGINFO, SIG_DFL); 595b39628e7SDag-Erling Smørgrav 596b39628e7SDag-Erling Smørgrav stat_end(&xs); 597b39628e7SDag-Erling Smørgrav 598dc161d55SDag-Erling Smørgrav /* 599dc161d55SDag-Erling Smørgrav * If the transfer timed out or was interrupted, we still want to 600dc161d55SDag-Erling Smørgrav * set the mtime in case the file is not removed (-r or -R) and 601dc161d55SDag-Erling Smørgrav * the user later restarts the transfer. 602dc161d55SDag-Erling Smørgrav */ 603dc161d55SDag-Erling Smørgrav signal: 60430204f98SDag-Erling Smørgrav /* set mtime of local file */ 605a6266f24SDag-Erling Smørgrav if (!n_flag && us.mtime && !o_stdout && of != NULL && 606a6266f24SDag-Erling Smørgrav (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) { 607b39628e7SDag-Erling Smørgrav struct timeval tv[2]; 608b39628e7SDag-Erling Smørgrav 60949e62d8fSDag-Erling Smørgrav fflush(of); 61049e62d8fSDag-Erling Smørgrav tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime); 611b39628e7SDag-Erling Smørgrav tv[1].tv_sec = (long)us.mtime; 612b39628e7SDag-Erling Smørgrav tv[0].tv_usec = tv[1].tv_usec = 0; 6138e7cf3deSDag-Erling Smørgrav if (utimes(tmppath ? tmppath : path, tv)) 6148e7cf3deSDag-Erling Smørgrav warn("%s: utimes()", tmppath ? tmppath : path); 615b39628e7SDag-Erling Smørgrav } 616b39628e7SDag-Erling Smørgrav 61749e62d8fSDag-Erling Smørgrav /* timed out or interrupted? */ 618ef50a72cSDag-Erling Smørgrav if (sigalrm) 619ef50a72cSDag-Erling Smørgrav warnx("transfer timed out"); 6209516ffa7SDag-Erling Smørgrav if (sigint) { 621ef50a72cSDag-Erling Smørgrav warnx("transfer interrupted"); 6229516ffa7SDag-Erling Smørgrav goto failure; 6239516ffa7SDag-Erling Smørgrav } 62449e62d8fSDag-Erling Smørgrav 62505f45e0cSBill Fenner /* timeout / interrupt before connection completley established? */ 62605f45e0cSBill Fenner if (f == NULL) 62705f45e0cSBill Fenner goto failure; 62805f45e0cSBill Fenner 6299516ffa7SDag-Erling Smørgrav if (!sigalrm) { 63049e62d8fSDag-Erling Smørgrav /* check the status of our files */ 63149e62d8fSDag-Erling Smørgrav if (ferror(f)) 63249e62d8fSDag-Erling Smørgrav warn("%s", URL); 63349e62d8fSDag-Erling Smørgrav if (ferror(of)) 63449e62d8fSDag-Erling Smørgrav warn("%s", path); 63549e62d8fSDag-Erling Smørgrav if (ferror(f) || ferror(of)) 63649e62d8fSDag-Erling Smørgrav goto failure; 63778394463SDag-Erling Smørgrav } 63849e62d8fSDag-Erling Smørgrav 63949e62d8fSDag-Erling Smørgrav /* did the transfer complete normally? */ 64049e62d8fSDag-Erling Smørgrav if (us.size != -1 && count < us.size) { 641bb11a878SDag-Erling Smørgrav warnx("%s appears to be truncated: %lld/%lld bytes", 64291404f38SDag-Erling Smørgrav path, (long long)count, (long long)us.size); 64349e62d8fSDag-Erling Smørgrav goto failure_keep; 644bb11a878SDag-Erling Smørgrav } 645bb11a878SDag-Erling Smørgrav 64630204f98SDag-Erling Smørgrav /* 64730204f98SDag-Erling Smørgrav * If the transfer timed out and we didn't know how much to 64830204f98SDag-Erling Smørgrav * expect, assume the worst (i.e. we didn't get all of it) 64930204f98SDag-Erling Smørgrav */ 65030204f98SDag-Erling Smørgrav if (sigalrm && us.size == -1) { 65130204f98SDag-Erling Smørgrav warnx("%s may be truncated", path); 65230204f98SDag-Erling Smørgrav goto failure_keep; 65330204f98SDag-Erling Smørgrav } 65430204f98SDag-Erling Smørgrav 655b39628e7SDag-Erling Smørgrav success: 65649e62d8fSDag-Erling Smørgrav r = 0; 657e9dc4f3bSDag-Erling Smørgrav if (tmppath != NULL && rename(tmppath, path) == -1) { 658e9dc4f3bSDag-Erling Smørgrav warn("%s: rename()", path); 659e9dc4f3bSDag-Erling Smørgrav goto failure_keep; 660e9dc4f3bSDag-Erling Smørgrav } 661b39628e7SDag-Erling Smørgrav goto done; 662b39628e7SDag-Erling Smørgrav failure: 66349e62d8fSDag-Erling Smørgrav if (of && of != stdout && !R_flag && !r_flag) 664e2b41a62SDag-Erling Smørgrav if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG)) 665e9dc4f3bSDag-Erling Smørgrav unlink(tmppath ? tmppath : path); 666e9dc4f3bSDag-Erling Smørgrav if (R_flag && tmppath != NULL && sb.st_size == -1) 667e9dc4f3bSDag-Erling Smørgrav rename(tmppath, path); /* ignore errors here */ 66849e62d8fSDag-Erling Smørgrav failure_keep: 669b39628e7SDag-Erling Smørgrav r = -1; 670b39628e7SDag-Erling Smørgrav goto done; 671b39628e7SDag-Erling Smørgrav done: 672b39628e7SDag-Erling Smørgrav if (f) 673b39628e7SDag-Erling Smørgrav fclose(f); 674b39628e7SDag-Erling Smørgrav if (of && of != stdout) 675b39628e7SDag-Erling Smørgrav fclose(of); 676ec850e74SDag-Erling Smørgrav if (url) 677b39628e7SDag-Erling Smørgrav fetchFreeURL(url); 678e9dc4f3bSDag-Erling Smørgrav if (tmppath != NULL) 679e9dc4f3bSDag-Erling Smørgrav free(tmppath); 680b39628e7SDag-Erling Smørgrav return r; 681b39628e7SDag-Erling Smørgrav } 682b39628e7SDag-Erling Smørgrav 68391404f38SDag-Erling Smørgrav static void 684b39628e7SDag-Erling Smørgrav usage(void) 685b39628e7SDag-Erling Smørgrav { 68691404f38SDag-Erling Smørgrav fprintf(stderr, "%s\n%s\n%s\n", 68713da7d99SDag-Erling Smørgrav "usage: fetch [-146AFMPRUadlmnpqrsv] [-N netrc] [-o outputfile]", 68813da7d99SDag-Erling Smørgrav " [-S bytes] [-B bytes] [-T seconds] [-w seconds]", 68991404f38SDag-Erling Smørgrav " [-h host -f file [-c dir] | URL ...]"); 690b39628e7SDag-Erling Smørgrav } 691b39628e7SDag-Erling Smørgrav 692b39628e7SDag-Erling Smørgrav 693db695db7SDag-Erling Smørgrav /* 694db695db7SDag-Erling Smørgrav * Entry point 695db695db7SDag-Erling Smørgrav */ 696b39628e7SDag-Erling Smørgrav int 697b39628e7SDag-Erling Smørgrav main(int argc, char *argv[]) 698b39628e7SDag-Erling Smørgrav { 699b39628e7SDag-Erling Smørgrav struct stat sb; 70078394463SDag-Erling Smørgrav struct sigaction sa; 70191404f38SDag-Erling Smørgrav const char *p, *s; 70232411a1bSDag-Erling Smørgrav char *end, *q; 703b39628e7SDag-Erling Smørgrav int c, e, r; 704b39628e7SDag-Erling Smørgrav 705b39628e7SDag-Erling Smørgrav while ((c = getopt(argc, argv, 70613da7d99SDag-Erling Smørgrav "146AaB:bc:dFf:Hh:lMmN:nPpo:qRrS:sT:tUvw:")) != -1) 707b39628e7SDag-Erling Smørgrav switch (c) { 708b39628e7SDag-Erling Smørgrav case '1': 709b39628e7SDag-Erling Smørgrav once_flag = 1; 710b39628e7SDag-Erling Smørgrav break; 711b39628e7SDag-Erling Smørgrav case '4': 712b39628e7SDag-Erling Smørgrav family = PF_INET; 713b39628e7SDag-Erling Smørgrav break; 714b39628e7SDag-Erling Smørgrav case '6': 715b39628e7SDag-Erling Smørgrav family = PF_INET6; 716b39628e7SDag-Erling Smørgrav break; 717b39628e7SDag-Erling Smørgrav case 'A': 718b39628e7SDag-Erling Smørgrav A_flag = 1; 719b39628e7SDag-Erling Smørgrav break; 720b39628e7SDag-Erling Smørgrav case 'a': 721b39628e7SDag-Erling Smørgrav a_flag = 1; 722b39628e7SDag-Erling Smørgrav break; 723b39628e7SDag-Erling Smørgrav case 'B': 72432411a1bSDag-Erling Smørgrav B_size = (off_t)strtol(optarg, &end, 10); 72532411a1bSDag-Erling Smørgrav if (*optarg == '\0' || *end != '\0') 7266d64e939SStefan Eßer errx(1, "invalid buffer size (%s)", optarg); 727b39628e7SDag-Erling Smørgrav break; 728b39628e7SDag-Erling Smørgrav case 'b': 729b39628e7SDag-Erling Smørgrav warnx("warning: the -b option is deprecated"); 730b39628e7SDag-Erling Smørgrav b_flag = 1; 731b39628e7SDag-Erling Smørgrav break; 732a8369cd9SDag-Erling Smørgrav case 'c': 733a8369cd9SDag-Erling Smørgrav c_dirname = optarg; 734a8369cd9SDag-Erling Smørgrav break; 735b39628e7SDag-Erling Smørgrav case 'd': 736b39628e7SDag-Erling Smørgrav d_flag = 1; 737b39628e7SDag-Erling Smørgrav break; 738b39628e7SDag-Erling Smørgrav case 'F': 739b39628e7SDag-Erling Smørgrav F_flag = 1; 740b39628e7SDag-Erling Smørgrav break; 741b39628e7SDag-Erling Smørgrav case 'f': 742b39628e7SDag-Erling Smørgrav f_filename = optarg; 743b39628e7SDag-Erling Smørgrav break; 744b39628e7SDag-Erling Smørgrav case 'H': 74515baa31aSPhilippe Charnier warnx("the -H option is now implicit, " 74691404f38SDag-Erling Smørgrav "use -U to disable"); 747b39628e7SDag-Erling Smørgrav break; 748b39628e7SDag-Erling Smørgrav case 'h': 749b39628e7SDag-Erling Smørgrav h_hostname = optarg; 750b39628e7SDag-Erling Smørgrav break; 751b39628e7SDag-Erling Smørgrav case 'l': 752b39628e7SDag-Erling Smørgrav l_flag = 1; 753b39628e7SDag-Erling Smørgrav break; 754b39628e7SDag-Erling Smørgrav case 'o': 755b39628e7SDag-Erling Smørgrav o_flag = 1; 756b39628e7SDag-Erling Smørgrav o_filename = optarg; 757b39628e7SDag-Erling Smørgrav break; 758b39628e7SDag-Erling Smørgrav case 'M': 759b39628e7SDag-Erling Smørgrav case 'm': 760e9a039c2SDag-Erling Smørgrav if (r_flag) 76191404f38SDag-Erling Smørgrav errx(1, "the -m and -r flags " 76291404f38SDag-Erling Smørgrav "are mutually exclusive"); 763b39628e7SDag-Erling Smørgrav m_flag = 1; 764b39628e7SDag-Erling Smørgrav break; 76513da7d99SDag-Erling Smørgrav case 'N': 76613da7d99SDag-Erling Smørgrav N_filename = optarg; 76713da7d99SDag-Erling Smørgrav break; 768b39628e7SDag-Erling Smørgrav case 'n': 769bb11a878SDag-Erling Smørgrav n_flag = 1; 770b39628e7SDag-Erling Smørgrav break; 771b39628e7SDag-Erling Smørgrav case 'P': 772b39628e7SDag-Erling Smørgrav case 'p': 773b39628e7SDag-Erling Smørgrav p_flag = 1; 774b39628e7SDag-Erling Smørgrav break; 775b39628e7SDag-Erling Smørgrav case 'q': 776b39628e7SDag-Erling Smørgrav v_level = 0; 777b39628e7SDag-Erling Smørgrav break; 778b39628e7SDag-Erling Smørgrav case 'R': 779b39628e7SDag-Erling Smørgrav R_flag = 1; 780b39628e7SDag-Erling Smørgrav break; 781b39628e7SDag-Erling Smørgrav case 'r': 782e9a039c2SDag-Erling Smørgrav if (m_flag) 78391404f38SDag-Erling Smørgrav errx(1, "the -m and -r flags " 78491404f38SDag-Erling Smørgrav "are mutually exclusive"); 785b39628e7SDag-Erling Smørgrav r_flag = 1; 786b39628e7SDag-Erling Smørgrav break; 787b39628e7SDag-Erling Smørgrav case 'S': 78832411a1bSDag-Erling Smørgrav S_size = (off_t)strtol(optarg, &end, 10); 78932411a1bSDag-Erling Smørgrav if (*optarg == '\0' || *end != '\0') 7906d64e939SStefan Eßer errx(1, "invalid size (%s)", optarg); 791b39628e7SDag-Erling Smørgrav break; 792b39628e7SDag-Erling Smørgrav case 's': 793b39628e7SDag-Erling Smørgrav s_flag = 1; 794b39628e7SDag-Erling Smørgrav break; 795b39628e7SDag-Erling Smørgrav case 'T': 79632411a1bSDag-Erling Smørgrav T_secs = strtol(optarg, &end, 10); 79732411a1bSDag-Erling Smørgrav if (*optarg == '\0' || *end != '\0') 7986d64e939SStefan Eßer errx(1, "invalid timeout (%s)", optarg); 799b39628e7SDag-Erling Smørgrav break; 800b39628e7SDag-Erling Smørgrav case 't': 801b39628e7SDag-Erling Smørgrav t_flag = 1; 802b39628e7SDag-Erling Smørgrav warnx("warning: the -t option is deprecated"); 803b39628e7SDag-Erling Smørgrav break; 8047c480c6cSDag-Erling Smørgrav case 'U': 8057c480c6cSDag-Erling Smørgrav U_flag = 1; 8067c480c6cSDag-Erling Smørgrav break; 807b39628e7SDag-Erling Smørgrav case 'v': 808b39628e7SDag-Erling Smørgrav v_level++; 809b39628e7SDag-Erling Smørgrav break; 810b39628e7SDag-Erling Smørgrav case 'w': 811b39628e7SDag-Erling Smørgrav a_flag = 1; 81232411a1bSDag-Erling Smørgrav w_secs = strtol(optarg, &end, 10); 81332411a1bSDag-Erling Smørgrav if (*optarg == '\0' || *end != '\0') 8146d64e939SStefan Eßer errx(1, "invalid delay (%s)", optarg); 815b39628e7SDag-Erling Smørgrav break; 816b39628e7SDag-Erling Smørgrav default: 817b39628e7SDag-Erling Smørgrav usage(); 818b39628e7SDag-Erling Smørgrav exit(EX_USAGE); 819b39628e7SDag-Erling Smørgrav } 820b39628e7SDag-Erling Smørgrav 821b39628e7SDag-Erling Smørgrav argc -= optind; 822b39628e7SDag-Erling Smørgrav argv += optind; 823b39628e7SDag-Erling Smørgrav 824a8369cd9SDag-Erling Smørgrav if (h_hostname || f_filename || c_dirname) { 825b39628e7SDag-Erling Smørgrav if (!h_hostname || !f_filename || argc) { 826b39628e7SDag-Erling Smørgrav usage(); 827b39628e7SDag-Erling Smørgrav exit(EX_USAGE); 828b39628e7SDag-Erling Smørgrav } 829b39628e7SDag-Erling Smørgrav /* XXX this is a hack. */ 830b39628e7SDag-Erling Smørgrav if (strcspn(h_hostname, "@:/") != strlen(h_hostname)) 831b39628e7SDag-Erling Smørgrav errx(1, "invalid hostname"); 832a8369cd9SDag-Erling Smørgrav if (asprintf(argv, "ftp://%s/%s/%s", h_hostname, 833a8369cd9SDag-Erling Smørgrav c_dirname ? c_dirname : "", f_filename) == -1) 834a6f339d2SKris Kennaway errx(1, "%s", strerror(ENOMEM)); 835b39628e7SDag-Erling Smørgrav argc++; 836b39628e7SDag-Erling Smørgrav } 837b39628e7SDag-Erling Smørgrav 838b39628e7SDag-Erling Smørgrav if (!argc) { 839b39628e7SDag-Erling Smørgrav usage(); 840b39628e7SDag-Erling Smørgrav exit(EX_USAGE); 841b39628e7SDag-Erling Smørgrav } 842b39628e7SDag-Erling Smørgrav 843b39628e7SDag-Erling Smørgrav /* allocate buffer */ 844b39628e7SDag-Erling Smørgrav if (B_size < MINBUFSIZE) 845b39628e7SDag-Erling Smørgrav B_size = MINBUFSIZE; 846b39628e7SDag-Erling Smørgrav if ((buf = malloc(B_size)) == NULL) 847a6f339d2SKris Kennaway errx(1, "%s", strerror(ENOMEM)); 848b39628e7SDag-Erling Smørgrav 84978394463SDag-Erling Smørgrav /* timeouts */ 850b39628e7SDag-Erling Smørgrav if ((s = getenv("FTP_TIMEOUT")) != NULL) { 85132411a1bSDag-Erling Smørgrav ftp_timeout = strtol(s, &end, 10); 852dacff752SDag-Erling Smørgrav if (*s == '\0' || *end != '\0' || ftp_timeout < 0) { 853dacff752SDag-Erling Smørgrav warnx("FTP_TIMEOUT (%s) is not a positive integer", s); 854b39628e7SDag-Erling Smørgrav ftp_timeout = 0; 855b39628e7SDag-Erling Smørgrav } 856b39628e7SDag-Erling Smørgrav } 857b39628e7SDag-Erling Smørgrav if ((s = getenv("HTTP_TIMEOUT")) != NULL) { 85832411a1bSDag-Erling Smørgrav http_timeout = strtol(s, &end, 10); 859dacff752SDag-Erling Smørgrav if (*s == '\0' || *end != '\0' || http_timeout < 0) { 860dacff752SDag-Erling Smørgrav warnx("HTTP_TIMEOUT (%s) is not a positive integer", s); 861b39628e7SDag-Erling Smørgrav http_timeout = 0; 862b39628e7SDag-Erling Smørgrav } 863b39628e7SDag-Erling Smørgrav } 864b39628e7SDag-Erling Smørgrav 86578394463SDag-Erling Smørgrav /* signal handling */ 86678394463SDag-Erling Smørgrav sa.sa_flags = 0; 86778394463SDag-Erling Smørgrav sa.sa_handler = sig_handler; 86878394463SDag-Erling Smørgrav sigemptyset(&sa.sa_mask); 869e9a039c2SDag-Erling Smørgrav sigaction(SIGALRM, &sa, NULL); 870e9a039c2SDag-Erling Smørgrav sa.sa_flags = SA_RESETHAND; 871e9a039c2SDag-Erling Smørgrav sigaction(SIGINT, &sa, NULL); 872e9a039c2SDag-Erling Smørgrav fetchRestartCalls = 0; 873ef50a72cSDag-Erling Smørgrav 874b39628e7SDag-Erling Smørgrav /* output file */ 875b39628e7SDag-Erling Smørgrav if (o_flag) { 876b39628e7SDag-Erling Smørgrav if (strcmp(o_filename, "-") == 0) { 877b39628e7SDag-Erling Smørgrav o_stdout = 1; 878b39628e7SDag-Erling Smørgrav } else if (stat(o_filename, &sb) == -1) { 879b39628e7SDag-Erling Smørgrav if (errno == ENOENT) { 880b39628e7SDag-Erling Smørgrav if (argc > 1) 88191404f38SDag-Erling Smørgrav errx(EX_USAGE, "%s is not a directory", 88291404f38SDag-Erling Smørgrav o_filename); 883b39628e7SDag-Erling Smørgrav } else { 884b39628e7SDag-Erling Smørgrav err(EX_IOERR, "%s", o_filename); 885b39628e7SDag-Erling Smørgrav } 886b39628e7SDag-Erling Smørgrav } else { 887b39628e7SDag-Erling Smørgrav if (sb.st_mode & S_IFDIR) 888b39628e7SDag-Erling Smørgrav o_directory = 1; 889b39628e7SDag-Erling Smørgrav } 890b39628e7SDag-Erling Smørgrav } 891b39628e7SDag-Erling Smørgrav 892b39628e7SDag-Erling Smørgrav /* check if output is to a tty (for progress report) */ 893bb11a878SDag-Erling Smørgrav v_tty = isatty(STDERR_FILENO); 894dbcc1983SDag-Erling Smørgrav if (v_tty) 895dbcc1983SDag-Erling Smørgrav pgrp = getpgrp(); 896dbcc1983SDag-Erling Smørgrav 897b39628e7SDag-Erling Smørgrav r = 0; 898b39628e7SDag-Erling Smørgrav 8990d60c709SDag-Erling Smørgrav /* authentication */ 9005ef824edSDag-Erling Smørgrav if (v_tty) 9010d60c709SDag-Erling Smørgrav fetchAuthMethod = query_auth; 90213da7d99SDag-Erling Smørgrav if (N_filename != NULL) 90313da7d99SDag-Erling Smørgrav setenv("NETRC", N_filename, 1); 9040d60c709SDag-Erling Smørgrav 905b39628e7SDag-Erling Smørgrav while (argc) { 906b39628e7SDag-Erling Smørgrav if ((p = strrchr(*argv, '/')) == NULL) 907b39628e7SDag-Erling Smørgrav p = *argv; 908b39628e7SDag-Erling Smørgrav else 909b39628e7SDag-Erling Smørgrav p++; 910b39628e7SDag-Erling Smørgrav 911b39628e7SDag-Erling Smørgrav if (!*p) 912b39628e7SDag-Erling Smørgrav p = "fetch.out"; 913b39628e7SDag-Erling Smørgrav 914b39628e7SDag-Erling Smørgrav fetchLastErrCode = 0; 915b39628e7SDag-Erling Smørgrav 916b39628e7SDag-Erling Smørgrav if (o_flag) { 917b39628e7SDag-Erling Smørgrav if (o_stdout) { 918b39628e7SDag-Erling Smørgrav e = fetch(*argv, "-"); 919b39628e7SDag-Erling Smørgrav } else if (o_directory) { 920b39628e7SDag-Erling Smørgrav asprintf(&q, "%s/%s", o_filename, p); 921b39628e7SDag-Erling Smørgrav e = fetch(*argv, q); 922b39628e7SDag-Erling Smørgrav free(q); 923b39628e7SDag-Erling Smørgrav } else { 924b39628e7SDag-Erling Smørgrav e = fetch(*argv, o_filename); 925b39628e7SDag-Erling Smørgrav } 926b39628e7SDag-Erling Smørgrav } else { 927b39628e7SDag-Erling Smørgrav e = fetch(*argv, p); 928b39628e7SDag-Erling Smørgrav } 929b39628e7SDag-Erling Smørgrav 930ef50a72cSDag-Erling Smørgrav if (sigint) 931e9a039c2SDag-Erling Smørgrav kill(getpid(), SIGINT); 932ef50a72cSDag-Erling Smørgrav 933b39628e7SDag-Erling Smørgrav if (e == 0 && once_flag) 934b39628e7SDag-Erling Smørgrav exit(0); 935b39628e7SDag-Erling Smørgrav 936b39628e7SDag-Erling Smørgrav if (e) { 937b39628e7SDag-Erling Smørgrav r = 1; 938b39628e7SDag-Erling Smørgrav if ((fetchLastErrCode 939b39628e7SDag-Erling Smørgrav && fetchLastErrCode != FETCH_UNAVAIL 940b39628e7SDag-Erling Smørgrav && fetchLastErrCode != FETCH_MOVED 941b39628e7SDag-Erling Smørgrav && fetchLastErrCode != FETCH_URL 942b39628e7SDag-Erling Smørgrav && fetchLastErrCode != FETCH_RESOLV 943b39628e7SDag-Erling Smørgrav && fetchLastErrCode != FETCH_UNKNOWN)) { 94491404f38SDag-Erling Smørgrav if (w_secs && v_level) 94532411a1bSDag-Erling Smørgrav fprintf(stderr, "Waiting %ld seconds " 94691404f38SDag-Erling Smørgrav "before retrying\n", w_secs); 94791404f38SDag-Erling Smørgrav if (w_secs) 948b39628e7SDag-Erling Smørgrav sleep(w_secs); 949b39628e7SDag-Erling Smørgrav if (a_flag) 950b39628e7SDag-Erling Smørgrav continue; 951b39628e7SDag-Erling Smørgrav } 952b39628e7SDag-Erling Smørgrav } 953b39628e7SDag-Erling Smørgrav 954b39628e7SDag-Erling Smørgrav argc--, argv++; 955b39628e7SDag-Erling Smørgrav } 956b39628e7SDag-Erling Smørgrav 957b39628e7SDag-Erling Smørgrav exit(r); 958b39628e7SDag-Erling Smørgrav } 959