fetch.c revision 1.42 1 1.42 lukem /* $NetBSD: fetch.c,v 1.42 1998/12/29 14:59:04 lukem Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.25 lukem * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1 lukem * by Jason Thorpe and Luke Mewburn.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted provided that the following conditions
12 1.1 lukem * are met:
13 1.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1 lukem * must display the following acknowledgement:
20 1.1 lukem * This product includes software developed by the NetBSD
21 1.1 lukem * Foundation, Inc. and its contributors.
22 1.1 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 lukem * contributors may be used to endorse or promote products derived
24 1.1 lukem * from this software without specific prior written permission.
25 1.1 lukem *
26 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.14 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.14 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
37 1.1 lukem */
38 1.1 lukem
39 1.12 lukem #include <sys/cdefs.h>
40 1.1 lukem #ifndef lint
41 1.42 lukem __RCSID("$NetBSD: fetch.c,v 1.42 1998/12/29 14:59:04 lukem Exp $");
42 1.1 lukem #endif /* not lint */
43 1.1 lukem
44 1.1 lukem /*
45 1.1 lukem * FTP User Program -- Command line file retrieval
46 1.1 lukem */
47 1.1 lukem
48 1.1 lukem #include <sys/types.h>
49 1.1 lukem #include <sys/param.h>
50 1.1 lukem #include <sys/socket.h>
51 1.30 lukem #include <sys/stat.h>
52 1.32 lukem #include <sys/time.h>
53 1.1 lukem
54 1.1 lukem #include <netinet/in.h>
55 1.1 lukem
56 1.2 lukem #include <arpa/ftp.h>
57 1.1 lukem #include <arpa/inet.h>
58 1.1 lukem
59 1.1 lukem #include <ctype.h>
60 1.1 lukem #include <err.h>
61 1.22 lukem #include <errno.h>
62 1.1 lukem #include <netdb.h>
63 1.1 lukem #include <fcntl.h>
64 1.3 lukem #include <signal.h>
65 1.1 lukem #include <stdio.h>
66 1.1 lukem #include <stdlib.h>
67 1.1 lukem #include <string.h>
68 1.1 lukem #include <unistd.h>
69 1.24 lukem #include <util.h>
70 1.1 lukem
71 1.1 lukem #include "ftp_var.h"
72 1.1 lukem
73 1.27 lukem typedef enum {
74 1.27 lukem UNKNOWN_URL_T=-1,
75 1.27 lukem HTTP_URL_T,
76 1.27 lukem FTP_URL_T,
77 1.27 lukem FILE_URL_T
78 1.27 lukem } url_t;
79 1.27 lukem
80 1.42 lukem static int go_fetch __P((const char *, const char *));
81 1.42 lukem static int fetch_ftp __P((const char *, const char *));
82 1.42 lukem static int fetch_url __P((const char *, const char *, const char *));
83 1.27 lukem static int parse_url __P((const char *, const char *, url_t *, char **,
84 1.27 lukem char **, char **, in_port_t *, char **));
85 1.12 lukem void aborthttp __P((int));
86 1.12 lukem
87 1.42 lukem static int redirect_loop;
88 1.42 lukem
89 1.12 lukem
90 1.25 lukem #define ABOUT_URL "about:" /* propaganda */
91 1.25 lukem #define FILE_URL "file://" /* file URL prefix */
92 1.1 lukem #define FTP_URL "ftp://" /* ftp URL prefix */
93 1.1 lukem #define HTTP_URL "http://" /* http URL prefix */
94 1.1 lukem
95 1.1 lukem
96 1.1 lukem #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
97 1.27 lukem #define FREEPTR(x) if ((x) != NULL) { free(x); (x) = NULL; }
98 1.27 lukem
99 1.27 lukem /*
100 1.27 lukem * Parse URL of form:
101 1.27 lukem * <type>://[<user>[:<password>@]]<host>[:<port>]/<url-path>
102 1.27 lukem * Returns -1 if a parse error occurred, otherwise 0.
103 1.27 lukem * Sets type to url_t, each of the given char ** pointers to a
104 1.27 lukem * malloc(3)ed strings of the relevant section, and port to
105 1.41 lukem * the number given, or ftpport if ftp://, or httpport if http://.
106 1.27 lukem */
107 1.27 lukem static int
108 1.27 lukem parse_url(url, desc, type, user, pass, host, port, path)
109 1.27 lukem const char *url;
110 1.27 lukem const char *desc;
111 1.27 lukem url_t *type;
112 1.27 lukem char **user;
113 1.27 lukem char **pass;
114 1.27 lukem char **host;
115 1.27 lukem in_port_t *port;
116 1.27 lukem char **path;
117 1.27 lukem {
118 1.27 lukem char *cp, *ep, *thost;
119 1.27 lukem
120 1.27 lukem if (url == NULL || desc == NULL || type == NULL || user == NULL
121 1.27 lukem || pass == NULL || host == NULL || port == NULL || path == NULL)
122 1.27 lukem errx(1, "parse_url: invoked with NULL argument!");
123 1.27 lukem
124 1.27 lukem *type = UNKNOWN_URL_T;
125 1.27 lukem *user = *pass = *host = *path = NULL;
126 1.27 lukem *port = 0;
127 1.27 lukem
128 1.27 lukem if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
129 1.27 lukem url += sizeof(HTTP_URL) - 1;
130 1.27 lukem *type = HTTP_URL_T;
131 1.41 lukem *port = httpport;
132 1.27 lukem } else if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
133 1.27 lukem url += sizeof(FTP_URL) - 1;
134 1.27 lukem *type = FTP_URL_T;
135 1.41 lukem *port = ftpport;
136 1.27 lukem } else if (strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
137 1.27 lukem url += sizeof(FILE_URL) - 1;
138 1.27 lukem *type = FILE_URL_T;
139 1.27 lukem } else {
140 1.27 lukem warnx("Invalid %s `%s'", desc, url);
141 1.27 lukem cleanup_parse_url:
142 1.27 lukem FREEPTR(*user);
143 1.27 lukem FREEPTR(*pass);
144 1.27 lukem FREEPTR(*host);
145 1.27 lukem FREEPTR(*path);
146 1.27 lukem return (-1);
147 1.27 lukem }
148 1.27 lukem
149 1.27 lukem if (*url == '\0')
150 1.27 lukem return (0);
151 1.27 lukem
152 1.27 lukem /* find [user[:pass]@]host[:port] */
153 1.27 lukem ep = strchr(url, '/');
154 1.27 lukem if (ep == NULL)
155 1.27 lukem thost = xstrdup(url);
156 1.27 lukem else {
157 1.27 lukem size_t len = ep - url;
158 1.27 lukem thost = (char *)xmalloc(len + 1);
159 1.27 lukem strncpy(thost, url, len);
160 1.27 lukem thost[len] = '\0';
161 1.27 lukem *path = xstrdup(ep);
162 1.27 lukem }
163 1.27 lukem
164 1.27 lukem cp = strchr(thost, '@');
165 1.27 lukem if (cp != NULL) {
166 1.27 lukem *user = thost;
167 1.27 lukem *cp = '\0';
168 1.27 lukem *host = xstrdup(cp + 1);
169 1.27 lukem cp = strchr(*user, ':');
170 1.27 lukem if (cp != NULL) {
171 1.27 lukem *cp = '\0';
172 1.27 lukem *pass = xstrdup(cp + 1);
173 1.27 lukem }
174 1.27 lukem } else
175 1.27 lukem *host = thost;
176 1.27 lukem
177 1.27 lukem /* look for [:port] */
178 1.27 lukem cp = strrchr(*host, ':');
179 1.27 lukem if (cp != NULL) {
180 1.27 lukem long nport;
181 1.27 lukem
182 1.27 lukem *cp = '\0';
183 1.27 lukem nport = strtol(cp + 1, &ep, 10);
184 1.27 lukem if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
185 1.42 lukem warnx("Invalid port `%s' in %s `%s'", cp, desc, url);
186 1.27 lukem goto cleanup_parse_url;
187 1.27 lukem }
188 1.27 lukem *port = htons((in_port_t)nport);
189 1.27 lukem }
190 1.27 lukem
191 1.27 lukem if (debug)
192 1.27 lukem fprintf(ttyout,
193 1.27 lukem "parse_url: user `%s', pass `%s', host %s:%d, path `%s'\n",
194 1.27 lukem *user ? *user : "", *pass ? *pass : "", *host ? *host : "",
195 1.27 lukem ntohs(*port), *path ? *path : "");
196 1.27 lukem
197 1.27 lukem return (0);
198 1.27 lukem }
199 1.27 lukem
200 1.1 lukem
201 1.2 lukem jmp_buf httpabort;
202 1.2 lukem
203 1.1 lukem /*
204 1.42 lukem * Retrieve URL, via a proxy if necessary. If proxyenv is set, use that for
205 1.42 lukem * the proxy, otherwise try ftp_proxy or http_proxy as appropriate.
206 1.42 lukem * Supports http redirects.
207 1.41 lukem * Returns -1 on failure, 0 on completed xfer, 1 if ftp connection
208 1.41 lukem * is still open (e.g, ftp xfer with trailing /)
209 1.1 lukem */
210 1.12 lukem static int
211 1.42 lukem fetch_url(url, outfile, proxyenv)
212 1.27 lukem const char *url;
213 1.22 lukem const char *outfile;
214 1.42 lukem const char *proxyenv;
215 1.1 lukem {
216 1.42 lukem struct sockaddr_in sin;
217 1.42 lukem struct hostent *hp;
218 1.42 lukem volatile sig_t oldintr, oldintp;
219 1.42 lukem volatile int s;
220 1.42 lukem int isproxy, rval, hcode;
221 1.42 lukem size_t len;
222 1.42 lukem char *cp, *ep, *buf, *location, *message, *savefile;
223 1.42 lukem char *user, *pass, *host, *path;
224 1.42 lukem off_t hashbytes;
225 1.42 lukem int (*closefunc) __P((FILE *));
226 1.42 lukem FILE *fin, *fout;
227 1.42 lukem time_t mtime;
228 1.42 lukem url_t urltype;
229 1.42 lukem in_port_t port;
230 1.1 lukem
231 1.22 lukem closefunc = NULL;
232 1.24 lukem fin = fout = NULL;
233 1.1 lukem s = -1;
234 1.42 lukem buf = location = message = savefile = NULL;
235 1.42 lukem isproxy = 0;
236 1.42 lukem rval = 1;
237 1.42 lukem hp = NULL;
238 1.1 lukem
239 1.27 lukem #ifdef __GNUC__ /* shut up gcc warnings */
240 1.22 lukem (void)&closefunc;
241 1.24 lukem (void)&fin;
242 1.22 lukem (void)&fout;
243 1.24 lukem (void)&buf;
244 1.12 lukem (void)&savefile;
245 1.42 lukem (void)&rval;
246 1.28 fvdl (void)&isproxy;
247 1.12 lukem #endif
248 1.12 lukem
249 1.27 lukem if (parse_url(url, "URL", &urltype, &user, &pass, &host, &port, &path)
250 1.27 lukem == -1)
251 1.42 lukem goto cleanup_fetch_url;
252 1.5 lukem
253 1.27 lukem if (urltype == FILE_URL_T && ! EMPTYSTRING(host)
254 1.27 lukem && strcasecmp(host, "localhost") != 0) {
255 1.27 lukem warnx("No support for non local file URL `%s'", url);
256 1.42 lukem goto cleanup_fetch_url;
257 1.9 lukem }
258 1.27 lukem
259 1.9 lukem if (EMPTYSTRING(path)) {
260 1.42 lukem if (urltype == FTP_URL_T) {
261 1.42 lukem rval = fetch_ftp(url, outfile);
262 1.42 lukem goto cleanup_fetch_url;
263 1.42 lukem }
264 1.27 lukem if (urltype != HTTP_URL_T || outfile == NULL) {
265 1.27 lukem warnx("Invalid URL (no file after host) `%s'", url);
266 1.42 lukem goto cleanup_fetch_url;
267 1.27 lukem }
268 1.9 lukem }
269 1.1 lukem
270 1.22 lukem if (outfile)
271 1.29 lukem savefile = xstrdup(outfile);
272 1.22 lukem else {
273 1.29 lukem cp = strrchr(path, '/'); /* find savefile */
274 1.29 lukem if (cp != NULL)
275 1.29 lukem savefile = xstrdup(cp + 1);
276 1.22 lukem else
277 1.29 lukem savefile = xstrdup(path);
278 1.22 lukem }
279 1.9 lukem if (EMPTYSTRING(savefile)) {
280 1.42 lukem if (urltype == FTP_URL_T) {
281 1.42 lukem rval = fetch_ftp(url, outfile);
282 1.42 lukem goto cleanup_fetch_url;
283 1.42 lukem }
284 1.27 lukem warnx("Invalid URL (no file after directory) `%s'", url);
285 1.42 lukem goto cleanup_fetch_url;
286 1.9 lukem }
287 1.1 lukem
288 1.25 lukem filesize = -1;
289 1.25 lukem mtime = -1;
290 1.25 lukem if (urltype == FILE_URL_T) { /* file:// URLs */
291 1.25 lukem struct stat sb;
292 1.25 lukem
293 1.25 lukem direction = "copied";
294 1.25 lukem fin = fopen(path, "r");
295 1.25 lukem if (fin == NULL) {
296 1.27 lukem warn("Cannot open file `%s'", path);
297 1.42 lukem goto cleanup_fetch_url;
298 1.5 lukem }
299 1.25 lukem if (fstat(fileno(fin), &sb) == 0) {
300 1.25 lukem mtime = sb.st_mtime;
301 1.25 lukem filesize = sb.st_size;
302 1.25 lukem }
303 1.25 lukem fprintf(ttyout, "Copying %s\n", path);
304 1.25 lukem } else { /* ftp:// or http:// URLs */
305 1.42 lukem if (proxyenv == NULL) {
306 1.42 lukem if (urltype == HTTP_URL_T)
307 1.42 lukem proxyenv = httpproxy;
308 1.42 lukem else if (urltype == FTP_URL_T)
309 1.42 lukem proxyenv = ftpproxy;
310 1.42 lukem }
311 1.25 lukem direction = "retrieved";
312 1.25 lukem if (proxyenv != NULL) { /* use proxy */
313 1.27 lukem url_t purltype;
314 1.27 lukem char *puser, *ppass, *phost;
315 1.27 lukem char *ppath;
316 1.27 lukem
317 1.27 lukem isproxy = 1;
318 1.27 lukem
319 1.27 lukem /* check URL against list of no_proxied sites */
320 1.27 lukem if (no_proxy != NULL) {
321 1.27 lukem char *np, *np_copy;
322 1.27 lukem long np_port;
323 1.27 lukem size_t hlen, plen;
324 1.27 lukem
325 1.27 lukem np_copy = xstrdup(no_proxy);
326 1.27 lukem hlen = strlen(host);
327 1.27 lukem while ((cp = strsep(&np_copy, " ,")) != NULL) {
328 1.27 lukem if (*cp == '\0')
329 1.27 lukem continue;
330 1.27 lukem if ((np = strchr(cp, ':')) != NULL) {
331 1.27 lukem *np = '\0';
332 1.27 lukem np_port =
333 1.27 lukem strtol(np + 1, &ep, 10);
334 1.27 lukem if (*ep != '\0')
335 1.27 lukem continue;
336 1.27 lukem if (port !=
337 1.27 lukem htons((in_port_t)np_port))
338 1.27 lukem continue;
339 1.27 lukem }
340 1.27 lukem plen = strlen(cp);
341 1.27 lukem if (strncasecmp(host + hlen - plen,
342 1.27 lukem cp, plen) == 0) {
343 1.27 lukem isproxy = 0;
344 1.27 lukem break;
345 1.27 lukem }
346 1.27 lukem }
347 1.27 lukem FREEPTR(np_copy);
348 1.25 lukem }
349 1.1 lukem
350 1.27 lukem if (isproxy) {
351 1.27 lukem if (parse_url(proxyenv, "proxy URL", &purltype,
352 1.41 lukem &puser, &ppass, &phost, &port, &ppath)
353 1.27 lukem == -1)
354 1.42 lukem goto cleanup_fetch_url;
355 1.27 lukem
356 1.27 lukem if ((purltype != HTTP_URL_T
357 1.27 lukem && purltype != FTP_URL_T) ||
358 1.27 lukem EMPTYSTRING(phost) ||
359 1.27 lukem (! EMPTYSTRING(ppath)
360 1.27 lukem && strcmp(ppath, "/") != 0)) {
361 1.27 lukem warnx("Malformed proxy URL `%s'",
362 1.27 lukem proxyenv);
363 1.27 lukem FREEPTR(puser);
364 1.27 lukem FREEPTR(ppass);
365 1.27 lukem FREEPTR(phost);
366 1.27 lukem FREEPTR(ppath);
367 1.42 lukem goto cleanup_fetch_url;
368 1.27 lukem }
369 1.12 lukem
370 1.27 lukem FREEPTR(user);
371 1.27 lukem user = puser;
372 1.27 lukem FREEPTR(pass);
373 1.27 lukem pass = ppass;
374 1.27 lukem FREEPTR(host);
375 1.27 lukem host = phost;
376 1.27 lukem FREEPTR(path);
377 1.27 lukem FREEPTR(ppath);
378 1.27 lukem path = xstrdup(url);
379 1.27 lukem }
380 1.27 lukem } /* proxyenv != NULL */
381 1.25 lukem
382 1.25 lukem memset(&sin, 0, sizeof(sin));
383 1.25 lukem sin.sin_family = AF_INET;
384 1.25 lukem
385 1.25 lukem if (isdigit((unsigned char)host[0])) {
386 1.25 lukem if (inet_aton(host, &sin.sin_addr) == 0) {
387 1.27 lukem warnx("Invalid IP address `%s'", host);
388 1.42 lukem goto cleanup_fetch_url;
389 1.25 lukem }
390 1.25 lukem } else {
391 1.25 lukem hp = gethostbyname(host);
392 1.25 lukem if (hp == NULL) {
393 1.25 lukem warnx("%s: %s", host, hstrerror(h_errno));
394 1.42 lukem goto cleanup_fetch_url;
395 1.25 lukem }
396 1.25 lukem if (hp->h_addrtype != AF_INET) {
397 1.27 lukem warnx("`%s': not an Internet address?", host);
398 1.42 lukem goto cleanup_fetch_url;
399 1.25 lukem }
400 1.25 lukem memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
401 1.1 lukem }
402 1.1 lukem
403 1.41 lukem if (port == 0) {
404 1.41 lukem warnx("Unknown port for URL `%s'", url);
405 1.42 lukem goto cleanup_fetch_url;
406 1.41 lukem }
407 1.25 lukem sin.sin_port = port;
408 1.25 lukem
409 1.25 lukem s = socket(AF_INET, SOCK_STREAM, 0);
410 1.25 lukem if (s == -1) {
411 1.25 lukem warn("Can't create socket");
412 1.42 lukem goto cleanup_fetch_url;
413 1.22 lukem }
414 1.1 lukem
415 1.25 lukem while (xconnect(s, (struct sockaddr *)&sin,
416 1.25 lukem sizeof(sin)) == -1) {
417 1.25 lukem if (errno == EINTR)
418 1.25 lukem continue;
419 1.25 lukem if (hp && hp->h_addr_list[1]) {
420 1.25 lukem int oerrno = errno;
421 1.25 lukem char *ia;
422 1.25 lukem
423 1.25 lukem ia = inet_ntoa(sin.sin_addr);
424 1.25 lukem errno = oerrno;
425 1.27 lukem warn("Connect to address `%s'", ia);
426 1.25 lukem hp->h_addr_list++;
427 1.25 lukem memcpy(&sin.sin_addr, hp->h_addr_list[0],
428 1.25 lukem (size_t)hp->h_length);
429 1.25 lukem fprintf(ttyout, "Trying %s...\n",
430 1.25 lukem inet_ntoa(sin.sin_addr));
431 1.25 lukem (void)close(s);
432 1.25 lukem s = socket(AF_INET, SOCK_STREAM, 0);
433 1.25 lukem if (s < 0) {
434 1.25 lukem warn("Can't create socket");
435 1.42 lukem goto cleanup_fetch_url;
436 1.25 lukem }
437 1.25 lukem continue;
438 1.25 lukem }
439 1.27 lukem warn("Can't connect to `%s'", host);
440 1.42 lukem goto cleanup_fetch_url;
441 1.25 lukem }
442 1.24 lukem
443 1.25 lukem fin = fdopen(s, "r+");
444 1.25 lukem /*
445 1.27 lukem * Construct and send the request.
446 1.27 lukem * Proxy requests don't want leading /.
447 1.25 lukem */
448 1.27 lukem if (isproxy) {
449 1.27 lukem fprintf(ttyout, "Requesting %s\n (via %s)\n",
450 1.27 lukem url, proxyenv);
451 1.27 lukem fprintf(fin, "GET %s HTTP/1.0\r\n\r\n", path);
452 1.27 lukem } else {
453 1.27 lukem fprintf(ttyout, "Requesting %s\n", url);
454 1.27 lukem fprintf(fin, "GET %s HTTP/1.1\r\n", path);
455 1.25 lukem fprintf(fin, "Host: %s\r\n", host);
456 1.40 explorer fprintf(fin, "Accept: */*\r\n");
457 1.42 lukem fprintf(fin, "User-Agent: NetBSD-ftp/1.4\r\n");
458 1.42 lukem fprintf(fin, "Connection: close\r\n");
459 1.42 lukem fprintf(fin, "\r\n");
460 1.25 lukem }
461 1.25 lukem if (fflush(fin) == EOF) {
462 1.25 lukem warn("Writing HTTP request");
463 1.42 lukem goto cleanup_fetch_url;
464 1.25 lukem }
465 1.1 lukem
466 1.25 lukem /* Read the response */
467 1.24 lukem if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
468 1.24 lukem warn("Receiving HTTP reply");
469 1.42 lukem goto cleanup_fetch_url;
470 1.24 lukem }
471 1.24 lukem while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
472 1.24 lukem buf[--len] = '\0';
473 1.24 lukem if (debug)
474 1.27 lukem fprintf(ttyout, "received `%s'\n", buf);
475 1.1 lukem
476 1.42 lukem /* Determine HTTP response code */
477 1.25 lukem cp = strchr(buf, ' ');
478 1.25 lukem if (cp == NULL)
479 1.25 lukem goto improper;
480 1.25 lukem else
481 1.25 lukem cp++;
482 1.42 lukem hcode = strtol(cp, &ep, 10);
483 1.42 lukem if (*ep != '\0' && !isspace((unsigned char)*ep))
484 1.42 lukem goto improper;
485 1.42 lukem message = xstrdup(cp);
486 1.25 lukem
487 1.25 lukem /* Read the rest of the header. */
488 1.27 lukem FREEPTR(buf);
489 1.25 lukem while (1) {
490 1.25 lukem if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0))
491 1.25 lukem == NULL) {
492 1.25 lukem warn("Receiving HTTP reply");
493 1.42 lukem goto cleanup_fetch_url;
494 1.25 lukem }
495 1.25 lukem while (len > 0 &&
496 1.25 lukem (buf[len-1] == '\r' || buf[len-1] == '\n'))
497 1.25 lukem buf[--len] = '\0';
498 1.25 lukem if (len == 0)
499 1.25 lukem break;
500 1.25 lukem if (debug)
501 1.27 lukem fprintf(ttyout, "received `%s'\n", buf);
502 1.25 lukem
503 1.25 lukem /* Look for some headers */
504 1.25 lukem cp = buf;
505 1.42 lukem
506 1.1 lukem #define CONTENTLEN "Content-Length: "
507 1.25 lukem if (strncasecmp(cp, CONTENTLEN,
508 1.25 lukem sizeof(CONTENTLEN) - 1) == 0) {
509 1.25 lukem cp += sizeof(CONTENTLEN) - 1;
510 1.25 lukem filesize = strtol(cp, &ep, 10);
511 1.25 lukem if (filesize < 1 || *ep != '\0')
512 1.25 lukem goto improper;
513 1.25 lukem if (debug)
514 1.25 lukem fprintf(ttyout,
515 1.25 lukem #ifndef NO_QUAD
516 1.25 lukem "parsed length as: %qd\n",
517 1.25 lukem (long long)filesize);
518 1.25 lukem #else
519 1.25 lukem "parsed length as: %ld\n",
520 1.25 lukem (long)filesize);
521 1.25 lukem #endif
522 1.42 lukem
523 1.25 lukem #define LASTMOD "Last-Modified: "
524 1.25 lukem } else if (strncasecmp(cp, LASTMOD,
525 1.25 lukem sizeof(LASTMOD) - 1) == 0) {
526 1.25 lukem struct tm parsed;
527 1.25 lukem char *t;
528 1.25 lukem
529 1.25 lukem cp += sizeof(LASTMOD) - 1;
530 1.25 lukem /* RFC 1123 */
531 1.25 lukem if ((t = strptime(cp,
532 1.25 lukem "%a, %d %b %Y %H:%M:%S GMT",
533 1.25 lukem &parsed))
534 1.25 lukem /* RFC 850 */
535 1.25 lukem || (t = strptime(cp,
536 1.25 lukem "%a, %d-%b-%y %H:%M:%S GMT",
537 1.25 lukem &parsed))
538 1.25 lukem /* asctime */
539 1.25 lukem || (t = strptime(cp,
540 1.25 lukem "%a, %b %d %H:%M:%S %Y",
541 1.25 lukem &parsed))) {
542 1.38 lukem parsed.tm_isdst = -1;
543 1.25 lukem if (*t == '\0')
544 1.39 itohy mtime = mkgmtime(&parsed);
545 1.38 lukem if (debug && mtime != -1) {
546 1.25 lukem fprintf(ttyout,
547 1.25 lukem "parsed date as: %s",
548 1.25 lukem ctime(&mtime));
549 1.38 lukem }
550 1.25 lukem }
551 1.42 lukem
552 1.24 lukem #define LOCATION "Location: "
553 1.42 lukem } else if (strncasecmp(cp, LOCATION,
554 1.42 lukem sizeof(LOCATION) - 1) == 0) {
555 1.25 lukem cp += sizeof(LOCATION) - 1;
556 1.42 lukem location = xstrdup(cp);
557 1.25 lukem if (debug)
558 1.25 lukem fprintf(ttyout,
559 1.25 lukem "parsed location as: %s\n", cp);
560 1.25 lukem }
561 1.24 lukem }
562 1.27 lukem FREEPTR(buf);
563 1.1 lukem }
564 1.1 lukem
565 1.42 lukem switch (hcode) {
566 1.42 lukem case 200:
567 1.42 lukem break;
568 1.42 lukem case 300:
569 1.42 lukem case 301:
570 1.42 lukem case 302:
571 1.42 lukem case 303:
572 1.42 lukem case 305:
573 1.42 lukem if (EMPTYSTRING(location)) {
574 1.42 lukem warnx("No redirection Location provided by server");
575 1.42 lukem goto cleanup_fetch_url;
576 1.42 lukem }
577 1.42 lukem if (redirect_loop++ > 5) {
578 1.42 lukem warnx("Too many redirections requested");
579 1.42 lukem goto cleanup_fetch_url;
580 1.42 lukem }
581 1.42 lukem if (hcode == 305) {
582 1.42 lukem if (verbose)
583 1.42 lukem fprintf(ttyout, "Redirected via %s\n",
584 1.42 lukem location);
585 1.42 lukem rval = fetch_url(url, outfile, location);
586 1.42 lukem } else {
587 1.42 lukem if (verbose)
588 1.42 lukem fprintf(ttyout, "Redirected to %s\n", location);
589 1.42 lukem rval = go_fetch(location, outfile);
590 1.42 lukem }
591 1.42 lukem goto cleanup_fetch_url;
592 1.42 lukem default:
593 1.42 lukem warnx("Error retrieving file - `%s'", message);
594 1.42 lukem goto cleanup_fetch_url;
595 1.42 lukem }
596 1.42 lukem
597 1.22 lukem oldintr = oldintp = NULL;
598 1.22 lukem
599 1.22 lukem /* Open the output file. */
600 1.22 lukem if (strcmp(savefile, "-") == 0) {
601 1.22 lukem fout = stdout;
602 1.22 lukem } else if (*savefile == '|') {
603 1.22 lukem oldintp = signal(SIGPIPE, SIG_IGN);
604 1.22 lukem fout = popen(savefile + 1, "w");
605 1.22 lukem if (fout == NULL) {
606 1.27 lukem warn("Can't run `%s'", savefile + 1);
607 1.42 lukem goto cleanup_fetch_url;
608 1.22 lukem }
609 1.22 lukem closefunc = pclose;
610 1.22 lukem } else {
611 1.22 lukem fout = fopen(savefile, "w");
612 1.22 lukem if (fout == NULL) {
613 1.27 lukem warn("Can't open `%s'", savefile);
614 1.42 lukem goto cleanup_fetch_url;
615 1.22 lukem }
616 1.22 lukem closefunc = fclose;
617 1.1 lukem }
618 1.1 lukem
619 1.22 lukem /* Trap signals */
620 1.2 lukem if (setjmp(httpabort)) {
621 1.2 lukem if (oldintr)
622 1.3 lukem (void)signal(SIGINT, oldintr);
623 1.22 lukem if (oldintp)
624 1.22 lukem (void)signal(SIGPIPE, oldintp);
625 1.42 lukem goto cleanup_fetch_url;
626 1.2 lukem }
627 1.2 lukem oldintr = signal(SIGINT, aborthttp);
628 1.2 lukem
629 1.1 lukem bytes = 0;
630 1.1 lukem hashbytes = mark;
631 1.1 lukem progressmeter(-1);
632 1.2 lukem
633 1.24 lukem /* Finally, suck down the file. */
634 1.27 lukem buf = xmalloc(BUFSIZ);
635 1.24 lukem while ((len = fread(buf, sizeof(char), BUFSIZ, fin)) > 0) {
636 1.1 lukem bytes += len;
637 1.25 lukem if (fwrite(buf, sizeof(char), len, fout) != len) {
638 1.27 lukem warn("Writing `%s'", savefile);
639 1.42 lukem goto cleanup_fetch_url;
640 1.1 lukem }
641 1.1 lukem if (hash && !progress) {
642 1.1 lukem while (bytes >= hashbytes) {
643 1.22 lukem (void)putc('#', ttyout);
644 1.1 lukem hashbytes += mark;
645 1.1 lukem }
646 1.22 lukem (void)fflush(ttyout);
647 1.1 lukem }
648 1.1 lukem }
649 1.1 lukem if (hash && !progress && bytes > 0) {
650 1.1 lukem if (bytes < mark)
651 1.22 lukem (void)putc('#', ttyout);
652 1.22 lukem (void)putc('\n', ttyout);
653 1.22 lukem (void)fflush(ttyout);
654 1.1 lukem }
655 1.25 lukem if (ferror(fin)) {
656 1.25 lukem warn("Reading file");
657 1.42 lukem goto cleanup_fetch_url;
658 1.1 lukem }
659 1.2 lukem progressmeter(1);
660 1.25 lukem (void)fflush(fout);
661 1.3 lukem (void)signal(SIGINT, oldintr);
662 1.22 lukem if (oldintp)
663 1.22 lukem (void)signal(SIGPIPE, oldintp);
664 1.25 lukem if (closefunc == fclose && mtime != -1) {
665 1.25 lukem struct timeval tval[2];
666 1.25 lukem
667 1.25 lukem (void)gettimeofday(&tval[0], NULL);
668 1.25 lukem tval[1].tv_sec = mtime;
669 1.25 lukem tval[1].tv_usec = 0;
670 1.31 lukem (*closefunc)(fout);
671 1.31 lukem fout = NULL;
672 1.31 lukem
673 1.31 lukem if (utimes(savefile, tval) == -1) {
674 1.25 lukem fprintf(ttyout,
675 1.25 lukem "Can't change modification time to %s",
676 1.25 lukem asctime(localtime(&mtime)));
677 1.25 lukem }
678 1.25 lukem }
679 1.25 lukem if (bytes > 0)
680 1.25 lukem ptransfer(0);
681 1.1 lukem
682 1.42 lukem rval = 0;
683 1.42 lukem goto cleanup_fetch_url;
684 1.11 lukem
685 1.1 lukem improper:
686 1.27 lukem warnx("Improper response from `%s'", host);
687 1.11 lukem
688 1.42 lukem cleanup_fetch_url:
689 1.23 thorpej resetsockbufsize();
690 1.24 lukem if (fin != NULL)
691 1.24 lukem fclose(fin);
692 1.24 lukem else if (s != -1)
693 1.1 lukem close(s);
694 1.22 lukem if (closefunc != NULL && fout != NULL)
695 1.22 lukem (*closefunc)(fout);
696 1.29 lukem FREEPTR(savefile);
697 1.27 lukem FREEPTR(user);
698 1.27 lukem FREEPTR(pass);
699 1.27 lukem FREEPTR(host);
700 1.27 lukem FREEPTR(path);
701 1.27 lukem FREEPTR(buf);
702 1.42 lukem FREEPTR(location);
703 1.42 lukem FREEPTR(message);
704 1.42 lukem return (rval);
705 1.1 lukem }
706 1.1 lukem
707 1.1 lukem /*
708 1.2 lukem * Abort a http retrieval
709 1.2 lukem */
710 1.2 lukem void
711 1.3 lukem aborthttp(notused)
712 1.3 lukem int notused;
713 1.2 lukem {
714 1.2 lukem
715 1.2 lukem alarmtimer(0);
716 1.24 lukem fputs("\nHTTP fetch aborted.\n", ttyout);
717 1.22 lukem (void)fflush(ttyout);
718 1.2 lukem longjmp(httpabort, 1);
719 1.2 lukem }
720 1.2 lukem
721 1.2 lukem /*
722 1.42 lukem * Retrieve ftp URL or classic ftp argument.
723 1.42 lukem * Returns -1 on failure, 0 on completed xfer, 1 if ftp connection
724 1.42 lukem * is still open (e.g, ftp xfer with trailing /)
725 1.42 lukem */
726 1.42 lukem static int
727 1.42 lukem fetch_ftp(url, outfile)
728 1.42 lukem const char *url;
729 1.42 lukem const char *outfile;
730 1.42 lukem {
731 1.42 lukem static char lasthost[MAXHOSTNAMELEN];
732 1.42 lukem char *cp, *xargv[5], rempath[MAXPATHLEN];
733 1.42 lukem char portnum[6]; /* large enough for "65535\0" */
734 1.42 lukem char *host, *path, *dir, *file, *user, *pass;
735 1.42 lukem in_port_t port;
736 1.42 lukem int dirhasglob, filehasglob, rval, xargc;
737 1.42 lukem
738 1.42 lukem host = path = dir = file = user = pass = NULL;
739 1.42 lukem port = 0;
740 1.42 lukem rval = 1;
741 1.42 lukem
742 1.42 lukem if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
743 1.42 lukem url_t urltype;
744 1.42 lukem
745 1.42 lukem if ((parse_url(url, "URL", &urltype, &user, &pass,
746 1.42 lukem &host, &port, &path) == -1) ||
747 1.42 lukem (user != NULL && *user == '\0') ||
748 1.42 lukem (pass != NULL && *pass == '\0') ||
749 1.42 lukem EMPTYSTRING(host)) {
750 1.42 lukem warnx("Invalid URL `%s'", url);
751 1.42 lukem goto cleanup_fetch_ftp;
752 1.42 lukem }
753 1.42 lukem } else { /* classic style `host:file' */
754 1.42 lukem host = xstrdup(url);
755 1.42 lukem cp = strchr(host, ':');
756 1.42 lukem if (cp != NULL) {
757 1.42 lukem *cp = '\0';
758 1.42 lukem path = xstrdup(cp + 1);
759 1.42 lukem }
760 1.42 lukem }
761 1.42 lukem if (EMPTYSTRING(host))
762 1.42 lukem goto cleanup_fetch_ftp;
763 1.42 lukem
764 1.42 lukem /*
765 1.42 lukem * Extract the file and (if present) directory name.
766 1.42 lukem */
767 1.42 lukem dir = path;
768 1.42 lukem if (! EMPTYSTRING(dir)) {
769 1.42 lukem if (*dir == '/')
770 1.42 lukem dir++; /* skip leading / */
771 1.42 lukem cp = strrchr(dir, '/');
772 1.42 lukem if (cp != NULL) {
773 1.42 lukem *cp++ = '\0';
774 1.42 lukem file = cp;
775 1.42 lukem } else {
776 1.42 lukem file = dir;
777 1.42 lukem dir = NULL;
778 1.42 lukem }
779 1.42 lukem }
780 1.42 lukem if (debug)
781 1.42 lukem fprintf(ttyout,
782 1.42 lukem "fetch_ftp: user `%s', pass `%s', host %s:%d, path, `%s', dir `%s', file `%s'\n",
783 1.42 lukem user ? user : "", pass ? pass : "",
784 1.42 lukem host ? host : "", ntohs(port), path ? path : "",
785 1.42 lukem dir ? dir : "", file ? file : "");
786 1.42 lukem
787 1.42 lukem dirhasglob = filehasglob = 0;
788 1.42 lukem if (doglob) {
789 1.42 lukem if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
790 1.42 lukem dirhasglob = 1;
791 1.42 lukem if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
792 1.42 lukem filehasglob = 1;
793 1.42 lukem }
794 1.42 lukem
795 1.42 lukem /*
796 1.42 lukem * Set up the connection if we don't have one.
797 1.42 lukem */
798 1.42 lukem if (strcasecmp(host, lasthost) != 0) {
799 1.42 lukem int oautologin;
800 1.42 lukem
801 1.42 lukem (void)strcpy(lasthost, host);
802 1.42 lukem if (connected)
803 1.42 lukem disconnect(0, NULL);
804 1.42 lukem xargv[0] = __progname;
805 1.42 lukem xargv[1] = host;
806 1.42 lukem xargv[2] = NULL;
807 1.42 lukem xargc = 2;
808 1.42 lukem if (port) {
809 1.42 lukem snprintf(portnum, sizeof(portnum), "%d", ntohs(port));
810 1.42 lukem xargv[2] = portnum;
811 1.42 lukem xargv[3] = NULL;
812 1.42 lukem xargc = 3;
813 1.42 lukem }
814 1.42 lukem oautologin = autologin;
815 1.42 lukem if (user != NULL)
816 1.42 lukem autologin = 0;
817 1.42 lukem setpeer(xargc, xargv);
818 1.42 lukem autologin = oautologin;
819 1.42 lukem if ((connected == 0)
820 1.42 lukem || ((connected == 1) && !ftp_login(host, user, pass)) ) {
821 1.42 lukem warnx("Can't connect or login to host `%s'",
822 1.42 lukem host);
823 1.42 lukem goto cleanup_fetch_ftp;
824 1.42 lukem }
825 1.42 lukem
826 1.42 lukem /* Always use binary transfers. */
827 1.42 lukem setbinary(0, NULL);
828 1.42 lukem } else {
829 1.42 lukem /* connection exists, cd back to `/' */
830 1.42 lukem xargv[0] = "cd";
831 1.42 lukem xargv[1] = "/";
832 1.42 lukem xargv[2] = NULL;
833 1.42 lukem dirchange = 0;
834 1.42 lukem cd(2, xargv);
835 1.42 lukem if (! dirchange)
836 1.42 lukem goto cleanup_fetch_ftp;
837 1.42 lukem }
838 1.42 lukem
839 1.42 lukem /* Change directories, if necessary. */
840 1.42 lukem if (! EMPTYSTRING(dir) && !dirhasglob) {
841 1.42 lukem xargv[0] = "cd";
842 1.42 lukem xargv[1] = dir;
843 1.42 lukem xargv[2] = NULL;
844 1.42 lukem dirchange = 0;
845 1.42 lukem cd(2, xargv);
846 1.42 lukem if (! dirchange)
847 1.42 lukem goto cleanup_fetch_ftp;
848 1.42 lukem }
849 1.42 lukem
850 1.42 lukem if (EMPTYSTRING(file)) {
851 1.42 lukem rval = -1;
852 1.42 lukem goto cleanup_fetch_ftp;
853 1.42 lukem }
854 1.42 lukem
855 1.42 lukem if (!verbose)
856 1.42 lukem fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
857 1.42 lukem
858 1.42 lukem if (dirhasglob) {
859 1.42 lukem snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
860 1.42 lukem file = rempath;
861 1.42 lukem }
862 1.42 lukem
863 1.42 lukem /* Fetch the file(s). */
864 1.42 lukem xargc = 2;
865 1.42 lukem xargv[0] = "get";
866 1.42 lukem xargv[1] = file;
867 1.42 lukem xargv[2] = NULL;
868 1.42 lukem if (dirhasglob || filehasglob) {
869 1.42 lukem int ointeractive;
870 1.42 lukem
871 1.42 lukem ointeractive = interactive;
872 1.42 lukem interactive = 0;
873 1.42 lukem xargv[0] = "mget";
874 1.42 lukem mget(xargc, xargv);
875 1.42 lukem interactive = ointeractive;
876 1.42 lukem } else {
877 1.42 lukem if (outfile != NULL) {
878 1.42 lukem xargv[2] = (char *)outfile;
879 1.42 lukem xargv[3] = NULL;
880 1.42 lukem xargc++;
881 1.42 lukem }
882 1.42 lukem get(xargc, xargv);
883 1.42 lukem if (outfile != NULL && strcmp(outfile, "-") != 0
884 1.42 lukem && outfile[0] != '|')
885 1.42 lukem outfile = NULL;
886 1.42 lukem }
887 1.42 lukem
888 1.42 lukem if ((code / 100) == COMPLETE)
889 1.42 lukem rval = 0;
890 1.42 lukem
891 1.42 lukem cleanup_fetch_ftp:
892 1.42 lukem FREEPTR(host);
893 1.42 lukem FREEPTR(path);
894 1.42 lukem FREEPTR(user);
895 1.42 lukem FREEPTR(pass);
896 1.42 lukem return (rval);
897 1.42 lukem }
898 1.42 lukem
899 1.42 lukem /*
900 1.42 lukem * Retrieve the given file to outfile.
901 1.42 lukem * Supports arguments of the form:
902 1.42 lukem * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else
903 1.42 lukem * call fetch_ftp()
904 1.42 lukem * "http://host/path" call fetch_url() to use http
905 1.42 lukem * "file:///path" call fetch_url() to copy
906 1.42 lukem * "about:..." print a message
907 1.42 lukem *
908 1.42 lukem * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
909 1.42 lukem * is still open (e.g, ftp xfer with trailing /)
910 1.42 lukem */
911 1.42 lukem static int
912 1.42 lukem go_fetch(url, outfile)
913 1.42 lukem const char *url;
914 1.42 lukem const char *outfile;
915 1.42 lukem {
916 1.42 lukem
917 1.42 lukem #ifndef SMALL
918 1.42 lukem /*
919 1.42 lukem * Check for about:*
920 1.42 lukem */
921 1.42 lukem if (strncasecmp(url, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) {
922 1.42 lukem url += sizeof(ABOUT_URL) -1;
923 1.42 lukem if (strcasecmp(url, "ftp") == 0) {
924 1.42 lukem fprintf(ttyout, "%s\n%s\n",
925 1.42 lukem "This version of ftp has been enhanced by Luke Mewburn <lukem (at) netbsd.org>.",
926 1.42 lukem "Execute 'man ftp' for more details");
927 1.42 lukem } else if (strcasecmp(url, "netbsd") == 0) {
928 1.42 lukem fprintf(ttyout, "%s\n%s\n",
929 1.42 lukem "NetBSD is a freely available and redistributable UNIX-like operating system.",
930 1.42 lukem "For more information, see http://www.netbsd.org/index.html");
931 1.42 lukem } else {
932 1.42 lukem fprintf(ttyout, "`%s' is an interesting topic.\n", url);
933 1.42 lukem }
934 1.42 lukem return (0);
935 1.42 lukem }
936 1.42 lukem #endif /* SMALL */
937 1.42 lukem
938 1.42 lukem /*
939 1.42 lukem * Check for file:// and http:// URLs.
940 1.42 lukem */
941 1.42 lukem if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
942 1.42 lukem strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0)
943 1.42 lukem return (fetch_url(url, outfile, NULL));
944 1.42 lukem
945 1.42 lukem /*
946 1.42 lukem * Try FTP URL-style and host:file arguments next.
947 1.42 lukem * If ftpproxy is set with an FTP URL, use fetch_url()
948 1.42 lukem * Othewise, use fetch_ftp().
949 1.42 lukem */
950 1.42 lukem if (ftpproxy && strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0)
951 1.42 lukem return (fetch_url(url, outfile, NULL));
952 1.42 lukem
953 1.42 lukem return (fetch_ftp(url, outfile));
954 1.42 lukem }
955 1.42 lukem
956 1.42 lukem /*
957 1.42 lukem * Retrieve multiple files from the command line,
958 1.42 lukem * calling go_fetch() for each file.
959 1.42 lukem *
960 1.42 lukem * If an ftp path has a trailing "/", the path will be cd-ed into and
961 1.41 lukem * the connection remains open, and the function will return -1
962 1.41 lukem * (to indicate the connection is alive).
963 1.1 lukem * If an error occurs the return value will be the offset+1 in
964 1.1 lukem * argv[] of the file that caused a problem (i.e, argv[x]
965 1.1 lukem * returns x+1)
966 1.1 lukem * Otherwise, 0 is returned if all files retrieved successfully.
967 1.1 lukem */
968 1.1 lukem int
969 1.22 lukem auto_fetch(argc, argv, outfile)
970 1.1 lukem int argc;
971 1.1 lukem char *argv[];
972 1.22 lukem char *outfile;
973 1.1 lukem {
974 1.42 lukem volatile int argpos;
975 1.42 lukem int rval;
976 1.22 lukem
977 1.2 lukem argpos = 0;
978 1.1 lukem
979 1.2 lukem if (setjmp(toplevel)) {
980 1.2 lukem if (connected)
981 1.2 lukem disconnect(0, NULL);
982 1.3 lukem return (argpos + 1);
983 1.2 lukem }
984 1.3 lukem (void)signal(SIGINT, (sig_t)intr);
985 1.3 lukem (void)signal(SIGPIPE, (sig_t)lostpeer);
986 1.1 lukem
987 1.1 lukem /*
988 1.1 lukem * Loop through as long as there's files to fetch.
989 1.1 lukem */
990 1.27 lukem for (rval = 0; (rval == 0) && (argpos < argc); argpos++) {
991 1.1 lukem if (strchr(argv[argpos], ':') == NULL)
992 1.1 lukem break;
993 1.42 lukem redirect_loop = 0;
994 1.42 lukem rval = go_fetch(argv[argpos], outfile);
995 1.42 lukem if (rval > 0)
996 1.1 lukem rval = argpos + 1;
997 1.42 lukem }
998 1.3 lukem
999 1.1 lukem if (connected && rval != -1)
1000 1.1 lukem disconnect(0, NULL);
1001 1.1 lukem return (rval);
1002 1.1 lukem }
1003