vm: fix potential null deref
[minix.git] / commands / fetch / fetch.c
blobe3c559fa2163f01bb9a674fb564f5887d380ebe2
1 /*-
2 * Copyright (c) 2000-2004 Dag-Erling Coïdan Smørgrav
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #if HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 #if !defined(NETBSD) && !defined(__minix)
33 #include <nbcompat.h>
34 #endif
36 #if HAVE_SYS_PARAM_H
37 #include <sys/param.h>
38 #endif
39 #if HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42 #if HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45 #if HAVE_SYS_STAT_H
46 #include <sys/stat.h>
47 #endif
48 #if HAVE_SYS_TIME_H
49 #include <sys/time.h>
50 #endif
51 #if HAVE_UTIME_H
52 #include <utime.h>
53 #endif
54 #include <ctype.h>
55 #if HAVE_ERR_H
56 #include <err.h>
57 #endif
58 #include <errno.h>
59 #include <signal.h>
60 #if HAVE_STDINT_H
61 #include <stdint.h>
62 #endif
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #if HAVE_TERMIOS_H
67 #include <termios.h>
68 #endif
69 #include <unistd.h>
71 #include <fetch.h>
73 #if HAVE_SYSEXITS_H
74 #include <sysexits.h>
75 #endif
77 #ifndef EX_USAGE
78 #define EX_USAGE 64
79 #endif
81 #ifndef EX_IOERR
82 #define EX_IOERR 74
83 #endif
85 #define MINBUFSIZE 4096
87 /* Option flags */
88 int A_flag; /* -A: do not follow 302 redirects */
89 int a_flag; /* -a: auto retry */
90 off_t B_size; /* -B: buffer size */
91 int d_flag; /* -d: direct connection */
92 int F_flag; /* -F: restart without checking mtime */
93 int i_flag; /* -i: fetch file if modified */
94 int l_flag; /* -l: link rather than copy file: URLs */
95 int m_flag; /* -[Mm]: mirror mode */
96 char *N_filename; /* -N: netrc file name */
97 int n_flag; /* -n: do not preserve modification time */
98 int o_flag; /* -o: specify output file */
99 int o_directory; /* output file is a directory */
100 char *o_filename; /* name of output file */
101 int o_stdout; /* output file is stdout */
102 int once_flag; /* -1: stop at first successful file */
103 int R_flag; /* -R: don't delete partially transferred files */
104 int r_flag; /* -r: restart previously interrupted transfer */
105 off_t S_size; /* -S: require size to match */
106 int s_flag; /* -s: show size, don't fetch */
107 long T_secs = 120; /* -T: transfer timeout in seconds */
108 int U_flag; /* -U: do not use high ports */
109 int v_level = 1; /* -v: verbosity level */
110 int v_tty; /* stdout is a tty */
111 pid_t pgrp; /* our process group */
112 long w_secs; /* -w: retry delay */
113 int family = PF_UNSPEC; /* -[46]: address family to use */
115 volatile int sigalrm; /* SIGALRM received */
116 #ifdef SIGINFO
117 volatile int siginfo; /* SIGINFO received */
118 #endif
119 volatile int sigint; /* SIGINT received */
121 long ftp_timeout; /* default timeout for FTP transfers */
122 long http_timeout; /* default timeout for HTTP transfers */
123 char *buf; /* transfer buffer */
127 * Signal handler
129 static void
130 sig_handler(int sig)
132 switch (sig) {
133 case SIGALRM:
134 fetchRestartCalls = 0;
135 sigalrm = 1;
136 break;
137 #ifdef SIGINFO
138 case SIGINFO:
139 siginfo = 1;
140 break;
141 #endif
142 case SIGINT:
143 fetchRestartCalls = 0;
144 sigint = 1;
145 break;
149 struct xferstat {
150 char name[64];
151 struct timeval start;
152 struct timeval last;
153 off_t size;
154 off_t offset;
155 off_t rcvd;
159 * Compute and display ETA
161 static const char *
162 stat_eta(struct xferstat *xs)
164 static char str[16];
165 long elapsed, eta;
166 off_t received, expected;
168 elapsed = xs->last.tv_sec - xs->start.tv_sec;
169 received = xs->rcvd - xs->offset;
170 expected = xs->size - xs->rcvd;
171 eta = (long)((double)elapsed * expected / received);
172 if (eta > 3600)
173 snprintf(str, sizeof str, "%02ldh%02ldm",
174 eta / 3600, (eta % 3600) / 60);
175 else
176 snprintf(str, sizeof str, "%02ldm%02lds",
177 eta / 60, eta % 60);
178 return (str);
182 * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
184 static const char *prefixes = " kMGTP";
185 static const char *
186 stat_bytes(off_t bytes)
188 static char str[16];
189 const char *prefix = prefixes;
191 while (bytes > 9999 && prefix[1] != '\0') {
192 bytes /= 1024;
193 prefix++;
195 snprintf(str, sizeof str, "%4jd %cB", (intmax_t)bytes, *prefix);
196 return (str);
200 * Compute and display transfer rate
202 static const char *
203 stat_bps(struct xferstat *xs)
205 static char str[16];
206 double delta, bps;
208 delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
209 - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6));
210 if (delta == 0.0) {
211 snprintf(str, sizeof str, "?? Bps");
212 } else {
213 bps = (xs->rcvd - xs->offset) / delta;
214 snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps));
216 return (str);
220 * Update the stats display
222 static void
223 stat_display(struct xferstat *xs, int force)
225 struct timeval now;
226 int ctty_pgrp;
228 /* Minix returns "Not a typewriter error" */
229 #if defined(TIOCGPGRP) && !defined(__minix)
230 /* check if we're the foreground process */
231 if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 ||
232 (pid_t)ctty_pgrp != pgrp)
233 return;
234 #endif
236 gettimeofday(&now, NULL);
237 if (!force && now.tv_sec <= xs->last.tv_sec)
238 return;
239 xs->last = now;
241 fprintf(stderr, "\r%-46.46s", xs->name);
242 if (xs->size <= 0) {
243 #if HAVE_SETPROCTITLE
244 setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd));
245 #endif
246 fprintf(stderr, " %s", stat_bytes(xs->rcvd));
247 } else {
248 #if HAVE_SETPROCTITLE
249 setproctitle("%s [%d%% of %s]", xs->name,
250 (int)((100.0 * xs->rcvd) / xs->size),
251 stat_bytes(xs->size));
252 #endif
253 fprintf(stderr, "%3d%% of %s",
254 (int)((100.0 * xs->rcvd) / xs->size),
255 stat_bytes(xs->size));
257 fprintf(stderr, " %s", stat_bps(xs));
258 if (xs->size > 0 && xs->rcvd > 0 &&
259 xs->last.tv_sec >= xs->start.tv_sec + 10)
260 fprintf(stderr, " %s", stat_eta(xs));
261 fflush(stderr);
265 * Initialize the transfer statistics
267 static void
268 stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
270 snprintf(xs->name, sizeof xs->name, "%s", name);
271 gettimeofday(&xs->start, NULL);
272 xs->last.tv_sec = xs->last.tv_usec = 0;
273 xs->size = size;
274 xs->offset = offset;
275 xs->rcvd = offset;
276 if (v_tty && v_level > 0)
277 stat_display(xs, 1);
278 else if (v_level > 0)
279 fprintf(stderr, "%-46s", xs->name);
283 * Update the transfer statistics
285 static void
286 stat_update(struct xferstat *xs, off_t rcvd)
288 xs->rcvd = rcvd;
289 if (v_tty && v_level > 0)
290 stat_display(xs, 0);
294 * Finalize the transfer statistics
296 static void
297 stat_end(struct xferstat *xs)
299 gettimeofday(&xs->last, NULL);
300 if (v_tty && v_level > 0) {
301 stat_display(xs, 1);
302 putc('\n', stderr);
303 } else if (v_level > 0) {
304 fprintf(stderr, " %s %s\n",
305 stat_bytes(xs->size), stat_bps(xs));
309 #if HAVE_TERMIOS_H && !defined(PREFER_GETPASS)
310 static int
311 read_password(const char *prompt, char *pwbuf, size_t pwbuf_len)
313 struct termios tios;
314 tcflag_t saved_flags;
315 int nopwd;
317 fprintf(stderr, prompt);
318 if (tcgetattr(STDIN_FILENO, &tios) != 0)
319 return (fgets(pwbuf, pwbuf_len, stdin) == NULL);
321 saved_flags = tios.c_lflag;
322 tios.c_lflag &= ~ECHO;
323 tios.c_lflag |= ECHONL|ICANON;
324 #ifndef __minix
325 tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
326 #else
327 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios);
328 #endif
329 nopwd = (fgets(pwbuf, pwbuf_len, stdin) == NULL);
330 tios.c_lflag = saved_flags;
331 #ifndef __minix
332 tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
333 #else
334 tcsetattr(STDIN_FILENO, TCSANOW, &tios);
335 #endif
337 return nopwd;
339 #elif HAVE_GETPASSPHRASE || HAVE_GETPASS
340 static int
341 read_password(const char *prompt, char *pwbuf, size_t pwbuf_len)
343 char *pass;
345 #if HAVE_GETPASSPHRASE && !defined(PREFER_GETPASS)
346 pass = getpassphrase(prompt);
347 #else
348 pass = getpass(prompt);
349 #endif
350 if (pass == NULL || strlen(pass) >= pwbuf_len)
351 return 1;
352 strcpy(pwbuf, pass);
353 return 0;
355 #else
356 static int
357 read_password(const char *prompt, char *pwbuf, size_t pwbuf_len)
360 fprintf(stderr, prompt);
361 return (fgets(pwbuf, pwbuf_len, stdin) == NULL);
363 #endif
366 * Ask the user for authentication details
368 static int
369 query_auth(struct url *URL)
371 int i, nopwd;
373 fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
374 URL->scheme, URL->host, URL->port);
376 fprintf(stderr, "Login: ");
377 if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
378 return (-1);
379 for (i = strlen(URL->user); i >= 0; --i)
380 if (URL->user[i] == '\r' || URL->user[i] == '\n')
381 URL->user[i] = '\0';
383 nopwd = read_password("Password: ", URL->pwd, sizeof(URL->pwd));
385 if (nopwd)
386 return (-1);
387 for (i = strlen(URL->pwd); i >= 0; --i)
388 if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
389 URL->pwd[i] = '\0';
391 return (0);
395 * Fetch a file
397 static int
398 fetch(char *URL, const char *path)
400 struct url *url;
401 struct url_stat us;
402 struct stat sb, nsb;
403 struct xferstat xs;
404 FILE *of;
405 fetchIO *f;
406 size_t size, wr;
407 ssize_t ssize;
408 off_t count;
409 char flags[8];
410 char *tmppath;
411 int r;
412 unsigned timeout;
413 char *ptr;
415 f = NULL;
416 of = NULL;
417 tmppath = NULL;
419 timeout = 0;
420 *flags = 0;
421 count = 0;
423 /* set verbosity level */
424 if (v_level > 1)
425 strcat(flags, "v");
426 if (v_level > 2)
427 fetchDebug = 1;
429 /* parse URL */
430 if ((url = fetchParseURL(URL)) == NULL) {
431 warnx("%s: parse error", URL);
432 goto failure;
435 /* if no scheme was specified, take a guess */
436 if (!*url->scheme) {
437 if (!*url->host)
438 strcpy(url->scheme, SCHEME_FILE);
439 else if (strncasecmp(url->host, "ftp.", 4) == 0)
440 strcpy(url->scheme, SCHEME_FTP);
441 else if (strncasecmp(url->host, "www.", 4) == 0)
442 strcpy(url->scheme, SCHEME_HTTP);
445 /* common flags */
446 switch (family) {
447 case PF_INET:
448 strcat(flags, "4");
449 break;
450 #ifndef __minix
451 case PF_INET6:
452 strcat(flags, "6");
453 break;
454 #endif
457 /* Protocol independent flags */
458 if (i_flag) {
459 if (stat(path, &sb) == 0) {
460 url->last_modified = sb.st_mtime;
461 strcat(flags, "i");
462 } else if (errno != ENOENT) {
463 warn("%s: stat()", path);
464 goto failure;
468 /* FTP specific flags */
469 if (strcmp(url->scheme, SCHEME_FTP) == 0) {
470 if (d_flag)
471 strcat(flags, "d");
472 if (U_flag)
473 strcat(flags, "l");
474 timeout = T_secs ? T_secs : ftp_timeout;
477 /* HTTP specific flags */
478 if (strcmp(url->scheme, SCHEME_HTTP) == 0) {
479 if (d_flag)
480 strcat(flags, "d");
481 if (A_flag)
482 strcat(flags, "A");
483 timeout = T_secs ? T_secs : http_timeout;
486 /* set the protocol timeout. */
487 fetchTimeout = timeout;
489 /* just print size */
490 if (s_flag) {
491 if (timeout)
492 alarm(timeout);
493 r = fetchStat(url, &us, flags);
494 if (timeout)
495 alarm(0);
496 if (sigalrm || sigint)
497 goto signal;
498 if (r == -1) {
499 warnx("%s", fetchLastErrString);
500 goto failure;
502 if (us.size == -1)
503 printf("Unknown\n");
504 else
505 printf("%jd\n", (intmax_t)us.size);
506 goto success;
510 * If the -r flag was specified, we have to compare the local
511 * and remote files, so we should really do a fetchStat()
512 * first, but I know of at least one HTTP server that only
513 * sends the content size in response to GET requests, and
514 * leaves it out of replies to HEAD requests. Also, in the
515 * (frequent) case that the local and remote files match but
516 * the local file is truncated, we have sufficient information
517 * before the compare to issue a correct request. Therefore,
518 * we always issue a GET request as if we were sure the local
519 * file was a truncated copy of the remote file; we can drop
520 * the connection later if we change our minds.
522 sb.st_size = -1;
523 if (!o_stdout) {
524 r = stat(path, &sb);
525 if (r == 0 && r_flag && S_ISREG(sb.st_mode)) {
526 url->offset = sb.st_size;
527 } else if (r == -1 || !S_ISREG(sb.st_mode)) {
529 * Whatever value sb.st_size has now is either
530 * wrong (if stat(2) failed) or irrelevant (if the
531 * path does not refer to a regular file)
533 sb.st_size = -1;
535 if (r == -1 && errno != ENOENT) {
536 warnx("%s: stat()", path);
537 goto failure;
541 /* start the transfer */
542 if (timeout)
543 alarm(timeout);
544 f = fetchXGet(url, &us, flags);
545 if (timeout)
546 alarm(0);
547 if (sigalrm || sigint)
548 goto signal;
549 if (f == NULL && i_flag && fetchLastErrCode == FETCH_UNCHANGED) {
550 /* URL was not modified, return OK. */
551 printf("%s: not modified\n", URL);
552 r = 0;
553 goto done;
555 if (f == NULL) {
556 warnx("%s: %s", URL, fetchLastErrString);
557 goto failure;
559 if (sigint)
560 goto signal;
562 /* check that size is as expected */
563 if (S_size) {
564 if (us.size == -1) {
565 warnx("%s: size unknown", URL);
566 } else if (us.size != S_size) {
567 warnx("%s: size mismatch: expected %jd, actual %jd",
568 URL, (intmax_t)S_size, (intmax_t)us.size);
569 goto failure;
573 /* symlink instead of copy */
574 if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
575 char *name = fetchUnquotePath(url);
576 if (name == NULL) {
577 warnx("Can't unquote URL");
578 goto failure;
580 if (symlink(name, path) == -1) {
581 warn("%s: symlink()", path);
582 free(name);
583 goto failure;
585 free(name);
586 goto success;
589 if (us.size == -1 && !o_stdout && v_level > 0)
590 warnx("%s: size of remote file is not known", URL);
591 if (v_level > 1) {
592 if (sb.st_size != -1)
593 fprintf(stderr, "local size / mtime: %jd / %ld\n",
594 (intmax_t)sb.st_size, (long)sb.st_mtime);
595 if (us.size != -1)
596 fprintf(stderr, "remote size / mtime: %jd / %ld\n",
597 (intmax_t)us.size, (long)us.mtime);
600 /* open output file */
601 if (o_stdout) {
602 /* output to stdout */
603 of = stdout;
604 } else if (r_flag && sb.st_size != -1) {
605 /* resume mode, local file exists */
606 if (!F_flag && us.mtime && sb.st_mtime != us.mtime) {
607 /* no match! have to refetch */
608 fetchIO_close(f);
609 /* if precious, warn the user and give up */
610 if (R_flag) {
611 warnx("%s: local modification time "
612 "does not match remote", path);
613 goto failure_keep;
615 } else if (us.size != -1) {
616 if (us.size == sb.st_size)
617 /* nothing to do */
618 goto success;
619 if (sb.st_size > us.size) {
620 /* local file too long! */
621 warnx("%s: local file (%jd bytes) is longer "
622 "than remote file (%jd bytes)", path,
623 (intmax_t)sb.st_size, (intmax_t)us.size);
624 goto failure;
626 /* we got it, open local file */
627 if ((of = fopen(path, "a")) == NULL) {
628 warn("%s: fopen()", path);
629 goto failure;
631 /* check that it didn't move under our feet */
632 if (fstat(fileno(of), &nsb) == -1) {
633 /* can't happen! */
634 warn("%s: fstat()", path);
635 goto failure;
637 if (nsb.st_dev != sb.st_dev ||
638 nsb.st_ino != nsb.st_ino ||
639 nsb.st_size != sb.st_size) {
640 warnx("%s: file has changed", URL);
641 fclose(of);
642 of = NULL;
643 sb = nsb;
646 } else if (m_flag && sb.st_size != -1) {
647 /* mirror mode, local file exists */
648 if (sb.st_size == us.size && sb.st_mtime == us.mtime)
649 goto success;
652 if (of == NULL) {
654 * We don't yet have an output file; either this is a
655 * vanilla run with no special flags, or the local and
656 * remote files didn't match.
659 if (url->offset > 0) {
661 * We tried to restart a transfer, but for
662 * some reason gave up - so we have to restart
663 * from scratch if we want the whole file
665 url->offset = 0;
666 if ((f = fetchXGet(url, &us, flags)) == NULL) {
667 warnx("%s: %s", URL, fetchLastErrString);
668 goto failure;
670 if (sigint)
671 goto signal;
674 /* construct a temp file name */
675 if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
676 #ifndef __minix
677 asprintf(&tmppath, "%s.fetch.XXXXXX", path);
678 #else
680 int len;
681 if((tmppath = malloc(sizeof(char)*MINBUFSIZE)) != NULL) {
682 len = snprintf(tmppath, MINBUFSIZE, "%s.fetch.XXXXXX", path);
683 if(len >= MINBUFSIZE) {
684 free(tmppath);
685 tmppath = NULL;
689 #endif
691 if (tmppath != NULL) {
692 int fd;
694 fd = mkstemp(tmppath);
695 if (fd == -1) {
696 warn("%s: mkstemp failed", tmppath);
697 goto failure;
699 fchown(fd, sb.st_uid, sb.st_gid);
700 fchmod(fd, sb.st_mode & ALLPERMS);
701 of = fdopen(fd, "w");
702 if (of == NULL) {
703 close(fd);
704 unlink(tmppath);
705 free(tmppath);
706 tmppath = NULL;
710 if (of == NULL)
711 of = fopen(path, "w");
712 if (of == NULL) {
713 warn("%s: open()", path);
714 goto failure;
717 count = url->offset;
719 /* start the counter */
720 stat_start(&xs, path, us.size, count);
722 sigalrm = sigint = 0;
724 /* suck in the data */
725 #ifdef SIGINFO
726 siginfo = 0;
727 signal(SIGINFO, sig_handler);
728 #endif
729 while (!sigint) {
730 if (us.size != -1 && us.size - count < B_size &&
731 us.size - count >= 0)
732 size = us.size - count;
733 else
734 size = B_size;
735 #ifdef SIGINFO
736 if (siginfo) {
737 stat_display(&xs, 1);
738 siginfo = 0;
740 #else
741 /* Constant info is better than none. */
742 if (v_level) {
743 stat_display(&xs, 1);
745 #endif
746 if ((ssize = fetchIO_read(f, buf, B_size)) == 0)
747 break;
748 if (ssize == -1 && errno == EINTR)
749 continue;
750 if (ssize == -1)
751 break;
752 size = ssize;
753 stat_update(&xs, count += size);
754 for (ptr = buf; size > 0; ptr += wr, size -= wr) {
755 if ((wr = fwrite(ptr, 1, size, of)) < size) {
756 if (ferror(of) && errno == EINTR && !sigint)
757 clearerr(of);
758 else
759 break;
762 if (size != 0)
763 break;
765 if (!sigalrm)
766 sigalrm = 0;
767 #ifdef SIGINFO
768 signal(SIGINFO, SIG_DFL);
769 #endif
771 stat_end(&xs);
774 * If the transfer timed out or was interrupted, we still want to
775 * set the mtime in case the file is not removed (-r or -R) and
776 * the user later restarts the transfer.
778 signal:
779 /* set mtime of local file */
780 #ifndef __minix
781 if (!n_flag && us.mtime && !o_stdout && of != NULL &&
782 (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
783 struct timeval tv[2];
785 fflush(of);
786 tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
787 tv[1].tv_sec = (long)us.mtime;
788 tv[0].tv_usec = tv[1].tv_usec = 0;
789 if (utimes(tmppath ? tmppath : path, tv))
790 warn("%s: utimes()", tmppath ? tmppath : path);
792 #else
793 if (!n_flag && us.mtime && !o_stdout && of != NULL &&
794 (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
795 struct utimbuf ut;
797 fflush(of);
798 ut.actime = (us.atime ? us.atime : us.mtime);
799 ut.modtime = us.mtime;
801 if (utime(tmppath ? tmppath : path, &ut))
802 warn("%s: utime()", tmppath ? tmppath : path);
804 #endif
806 /* timed out or interrupted? */
807 if (fetchLastErrCode == FETCH_TIMEOUT)
808 sigalrm = 1;
809 if (sigalrm)
810 warnx("transfer timed out");
811 if (sigint) {
812 warnx("transfer interrupted");
813 goto failure;
816 /* timeout / interrupt before connection completley established? */
817 if (f == NULL)
818 goto failure;
820 if (!sigalrm && ferror(of)) {
821 /* check the status of our files */
822 warn("writing to %s failed", path);
823 goto failure;
826 /* did the transfer complete normally? */
827 if (us.size != -1 && count < us.size) {
828 warnx("%s appears to be truncated: %jd/%jd bytes",
829 path, (intmax_t)count, (intmax_t)us.size);
830 goto failure_keep;
834 * If the transfer timed out and we didn't know how much to
835 * expect, assume the worst (i.e. we didn't get all of it)
837 if (sigalrm && us.size == -1) {
838 warnx("%s may be truncated", path);
839 goto failure_keep;
842 success:
843 r = 0;
844 if (tmppath != NULL && rename(tmppath, path) == -1) {
845 warn("%s: rename()", path);
846 goto failure_keep;
848 goto done;
849 failure:
850 if (of && of != stdout && !R_flag && !r_flag)
851 if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
852 unlink(tmppath ? tmppath : path);
853 if (R_flag && tmppath != NULL && sb.st_size == -1)
854 rename(tmppath, path); /* ignore errors here */
855 failure_keep:
856 r = -1;
857 goto done;
858 done:
859 if (f)
860 fetchIO_close(f);
861 if (of && of != stdout)
862 fclose(of);
863 if (url)
864 fetchFreeURL(url);
865 if (tmppath != NULL)
866 free(tmppath);
867 return (r);
870 static void
871 usage(void)
873 #ifndef __minix
874 fprintf(stderr, "%s\n%s\n%s\n",
875 "usage: fetch [-146AFMPRUadilmnpqrsv] [-N netrc] [-o outputfile]",
876 " [-S bytes] [-B bytes] [-T seconds] [-w seconds]",
877 " [-h host -f file [-c dir] | URL ...]");
878 #else
879 fprintf(stderr, "%s\n%s\n%s\n",
880 "usage: fetch [-146AFMPRUadilmnpqrsv] [-N netrc] [-o outputfile]",
881 " [-S bytes] [-B bytes] [-T seconds] [-w seconds]",
882 " [-h host -f file [-c dir] | URL ...]");
883 #endif
888 * Entry point
891 main(int argc, char *argv[])
893 struct stat sb;
894 struct sigaction sa;
895 const char *p, *s;
896 char *end, *q;
897 int c, e, r;
898 #ifndef __minix
899 while ((c = getopt(argc, argv,
900 "14AaB:dFilMmN:no:qRrS:sT:Uvw:")) != -1)
901 #else
902 while ((c = getopt(argc, argv,
903 "146AaB:dFilMmN:no:qRrS:sT:Uvw:")) != -1)
904 #endif
905 switch (c) {
906 case '1':
907 once_flag = 1;
908 break;
909 case '4':
910 family = PF_INET;
911 break;
912 #ifndef __minix
913 case '6':
914 family = PF_INET6;
915 break;
916 #endif
917 case 'A':
918 A_flag = 1;
919 break;
920 case 'a':
921 a_flag = 1;
922 break;
923 case 'B':
924 B_size = (off_t)strtol(optarg, &end, 10);
925 if (*optarg == '\0' || *end != '\0')
926 errx(1, "invalid buffer size (%s)", optarg);
927 break;
928 case 'd':
929 d_flag = 1;
930 break;
931 case 'F':
932 F_flag = 1;
933 break;
934 case 'i':
935 i_flag = 1;
936 break;
937 case 'l':
938 l_flag = 1;
939 break;
940 case 'o':
941 o_flag = 1;
942 o_filename = optarg;
943 break;
944 case 'M':
945 case 'm':
946 if (r_flag)
947 errx(1, "the -m and -r flags "
948 "are mutually exclusive");
949 m_flag = 1;
950 break;
951 case 'N':
952 N_filename = optarg;
953 break;
954 case 'n':
955 n_flag = 1;
956 break;
957 case 'q':
958 v_level = 0;
959 break;
960 case 'R':
961 R_flag = 1;
962 break;
963 case 'r':
964 if (m_flag)
965 errx(1, "the -m and -r flags "
966 "are mutually exclusive");
967 r_flag = 1;
968 break;
969 case 'S':
970 S_size = (off_t)strtol(optarg, &end, 10);
971 if (*optarg == '\0' || *end != '\0')
972 errx(1, "invalid size (%s)", optarg);
973 break;
974 case 's':
975 s_flag = 1;
976 break;
977 case 'T':
978 T_secs = strtol(optarg, &end, 10);
979 if (*optarg == '\0' || *end != '\0')
980 errx(1, "invalid timeout (%s)", optarg);
981 break;
982 case 'U':
983 U_flag = 1;
984 break;
985 case 'v':
986 v_level++;
987 break;
988 case 'w':
989 a_flag = 1;
990 w_secs = strtol(optarg, &end, 10);
991 if (*optarg == '\0' || *end != '\0')
992 errx(1, "invalid delay (%s)", optarg);
993 break;
994 default:
995 usage();
996 exit(EX_USAGE);
999 argc -= optind;
1000 argv += optind;
1002 if (!argc) {
1003 usage();
1004 exit(EX_USAGE);
1007 fetchConnectionCacheInit(10, 1);
1009 /* allocate buffer */
1010 if (B_size < MINBUFSIZE)
1011 B_size = MINBUFSIZE;
1012 if ((buf = malloc(B_size)) == NULL)
1013 errx(1, "%s", strerror(ENOMEM));
1015 /* timeouts */
1016 if ((s = getenv("FTP_TIMEOUT")) != NULL) {
1017 ftp_timeout = strtol(s, &end, 10);
1018 if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
1019 warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
1020 ftp_timeout = 0;
1023 if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
1024 http_timeout = strtol(s, &end, 10);
1025 if (*s == '\0' || *end != '\0' || http_timeout < 0) {
1026 warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
1027 http_timeout = 0;
1031 /* signal handling */
1032 sa.sa_flags = 0;
1033 sa.sa_handler = sig_handler;
1034 sigemptyset(&sa.sa_mask);
1035 sigaction(SIGALRM, &sa, NULL);
1036 sa.sa_flags = SA_RESETHAND;
1037 sigaction(SIGINT, &sa, NULL);
1039 /* output file */
1040 if (o_flag) {
1041 if (strcmp(o_filename, "-") == 0) {
1042 o_stdout = 1;
1043 if (i_flag) {
1044 warnx("-i and -o - are incompatible, dropping -i");
1045 i_flag = 0;
1047 } else if (stat(o_filename, &sb) == -1) {
1048 if (errno == ENOENT) {
1049 if (argc > 1)
1050 errx(EX_USAGE, "%s is not a directory",
1051 o_filename);
1052 } else {
1053 err(EX_IOERR, "%s", o_filename);
1055 } else {
1056 if (sb.st_mode & S_IFDIR)
1057 o_directory = 1;
1061 /* check if output is to a tty (for progress report) */
1062 v_tty = isatty(STDERR_FILENO);
1063 if (v_tty)
1064 pgrp = getpgrp();
1066 r = 0;
1068 /* authentication */
1069 if (v_tty)
1070 fetchAuthMethod = query_auth;
1071 if (N_filename != NULL)
1072 setenv("NETRC", N_filename, 1);
1074 while (argc) {
1075 if ((p = strrchr(*argv, '/')) == NULL)
1076 p = *argv;
1077 else
1078 p++;
1080 if (!*p)
1081 p = "fetch.out";
1083 fetchLastErrCode = 0;
1085 if (o_flag) {
1086 if (o_stdout) {
1087 e = fetch(*argv, "-");
1088 } else if (o_directory) {
1089 #ifndef __minix
1090 asprintf(&q, "%s/%s", o_filename, p);
1091 #else
1093 int len;
1095 if ((q = malloc(sizeof(char)*MINBUFSIZE)) != NULL) {
1096 len = snprintf(q, MINBUFSIZE, "%s/%s", o_filename, p);
1097 if (len >= MINBUFSIZE) {
1098 free(q);
1099 q = NULL;
1101 }else{
1102 err(1, "Unable to allocate memory");
1105 #endif
1106 e = fetch(*argv, q);
1107 free(q);
1108 } else {
1109 e = fetch(*argv, o_filename);
1111 } else {
1112 e = fetch(*argv, p);
1115 if (sigint)
1116 kill(getpid(), SIGINT);
1118 if (e == 0 && once_flag)
1119 exit(0);
1121 if (e) {
1122 r = 1;
1123 if ((fetchLastErrCode
1124 && fetchLastErrCode != FETCH_UNAVAIL
1125 && fetchLastErrCode != FETCH_MOVED
1126 && fetchLastErrCode != FETCH_URL
1127 && fetchLastErrCode != FETCH_RESOLV
1128 && fetchLastErrCode != FETCH_UNKNOWN)) {
1129 if (w_secs && v_level)
1130 fprintf(stderr, "Waiting %ld seconds "
1131 "before retrying\n", w_secs);
1132 if (w_secs)
1133 sleep(w_secs);
1134 if (a_flag)
1135 continue;
1139 argc--, argv++;
1142 exit(r);