xref: /minix3/minix/commands/fetch/fetch.c (revision d0055759dd8892194db7fce6acc5085d5c9aeaee)
1433d6423SLionel Sambuc /*-
2433d6423SLionel Sambuc  * Copyright (c) 2000-2004 Dag-Erling Co�dan Sm�rgrav
3433d6423SLionel Sambuc  * All rights reserved.
4433d6423SLionel Sambuc  *
5433d6423SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
6433d6423SLionel Sambuc  * modification, are permitted provided that the following conditions
7433d6423SLionel Sambuc  * are met:
8433d6423SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
9433d6423SLionel Sambuc  *    notice, this list of conditions and the following disclaimer
10433d6423SLionel Sambuc  *    in this position and unchanged.
11433d6423SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
12433d6423SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
13433d6423SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
14433d6423SLionel Sambuc  * 3. The name of the author may not be used to endorse or promote products
15433d6423SLionel Sambuc  *    derived from this software without specific prior written permission
16433d6423SLionel Sambuc  *
17433d6423SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18433d6423SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19433d6423SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20433d6423SLionel Sambuc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21433d6423SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22433d6423SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23433d6423SLionel Sambuc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24433d6423SLionel Sambuc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25433d6423SLionel Sambuc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26433d6423SLionel Sambuc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27433d6423SLionel Sambuc  */
28433d6423SLionel Sambuc 
29433d6423SLionel Sambuc #if HAVE_CONFIG_H
30433d6423SLionel Sambuc #include "config.h"
31433d6423SLionel Sambuc #endif
32433d6423SLionel Sambuc #if !defined(NETBSD) && !defined(__minix)
33433d6423SLionel Sambuc #include <nbcompat.h>
34433d6423SLionel Sambuc #endif
35433d6423SLionel Sambuc 
36433d6423SLionel Sambuc #if HAVE_SYS_PARAM_H
37433d6423SLionel Sambuc #include <sys/param.h>
38433d6423SLionel Sambuc #endif
39433d6423SLionel Sambuc #if HAVE_SYS_IOCTL_H
40433d6423SLionel Sambuc #include <sys/ioctl.h>
41433d6423SLionel Sambuc #endif
42433d6423SLionel Sambuc #if HAVE_SYS_SOCKET_H
43433d6423SLionel Sambuc #include <sys/socket.h>
44433d6423SLionel Sambuc #endif
45433d6423SLionel Sambuc #if HAVE_SYS_STAT_H
46433d6423SLionel Sambuc #include <sys/stat.h>
47433d6423SLionel Sambuc #endif
48433d6423SLionel Sambuc #if HAVE_SYS_TIME_H
49433d6423SLionel Sambuc #include <sys/time.h>
50433d6423SLionel Sambuc #endif
51433d6423SLionel Sambuc #if HAVE_UTIME_H
52433d6423SLionel Sambuc #include <utime.h>
53433d6423SLionel Sambuc #endif
54433d6423SLionel Sambuc #include <ctype.h>
55433d6423SLionel Sambuc #if HAVE_ERR_H
56433d6423SLionel Sambuc #include <err.h>
57433d6423SLionel Sambuc #endif
58433d6423SLionel Sambuc #include <errno.h>
59433d6423SLionel Sambuc #include <signal.h>
60433d6423SLionel Sambuc #if HAVE_STDINT_H
61433d6423SLionel Sambuc #include <stdint.h>
62433d6423SLionel Sambuc #endif
63433d6423SLionel Sambuc #include <stdio.h>
64433d6423SLionel Sambuc #include <stdlib.h>
65433d6423SLionel Sambuc #include <string.h>
66433d6423SLionel Sambuc #if HAVE_TERMIOS_H
67433d6423SLionel Sambuc #include <termios.h>
68433d6423SLionel Sambuc #endif
69433d6423SLionel Sambuc #include <unistd.h>
70433d6423SLionel Sambuc 
71433d6423SLionel Sambuc #include <fetch.h>
72433d6423SLionel Sambuc 
73433d6423SLionel Sambuc #if HAVE_SYSEXITS_H
74433d6423SLionel Sambuc #include <sysexits.h>
75433d6423SLionel Sambuc #endif
76433d6423SLionel Sambuc 
77433d6423SLionel Sambuc #ifndef EX_USAGE
78433d6423SLionel Sambuc #define	EX_USAGE	64
79433d6423SLionel Sambuc #endif
80433d6423SLionel Sambuc 
81433d6423SLionel Sambuc #ifndef EX_IOERR
82433d6423SLionel Sambuc #define	EX_IOERR	74
83433d6423SLionel Sambuc #endif
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc #define MINBUFSIZE	4096
86433d6423SLionel Sambuc 
87433d6423SLionel Sambuc /* Option flags */
88433d6423SLionel Sambuc int	 A_flag;	/*    -A: do not follow 302 redirects */
89433d6423SLionel Sambuc int	 a_flag;	/*    -a: auto retry */
90433d6423SLionel Sambuc off_t	 B_size;	/*    -B: buffer size */
91433d6423SLionel Sambuc int	 d_flag;	/*    -d: direct connection */
92433d6423SLionel Sambuc int	 F_flag;	/*    -F: restart without checking mtime  */
93433d6423SLionel Sambuc int	 i_flag;	/*    -i: fetch file if modified */
94433d6423SLionel Sambuc int	 l_flag;	/*    -l: link rather than copy file: URLs */
95433d6423SLionel Sambuc int	 m_flag;	/* -[Mm]: mirror mode */
96433d6423SLionel Sambuc char	*N_filename;	/*    -N: netrc file name */
97433d6423SLionel Sambuc int	 n_flag;	/*    -n: do not preserve modification time */
98433d6423SLionel Sambuc int	 o_flag;	/*    -o: specify output file */
99433d6423SLionel Sambuc int	 o_directory;	/*        output file is a directory */
100433d6423SLionel Sambuc char	*o_filename;	/*        name of output file */
101433d6423SLionel Sambuc int	 o_stdout;	/*        output file is stdout */
102433d6423SLionel Sambuc int	 once_flag;	/*    -1: stop at first successful file */
103433d6423SLionel Sambuc int	 R_flag;	/*    -R: don't delete partially transferred files */
104433d6423SLionel Sambuc int	 r_flag;	/*    -r: restart previously interrupted transfer */
105433d6423SLionel Sambuc off_t	 S_size;        /*    -S: require size to match */
106433d6423SLionel Sambuc int	 s_flag;        /*    -s: show size, don't fetch */
107433d6423SLionel Sambuc long	 T_secs = 120;	/*    -T: transfer timeout in seconds */
108433d6423SLionel Sambuc int	 U_flag;	/*    -U: do not use high ports */
109433d6423SLionel Sambuc int	 v_level = 1;	/*    -v: verbosity level */
110433d6423SLionel Sambuc int	 v_tty;		/*        stdout is a tty */
111433d6423SLionel Sambuc pid_t	 pgrp;		/*        our process group */
112433d6423SLionel Sambuc long	 w_secs;	/*    -w: retry delay */
113433d6423SLionel Sambuc int	 family = PF_UNSPEC;	/* -[46]: address family to use */
114433d6423SLionel Sambuc 
115433d6423SLionel Sambuc volatile int	 sigalrm;	/* SIGALRM received */
116433d6423SLionel Sambuc #ifdef SIGINFO
117433d6423SLionel Sambuc volatile int	 siginfo;	/* SIGINFO received */
118433d6423SLionel Sambuc #endif
119433d6423SLionel Sambuc volatile int	 sigint;	/* SIGINT received */
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc long	 ftp_timeout;	/* default timeout for FTP transfers */
122433d6423SLionel Sambuc long	 http_timeout;	/* default timeout for HTTP transfers */
123433d6423SLionel Sambuc char	*buf;		/* transfer buffer */
124433d6423SLionel Sambuc 
125433d6423SLionel Sambuc 
126433d6423SLionel Sambuc /*
127433d6423SLionel Sambuc  * Signal handler
128433d6423SLionel Sambuc  */
129433d6423SLionel Sambuc static void
sig_handler(int sig)130433d6423SLionel Sambuc sig_handler(int sig)
131433d6423SLionel Sambuc {
132433d6423SLionel Sambuc 	switch (sig) {
133433d6423SLionel Sambuc 	case SIGALRM:
134433d6423SLionel Sambuc 		fetchRestartCalls = 0;
135433d6423SLionel Sambuc 		sigalrm = 1;
136433d6423SLionel Sambuc 		break;
137433d6423SLionel Sambuc #ifdef SIGINFO
138433d6423SLionel Sambuc 	case SIGINFO:
139433d6423SLionel Sambuc 		siginfo = 1;
140433d6423SLionel Sambuc 		break;
141433d6423SLionel Sambuc #endif
142433d6423SLionel Sambuc 	case SIGINT:
143433d6423SLionel Sambuc 		fetchRestartCalls = 0;
144433d6423SLionel Sambuc 		sigint = 1;
145433d6423SLionel Sambuc 		break;
146433d6423SLionel Sambuc 	}
147433d6423SLionel Sambuc }
148433d6423SLionel Sambuc 
149433d6423SLionel Sambuc struct xferstat {
150433d6423SLionel Sambuc 	char		 name[64];
151433d6423SLionel Sambuc 	struct timeval	 start;
152433d6423SLionel Sambuc 	struct timeval	 last;
153433d6423SLionel Sambuc 	off_t		 size;
154433d6423SLionel Sambuc 	off_t		 offset;
155433d6423SLionel Sambuc 	off_t		 rcvd;
156433d6423SLionel Sambuc };
157433d6423SLionel Sambuc 
158433d6423SLionel Sambuc /*
159433d6423SLionel Sambuc  * Compute and display ETA
160433d6423SLionel Sambuc  */
161433d6423SLionel Sambuc static const char *
stat_eta(struct xferstat * xs)162433d6423SLionel Sambuc stat_eta(struct xferstat *xs)
163433d6423SLionel Sambuc {
164433d6423SLionel Sambuc 	static char str[16];
165433d6423SLionel Sambuc 	long elapsed, eta;
166433d6423SLionel Sambuc 	off_t received, expected;
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc 	elapsed = xs->last.tv_sec - xs->start.tv_sec;
169433d6423SLionel Sambuc 	received = xs->rcvd - xs->offset;
170433d6423SLionel Sambuc 	expected = xs->size - xs->rcvd;
171433d6423SLionel Sambuc 	eta = (long)((double)elapsed * expected / received);
172433d6423SLionel Sambuc 	if (eta > 3600)
173433d6423SLionel Sambuc 		snprintf(str, sizeof str, "%02ldh%02ldm",
174433d6423SLionel Sambuc 		    eta / 3600, (eta % 3600) / 60);
175433d6423SLionel Sambuc 	else
176433d6423SLionel Sambuc 		snprintf(str, sizeof str, "%02ldm%02lds",
177433d6423SLionel Sambuc 		    eta / 60, eta % 60);
178433d6423SLionel Sambuc 	return (str);
179433d6423SLionel Sambuc }
180433d6423SLionel Sambuc 
181433d6423SLionel Sambuc /*
182433d6423SLionel Sambuc  * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
183433d6423SLionel Sambuc  */
184433d6423SLionel Sambuc static const char *prefixes = " kMGTP";
185433d6423SLionel Sambuc static const char *
stat_bytes(off_t bytes)186433d6423SLionel Sambuc stat_bytes(off_t bytes)
187433d6423SLionel Sambuc {
188433d6423SLionel Sambuc 	static char str[16];
189433d6423SLionel Sambuc 	const char *prefix = prefixes;
190433d6423SLionel Sambuc 
191433d6423SLionel Sambuc 	while (bytes > 9999 && prefix[1] != '\0') {
192433d6423SLionel Sambuc 		bytes /= 1024;
193433d6423SLionel Sambuc 		prefix++;
194433d6423SLionel Sambuc 	}
195433d6423SLionel Sambuc 	snprintf(str, sizeof str, "%4jd %cB", (intmax_t)bytes, *prefix);
196433d6423SLionel Sambuc 	return (str);
197433d6423SLionel Sambuc }
198433d6423SLionel Sambuc 
199433d6423SLionel Sambuc /*
200433d6423SLionel Sambuc  * Compute and display transfer rate
201433d6423SLionel Sambuc  */
202433d6423SLionel Sambuc static const char *
stat_bps(struct xferstat * xs)203433d6423SLionel Sambuc stat_bps(struct xferstat *xs)
204433d6423SLionel Sambuc {
205433d6423SLionel Sambuc 	static char str[16];
206433d6423SLionel Sambuc 	double delta, bps;
207433d6423SLionel Sambuc 
208433d6423SLionel Sambuc 	delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
209433d6423SLionel Sambuc 	    - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6));
210433d6423SLionel Sambuc 	if (delta == 0.0) {
211433d6423SLionel Sambuc 		snprintf(str, sizeof str, "?? Bps");
212433d6423SLionel Sambuc 	} else {
213433d6423SLionel Sambuc 		bps = (xs->rcvd - xs->offset) / delta;
214433d6423SLionel Sambuc 		snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps));
215433d6423SLionel Sambuc 	}
216433d6423SLionel Sambuc 	return (str);
217433d6423SLionel Sambuc }
218433d6423SLionel Sambuc 
219433d6423SLionel Sambuc /*
220433d6423SLionel Sambuc  * Update the stats display
221433d6423SLionel Sambuc  */
222433d6423SLionel Sambuc static void
stat_display(struct xferstat * xs,int force)223433d6423SLionel Sambuc stat_display(struct xferstat *xs, int force)
224433d6423SLionel Sambuc {
225433d6423SLionel Sambuc 	struct timeval now;
226433d6423SLionel Sambuc #if !defined(__minix)
227433d6423SLionel Sambuc 	int ctty_pgrp;
228433d6423SLionel Sambuc #endif /* !defined(__minix) */
229433d6423SLionel Sambuc 
230433d6423SLionel Sambuc 	/* Minix returns "Not a typewriter error" */
231433d6423SLionel Sambuc #if defined(TIOCGPGRP) && !defined(__minix)
232433d6423SLionel Sambuc 	/* check if we're the foreground process */
233433d6423SLionel Sambuc 	if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 ||
234433d6423SLionel Sambuc 	    (pid_t)ctty_pgrp != pgrp)
235433d6423SLionel Sambuc 		return;
236433d6423SLionel Sambuc #endif
237433d6423SLionel Sambuc 
238433d6423SLionel Sambuc 	gettimeofday(&now, NULL);
239433d6423SLionel Sambuc 	if (!force && now.tv_sec <= xs->last.tv_sec)
240433d6423SLionel Sambuc 		return;
241433d6423SLionel Sambuc 	xs->last = now;
242433d6423SLionel Sambuc 
243433d6423SLionel Sambuc 	fprintf(stderr, "\r%-46.46s", xs->name);
244433d6423SLionel Sambuc 	if (xs->size <= 0) {
245433d6423SLionel Sambuc #if HAVE_SETPROCTITLE
246433d6423SLionel Sambuc 		setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd));
247433d6423SLionel Sambuc #endif
248433d6423SLionel Sambuc 		fprintf(stderr, "        %s", stat_bytes(xs->rcvd));
249433d6423SLionel Sambuc 	} else {
250433d6423SLionel Sambuc #if HAVE_SETPROCTITLE
251433d6423SLionel Sambuc 		setproctitle("%s [%d%% of %s]", xs->name,
252433d6423SLionel Sambuc 		    (int)((100.0 * xs->rcvd) / xs->size),
253433d6423SLionel Sambuc 		    stat_bytes(xs->size));
254433d6423SLionel Sambuc #endif
255433d6423SLionel Sambuc 		fprintf(stderr, "%3d%% of %s",
256433d6423SLionel Sambuc 		    (int)((100.0 * xs->rcvd) / xs->size),
257433d6423SLionel Sambuc 		    stat_bytes(xs->size));
258433d6423SLionel Sambuc 	}
259433d6423SLionel Sambuc 	fprintf(stderr, " %s", stat_bps(xs));
260433d6423SLionel Sambuc 	if (xs->size > 0 && xs->rcvd > 0 &&
261433d6423SLionel Sambuc 	    xs->last.tv_sec >= xs->start.tv_sec + 10)
262433d6423SLionel Sambuc 		fprintf(stderr, " %s", stat_eta(xs));
263433d6423SLionel Sambuc 	fflush(stderr);
264433d6423SLionel Sambuc }
265433d6423SLionel Sambuc 
266433d6423SLionel Sambuc /*
267433d6423SLionel Sambuc  * Initialize the transfer statistics
268433d6423SLionel Sambuc  */
269433d6423SLionel Sambuc static void
stat_start(struct xferstat * xs,const char * name,off_t size,off_t offset)270433d6423SLionel Sambuc stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
271433d6423SLionel Sambuc {
272433d6423SLionel Sambuc 	snprintf(xs->name, sizeof xs->name, "%s", name);
273433d6423SLionel Sambuc 	gettimeofday(&xs->start, NULL);
274433d6423SLionel Sambuc 	xs->last.tv_sec = xs->last.tv_usec = 0;
275433d6423SLionel Sambuc 	xs->size = size;
276433d6423SLionel Sambuc 	xs->offset = offset;
277433d6423SLionel Sambuc 	xs->rcvd = offset;
278433d6423SLionel Sambuc 	if (v_tty && v_level > 0)
279433d6423SLionel Sambuc 		stat_display(xs, 1);
280433d6423SLionel Sambuc 	else if (v_level > 0)
281433d6423SLionel Sambuc 		fprintf(stderr, "%-46s", xs->name);
282433d6423SLionel Sambuc }
283433d6423SLionel Sambuc 
284433d6423SLionel Sambuc /*
285433d6423SLionel Sambuc  * Update the transfer statistics
286433d6423SLionel Sambuc  */
287433d6423SLionel Sambuc static void
stat_update(struct xferstat * xs,off_t rcvd)288433d6423SLionel Sambuc stat_update(struct xferstat *xs, off_t rcvd)
289433d6423SLionel Sambuc {
290433d6423SLionel Sambuc 	xs->rcvd = rcvd;
291433d6423SLionel Sambuc 	if (v_tty && v_level > 0)
292433d6423SLionel Sambuc 		stat_display(xs, 0);
293433d6423SLionel Sambuc }
294433d6423SLionel Sambuc 
295433d6423SLionel Sambuc /*
296433d6423SLionel Sambuc  * Finalize the transfer statistics
297433d6423SLionel Sambuc  */
298433d6423SLionel Sambuc static void
stat_end(struct xferstat * xs)299433d6423SLionel Sambuc stat_end(struct xferstat *xs)
300433d6423SLionel Sambuc {
301433d6423SLionel Sambuc 	gettimeofday(&xs->last, NULL);
302433d6423SLionel Sambuc 	if (v_tty && v_level > 0) {
303433d6423SLionel Sambuc 		stat_display(xs, 1);
304433d6423SLionel Sambuc 		putc('\n', stderr);
305433d6423SLionel Sambuc 	} else if (v_level > 0) {
306433d6423SLionel Sambuc 		fprintf(stderr, "        %s %s\n",
307433d6423SLionel Sambuc 		    stat_bytes(xs->size), stat_bps(xs));
308433d6423SLionel Sambuc 	}
309433d6423SLionel Sambuc }
310433d6423SLionel Sambuc 
311433d6423SLionel Sambuc #if HAVE_TERMIOS_H && !defined(PREFER_GETPASS)
312433d6423SLionel Sambuc static int
read_password(const char * prompt,char * pwbuf,size_t pwbuf_len)313433d6423SLionel Sambuc read_password(const char *prompt, char *pwbuf, size_t pwbuf_len)
314433d6423SLionel Sambuc {
315433d6423SLionel Sambuc 	struct termios tios;
316433d6423SLionel Sambuc 	tcflag_t saved_flags;
317433d6423SLionel Sambuc 	int nopwd;
318433d6423SLionel Sambuc 
319*d0055759SDavid van Moolenbroek 	fprintf(stderr, "%s", prompt);
320433d6423SLionel Sambuc 	if (tcgetattr(STDIN_FILENO, &tios) != 0)
321433d6423SLionel Sambuc 		return (fgets(pwbuf, pwbuf_len, stdin) == NULL);
322433d6423SLionel Sambuc 
323433d6423SLionel Sambuc 	saved_flags = tios.c_lflag;
324433d6423SLionel Sambuc 	tios.c_lflag &= ~ECHO;
325433d6423SLionel Sambuc 	tios.c_lflag |= ECHONL|ICANON;
326433d6423SLionel Sambuc #ifndef __minix
327433d6423SLionel Sambuc 	tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
328433d6423SLionel Sambuc #else
329433d6423SLionel Sambuc 	tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios);
330433d6423SLionel Sambuc #endif
331433d6423SLionel Sambuc 	nopwd = (fgets(pwbuf, pwbuf_len, stdin) == NULL);
332433d6423SLionel Sambuc 	tios.c_lflag = saved_flags;
333433d6423SLionel Sambuc #ifndef __minix
334433d6423SLionel Sambuc 	tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
335433d6423SLionel Sambuc #else
336433d6423SLionel Sambuc 	tcsetattr(STDIN_FILENO, TCSANOW, &tios);
337433d6423SLionel Sambuc #endif
338433d6423SLionel Sambuc 
339433d6423SLionel Sambuc 	return nopwd;
340433d6423SLionel Sambuc }
341433d6423SLionel Sambuc #elif HAVE_GETPASSPHRASE || HAVE_GETPASS
342433d6423SLionel Sambuc static int
read_password(const char * prompt,char * pwbuf,size_t pwbuf_len)343433d6423SLionel Sambuc read_password(const char *prompt, char *pwbuf, size_t pwbuf_len)
344433d6423SLionel Sambuc {
345433d6423SLionel Sambuc 	char *pass;
346433d6423SLionel Sambuc 
347433d6423SLionel Sambuc #if HAVE_GETPASSPHRASE && !defined(PREFER_GETPASS)
348433d6423SLionel Sambuc 	pass = getpassphrase(prompt);
349433d6423SLionel Sambuc #else
350433d6423SLionel Sambuc 	pass = getpass(prompt);
351433d6423SLionel Sambuc #endif
352433d6423SLionel Sambuc 	if (pass == NULL || strlen(pass) >= pwbuf_len)
353433d6423SLionel Sambuc 		return 1;
354433d6423SLionel Sambuc 	strcpy(pwbuf, pass);
355433d6423SLionel Sambuc 	return 0;
356433d6423SLionel Sambuc }
357433d6423SLionel Sambuc #else
358433d6423SLionel Sambuc static int
read_password(const char * prompt,char * pwbuf,size_t pwbuf_len)359433d6423SLionel Sambuc read_password(const char *prompt, char *pwbuf, size_t pwbuf_len)
360433d6423SLionel Sambuc {
361433d6423SLionel Sambuc 
362433d6423SLionel Sambuc 	fprintf(stderr, prompt);
363433d6423SLionel Sambuc 	return (fgets(pwbuf, pwbuf_len, stdin) == NULL);
364433d6423SLionel Sambuc }
365433d6423SLionel Sambuc #endif
366433d6423SLionel Sambuc 
367433d6423SLionel Sambuc /*
368433d6423SLionel Sambuc  * Ask the user for authentication details
369433d6423SLionel Sambuc  */
370433d6423SLionel Sambuc static int
query_auth(struct url * URL)371433d6423SLionel Sambuc query_auth(struct url *URL)
372433d6423SLionel Sambuc {
373433d6423SLionel Sambuc 	int i, nopwd;
374433d6423SLionel Sambuc 
375433d6423SLionel Sambuc 	fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
376433d6423SLionel Sambuc 	    URL->scheme, URL->host, URL->port);
377433d6423SLionel Sambuc 
378433d6423SLionel Sambuc 	fprintf(stderr, "Login: ");
379433d6423SLionel Sambuc 	if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
380433d6423SLionel Sambuc 		return (-1);
381433d6423SLionel Sambuc 	for (i = strlen(URL->user); i >= 0; --i)
382433d6423SLionel Sambuc 		if (URL->user[i] == '\r' || URL->user[i] == '\n')
383433d6423SLionel Sambuc 			URL->user[i] = '\0';
384433d6423SLionel Sambuc 
385433d6423SLionel Sambuc 	nopwd = read_password("Password: ",  URL->pwd, sizeof(URL->pwd));
386433d6423SLionel Sambuc 
387433d6423SLionel Sambuc 	if (nopwd)
388433d6423SLionel Sambuc 		return (-1);
389433d6423SLionel Sambuc 	for (i = strlen(URL->pwd); i >= 0; --i)
390433d6423SLionel Sambuc 		if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
391433d6423SLionel Sambuc 			URL->pwd[i] = '\0';
392433d6423SLionel Sambuc 
393433d6423SLionel Sambuc 	return (0);
394433d6423SLionel Sambuc }
395433d6423SLionel Sambuc 
396433d6423SLionel Sambuc /*
397433d6423SLionel Sambuc  * Fetch a file
398433d6423SLionel Sambuc  */
399433d6423SLionel Sambuc static int
fetch(char * URL,const char * path)400433d6423SLionel Sambuc fetch(char *URL, const char *path)
401433d6423SLionel Sambuc {
402433d6423SLionel Sambuc 	struct url *url;
403433d6423SLionel Sambuc 	struct url_stat us;
404433d6423SLionel Sambuc 	struct stat sb, nsb;
405433d6423SLionel Sambuc 	struct xferstat xs;
406433d6423SLionel Sambuc 	FILE *of;
407433d6423SLionel Sambuc 	fetchIO *f;
408433d6423SLionel Sambuc 	size_t size, wr;
409433d6423SLionel Sambuc 	ssize_t ssize;
410433d6423SLionel Sambuc 	off_t count;
411433d6423SLionel Sambuc 	char flags[8];
412433d6423SLionel Sambuc 	char *tmppath;
413433d6423SLionel Sambuc 	int r;
414433d6423SLionel Sambuc 	unsigned timeout;
415433d6423SLionel Sambuc 	char *ptr;
416433d6423SLionel Sambuc 
417433d6423SLionel Sambuc 	f = NULL;
418433d6423SLionel Sambuc 	of = NULL;
419433d6423SLionel Sambuc 	tmppath = NULL;
420433d6423SLionel Sambuc 
421433d6423SLionel Sambuc 	timeout = 0;
422433d6423SLionel Sambuc 	*flags = 0;
423433d6423SLionel Sambuc 	count = 0;
424433d6423SLionel Sambuc 
425433d6423SLionel Sambuc 	/* set verbosity level */
426433d6423SLionel Sambuc 	if (v_level > 1)
427433d6423SLionel Sambuc 		strcat(flags, "v");
428433d6423SLionel Sambuc 	if (v_level > 2)
429433d6423SLionel Sambuc 		fetchDebug = 1;
430433d6423SLionel Sambuc 
431433d6423SLionel Sambuc 	/* parse URL */
432433d6423SLionel Sambuc 	if ((url = fetchParseURL(URL)) == NULL) {
433433d6423SLionel Sambuc 		warnx("%s: parse error", URL);
434433d6423SLionel Sambuc 		goto failure;
435433d6423SLionel Sambuc 	}
436433d6423SLionel Sambuc 
437433d6423SLionel Sambuc 	/* if no scheme was specified, take a guess */
438433d6423SLionel Sambuc 	if (!*url->scheme) {
439433d6423SLionel Sambuc 		if (!*url->host)
440433d6423SLionel Sambuc 			strcpy(url->scheme, SCHEME_FILE);
441433d6423SLionel Sambuc 		else if (strncasecmp(url->host, "ftp.", 4) == 0)
442433d6423SLionel Sambuc 			strcpy(url->scheme, SCHEME_FTP);
443433d6423SLionel Sambuc 		else if (strncasecmp(url->host, "www.", 4) == 0)
444433d6423SLionel Sambuc 			strcpy(url->scheme, SCHEME_HTTP);
445433d6423SLionel Sambuc 	}
446433d6423SLionel Sambuc 
447433d6423SLionel Sambuc 	/* common flags */
448433d6423SLionel Sambuc 	switch (family) {
449433d6423SLionel Sambuc 	case PF_INET:
450433d6423SLionel Sambuc 		strcat(flags, "4");
451433d6423SLionel Sambuc 		break;
452433d6423SLionel Sambuc #ifndef __minix
453433d6423SLionel Sambuc 	case PF_INET6:
454433d6423SLionel Sambuc 		strcat(flags, "6");
455433d6423SLionel Sambuc 		break;
456433d6423SLionel Sambuc #endif
457433d6423SLionel Sambuc 	}
458433d6423SLionel Sambuc 
459433d6423SLionel Sambuc 	/* Protocol independent flags */
460433d6423SLionel Sambuc 	if (i_flag) {
461433d6423SLionel Sambuc 		if (stat(path, &sb) == 0) {
462433d6423SLionel Sambuc 			url->last_modified = sb.st_mtime;
463433d6423SLionel Sambuc 			strcat(flags, "i");
464433d6423SLionel Sambuc 		} else if (errno != ENOENT) {
465433d6423SLionel Sambuc 			warn("%s: stat()", path);
466433d6423SLionel Sambuc 			goto failure;
467433d6423SLionel Sambuc 		}
468433d6423SLionel Sambuc 	}
469433d6423SLionel Sambuc 
470433d6423SLionel Sambuc 	/* FTP specific flags */
471433d6423SLionel Sambuc 	if (strcmp(url->scheme, SCHEME_FTP) == 0) {
472433d6423SLionel Sambuc 		if (d_flag)
473433d6423SLionel Sambuc 			strcat(flags, "d");
474433d6423SLionel Sambuc 		if (U_flag)
475433d6423SLionel Sambuc 			strcat(flags, "l");
476433d6423SLionel Sambuc 		timeout = T_secs ? T_secs : ftp_timeout;
477433d6423SLionel Sambuc 	}
478433d6423SLionel Sambuc 
479433d6423SLionel Sambuc 	/* HTTP specific flags */
480433d6423SLionel Sambuc 	if (strcmp(url->scheme, SCHEME_HTTP) == 0) {
481433d6423SLionel Sambuc 		if (d_flag)
482433d6423SLionel Sambuc 			strcat(flags, "d");
483433d6423SLionel Sambuc 		if (A_flag)
484433d6423SLionel Sambuc 			strcat(flags, "A");
485433d6423SLionel Sambuc 		timeout = T_secs ? T_secs : http_timeout;
486433d6423SLionel Sambuc 	}
487433d6423SLionel Sambuc 
488433d6423SLionel Sambuc 	/* set the protocol timeout. */
489433d6423SLionel Sambuc 	fetchTimeout = timeout;
490433d6423SLionel Sambuc 
491433d6423SLionel Sambuc 	/* just print size */
492433d6423SLionel Sambuc 	if (s_flag) {
493433d6423SLionel Sambuc 		if (timeout)
494433d6423SLionel Sambuc 			alarm(timeout);
495433d6423SLionel Sambuc 		r = fetchStat(url, &us, flags);
496433d6423SLionel Sambuc 		if (timeout)
497433d6423SLionel Sambuc 			alarm(0);
498433d6423SLionel Sambuc 		if (sigalrm || sigint)
499433d6423SLionel Sambuc 			goto signal;
500433d6423SLionel Sambuc 		if (r == -1) {
501433d6423SLionel Sambuc 			warnx("%s", fetchLastErrString);
502433d6423SLionel Sambuc 			goto failure;
503433d6423SLionel Sambuc 		}
504433d6423SLionel Sambuc 		if (us.size == -1)
505433d6423SLionel Sambuc 			printf("Unknown\n");
506433d6423SLionel Sambuc 		else
507433d6423SLionel Sambuc 			printf("%jd\n", (intmax_t)us.size);
508433d6423SLionel Sambuc 		goto success;
509433d6423SLionel Sambuc 	}
510433d6423SLionel Sambuc 
511433d6423SLionel Sambuc 	/*
512433d6423SLionel Sambuc 	 * If the -r flag was specified, we have to compare the local
513433d6423SLionel Sambuc 	 * and remote files, so we should really do a fetchStat()
514433d6423SLionel Sambuc 	 * first, but I know of at least one HTTP server that only
515433d6423SLionel Sambuc 	 * sends the content size in response to GET requests, and
516433d6423SLionel Sambuc 	 * leaves it out of replies to HEAD requests.  Also, in the
517433d6423SLionel Sambuc 	 * (frequent) case that the local and remote files match but
518433d6423SLionel Sambuc 	 * the local file is truncated, we have sufficient information
519433d6423SLionel Sambuc 	 * before the compare to issue a correct request.  Therefore,
520433d6423SLionel Sambuc 	 * we always issue a GET request as if we were sure the local
521433d6423SLionel Sambuc 	 * file was a truncated copy of the remote file; we can drop
522433d6423SLionel Sambuc 	 * the connection later if we change our minds.
523433d6423SLionel Sambuc 	 */
524433d6423SLionel Sambuc 	sb.st_size = -1;
525433d6423SLionel Sambuc 	if (!o_stdout) {
526433d6423SLionel Sambuc 		r = stat(path, &sb);
527433d6423SLionel Sambuc 		if (r == 0 && r_flag && S_ISREG(sb.st_mode)) {
528433d6423SLionel Sambuc 			url->offset = sb.st_size;
529433d6423SLionel Sambuc 		} else if (r == -1 || !S_ISREG(sb.st_mode)) {
530433d6423SLionel Sambuc 			/*
531433d6423SLionel Sambuc 			 * Whatever value sb.st_size has now is either
532433d6423SLionel Sambuc 			 * wrong (if stat(2) failed) or irrelevant (if the
533433d6423SLionel Sambuc 			 * path does not refer to a regular file)
534433d6423SLionel Sambuc 			 */
535433d6423SLionel Sambuc 			sb.st_size = -1;
536433d6423SLionel Sambuc 		}
537433d6423SLionel Sambuc 		if (r == -1 && errno != ENOENT) {
538433d6423SLionel Sambuc 			warnx("%s: stat()", path);
539433d6423SLionel Sambuc 			goto failure;
540433d6423SLionel Sambuc 		}
541433d6423SLionel Sambuc 	}
542433d6423SLionel Sambuc 
543433d6423SLionel Sambuc 	/* start the transfer */
544433d6423SLionel Sambuc 	if (timeout)
545433d6423SLionel Sambuc 		alarm(timeout);
546433d6423SLionel Sambuc 	f = fetchXGet(url, &us, flags);
547433d6423SLionel Sambuc 	if (timeout)
548433d6423SLionel Sambuc 		alarm(0);
549433d6423SLionel Sambuc 	if (sigalrm || sigint)
550433d6423SLionel Sambuc 		goto signal;
551433d6423SLionel Sambuc 	if (f == NULL && i_flag && fetchLastErrCode == FETCH_UNCHANGED) {
552433d6423SLionel Sambuc 		/* URL was not modified, return OK. */
553433d6423SLionel Sambuc 		printf("%s: not modified\n", URL);
554433d6423SLionel Sambuc 		r = 0;
555433d6423SLionel Sambuc 		goto done;
556433d6423SLionel Sambuc 	}
557433d6423SLionel Sambuc 	if (f == NULL) {
558433d6423SLionel Sambuc 		warnx("%s: %s", URL, fetchLastErrString);
559433d6423SLionel Sambuc 		goto failure;
560433d6423SLionel Sambuc 	}
561433d6423SLionel Sambuc 	if (sigint)
562433d6423SLionel Sambuc 		goto signal;
563433d6423SLionel Sambuc 
564433d6423SLionel Sambuc 	/* check that size is as expected */
565433d6423SLionel Sambuc 	if (S_size) {
566433d6423SLionel Sambuc 		if (us.size == -1) {
567433d6423SLionel Sambuc 			warnx("%s: size unknown", URL);
568433d6423SLionel Sambuc 		} else if (us.size != S_size) {
569433d6423SLionel Sambuc 			warnx("%s: size mismatch: expected %jd, actual %jd",
570433d6423SLionel Sambuc 			    URL, (intmax_t)S_size, (intmax_t)us.size);
571433d6423SLionel Sambuc 			goto failure;
572433d6423SLionel Sambuc 		}
573433d6423SLionel Sambuc 	}
574433d6423SLionel Sambuc 
575433d6423SLionel Sambuc 	/* symlink instead of copy */
576433d6423SLionel Sambuc 	if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
577433d6423SLionel Sambuc 		char *name = fetchUnquotePath(url);
578433d6423SLionel Sambuc 		if (name == NULL) {
579433d6423SLionel Sambuc 			warnx("Can't unquote URL");
580433d6423SLionel Sambuc 			goto failure;
581433d6423SLionel Sambuc 		}
582433d6423SLionel Sambuc 		if (symlink(name, path) == -1) {
583433d6423SLionel Sambuc 			warn("%s: symlink()", path);
584433d6423SLionel Sambuc 			free(name);
585433d6423SLionel Sambuc 			goto failure;
586433d6423SLionel Sambuc 		}
587433d6423SLionel Sambuc 		free(name);
588433d6423SLionel Sambuc 		goto success;
589433d6423SLionel Sambuc 	}
590433d6423SLionel Sambuc 
591433d6423SLionel Sambuc 	if (us.size == -1 && !o_stdout && v_level > 0)
592433d6423SLionel Sambuc 		warnx("%s: size of remote file is not known", URL);
593433d6423SLionel Sambuc 	if (v_level > 1) {
594433d6423SLionel Sambuc 		if (sb.st_size != -1)
595433d6423SLionel Sambuc 			fprintf(stderr, "local size / mtime: %jd / %ld\n",
596433d6423SLionel Sambuc 			    (intmax_t)sb.st_size, (long)sb.st_mtime);
597433d6423SLionel Sambuc 		if (us.size != -1)
598433d6423SLionel Sambuc 			fprintf(stderr, "remote size / mtime: %jd / %ld\n",
599433d6423SLionel Sambuc 			    (intmax_t)us.size, (long)us.mtime);
600433d6423SLionel Sambuc 	}
601433d6423SLionel Sambuc 
602433d6423SLionel Sambuc 	/* open output file */
603433d6423SLionel Sambuc 	if (o_stdout) {
604433d6423SLionel Sambuc 		/* output to stdout */
605433d6423SLionel Sambuc 		of = stdout;
606433d6423SLionel Sambuc 	} else if (r_flag && sb.st_size != -1) {
607433d6423SLionel Sambuc 		/* resume mode, local file exists */
608433d6423SLionel Sambuc 		if (!F_flag && us.mtime && sb.st_mtime != us.mtime) {
609433d6423SLionel Sambuc 			/* no match! have to refetch */
610433d6423SLionel Sambuc 			fetchIO_close(f);
611433d6423SLionel Sambuc 			/* if precious, warn the user and give up */
612433d6423SLionel Sambuc 			if (R_flag) {
613433d6423SLionel Sambuc 				warnx("%s: local modification time "
614433d6423SLionel Sambuc 				    "does not match remote", path);
615433d6423SLionel Sambuc 				goto failure_keep;
616433d6423SLionel Sambuc 			}
617433d6423SLionel Sambuc 		} else if (us.size != -1) {
618433d6423SLionel Sambuc 			if (us.size == sb.st_size)
619433d6423SLionel Sambuc 				/* nothing to do */
620433d6423SLionel Sambuc 				goto success;
621433d6423SLionel Sambuc 			if (sb.st_size > us.size) {
622433d6423SLionel Sambuc 				/* local file too long! */
623433d6423SLionel Sambuc 				warnx("%s: local file (%jd bytes) is longer "
624433d6423SLionel Sambuc 				    "than remote file (%jd bytes)", path,
625433d6423SLionel Sambuc 				    (intmax_t)sb.st_size, (intmax_t)us.size);
626433d6423SLionel Sambuc 				goto failure;
627433d6423SLionel Sambuc 			}
628433d6423SLionel Sambuc 			/* we got it, open local file */
629433d6423SLionel Sambuc 			if ((of = fopen(path, "a")) == NULL) {
630433d6423SLionel Sambuc 				warn("%s: fopen()", path);
631433d6423SLionel Sambuc 				goto failure;
632433d6423SLionel Sambuc 			}
633433d6423SLionel Sambuc 			/* check that it didn't move under our feet */
634433d6423SLionel Sambuc 			if (fstat(fileno(of), &nsb) == -1) {
635433d6423SLionel Sambuc 				/* can't happen! */
636433d6423SLionel Sambuc 				warn("%s: fstat()", path);
637433d6423SLionel Sambuc 				goto failure;
638433d6423SLionel Sambuc 			}
639433d6423SLionel Sambuc 			if (nsb.st_dev != sb.st_dev ||
640433d6423SLionel Sambuc 			    nsb.st_ino != nsb.st_ino ||
641433d6423SLionel Sambuc 			    nsb.st_size != sb.st_size) {
642433d6423SLionel Sambuc 				warnx("%s: file has changed", URL);
643433d6423SLionel Sambuc 				fclose(of);
644433d6423SLionel Sambuc 				of = NULL;
645433d6423SLionel Sambuc 				sb = nsb;
646433d6423SLionel Sambuc 			}
647433d6423SLionel Sambuc 		}
648433d6423SLionel Sambuc 	} else if (m_flag && sb.st_size != -1) {
649433d6423SLionel Sambuc 		/* mirror mode, local file exists */
650433d6423SLionel Sambuc 		if (sb.st_size == us.size && sb.st_mtime == us.mtime)
651433d6423SLionel Sambuc 			goto success;
652433d6423SLionel Sambuc 	}
653433d6423SLionel Sambuc 
654433d6423SLionel Sambuc 	if (of == NULL) {
655433d6423SLionel Sambuc 		/*
656433d6423SLionel Sambuc 		 * We don't yet have an output file; either this is a
657433d6423SLionel Sambuc 		 * vanilla run with no special flags, or the local and
658433d6423SLionel Sambuc 		 * remote files didn't match.
659433d6423SLionel Sambuc 		 */
660433d6423SLionel Sambuc 
661433d6423SLionel Sambuc 		if (url->offset > 0) {
662433d6423SLionel Sambuc 			/*
663433d6423SLionel Sambuc 			 * We tried to restart a transfer, but for
664433d6423SLionel Sambuc 			 * some reason gave up - so we have to restart
665433d6423SLionel Sambuc 			 * from scratch if we want the whole file
666433d6423SLionel Sambuc 			 */
667433d6423SLionel Sambuc 			url->offset = 0;
668433d6423SLionel Sambuc 			if ((f = fetchXGet(url, &us, flags)) == NULL) {
669433d6423SLionel Sambuc 				warnx("%s: %s", URL, fetchLastErrString);
670433d6423SLionel Sambuc 				goto failure;
671433d6423SLionel Sambuc 			}
672433d6423SLionel Sambuc 			if (sigint)
673433d6423SLionel Sambuc 				goto signal;
674433d6423SLionel Sambuc 		}
675433d6423SLionel Sambuc 
676433d6423SLionel Sambuc 		/* construct a temp file name */
677433d6423SLionel Sambuc 		if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
678433d6423SLionel Sambuc #ifndef __minix
679433d6423SLionel Sambuc 			asprintf(&tmppath, "%s.fetch.XXXXXX", path);
680433d6423SLionel Sambuc #else
681433d6423SLionel Sambuc 			{
682433d6423SLionel Sambuc 				int len;
683433d6423SLionel Sambuc 				if((tmppath = malloc(sizeof(char)*MINBUFSIZE)) != NULL) {
684433d6423SLionel Sambuc 					len = snprintf(tmppath, MINBUFSIZE, "%s.fetch.XXXXXX", path);
685433d6423SLionel Sambuc 					if(len >= MINBUFSIZE) {
686433d6423SLionel Sambuc 						free(tmppath);
687433d6423SLionel Sambuc 						tmppath = NULL;
688433d6423SLionel Sambuc 					}
689433d6423SLionel Sambuc 				}
690433d6423SLionel Sambuc 			}
691433d6423SLionel Sambuc #endif
692433d6423SLionel Sambuc 
693433d6423SLionel Sambuc 			if (tmppath != NULL) {
694433d6423SLionel Sambuc 				int fd;
695433d6423SLionel Sambuc 
696433d6423SLionel Sambuc 				fd = mkstemp(tmppath);
697433d6423SLionel Sambuc 				if (fd == -1) {
698433d6423SLionel Sambuc 					warn("%s: mkstemp failed", tmppath);
699433d6423SLionel Sambuc 					goto failure;
700433d6423SLionel Sambuc 				}
701433d6423SLionel Sambuc 				fchown(fd, sb.st_uid, sb.st_gid);
702433d6423SLionel Sambuc 				fchmod(fd, sb.st_mode & ALLPERMS);
703433d6423SLionel Sambuc 				of = fdopen(fd, "w");
704433d6423SLionel Sambuc 				if (of == NULL) {
705433d6423SLionel Sambuc 					close(fd);
706433d6423SLionel Sambuc 					unlink(tmppath);
707433d6423SLionel Sambuc 					free(tmppath);
708433d6423SLionel Sambuc 					tmppath = NULL;
709433d6423SLionel Sambuc 				}
710433d6423SLionel Sambuc 			}
711433d6423SLionel Sambuc 		}
712433d6423SLionel Sambuc 		if (of == NULL)
713433d6423SLionel Sambuc 			of = fopen(path, "w");
714433d6423SLionel Sambuc 		if (of == NULL) {
715433d6423SLionel Sambuc 			warn("%s: open()", path);
716433d6423SLionel Sambuc 			goto failure;
717433d6423SLionel Sambuc 		}
718433d6423SLionel Sambuc 	}
719433d6423SLionel Sambuc 	count = url->offset;
720433d6423SLionel Sambuc 
721433d6423SLionel Sambuc 	/* start the counter */
722433d6423SLionel Sambuc 	stat_start(&xs, path, us.size, count);
723433d6423SLionel Sambuc 
724433d6423SLionel Sambuc 	sigalrm = sigint = 0;
725433d6423SLionel Sambuc 
726433d6423SLionel Sambuc 	/* suck in the data */
727433d6423SLionel Sambuc #ifdef SIGINFO
728433d6423SLionel Sambuc 	siginfo = 0;
729433d6423SLionel Sambuc 	signal(SIGINFO, sig_handler);
730433d6423SLionel Sambuc #endif
731433d6423SLionel Sambuc 	while (!sigint) {
732433d6423SLionel Sambuc 		if (us.size != -1 && us.size - count < B_size &&
733433d6423SLionel Sambuc 		    us.size - count >= 0)
734433d6423SLionel Sambuc 			size = us.size - count;
735433d6423SLionel Sambuc 		else
736433d6423SLionel Sambuc 			size = B_size;
737433d6423SLionel Sambuc #ifdef SIGINFO
738433d6423SLionel Sambuc 		if (siginfo) {
739433d6423SLionel Sambuc 			stat_display(&xs, 1);
740433d6423SLionel Sambuc 			siginfo = 0;
741433d6423SLionel Sambuc 		}
742433d6423SLionel Sambuc #else
743433d6423SLionel Sambuc 		/* Constant info is better than none. */
744433d6423SLionel Sambuc 		if (v_level) {
745433d6423SLionel Sambuc 			stat_display(&xs, 1);
746433d6423SLionel Sambuc 		}
747433d6423SLionel Sambuc #endif
748433d6423SLionel Sambuc 		if ((ssize = fetchIO_read(f, buf, B_size)) == 0)
749433d6423SLionel Sambuc 			break;
750433d6423SLionel Sambuc 		if (ssize == -1 && errno == EINTR)
751433d6423SLionel Sambuc 			continue;
752433d6423SLionel Sambuc 		if (ssize == -1)
753433d6423SLionel Sambuc 			break;
754433d6423SLionel Sambuc 		size = ssize;
755433d6423SLionel Sambuc 		stat_update(&xs, count += size);
756433d6423SLionel Sambuc 		for (ptr = buf; size > 0; ptr += wr, size -= wr) {
757433d6423SLionel Sambuc 			if ((wr = fwrite(ptr, 1, size, of)) < size) {
758433d6423SLionel Sambuc 				if (ferror(of) && errno == EINTR && !sigint)
759433d6423SLionel Sambuc 					clearerr(of);
760433d6423SLionel Sambuc 				else
761433d6423SLionel Sambuc 					break;
762433d6423SLionel Sambuc 			}
763433d6423SLionel Sambuc 		}
764433d6423SLionel Sambuc 		if (size != 0)
765433d6423SLionel Sambuc 			break;
766433d6423SLionel Sambuc 	}
767433d6423SLionel Sambuc 	if (!sigalrm)
768433d6423SLionel Sambuc 		sigalrm = 0;
769433d6423SLionel Sambuc #ifdef SIGINFO
770433d6423SLionel Sambuc 	signal(SIGINFO, SIG_DFL);
771433d6423SLionel Sambuc #endif
772433d6423SLionel Sambuc 
773433d6423SLionel Sambuc 	stat_end(&xs);
774433d6423SLionel Sambuc 
775433d6423SLionel Sambuc 	/*
776433d6423SLionel Sambuc 	 * If the transfer timed out or was interrupted, we still want to
777433d6423SLionel Sambuc 	 * set the mtime in case the file is not removed (-r or -R) and
778433d6423SLionel Sambuc 	 * the user later restarts the transfer.
779433d6423SLionel Sambuc 	 */
780433d6423SLionel Sambuc  signal:
781433d6423SLionel Sambuc 	/* set mtime of local file */
782433d6423SLionel Sambuc 	if (!n_flag && us.mtime && !o_stdout && of != NULL &&
783433d6423SLionel Sambuc 	    (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
784433d6423SLionel Sambuc 		struct timeval tv[2];
785433d6423SLionel Sambuc 
786433d6423SLionel Sambuc 		fflush(of);
787433d6423SLionel Sambuc 		tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
788433d6423SLionel Sambuc 		tv[1].tv_sec = (long)us.mtime;
789433d6423SLionel Sambuc 		tv[0].tv_usec = tv[1].tv_usec = 0;
790433d6423SLionel Sambuc 		if (utimes(tmppath ? tmppath : path, tv))
791433d6423SLionel Sambuc 			warn("%s: utimes()", tmppath ? tmppath : path);
792433d6423SLionel Sambuc 	}
793433d6423SLionel Sambuc 
794433d6423SLionel Sambuc 	/* timed out or interrupted? */
795433d6423SLionel Sambuc 	if (fetchLastErrCode == FETCH_TIMEOUT)
796433d6423SLionel Sambuc 		sigalrm = 1;
797433d6423SLionel Sambuc 	if (sigalrm)
798433d6423SLionel Sambuc 		warnx("transfer timed out");
799433d6423SLionel Sambuc 	if (sigint) {
800433d6423SLionel Sambuc 		warnx("transfer interrupted");
801433d6423SLionel Sambuc 		goto failure;
802433d6423SLionel Sambuc 	}
803433d6423SLionel Sambuc 
804433d6423SLionel Sambuc 	/* timeout / interrupt before connection completley established? */
805433d6423SLionel Sambuc 	if (f == NULL)
806433d6423SLionel Sambuc 		goto failure;
807433d6423SLionel Sambuc 
808433d6423SLionel Sambuc 	if (!sigalrm && ferror(of)) {
809433d6423SLionel Sambuc 		/* check the status of our files */
810433d6423SLionel Sambuc 		warn("writing to %s failed", path);
811433d6423SLionel Sambuc 		goto failure;
812433d6423SLionel Sambuc 	}
813433d6423SLionel Sambuc 
814433d6423SLionel Sambuc 	/* did the transfer complete normally? */
815433d6423SLionel Sambuc 	if (us.size != -1 && count < us.size) {
816433d6423SLionel Sambuc 		warnx("%s appears to be truncated: %jd/%jd bytes",
817433d6423SLionel Sambuc 		    path, (intmax_t)count, (intmax_t)us.size);
818433d6423SLionel Sambuc 		goto failure_keep;
819433d6423SLionel Sambuc 	}
820433d6423SLionel Sambuc 
821433d6423SLionel Sambuc 	/*
822433d6423SLionel Sambuc 	 * If the transfer timed out and we didn't know how much to
823433d6423SLionel Sambuc 	 * expect, assume the worst (i.e. we didn't get all of it)
824433d6423SLionel Sambuc 	 */
825433d6423SLionel Sambuc 	if (sigalrm && us.size == -1) {
826433d6423SLionel Sambuc 		warnx("%s may be truncated", path);
827433d6423SLionel Sambuc 		goto failure_keep;
828433d6423SLionel Sambuc 	}
829433d6423SLionel Sambuc 
830433d6423SLionel Sambuc  success:
831433d6423SLionel Sambuc 	r = 0;
832433d6423SLionel Sambuc 	if (tmppath != NULL && rename(tmppath, path) == -1) {
833433d6423SLionel Sambuc 		warn("%s: rename()", path);
834433d6423SLionel Sambuc 		goto failure_keep;
835433d6423SLionel Sambuc 	}
836433d6423SLionel Sambuc 	goto done;
837433d6423SLionel Sambuc  failure:
838433d6423SLionel Sambuc 	if (of && of != stdout && !R_flag && !r_flag)
839433d6423SLionel Sambuc 		if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
840433d6423SLionel Sambuc 			unlink(tmppath ? tmppath : path);
841433d6423SLionel Sambuc 	if (R_flag && tmppath != NULL && sb.st_size == -1)
842433d6423SLionel Sambuc 		rename(tmppath, path); /* ignore errors here */
843433d6423SLionel Sambuc  failure_keep:
844433d6423SLionel Sambuc 	r = -1;
845433d6423SLionel Sambuc 	goto done;
846433d6423SLionel Sambuc  done:
847433d6423SLionel Sambuc 	if (f)
848433d6423SLionel Sambuc 		fetchIO_close(f);
849433d6423SLionel Sambuc 	if (of && of != stdout)
850433d6423SLionel Sambuc 		fclose(of);
851433d6423SLionel Sambuc 	if (url)
852433d6423SLionel Sambuc 		fetchFreeURL(url);
853433d6423SLionel Sambuc 	if (tmppath != NULL)
854433d6423SLionel Sambuc 		free(tmppath);
855433d6423SLionel Sambuc 	return (r);
856433d6423SLionel Sambuc }
857433d6423SLionel Sambuc 
858433d6423SLionel Sambuc static void
usage(void)859433d6423SLionel Sambuc usage(void)
860433d6423SLionel Sambuc {
861433d6423SLionel Sambuc #ifndef __minix
862433d6423SLionel Sambuc 	fprintf(stderr, "%s\n%s\n%s\n",
863433d6423SLionel Sambuc 	    "usage: fetch [-146AFMPRUadilmnpqrsv] [-N netrc] [-o outputfile]",
864433d6423SLionel Sambuc 	    "             [-S bytes] [-B bytes] [-T seconds] [-w seconds]",
865433d6423SLionel Sambuc 	    "             [-h host -f file [-c dir] | URL ...]");
866433d6423SLionel Sambuc #else
867433d6423SLionel Sambuc 	fprintf(stderr, "%s\n%s\n%s\n",
868433d6423SLionel Sambuc 	    "usage: fetch [-146AFMPRUadilmnpqrsv] [-N netrc] [-o outputfile]",
869433d6423SLionel Sambuc 	    "             [-S bytes] [-B bytes] [-T seconds] [-w seconds]",
870433d6423SLionel Sambuc 	    "             [-h host -f file [-c dir] | URL ...]");
871433d6423SLionel Sambuc #endif
872433d6423SLionel Sambuc }
873433d6423SLionel Sambuc 
874433d6423SLionel Sambuc 
875433d6423SLionel Sambuc /*
876433d6423SLionel Sambuc  * Entry point
877433d6423SLionel Sambuc  */
878433d6423SLionel Sambuc int
main(int argc,char * argv[])879433d6423SLionel Sambuc main(int argc, char *argv[])
880433d6423SLionel Sambuc {
881433d6423SLionel Sambuc 	struct stat sb;
882433d6423SLionel Sambuc 	struct sigaction sa;
883433d6423SLionel Sambuc 	const char *p, *s;
884433d6423SLionel Sambuc 	char *end, *q;
885433d6423SLionel Sambuc 	int c, e, r;
886433d6423SLionel Sambuc #ifndef __minix
887433d6423SLionel Sambuc 	while ((c = getopt(argc, argv,
888433d6423SLionel Sambuc 	    "14AaB:dFilMmN:no:qRrS:sT:Uvw:")) != -1)
889433d6423SLionel Sambuc #else
890433d6423SLionel Sambuc 	while ((c = getopt(argc, argv,
891433d6423SLionel Sambuc 	    "146AaB:dFilMmN:no:qRrS:sT:Uvw:")) != -1)
892433d6423SLionel Sambuc #endif
893433d6423SLionel Sambuc 		switch (c) {
894433d6423SLionel Sambuc 		case '1':
895433d6423SLionel Sambuc 			once_flag = 1;
896433d6423SLionel Sambuc 			break;
897433d6423SLionel Sambuc 		case '4':
898433d6423SLionel Sambuc 			family = PF_INET;
899433d6423SLionel Sambuc 			break;
900433d6423SLionel Sambuc #ifndef __minix
901433d6423SLionel Sambuc 		case '6':
902433d6423SLionel Sambuc 			family = PF_INET6;
903433d6423SLionel Sambuc 			break;
904433d6423SLionel Sambuc #endif
905433d6423SLionel Sambuc 		case 'A':
906433d6423SLionel Sambuc 			A_flag = 1;
907433d6423SLionel Sambuc 			break;
908433d6423SLionel Sambuc 		case 'a':
909433d6423SLionel Sambuc 			a_flag = 1;
910433d6423SLionel Sambuc 			break;
911433d6423SLionel Sambuc 		case 'B':
912433d6423SLionel Sambuc 			B_size = (off_t)strtol(optarg, &end, 10);
913433d6423SLionel Sambuc 			if (*optarg == '\0' || *end != '\0')
914433d6423SLionel Sambuc 				errx(1, "invalid buffer size (%s)", optarg);
915433d6423SLionel Sambuc 			break;
916433d6423SLionel Sambuc 		case 'd':
917433d6423SLionel Sambuc 			d_flag = 1;
918433d6423SLionel Sambuc 			break;
919433d6423SLionel Sambuc 		case 'F':
920433d6423SLionel Sambuc 			F_flag = 1;
921433d6423SLionel Sambuc 			break;
922433d6423SLionel Sambuc 		case 'i':
923433d6423SLionel Sambuc 			i_flag = 1;
924433d6423SLionel Sambuc 			break;
925433d6423SLionel Sambuc 		case 'l':
926433d6423SLionel Sambuc 			l_flag = 1;
927433d6423SLionel Sambuc 			break;
928433d6423SLionel Sambuc 		case 'o':
929433d6423SLionel Sambuc 			o_flag = 1;
930433d6423SLionel Sambuc 			o_filename = optarg;
931433d6423SLionel Sambuc 			break;
932433d6423SLionel Sambuc 		case 'M':
933433d6423SLionel Sambuc 		case 'm':
934433d6423SLionel Sambuc 			if (r_flag)
935433d6423SLionel Sambuc 				errx(1, "the -m and -r flags "
936433d6423SLionel Sambuc 				    "are mutually exclusive");
937433d6423SLionel Sambuc 			m_flag = 1;
938433d6423SLionel Sambuc 			break;
939433d6423SLionel Sambuc 		case 'N':
940433d6423SLionel Sambuc 			N_filename = optarg;
941433d6423SLionel Sambuc 			break;
942433d6423SLionel Sambuc 		case 'n':
943433d6423SLionel Sambuc 			n_flag = 1;
944433d6423SLionel Sambuc 			break;
945433d6423SLionel Sambuc 		case 'q':
946433d6423SLionel Sambuc 			v_level = 0;
947433d6423SLionel Sambuc 			break;
948433d6423SLionel Sambuc 		case 'R':
949433d6423SLionel Sambuc 			R_flag = 1;
950433d6423SLionel Sambuc 			break;
951433d6423SLionel Sambuc 		case 'r':
952433d6423SLionel Sambuc 			if (m_flag)
953433d6423SLionel Sambuc 				errx(1, "the -m and -r flags "
954433d6423SLionel Sambuc 				    "are mutually exclusive");
955433d6423SLionel Sambuc 			r_flag = 1;
956433d6423SLionel Sambuc 			break;
957433d6423SLionel Sambuc 		case 'S':
958433d6423SLionel Sambuc 			S_size = (off_t)strtol(optarg, &end, 10);
959433d6423SLionel Sambuc 			if (*optarg == '\0' || *end != '\0')
960433d6423SLionel Sambuc 				errx(1, "invalid size (%s)", optarg);
961433d6423SLionel Sambuc 			break;
962433d6423SLionel Sambuc 		case 's':
963433d6423SLionel Sambuc 			s_flag = 1;
964433d6423SLionel Sambuc 			break;
965433d6423SLionel Sambuc 		case 'T':
966433d6423SLionel Sambuc 			T_secs = strtol(optarg, &end, 10);
967433d6423SLionel Sambuc 			if (*optarg == '\0' || *end != '\0')
968433d6423SLionel Sambuc 				errx(1, "invalid timeout (%s)", optarg);
969433d6423SLionel Sambuc 			break;
970433d6423SLionel Sambuc 		case 'U':
971433d6423SLionel Sambuc 			U_flag = 1;
972433d6423SLionel Sambuc 			break;
973433d6423SLionel Sambuc 		case 'v':
974433d6423SLionel Sambuc 			v_level++;
975433d6423SLionel Sambuc 			break;
976433d6423SLionel Sambuc 		case 'w':
977433d6423SLionel Sambuc 			a_flag = 1;
978433d6423SLionel Sambuc 			w_secs = strtol(optarg, &end, 10);
979433d6423SLionel Sambuc 			if (*optarg == '\0' || *end != '\0')
980433d6423SLionel Sambuc 				errx(1, "invalid delay (%s)", optarg);
981433d6423SLionel Sambuc 			break;
982433d6423SLionel Sambuc 		default:
983433d6423SLionel Sambuc 			usage();
984433d6423SLionel Sambuc 			exit(EX_USAGE);
985433d6423SLionel Sambuc 		}
986433d6423SLionel Sambuc 
987433d6423SLionel Sambuc 	argc -= optind;
988433d6423SLionel Sambuc 	argv += optind;
989433d6423SLionel Sambuc 
990433d6423SLionel Sambuc 	if (!argc) {
991433d6423SLionel Sambuc 		usage();
992433d6423SLionel Sambuc 		exit(EX_USAGE);
993433d6423SLionel Sambuc 	}
994433d6423SLionel Sambuc 
995433d6423SLionel Sambuc 	fetchConnectionCacheInit(10, 1);
996433d6423SLionel Sambuc 
997433d6423SLionel Sambuc 	/* allocate buffer */
998433d6423SLionel Sambuc 	if (B_size < MINBUFSIZE)
999433d6423SLionel Sambuc 		B_size = MINBUFSIZE;
1000433d6423SLionel Sambuc 	if ((buf = malloc(B_size)) == NULL)
1001433d6423SLionel Sambuc 		errx(1, "%s", strerror(ENOMEM));
1002433d6423SLionel Sambuc 
1003433d6423SLionel Sambuc 	/* timeouts */
1004433d6423SLionel Sambuc 	if ((s = getenv("FTP_TIMEOUT")) != NULL) {
1005433d6423SLionel Sambuc 		ftp_timeout = strtol(s, &end, 10);
1006433d6423SLionel Sambuc 		if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
1007433d6423SLionel Sambuc 			warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
1008433d6423SLionel Sambuc 			ftp_timeout = 0;
1009433d6423SLionel Sambuc 		}
1010433d6423SLionel Sambuc 	}
1011433d6423SLionel Sambuc 	if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
1012433d6423SLionel Sambuc 		http_timeout = strtol(s, &end, 10);
1013433d6423SLionel Sambuc 		if (*s == '\0' || *end != '\0' || http_timeout < 0) {
1014433d6423SLionel Sambuc 			warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
1015433d6423SLionel Sambuc 			http_timeout = 0;
1016433d6423SLionel Sambuc 		}
1017433d6423SLionel Sambuc 	}
1018433d6423SLionel Sambuc 
1019433d6423SLionel Sambuc 	/* signal handling */
1020433d6423SLionel Sambuc 	sa.sa_flags = 0;
1021433d6423SLionel Sambuc 	sa.sa_handler = sig_handler;
1022433d6423SLionel Sambuc 	sigemptyset(&sa.sa_mask);
1023433d6423SLionel Sambuc 	sigaction(SIGALRM, &sa, NULL);
1024433d6423SLionel Sambuc 	sa.sa_flags = SA_RESETHAND;
1025433d6423SLionel Sambuc 	sigaction(SIGINT, &sa, NULL);
1026433d6423SLionel Sambuc 
1027433d6423SLionel Sambuc 	/* output file */
1028433d6423SLionel Sambuc 	if (o_flag) {
1029433d6423SLionel Sambuc 		if (strcmp(o_filename, "-") == 0) {
1030433d6423SLionel Sambuc 			o_stdout = 1;
1031433d6423SLionel Sambuc 			if (i_flag) {
1032433d6423SLionel Sambuc 				warnx("-i and -o - are incompatible, dropping -i");
1033433d6423SLionel Sambuc 				i_flag = 0;
1034433d6423SLionel Sambuc 			}
1035433d6423SLionel Sambuc 		} else if (stat(o_filename, &sb) == -1) {
1036433d6423SLionel Sambuc 			if (errno == ENOENT) {
1037433d6423SLionel Sambuc 				if (argc > 1)
1038433d6423SLionel Sambuc 					errx(EX_USAGE, "%s is not a directory",
1039433d6423SLionel Sambuc 					    o_filename);
1040433d6423SLionel Sambuc 			} else {
1041433d6423SLionel Sambuc 				err(EX_IOERR, "%s", o_filename);
1042433d6423SLionel Sambuc 			}
1043433d6423SLionel Sambuc 		} else {
1044433d6423SLionel Sambuc 			if (sb.st_mode & S_IFDIR)
1045433d6423SLionel Sambuc 				o_directory = 1;
1046433d6423SLionel Sambuc 		}
1047433d6423SLionel Sambuc 	}
1048433d6423SLionel Sambuc 
1049433d6423SLionel Sambuc 	/* check if output is to a tty (for progress report) */
1050433d6423SLionel Sambuc 	v_tty = isatty(STDERR_FILENO);
1051433d6423SLionel Sambuc 	if (v_tty)
1052433d6423SLionel Sambuc 		pgrp = getpgrp();
1053433d6423SLionel Sambuc 
1054433d6423SLionel Sambuc 	r = 0;
1055433d6423SLionel Sambuc 
1056433d6423SLionel Sambuc 	/* authentication */
1057433d6423SLionel Sambuc 	if (v_tty)
1058433d6423SLionel Sambuc 		fetchAuthMethod = query_auth;
1059433d6423SLionel Sambuc 	if (N_filename != NULL)
1060433d6423SLionel Sambuc 		setenv("NETRC", N_filename, 1);
1061433d6423SLionel Sambuc 
1062433d6423SLionel Sambuc 	while (argc) {
1063433d6423SLionel Sambuc 		if ((p = strrchr(*argv, '/')) == NULL)
1064433d6423SLionel Sambuc 			p = *argv;
1065433d6423SLionel Sambuc 		else
1066433d6423SLionel Sambuc 			p++;
1067433d6423SLionel Sambuc 
1068433d6423SLionel Sambuc 		if (!*p)
1069433d6423SLionel Sambuc 			p = "fetch.out";
1070433d6423SLionel Sambuc 
1071433d6423SLionel Sambuc 		fetchLastErrCode = 0;
1072433d6423SLionel Sambuc 
1073433d6423SLionel Sambuc 		if (o_flag) {
1074433d6423SLionel Sambuc 			if (o_stdout) {
1075433d6423SLionel Sambuc 				e = fetch(*argv, "-");
1076433d6423SLionel Sambuc 			} else if (o_directory) {
1077433d6423SLionel Sambuc #ifndef __minix
1078433d6423SLionel Sambuc 				asprintf(&q, "%s/%s", o_filename, p);
1079433d6423SLionel Sambuc #else
1080433d6423SLionel Sambuc 				{
1081433d6423SLionel Sambuc 					int len;
1082433d6423SLionel Sambuc 
1083433d6423SLionel Sambuc 					if ((q = malloc(sizeof(char)*MINBUFSIZE)) != NULL) {
1084433d6423SLionel Sambuc 						len = snprintf(q, MINBUFSIZE, "%s/%s", o_filename, p);
1085433d6423SLionel Sambuc 						if (len >= MINBUFSIZE) {
1086433d6423SLionel Sambuc 							free(q);
1087433d6423SLionel Sambuc 							q = NULL;
1088433d6423SLionel Sambuc 						}
1089433d6423SLionel Sambuc 					}else{
1090433d6423SLionel Sambuc 						err(1, "Unable to allocate memory");
1091433d6423SLionel Sambuc 					}
1092433d6423SLionel Sambuc 				}
1093433d6423SLionel Sambuc #endif
1094433d6423SLionel Sambuc 				e = fetch(*argv, q);
1095433d6423SLionel Sambuc 				free(q);
1096433d6423SLionel Sambuc 			} else {
1097433d6423SLionel Sambuc 				e = fetch(*argv, o_filename);
1098433d6423SLionel Sambuc 			}
1099433d6423SLionel Sambuc 		} else {
1100433d6423SLionel Sambuc 			e = fetch(*argv, p);
1101433d6423SLionel Sambuc 		}
1102433d6423SLionel Sambuc 
1103433d6423SLionel Sambuc 		if (sigint)
1104433d6423SLionel Sambuc 			kill(getpid(), SIGINT);
1105433d6423SLionel Sambuc 
1106433d6423SLionel Sambuc 		if (e == 0 && once_flag)
1107433d6423SLionel Sambuc 			exit(0);
1108433d6423SLionel Sambuc 
1109433d6423SLionel Sambuc 		if (e) {
1110433d6423SLionel Sambuc 			r = 1;
1111433d6423SLionel Sambuc 			if ((fetchLastErrCode
1112433d6423SLionel Sambuc 			    && fetchLastErrCode != FETCH_UNAVAIL
1113433d6423SLionel Sambuc 			    && fetchLastErrCode != FETCH_MOVED
1114433d6423SLionel Sambuc 			    && fetchLastErrCode != FETCH_URL
1115433d6423SLionel Sambuc 			    && fetchLastErrCode != FETCH_RESOLV
1116433d6423SLionel Sambuc 			    && fetchLastErrCode != FETCH_UNKNOWN)) {
1117433d6423SLionel Sambuc 				if (w_secs && v_level)
1118433d6423SLionel Sambuc 					fprintf(stderr, "Waiting %ld seconds "
1119433d6423SLionel Sambuc 					    "before retrying\n", w_secs);
1120433d6423SLionel Sambuc 				if (w_secs)
1121433d6423SLionel Sambuc 					sleep(w_secs);
1122433d6423SLionel Sambuc 				if (a_flag)
1123433d6423SLionel Sambuc 					continue;
1124433d6423SLionel Sambuc 			}
1125433d6423SLionel Sambuc 		}
1126433d6423SLionel Sambuc 
1127433d6423SLionel Sambuc 		argc--, argv++;
1128433d6423SLionel Sambuc 	}
1129433d6423SLionel Sambuc 
1130433d6423SLionel Sambuc 	exit(r);
1131433d6423SLionel Sambuc }
1132