util.c revision 1.51 1 1.51 christos /* $NetBSD: util.c,v 1.51 1999/06/24 23:21:02 christos Exp $ */
2 1.27 thorpej
3 1.27 thorpej /*-
4 1.41 lukem * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 1.27 thorpej * All rights reserved.
6 1.27 thorpej *
7 1.27 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.27 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.27 thorpej * NASA Ames Research Center.
10 1.27 thorpej *
11 1.41 lukem * This code is derived from software contributed to The NetBSD Foundation
12 1.41 lukem * by Luke Mewburn.
13 1.41 lukem *
14 1.27 thorpej * Redistribution and use in source and binary forms, with or without
15 1.27 thorpej * modification, are permitted provided that the following conditions
16 1.27 thorpej * are met:
17 1.27 thorpej * 1. Redistributions of source code must retain the above copyright
18 1.27 thorpej * notice, this list of conditions and the following disclaimer.
19 1.27 thorpej * 2. Redistributions in binary form must reproduce the above copyright
20 1.27 thorpej * notice, this list of conditions and the following disclaimer in the
21 1.27 thorpej * documentation and/or other materials provided with the distribution.
22 1.27 thorpej * 3. All advertising materials mentioning features or use of this software
23 1.27 thorpej * must display the following acknowledgement:
24 1.27 thorpej * This product includes software developed by the NetBSD
25 1.27 thorpej * Foundation, Inc. and its contributors.
26 1.27 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
27 1.27 thorpej * contributors may be used to endorse or promote products derived
28 1.27 thorpej * from this software without specific prior written permission.
29 1.27 thorpej *
30 1.27 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 1.27 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 1.27 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 1.27 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 1.27 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 1.27 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 1.27 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 1.27 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 1.27 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.27 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.27 thorpej * POSSIBILITY OF SUCH DAMAGE.
41 1.27 thorpej */
42 1.1 lukem
43 1.1 lukem /*
44 1.1 lukem * Copyright (c) 1985, 1989, 1993, 1994
45 1.1 lukem * The Regents of the University of California. All rights reserved.
46 1.1 lukem *
47 1.1 lukem * Redistribution and use in source and binary forms, with or without
48 1.1 lukem * modification, are permitted provided that the following conditions
49 1.1 lukem * are met:
50 1.1 lukem * 1. Redistributions of source code must retain the above copyright
51 1.1 lukem * notice, this list of conditions and the following disclaimer.
52 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 lukem * notice, this list of conditions and the following disclaimer in the
54 1.1 lukem * documentation and/or other materials provided with the distribution.
55 1.1 lukem * 3. All advertising materials mentioning features or use of this software
56 1.1 lukem * must display the following acknowledgement:
57 1.1 lukem * This product includes software developed by the University of
58 1.1 lukem * California, Berkeley and its contributors.
59 1.1 lukem * 4. Neither the name of the University nor the names of its contributors
60 1.1 lukem * may be used to endorse or promote products derived from this software
61 1.1 lukem * without specific prior written permission.
62 1.1 lukem *
63 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 lukem * SUCH DAMAGE.
74 1.1 lukem */
75 1.1 lukem
76 1.10 lukem #include <sys/cdefs.h>
77 1.1 lukem #ifndef lint
78 1.51 christos __RCSID("$NetBSD: util.c,v 1.51 1999/06/24 23:21:02 christos Exp $");
79 1.1 lukem #endif /* not lint */
80 1.1 lukem
81 1.1 lukem /*
82 1.1 lukem * FTP User Program -- Misc support routines
83 1.1 lukem */
84 1.27 thorpej #include <sys/types.h>
85 1.27 thorpej #include <sys/socket.h>
86 1.1 lukem #include <sys/ioctl.h>
87 1.1 lukem #include <sys/time.h>
88 1.1 lukem #include <arpa/ftp.h>
89 1.1 lukem
90 1.1 lukem #include <ctype.h>
91 1.1 lukem #include <err.h>
92 1.1 lukem #include <fcntl.h>
93 1.1 lukem #include <glob.h>
94 1.23 christos #include <termios.h>
95 1.23 christos #include <signal.h>
96 1.10 lukem #include <limits.h>
97 1.7 lukem #include <pwd.h>
98 1.1 lukem #include <stdio.h>
99 1.10 lukem #include <stdlib.h>
100 1.1 lukem #include <string.h>
101 1.1 lukem #include <time.h>
102 1.17 lukem #include <tzfile.h>
103 1.1 lukem #include <unistd.h>
104 1.1 lukem
105 1.1 lukem #include "ftp_var.h"
106 1.1 lukem #include "pathnames.h"
107 1.1 lukem
108 1.36 itohy #ifndef HAVE_TIMEGM
109 1.36 itohy static time_t sub_mkgmt __P((struct tm *tm));
110 1.36 itohy #endif
111 1.36 itohy
112 1.1 lukem /*
113 1.1 lukem * Connect to peer server and
114 1.1 lukem * auto-login, if possible.
115 1.1 lukem */
116 1.1 lukem void
117 1.1 lukem setpeer(argc, argv)
118 1.1 lukem int argc;
119 1.1 lukem char *argv[];
120 1.1 lukem {
121 1.1 lukem char *host;
122 1.19 lukem in_port_t port;
123 1.1 lukem
124 1.1 lukem if (connected) {
125 1.24 lukem fprintf(ttyout, "Already connected to %s, use close first.\n",
126 1.5 lukem hostname);
127 1.1 lukem code = -1;
128 1.1 lukem return;
129 1.1 lukem }
130 1.1 lukem if (argc < 2)
131 1.5 lukem (void)another(&argc, &argv, "to");
132 1.1 lukem if (argc < 2 || argc > 3) {
133 1.24 lukem fprintf(ttyout, "usage: %s host-name [port]\n", argv[0]);
134 1.1 lukem code = -1;
135 1.1 lukem return;
136 1.1 lukem }
137 1.12 lukem if (gatemode)
138 1.12 lukem port = gateport;
139 1.12 lukem else
140 1.12 lukem port = ftpport;
141 1.1 lukem if (argc > 2) {
142 1.10 lukem char *ep;
143 1.10 lukem long nport;
144 1.10 lukem
145 1.10 lukem nport = strtol(argv[2], &ep, 10);
146 1.20 lukem if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
147 1.24 lukem fprintf(ttyout, "%s: bad port number '%s'.\n",
148 1.24 lukem argv[1], argv[2]);
149 1.24 lukem fprintf(ttyout, "usage: %s host-name [port]\n",
150 1.24 lukem argv[0]);
151 1.1 lukem code = -1;
152 1.1 lukem return;
153 1.1 lukem }
154 1.19 lukem port = htons((in_port_t)nport);
155 1.1 lukem }
156 1.12 lukem
157 1.12 lukem if (gatemode) {
158 1.12 lukem if (gateserver == NULL || *gateserver == '\0')
159 1.12 lukem errx(1, "gateserver not defined (shouldn't happen)");
160 1.12 lukem host = hookup(gateserver, port);
161 1.12 lukem } else
162 1.12 lukem host = hookup(argv[1], port);
163 1.12 lukem
164 1.1 lukem if (host) {
165 1.1 lukem int overbose;
166 1.1 lukem
167 1.49 lukem if (gatemode && verbose) {
168 1.49 lukem fprintf(ttyout,
169 1.49 lukem "Connecting via pass-through server %s\n",
170 1.49 lukem gateserver);
171 1.12 lukem }
172 1.12 lukem
173 1.1 lukem connected = 1;
174 1.1 lukem /*
175 1.1 lukem * Set up defaults for FTP.
176 1.1 lukem */
177 1.5 lukem (void)strcpy(typename, "ascii"), type = TYPE_A;
178 1.1 lukem curtype = TYPE_A;
179 1.5 lukem (void)strcpy(formname, "non-print"), form = FORM_N;
180 1.5 lukem (void)strcpy(modename, "stream"), mode = MODE_S;
181 1.5 lukem (void)strcpy(structname, "file"), stru = STRU_F;
182 1.5 lukem (void)strcpy(bytename, "8"), bytesize = 8;
183 1.1 lukem if (autologin)
184 1.28 lukem (void)ftp_login(argv[1], NULL, NULL);
185 1.1 lukem
186 1.1 lukem overbose = verbose;
187 1.1 lukem if (debug == 0)
188 1.1 lukem verbose = -1;
189 1.1 lukem if (command("SYST") == COMPLETE && overbose) {
190 1.1 lukem char *cp, c;
191 1.1 lukem c = 0;
192 1.24 lukem cp = strchr(reply_string + 4, ' ');
193 1.1 lukem if (cp == NULL)
194 1.24 lukem cp = strchr(reply_string + 4, '\r');
195 1.1 lukem if (cp) {
196 1.1 lukem if (cp[-1] == '.')
197 1.1 lukem cp--;
198 1.1 lukem c = *cp;
199 1.1 lukem *cp = '\0';
200 1.1 lukem }
201 1.1 lukem
202 1.24 lukem fprintf(ttyout, "Remote system type is %s.\n",
203 1.24 lukem reply_string + 4);
204 1.1 lukem if (cp)
205 1.1 lukem *cp = c;
206 1.1 lukem }
207 1.1 lukem if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
208 1.1 lukem if (proxy)
209 1.1 lukem unix_proxy = 1;
210 1.1 lukem else
211 1.1 lukem unix_server = 1;
212 1.1 lukem /*
213 1.1 lukem * Set type to 0 (not specified by user),
214 1.1 lukem * meaning binary by default, but don't bother
215 1.1 lukem * telling server. We can use binary
216 1.1 lukem * for text files unless changed by the user.
217 1.1 lukem */
218 1.1 lukem type = 0;
219 1.5 lukem (void)strcpy(typename, "binary");
220 1.1 lukem if (overbose)
221 1.24 lukem fprintf(ttyout,
222 1.24 lukem "Using %s mode to transfer files.\n",
223 1.1 lukem typename);
224 1.1 lukem } else {
225 1.1 lukem if (proxy)
226 1.1 lukem unix_proxy = 0;
227 1.1 lukem else
228 1.1 lukem unix_server = 0;
229 1.1 lukem if (overbose &&
230 1.1 lukem !strncmp(reply_string, "215 TOPS20", 10))
231 1.24 lukem fputs(
232 1.24 lukem "Remember to set tenex mode when transferring binary files from this machine.\n",
233 1.24 lukem ttyout);
234 1.1 lukem }
235 1.1 lukem verbose = overbose;
236 1.1 lukem }
237 1.7 lukem }
238 1.7 lukem
239 1.7 lukem /*
240 1.7 lukem * login to remote host, using given username & password if supplied
241 1.7 lukem */
242 1.7 lukem int
243 1.28 lukem ftp_login(host, user, pass)
244 1.7 lukem const char *host;
245 1.29 mycroft const char *user, *pass;
246 1.7 lukem {
247 1.7 lukem char tmp[80];
248 1.29 mycroft const char *acct;
249 1.14 lukem struct passwd *pw;
250 1.49 lukem int n, aflag, rval, freeuser, freepass, freeacct, len;
251 1.7 lukem
252 1.7 lukem acct = NULL;
253 1.47 lukem aflag = rval = freeuser = freepass = freeacct = 0;
254 1.7 lukem
255 1.7 lukem /*
256 1.7 lukem * Set up arguments for an anonymous FTP session, if necessary.
257 1.7 lukem */
258 1.47 lukem if (anonftp) {
259 1.7 lukem /*
260 1.7 lukem * Set up anonymous login password.
261 1.7 lukem */
262 1.31 lukem if ((pass = getenv("FTPANONPASS")) == NULL) {
263 1.49 lukem char *anonpass;
264 1.49 lukem
265 1.31 lukem if ((pass = getlogin()) == NULL) {
266 1.31 lukem if ((pw = getpwuid(getuid())) == NULL)
267 1.31 lukem pass = "anonymous";
268 1.31 lukem else
269 1.31 lukem pass = pw->pw_name;
270 1.31 lukem }
271 1.31 lukem /*
272 1.31 lukem * Every anonymous FTP server I've encountered
273 1.31 lukem * will accept the string "username@", and will
274 1.31 lukem * append the hostname itself. We do this by default
275 1.31 lukem * since many servers are picky about not having
276 1.31 lukem * a FQDN in the anonymous password.
277 1.31 lukem * - thorpej (at) netbsd.org
278 1.31 lukem */
279 1.49 lukem len = strlen(pass) + 2;
280 1.49 lukem anonpass = xmalloc(len);
281 1.49 lukem snprintf(anonpass, len, "%s@", pass);
282 1.31 lukem pass = anonpass;
283 1.49 lukem freepass = 1;
284 1.14 lukem }
285 1.9 lukem user = "anonymous"; /* as per RFC 1635 */
286 1.7 lukem }
287 1.7 lukem
288 1.47 lukem if (user == NULL) {
289 1.47 lukem freeuser = 1;
290 1.47 lukem if (pass == NULL)
291 1.47 lukem freepass = 1;
292 1.47 lukem freeacct = 1;
293 1.47 lukem if (ruserpass(host, &user, &pass, &acct) < 0) {
294 1.47 lukem code = -1;
295 1.47 lukem goto cleanup_ftp_login;
296 1.47 lukem }
297 1.47 lukem }
298 1.47 lukem
299 1.7 lukem while (user == NULL) {
300 1.29 mycroft const char *myname = getlogin();
301 1.7 lukem
302 1.14 lukem if (myname == NULL && (pw = getpwuid(getuid())) != NULL)
303 1.14 lukem myname = pw->pw_name;
304 1.7 lukem if (myname)
305 1.24 lukem fprintf(ttyout, "Name (%s:%s): ", host, myname);
306 1.7 lukem else
307 1.24 lukem fprintf(ttyout, "Name (%s): ", host);
308 1.19 lukem *tmp = '\0';
309 1.49 lukem if (fgets(tmp, sizeof(tmp) - 1, stdin) == NULL) {
310 1.49 lukem fprintf(ttyout, "\nEOF received; login aborted.\n");
311 1.49 lukem code = -1;
312 1.49 lukem goto cleanup_ftp_login;
313 1.49 lukem }
314 1.7 lukem tmp[strlen(tmp) - 1] = '\0';
315 1.51 christos freeuser = 0;
316 1.7 lukem if (*tmp == '\0')
317 1.7 lukem user = myname;
318 1.7 lukem else
319 1.7 lukem user = tmp;
320 1.7 lukem }
321 1.49 lukem
322 1.49 lukem if (gatemode) {
323 1.49 lukem char *nuser;
324 1.49 lukem int len;
325 1.49 lukem
326 1.49 lukem len = strlen(user) + 1 + strlen(host) + 1;
327 1.49 lukem nuser = xmalloc(len);
328 1.49 lukem snprintf(nuser, len, "%s@%s", user, host);
329 1.51 christos freeuser = 1;
330 1.49 lukem user = nuser;
331 1.49 lukem }
332 1.49 lukem
333 1.7 lukem n = command("USER %s", user);
334 1.7 lukem if (n == CONTINUE) {
335 1.51 christos if (pass == NULL) {
336 1.51 christos freepass = 0;
337 1.7 lukem pass = getpass("Password:");
338 1.51 christos }
339 1.7 lukem n = command("PASS %s", pass);
340 1.7 lukem }
341 1.7 lukem if (n == CONTINUE) {
342 1.7 lukem aflag++;
343 1.47 lukem if (acct == NULL) {
344 1.51 christos freeacct = 0;
345 1.7 lukem acct = getpass("Account:");
346 1.49 lukem }
347 1.49 lukem if (acct[0] == '\0') {
348 1.49 lukem warnx("Login failed.");
349 1.49 lukem goto cleanup_ftp_login;
350 1.47 lukem }
351 1.7 lukem n = command("ACCT %s", acct);
352 1.7 lukem }
353 1.7 lukem if ((n != COMPLETE) ||
354 1.7 lukem (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
355 1.7 lukem warnx("Login failed.");
356 1.47 lukem goto cleanup_ftp_login;
357 1.47 lukem }
358 1.48 matthias rval = 1;
359 1.48 matthias if (proxy)
360 1.47 lukem goto cleanup_ftp_login;
361 1.48 matthias
362 1.7 lukem connected = -1;
363 1.7 lukem for (n = 0; n < macnum; ++n) {
364 1.7 lukem if (!strcmp("init", macros[n].mac_name)) {
365 1.7 lukem (void)strcpy(line, "$init");
366 1.7 lukem makeargv();
367 1.7 lukem domacro(margc, margv);
368 1.7 lukem break;
369 1.7 lukem }
370 1.7 lukem }
371 1.47 lukem cleanup_ftp_login:
372 1.47 lukem if (user != NULL && freeuser)
373 1.47 lukem free((char *)user);
374 1.47 lukem if (pass != NULL && freepass)
375 1.47 lukem free((char *)pass);
376 1.47 lukem if (acct != NULL && freeacct)
377 1.47 lukem free((char *)acct);
378 1.47 lukem return (rval);
379 1.1 lukem }
380 1.1 lukem
381 1.1 lukem /*
382 1.5 lukem * `another' gets another argument, and stores the new argc and argv.
383 1.1 lukem * It reverts to the top level (via main.c's intr()) on EOF/error.
384 1.1 lukem *
385 1.1 lukem * Returns false if no new arguments have been added.
386 1.1 lukem */
387 1.1 lukem int
388 1.1 lukem another(pargc, pargv, prompt)
389 1.1 lukem int *pargc;
390 1.1 lukem char ***pargv;
391 1.1 lukem const char *prompt;
392 1.1 lukem {
393 1.1 lukem int len = strlen(line), ret;
394 1.1 lukem
395 1.1 lukem if (len >= sizeof(line) - 3) {
396 1.24 lukem fputs("sorry, arguments too long.\n", ttyout);
397 1.1 lukem intr();
398 1.1 lukem }
399 1.24 lukem fprintf(ttyout, "(%s) ", prompt);
400 1.1 lukem line[len++] = ' ';
401 1.1 lukem if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
402 1.1 lukem intr();
403 1.1 lukem len += strlen(&line[len]);
404 1.1 lukem if (len > 0 && line[len - 1] == '\n')
405 1.1 lukem line[len - 1] = '\0';
406 1.1 lukem makeargv();
407 1.1 lukem ret = margc > *pargc;
408 1.1 lukem *pargc = margc;
409 1.1 lukem *pargv = margv;
410 1.1 lukem return (ret);
411 1.1 lukem }
412 1.1 lukem
413 1.5 lukem /*
414 1.5 lukem * glob files given in argv[] from the remote server.
415 1.5 lukem * if errbuf isn't NULL, store error messages there instead
416 1.5 lukem * of writing to the screen.
417 1.5 lukem */
418 1.1 lukem char *
419 1.5 lukem remglob(argv, doswitch, errbuf)
420 1.1 lukem char *argv[];
421 1.1 lukem int doswitch;
422 1.5 lukem char **errbuf;
423 1.1 lukem {
424 1.1 lukem char temp[MAXPATHLEN];
425 1.1 lukem static char buf[MAXPATHLEN];
426 1.1 lukem static FILE *ftemp = NULL;
427 1.1 lukem static char **args;
428 1.1 lukem int oldverbose, oldhash, fd;
429 1.1 lukem char *cp, *mode;
430 1.1 lukem
431 1.1 lukem if (!mflag) {
432 1.5 lukem if (!doglob)
433 1.1 lukem args = NULL;
434 1.1 lukem else {
435 1.1 lukem if (ftemp) {
436 1.5 lukem (void)fclose(ftemp);
437 1.1 lukem ftemp = NULL;
438 1.1 lukem }
439 1.1 lukem }
440 1.1 lukem return (NULL);
441 1.1 lukem }
442 1.1 lukem if (!doglob) {
443 1.1 lukem if (args == NULL)
444 1.1 lukem args = argv;
445 1.1 lukem if ((cp = *++args) == NULL)
446 1.1 lukem args = NULL;
447 1.1 lukem return (cp);
448 1.1 lukem }
449 1.1 lukem if (ftemp == NULL) {
450 1.13 lukem (void)snprintf(temp, sizeof(temp), "%s/%s", tmpdir, TMPFILE);
451 1.5 lukem if ((fd = mkstemp(temp)) < 0) {
452 1.1 lukem warn("unable to create temporary file %s", temp);
453 1.1 lukem return (NULL);
454 1.1 lukem }
455 1.1 lukem close(fd);
456 1.5 lukem oldverbose = verbose;
457 1.5 lukem verbose = (errbuf != NULL) ? -1 : 0;
458 1.5 lukem oldhash = hash;
459 1.5 lukem hash = 0;
460 1.5 lukem if (doswitch)
461 1.1 lukem pswitch(!proxy);
462 1.1 lukem for (mode = "w"; *++argv != NULL; mode = "a")
463 1.12 lukem recvrequest("NLST", temp, *argv, mode, 0, 0);
464 1.5 lukem if ((code / 100) != COMPLETE) {
465 1.5 lukem if (errbuf != NULL)
466 1.5 lukem *errbuf = reply_string;
467 1.5 lukem }
468 1.5 lukem if (doswitch)
469 1.1 lukem pswitch(!proxy);
470 1.5 lukem verbose = oldverbose;
471 1.5 lukem hash = oldhash;
472 1.1 lukem ftemp = fopen(temp, "r");
473 1.5 lukem (void)unlink(temp);
474 1.1 lukem if (ftemp == NULL) {
475 1.5 lukem if (errbuf == NULL)
476 1.24 lukem fputs(
477 1.24 lukem "can't find list of remote files, oops.\n",
478 1.24 lukem ttyout);
479 1.5 lukem else
480 1.5 lukem *errbuf =
481 1.5 lukem "can't find list of remote files, oops.";
482 1.1 lukem return (NULL);
483 1.1 lukem }
484 1.1 lukem }
485 1.5 lukem if (fgets(buf, sizeof(buf), ftemp) == NULL) {
486 1.5 lukem (void)fclose(ftemp);
487 1.5 lukem ftemp = NULL;
488 1.1 lukem return (NULL);
489 1.1 lukem }
490 1.1 lukem if ((cp = strchr(buf, '\n')) != NULL)
491 1.1 lukem *cp = '\0';
492 1.1 lukem return (buf);
493 1.1 lukem }
494 1.1 lukem
495 1.1 lukem int
496 1.1 lukem confirm(cmd, file)
497 1.1 lukem const char *cmd, *file;
498 1.1 lukem {
499 1.1 lukem char line[BUFSIZ];
500 1.1 lukem
501 1.1 lukem if (!interactive || confirmrest)
502 1.1 lukem return (1);
503 1.24 lukem fprintf(ttyout, "%s %s? ", cmd, file);
504 1.24 lukem (void)fflush(ttyout);
505 1.1 lukem if (fgets(line, sizeof(line), stdin) == NULL)
506 1.1 lukem return (0);
507 1.1 lukem switch (tolower(*line)) {
508 1.1 lukem case 'n':
509 1.1 lukem return (0);
510 1.1 lukem case 'p':
511 1.1 lukem interactive = 0;
512 1.24 lukem fputs("Interactive mode: off.\n", ttyout);
513 1.1 lukem break;
514 1.1 lukem case 'a':
515 1.1 lukem confirmrest = 1;
516 1.24 lukem fprintf(ttyout, "Prompting off for duration of %s.\n",
517 1.24 lukem cmd);
518 1.1 lukem break;
519 1.1 lukem }
520 1.1 lukem return (1);
521 1.1 lukem }
522 1.1 lukem
523 1.1 lukem /*
524 1.1 lukem * Glob a local file name specification with
525 1.1 lukem * the expectation of a single return value.
526 1.1 lukem * Can't control multiple values being expanded
527 1.1 lukem * from the expression, we return only the first.
528 1.1 lukem */
529 1.1 lukem int
530 1.1 lukem globulize(cpp)
531 1.1 lukem char **cpp;
532 1.1 lukem {
533 1.1 lukem glob_t gl;
534 1.1 lukem int flags;
535 1.1 lukem
536 1.1 lukem if (!doglob)
537 1.1 lukem return (1);
538 1.1 lukem
539 1.25 kleink flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
540 1.1 lukem memset(&gl, 0, sizeof(gl));
541 1.43 lukem if (glob(*cpp, flags, NULL, &gl) || gl.gl_pathc == 0) {
542 1.1 lukem warnx("%s: not found", *cpp);
543 1.1 lukem globfree(&gl);
544 1.1 lukem return (0);
545 1.1 lukem }
546 1.12 lukem /* XXX: caller should check if *cpp changed, and
547 1.12 lukem * free(*cpp) if that is the case
548 1.12 lukem */
549 1.31 lukem *cpp = xstrdup(gl.gl_pathv[0]);
550 1.1 lukem globfree(&gl);
551 1.1 lukem return (1);
552 1.1 lukem }
553 1.1 lukem
554 1.1 lukem /*
555 1.1 lukem * determine size of remote file
556 1.1 lukem */
557 1.1 lukem off_t
558 1.1 lukem remotesize(file, noisy)
559 1.1 lukem const char *file;
560 1.1 lukem int noisy;
561 1.1 lukem {
562 1.1 lukem int overbose;
563 1.1 lukem off_t size;
564 1.1 lukem
565 1.1 lukem overbose = verbose;
566 1.1 lukem size = -1;
567 1.1 lukem if (debug == 0)
568 1.1 lukem verbose = -1;
569 1.11 lukem if (command("SIZE %s", file) == COMPLETE) {
570 1.11 lukem char *cp, *ep;
571 1.11 lukem
572 1.11 lukem cp = strchr(reply_string, ' ');
573 1.11 lukem if (cp != NULL) {
574 1.11 lukem cp++;
575 1.32 lukem #ifndef NO_QUAD
576 1.32 lukem size = strtoq(cp, &ep, 10);
577 1.32 lukem #else
578 1.23 christos size = strtol(cp, &ep, 10);
579 1.23 christos #endif
580 1.23 christos if (*ep != '\0' && !isspace((unsigned char)*ep))
581 1.11 lukem size = -1;
582 1.11 lukem }
583 1.24 lukem } else if (noisy && debug == 0) {
584 1.24 lukem fputs(reply_string, ttyout);
585 1.24 lukem putc('\n', ttyout);
586 1.24 lukem }
587 1.1 lukem verbose = overbose;
588 1.1 lukem return (size);
589 1.1 lukem }
590 1.1 lukem
591 1.1 lukem /*
592 1.1 lukem * determine last modification time (in GMT) of remote file
593 1.1 lukem */
594 1.1 lukem time_t
595 1.1 lukem remotemodtime(file, noisy)
596 1.1 lukem const char *file;
597 1.1 lukem int noisy;
598 1.1 lukem {
599 1.1 lukem int overbose;
600 1.1 lukem time_t rtime;
601 1.14 lukem int ocode;
602 1.1 lukem
603 1.1 lukem overbose = verbose;
604 1.14 lukem ocode = code;
605 1.1 lukem rtime = -1;
606 1.1 lukem if (debug == 0)
607 1.1 lukem verbose = -1;
608 1.1 lukem if (command("MDTM %s", file) == COMPLETE) {
609 1.1 lukem struct tm timebuf;
610 1.1 lukem int yy, mo, day, hour, min, sec;
611 1.1 lukem sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
612 1.1 lukem &day, &hour, &min, &sec);
613 1.1 lukem memset(&timebuf, 0, sizeof(timebuf));
614 1.1 lukem timebuf.tm_sec = sec;
615 1.1 lukem timebuf.tm_min = min;
616 1.1 lukem timebuf.tm_hour = hour;
617 1.1 lukem timebuf.tm_mday = day;
618 1.1 lukem timebuf.tm_mon = mo - 1;
619 1.22 christos timebuf.tm_year = yy - TM_YEAR_BASE;
620 1.1 lukem timebuf.tm_isdst = -1;
621 1.36 itohy rtime = mkgmtime(&timebuf);
622 1.1 lukem if (rtime == -1 && (noisy || debug != 0))
623 1.24 lukem fprintf(ttyout, "Can't convert %s to a time.\n",
624 1.24 lukem reply_string);
625 1.24 lukem } else if (noisy && debug == 0) {
626 1.24 lukem fputs(reply_string, ttyout);
627 1.24 lukem putc('\n', ttyout);
628 1.24 lukem }
629 1.1 lukem verbose = overbose;
630 1.14 lukem if (rtime == -1)
631 1.14 lukem code = ocode;
632 1.1 lukem return (rtime);
633 1.1 lukem }
634 1.36 itohy
635 1.36 itohy /*
636 1.36 itohy * UTC version of mktime(3)
637 1.36 itohy */
638 1.36 itohy #ifdef HAVE_TIMEGM
639 1.36 itohy time_t
640 1.36 itohy mkgmtime(tm)
641 1.36 itohy struct tm *tm;
642 1.36 itohy {
643 1.36 itohy
644 1.36 itohy /* This is very clean, but not portable at all. */
645 1.37 lukem return (timegm(tm));
646 1.36 itohy }
647 1.36 itohy
648 1.36 itohy #else /* not HAVE_TIMEGM */
649 1.36 itohy
650 1.36 itohy /*
651 1.36 itohy * This code is not portable, but works on most Unix-like systems.
652 1.36 itohy * If the local timezone has no summer time, using mktime(3) function
653 1.36 itohy * and adjusting offset would be usable (adjusting leap seconds
654 1.36 itohy * is still required, though), but the assumption is not always true.
655 1.36 itohy *
656 1.36 itohy * Anyway, no portable and correct implementation of UTC to time_t
657 1.36 itohy * conversion exists....
658 1.36 itohy */
659 1.36 itohy
660 1.36 itohy static time_t
661 1.36 itohy sub_mkgmt(tm)
662 1.36 itohy struct tm *tm;
663 1.36 itohy {
664 1.36 itohy int y, nleapdays;
665 1.36 itohy time_t t;
666 1.36 itohy /* days before the month */
667 1.36 itohy static const unsigned short moff[12] = {
668 1.36 itohy 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
669 1.36 itohy };
670 1.36 itohy
671 1.36 itohy /*
672 1.36 itohy * XXX This code assumes the given time to be normalized.
673 1.36 itohy * Normalizing here is impossible in case the given time is a leap
674 1.36 itohy * second but the local time library is ignorant of leap seconds.
675 1.36 itohy */
676 1.36 itohy
677 1.36 itohy /* minimal sanity checking not to access outside of the array */
678 1.36 itohy if ((unsigned) tm->tm_mon >= 12)
679 1.36 itohy return (time_t) -1;
680 1.36 itohy if (tm->tm_year < EPOCH_YEAR - TM_YEAR_BASE)
681 1.36 itohy return (time_t) -1;
682 1.36 itohy
683 1.36 itohy y = tm->tm_year + TM_YEAR_BASE - (tm->tm_mon < 2);
684 1.36 itohy nleapdays = y / 4 - y / 100 + y / 400 -
685 1.36 itohy ((EPOCH_YEAR-1) / 4 - (EPOCH_YEAR-1) / 100 + (EPOCH_YEAR-1) / 400);
686 1.36 itohy t = ((((time_t) (tm->tm_year - (EPOCH_YEAR - TM_YEAR_BASE)) * 365 +
687 1.36 itohy moff[tm->tm_mon] + tm->tm_mday - 1 + nleapdays) * 24 +
688 1.36 itohy tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
689 1.36 itohy
690 1.37 lukem return (t < 0 ? (time_t) -1 : t);
691 1.36 itohy }
692 1.36 itohy
693 1.36 itohy time_t
694 1.36 itohy mkgmtime(tm)
695 1.36 itohy struct tm *tm;
696 1.36 itohy {
697 1.36 itohy time_t t, t2;
698 1.36 itohy struct tm *tm2;
699 1.36 itohy int sec;
700 1.36 itohy
701 1.36 itohy /* Do the first guess. */
702 1.36 itohy if ((t = sub_mkgmt(tm)) == (time_t) -1)
703 1.36 itohy return (time_t) -1;
704 1.36 itohy
705 1.36 itohy /* save value in case *tm is overwritten by gmtime() */
706 1.36 itohy sec = tm->tm_sec;
707 1.36 itohy
708 1.36 itohy tm2 = gmtime(&t);
709 1.36 itohy if ((t2 = sub_mkgmt(tm2)) == (time_t) -1)
710 1.36 itohy return (time_t) -1;
711 1.36 itohy
712 1.36 itohy if (t2 < t || tm2->tm_sec != sec) {
713 1.36 itohy /*
714 1.36 itohy * Adjust for leap seconds.
715 1.36 itohy *
716 1.36 itohy * real time_t time
717 1.36 itohy * |
718 1.36 itohy * tm
719 1.36 itohy * / ... (a) first sub_mkgmt() conversion
720 1.36 itohy * t
721 1.36 itohy * |
722 1.36 itohy * tm2
723 1.36 itohy * / ... (b) second sub_mkgmt() conversion
724 1.36 itohy * t2
725 1.36 itohy * --->time
726 1.36 itohy */
727 1.36 itohy /*
728 1.36 itohy * Do the second guess, assuming (a) and (b) are almost equal.
729 1.36 itohy */
730 1.36 itohy t += t - t2;
731 1.36 itohy tm2 = gmtime(&t);
732 1.36 itohy
733 1.36 itohy /*
734 1.36 itohy * Either (a) or (b), may include one or two extra
735 1.36 itohy * leap seconds. Try t, t + 2, t - 2, t + 1, and t - 1.
736 1.36 itohy */
737 1.36 itohy if (tm2->tm_sec == sec
738 1.36 itohy || (t += 2, tm2 = gmtime(&t), tm2->tm_sec == sec)
739 1.36 itohy || (t -= 4, tm2 = gmtime(&t), tm2->tm_sec == sec)
740 1.36 itohy || (t += 3, tm2 = gmtime(&t), tm2->tm_sec == sec)
741 1.36 itohy || (t -= 2, tm2 = gmtime(&t), tm2->tm_sec == sec))
742 1.36 itohy ; /* found */
743 1.36 itohy else {
744 1.36 itohy /*
745 1.36 itohy * Not found.
746 1.36 itohy */
747 1.36 itohy if (sec >= 60)
748 1.36 itohy /*
749 1.36 itohy * The given time is a leap second
750 1.36 itohy * (sec 60 or 61), but the time library
751 1.36 itohy * is ignorant of the leap second.
752 1.36 itohy */
753 1.36 itohy ; /* treat sec 60 as 59,
754 1.36 itohy sec 61 as 0 of the next minute */
755 1.36 itohy else
756 1.36 itohy /* The given time may not be normalized. */
757 1.36 itohy t++; /* restore t */
758 1.36 itohy }
759 1.36 itohy }
760 1.36 itohy
761 1.37 lukem return (t < 0 ? (time_t) -1 : t);
762 1.36 itohy }
763 1.36 itohy #endif /* not HAVE_TIMEGM */
764 1.1 lukem
765 1.50 cgd #ifndef NO_PROGRESS
766 1.10 lukem
767 1.24 lukem /*
768 1.24 lukem * return non-zero if we're the current foreground process
769 1.24 lukem */
770 1.24 lukem int
771 1.24 lukem foregroundproc()
772 1.3 lukem {
773 1.9 lukem static pid_t pgrp = -1;
774 1.9 lukem int ctty_pgrp;
775 1.3 lukem
776 1.9 lukem if (pgrp == -1)
777 1.9 lukem pgrp = getpgrp();
778 1.9 lukem
779 1.24 lukem return ((ioctl(fileno(ttyout), TIOCGPGRP, &ctty_pgrp) != -1 &&
780 1.24 lukem ctty_pgrp == (int)pgrp));
781 1.24 lukem }
782 1.24 lukem
783 1.24 lukem
784 1.24 lukem static void updateprogressmeter __P((int));
785 1.24 lukem
786 1.24 lukem static void
787 1.24 lukem updateprogressmeter(dummy)
788 1.24 lukem int dummy;
789 1.24 lukem {
790 1.24 lukem
791 1.45 lukem progressmeter(0);
792 1.3 lukem }
793 1.50 cgd #endif /* NO_PROGRESS */
794 1.3 lukem
795 1.38 lukem
796 1.38 lukem /*
797 1.38 lukem * List of order of magnitude prefixes.
798 1.38 lukem * The last is `P', as 2^64 = 16384 Petabytes
799 1.38 lukem */
800 1.38 lukem static const char prefixes[] = " KMGTP";
801 1.38 lukem
802 1.1 lukem /*
803 1.1 lukem * Display a transfer progress bar if progress is non-zero.
804 1.3 lukem * SIGALRM is hijacked for use by this function.
805 1.3 lukem * - Before the transfer, set filesize to size of file (or -1 if unknown),
806 1.3 lukem * and call with flag = -1. This starts the once per second timer,
807 1.3 lukem * and a call to updateprogressmeter() upon SIGALRM.
808 1.3 lukem * - During the transfer, updateprogressmeter will call progressmeter
809 1.3 lukem * with flag = 0
810 1.3 lukem * - After the transfer, call with flag = 1
811 1.1 lukem */
812 1.50 cgd #ifndef NO_PROGRESS
813 1.1 lukem static struct timeval start;
814 1.21 lukem static struct timeval lastupdate;
815 1.50 cgd #endif
816 1.1 lukem
817 1.1 lukem void
818 1.1 lukem progressmeter(flag)
819 1.1 lukem int flag;
820 1.1 lukem {
821 1.50 cgd #ifndef NO_PROGRESS
822 1.3 lukem static off_t lastsize;
823 1.3 lukem struct timeval now, td, wait;
824 1.34 lukem off_t cursize, abbrevsize, bytespersec;
825 1.17 lukem double elapsed;
826 1.17 lukem int ratio, barlength, i, len, remaining;
827 1.3 lukem char buf[256];
828 1.1 lukem
829 1.15 mycroft len = 0;
830 1.3 lukem if (flag == -1) {
831 1.19 lukem (void)gettimeofday(&start, NULL);
832 1.3 lukem lastupdate = start;
833 1.3 lukem lastsize = restart_point;
834 1.3 lukem }
835 1.2 thorpej if (!progress || filesize <= 0)
836 1.1 lukem return;
837 1.45 lukem
838 1.45 lukem (void)gettimeofday(&now, NULL);
839 1.1 lukem cursize = bytes + restart_point;
840 1.45 lukem timersub(&now, &lastupdate, &wait);
841 1.45 lukem if (cursize > lastsize) {
842 1.45 lukem lastupdate = now;
843 1.45 lukem lastsize = cursize;
844 1.45 lukem if (wait.tv_sec >= STALLTIME) { /* fudge out stalled time */
845 1.45 lukem start.tv_sec += wait.tv_sec;
846 1.45 lukem start.tv_usec += wait.tv_usec;
847 1.45 lukem }
848 1.45 lukem wait.tv_sec = 0;
849 1.45 lukem }
850 1.45 lukem
851 1.45 lukem /*
852 1.45 lukem * print progress bar only if we are foreground process.
853 1.45 lukem */
854 1.45 lukem if (! foregroundproc())
855 1.45 lukem return;
856 1.1 lukem
857 1.34 lukem ratio = (int)((double)cursize * 100.0 / (double)filesize);
858 1.1 lukem ratio = MAX(ratio, 0);
859 1.1 lukem ratio = MIN(ratio, 100);
860 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len, "\r%3d%% ", ratio);
861 1.1 lukem
862 1.38 lukem barlength = ttywidth - 43;
863 1.1 lukem if (barlength > 0) {
864 1.1 lukem i = barlength * ratio / 100;
865 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
866 1.46 lukem "|%.*s%*s|", i,
867 1.1 lukem "*****************************************************************************"
868 1.1 lukem "*****************************************************************************",
869 1.1 lukem barlength - i, "");
870 1.1 lukem }
871 1.1 lukem
872 1.1 lukem abbrevsize = cursize;
873 1.34 lukem for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
874 1.1 lukem abbrevsize >>= 10;
875 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
876 1.32 lukem #ifndef NO_QUAD
877 1.32 lukem " %5qd %c%c ", (long long)abbrevsize,
878 1.32 lukem #else
879 1.32 lukem " %5ld %c%c ", (long)abbrevsize,
880 1.32 lukem #endif
881 1.38 lukem prefixes[i],
882 1.34 lukem i == 0 ? ' ' : 'B');
883 1.3 lukem
884 1.1 lukem timersub(&now, &start, &td);
885 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
886 1.3 lukem
887 1.34 lukem bytespersec = 0;
888 1.34 lukem if (bytes > 0) {
889 1.34 lukem bytespersec = bytes;
890 1.34 lukem if (elapsed > 0.0)
891 1.34 lukem bytespersec /= elapsed;
892 1.34 lukem }
893 1.39 lukem for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
894 1.34 lukem bytespersec >>= 10;
895 1.34 lukem len += snprintf(buf + len, sizeof(buf) - len,
896 1.34 lukem #ifndef NO_QUAD
897 1.39 lukem " %3qd.%02d %cB/s ", (long long)bytespersec / 1024,
898 1.34 lukem #else
899 1.39 lukem " %3ld.%02d %cB/s ", (long)bytespersec / 1024,
900 1.34 lukem #endif
901 1.39 lukem (int)((bytespersec % 1024) * 100 / 1024),
902 1.38 lukem prefixes[i]);
903 1.34 lukem
904 1.8 lukem if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
905 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
906 1.3 lukem " --:-- ETA");
907 1.3 lukem } else if (wait.tv_sec >= STALLTIME) {
908 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
909 1.3 lukem " - stalled -");
910 1.1 lukem } else {
911 1.17 lukem remaining = (int)
912 1.17 lukem ((filesize - restart_point) / (bytes / elapsed) - elapsed);
913 1.17 lukem if (remaining >= 100 * SECSPERHOUR)
914 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
915 1.15 mycroft " --:-- ETA");
916 1.15 mycroft else {
917 1.17 lukem i = remaining / SECSPERHOUR;
918 1.15 mycroft if (i)
919 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
920 1.15 mycroft "%2d:", i);
921 1.15 mycroft else
922 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
923 1.15 mycroft " ");
924 1.17 lukem i = remaining % SECSPERHOUR;
925 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
926 1.15 mycroft "%02d:%02d ETA", i / 60, i % 60);
927 1.15 mycroft }
928 1.1 lukem }
929 1.42 lukem (void)write(fileno(ttyout), buf, len);
930 1.1 lukem
931 1.3 lukem if (flag == -1) {
932 1.33 lukem (void)xsignal(SIGALRM, updateprogressmeter);
933 1.3 lukem alarmtimer(1); /* set alarm timer for 1 Hz */
934 1.3 lukem } else if (flag == 1) {
935 1.33 lukem (void)xsignal(SIGALRM, SIG_DFL);
936 1.3 lukem alarmtimer(0);
937 1.24 lukem (void)putc('\n', ttyout);
938 1.3 lukem }
939 1.24 lukem fflush(ttyout);
940 1.50 cgd #endif /* NO_PROGRESS */
941 1.1 lukem }
942 1.1 lukem
943 1.1 lukem /*
944 1.1 lukem * Display transfer statistics.
945 1.1 lukem * Requires start to be initialised by progressmeter(-1),
946 1.1 lukem * direction to be defined by xfer routines, and filesize and bytes
947 1.1 lukem * to be updated by xfer routines
948 1.24 lukem * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
949 1.24 lukem * instead of ttyout.
950 1.1 lukem */
951 1.1 lukem void
952 1.1 lukem ptransfer(siginfo)
953 1.1 lukem int siginfo;
954 1.1 lukem {
955 1.50 cgd #ifndef NO_PROGRESS
956 1.21 lukem struct timeval now, td, wait;
957 1.1 lukem double elapsed;
958 1.34 lukem off_t bytespersec;
959 1.38 lukem int remaining, hh, i, len;
960 1.1 lukem char buf[100];
961 1.1 lukem
962 1.1 lukem if (!verbose && !siginfo)
963 1.1 lukem return;
964 1.1 lukem
965 1.19 lukem (void)gettimeofday(&now, NULL);
966 1.1 lukem timersub(&now, &start, &td);
967 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
968 1.34 lukem bytespersec = 0;
969 1.34 lukem if (bytes > 0) {
970 1.34 lukem bytespersec = bytes;
971 1.34 lukem if (elapsed > 0.0)
972 1.34 lukem bytespersec /= elapsed;
973 1.34 lukem }
974 1.17 lukem len = 0;
975 1.17 lukem len += snprintf(buf + len, sizeof(buf) - len,
976 1.23 christos #ifndef NO_QUAD
977 1.23 christos "%qd byte%s %s in ", (long long)bytes,
978 1.23 christos #else
979 1.23 christos "%ld byte%s %s in ", (long)bytes,
980 1.23 christos #endif
981 1.23 christos bytes == 1 ? "" : "s", direction);
982 1.21 lukem remaining = (int)elapsed;
983 1.21 lukem if (remaining > SECSPERDAY) {
984 1.21 lukem int days;
985 1.21 lukem
986 1.21 lukem days = remaining / SECSPERDAY;
987 1.21 lukem remaining %= SECSPERDAY;
988 1.21 lukem len += snprintf(buf + len, sizeof(buf) - len,
989 1.21 lukem "%d day%s ", days, days == 1 ? "" : "s");
990 1.21 lukem }
991 1.21 lukem hh = remaining / SECSPERHOUR;
992 1.21 lukem remaining %= SECSPERHOUR;
993 1.21 lukem if (hh)
994 1.21 lukem len += snprintf(buf + len, sizeof(buf) - len, "%2d:", hh);
995 1.21 lukem len += snprintf(buf + len, sizeof(buf) - len,
996 1.38 lukem "%02d:%02d ", remaining / 60, remaining % 60);
997 1.38 lukem
998 1.39 lukem for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
999 1.38 lukem bytespersec >>= 10;
1000 1.38 lukem len += snprintf(buf + len, sizeof(buf) - len,
1001 1.38 lukem #ifndef NO_QUAD
1002 1.39 lukem "(%qd.%02d %cB/s)", (long long)bytespersec / 1024,
1003 1.38 lukem #else
1004 1.39 lukem "(%ld.%02d %cB/s)", (long)bytespersec / 1024,
1005 1.38 lukem #endif
1006 1.39 lukem (int)((bytespersec % 1024) * 100 / 1024),
1007 1.38 lukem prefixes[i]);
1008 1.21 lukem
1009 1.8 lukem if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
1010 1.8 lukem && bytes + restart_point <= filesize) {
1011 1.1 lukem remaining = (int)((filesize - restart_point) /
1012 1.1 lukem (bytes / elapsed) - elapsed);
1013 1.17 lukem hh = remaining / SECSPERHOUR;
1014 1.17 lukem remaining %= SECSPERHOUR;
1015 1.21 lukem len += snprintf(buf + len, sizeof(buf) - len, " ETA: ");
1016 1.21 lukem if (hh)
1017 1.21 lukem len += snprintf(buf + len, sizeof(buf) - len, "%2d:",
1018 1.21 lukem hh);
1019 1.17 lukem len += snprintf(buf + len, sizeof(buf) - len,
1020 1.21 lukem "%02d:%02d", remaining / 60, remaining % 60);
1021 1.21 lukem timersub(&now, &lastupdate, &wait);
1022 1.21 lukem if (wait.tv_sec >= STALLTIME)
1023 1.21 lukem len += snprintf(buf + len, sizeof(buf) - len,
1024 1.21 lukem " (stalled)");
1025 1.1 lukem }
1026 1.21 lukem len += snprintf(buf + len, sizeof(buf) - len, "\n");
1027 1.42 lukem (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
1028 1.50 cgd #endif /* NO_PROGRESS */
1029 1.1 lukem }
1030 1.1 lukem
1031 1.1 lukem /*
1032 1.1 lukem * List words in stringlist, vertically arranged
1033 1.1 lukem */
1034 1.1 lukem void
1035 1.1 lukem list_vertical(sl)
1036 1.1 lukem StringList *sl;
1037 1.1 lukem {
1038 1.1 lukem int i, j, w;
1039 1.1 lukem int columns, width, lines, items;
1040 1.1 lukem char *p;
1041 1.1 lukem
1042 1.1 lukem width = items = 0;
1043 1.1 lukem
1044 1.1 lukem for (i = 0 ; i < sl->sl_cur ; i++) {
1045 1.1 lukem w = strlen(sl->sl_str[i]);
1046 1.1 lukem if (w > width)
1047 1.1 lukem width = w;
1048 1.1 lukem }
1049 1.1 lukem width = (width + 8) &~ 7;
1050 1.1 lukem
1051 1.1 lukem columns = ttywidth / width;
1052 1.1 lukem if (columns == 0)
1053 1.1 lukem columns = 1;
1054 1.1 lukem lines = (sl->sl_cur + columns - 1) / columns;
1055 1.1 lukem for (i = 0; i < lines; i++) {
1056 1.1 lukem for (j = 0; j < columns; j++) {
1057 1.1 lukem p = sl->sl_str[j * lines + i];
1058 1.1 lukem if (p)
1059 1.24 lukem fputs(p, ttyout);
1060 1.1 lukem if (j * lines + i + lines >= sl->sl_cur) {
1061 1.24 lukem putc('\n', ttyout);
1062 1.1 lukem break;
1063 1.1 lukem }
1064 1.1 lukem w = strlen(p);
1065 1.1 lukem while (w < width) {
1066 1.1 lukem w = (w + 8) &~ 7;
1067 1.24 lukem (void)putc('\t', ttyout);
1068 1.1 lukem }
1069 1.1 lukem }
1070 1.1 lukem }
1071 1.3 lukem }
1072 1.3 lukem
1073 1.3 lukem /*
1074 1.3 lukem * Update the global ttywidth value, using TIOCGWINSZ.
1075 1.3 lukem */
1076 1.3 lukem void
1077 1.3 lukem setttywidth(a)
1078 1.3 lukem int a;
1079 1.3 lukem {
1080 1.3 lukem struct winsize winsize;
1081 1.3 lukem
1082 1.34 lukem if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1 &&
1083 1.34 lukem winsize.ws_col != 0)
1084 1.3 lukem ttywidth = winsize.ws_col;
1085 1.3 lukem else
1086 1.3 lukem ttywidth = 80;
1087 1.3 lukem }
1088 1.3 lukem
1089 1.3 lukem /*
1090 1.3 lukem * Set the SIGALRM interval timer for wait seconds, 0 to disable.
1091 1.3 lukem */
1092 1.3 lukem void
1093 1.3 lukem alarmtimer(wait)
1094 1.3 lukem int wait;
1095 1.3 lukem {
1096 1.3 lukem struct itimerval itv;
1097 1.3 lukem
1098 1.3 lukem itv.it_value.tv_sec = wait;
1099 1.3 lukem itv.it_value.tv_usec = 0;
1100 1.3 lukem itv.it_interval = itv.it_value;
1101 1.3 lukem setitimer(ITIMER_REAL, &itv, NULL);
1102 1.1 lukem }
1103 1.6 lukem
1104 1.6 lukem /*
1105 1.6 lukem * Setup or cleanup EditLine structures
1106 1.6 lukem */
1107 1.50 cgd #ifndef NO_EDITCOMPLETE
1108 1.6 lukem void
1109 1.6 lukem controlediting()
1110 1.6 lukem {
1111 1.6 lukem if (editing && el == NULL && hist == NULL) {
1112 1.16 christos HistEvent ev;
1113 1.30 lukem int editmode;
1114 1.16 christos
1115 1.24 lukem el = el_init(__progname, stdin, ttyout, stderr);
1116 1.23 christos /* init editline */
1117 1.6 lukem hist = history_init(); /* init the builtin history */
1118 1.23 christos history(hist, &ev, H_SETSIZE, 100);/* remember 100 events */
1119 1.6 lukem el_set(el, EL_HIST, history, hist); /* use history */
1120 1.6 lukem
1121 1.6 lukem el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
1122 1.6 lukem el_set(el, EL_PROMPT, prompt); /* set the prompt function */
1123 1.6 lukem
1124 1.6 lukem /* add local file completion, bind to TAB */
1125 1.6 lukem el_set(el, EL_ADDFN, "ftp-complete",
1126 1.6 lukem "Context sensitive argument completion",
1127 1.6 lukem complete);
1128 1.6 lukem el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
1129 1.6 lukem el_source(el, NULL); /* read ~/.editrc */
1130 1.30 lukem if ((el_get(el, EL_EDITMODE, &editmode) != -1) && editmode == 0)
1131 1.30 lukem editing = 0; /* the user doesn't want editing,
1132 1.30 lukem * so disable, and let statement
1133 1.30 lukem * below cleanup */
1134 1.30 lukem else
1135 1.30 lukem el_set(el, EL_SIGNAL, 1);
1136 1.30 lukem }
1137 1.30 lukem if (!editing) {
1138 1.6 lukem if (hist) {
1139 1.6 lukem history_end(hist);
1140 1.6 lukem hist = NULL;
1141 1.6 lukem }
1142 1.6 lukem if (el) {
1143 1.6 lukem el_end(el);
1144 1.6 lukem el = NULL;
1145 1.6 lukem }
1146 1.6 lukem }
1147 1.6 lukem }
1148 1.50 cgd #endif /* !NO_EDITCOMPLETE */
1149 1.27 thorpej
1150 1.27 thorpej /*
1151 1.27 thorpej * Parse the specified socket buffer size.
1152 1.27 thorpej */
1153 1.27 thorpej int
1154 1.27 thorpej getsockbufsize(arg)
1155 1.27 thorpej const char *arg;
1156 1.27 thorpej {
1157 1.27 thorpej char *cp;
1158 1.27 thorpej int val;
1159 1.27 thorpej
1160 1.35 christos if (!isdigit((unsigned char)arg[0]))
1161 1.27 thorpej return (-1);
1162 1.27 thorpej
1163 1.27 thorpej val = strtol(arg, &cp, 10);
1164 1.27 thorpej if (cp != NULL) {
1165 1.27 thorpej if (cp[1] != '\0')
1166 1.27 thorpej return (-1);
1167 1.27 thorpej if (cp[0] == 'k')
1168 1.27 thorpej val *= 1024;
1169 1.27 thorpej if (cp[0] == 'm')
1170 1.27 thorpej val *= 1024 * 1024;
1171 1.27 thorpej }
1172 1.27 thorpej
1173 1.27 thorpej if (val < 0)
1174 1.27 thorpej return (-1);
1175 1.27 thorpej
1176 1.27 thorpej return (val);
1177 1.27 thorpej }
1178 1.27 thorpej
1179 1.27 thorpej /*
1180 1.27 thorpej * Set up socket buffer sizes before a connection is made.
1181 1.27 thorpej */
1182 1.27 thorpej void
1183 1.27 thorpej setupsockbufsize(sock)
1184 1.27 thorpej int sock;
1185 1.27 thorpej {
1186 1.27 thorpej static int sndbuf_default, rcvbuf_default;
1187 1.27 thorpej int len, size;
1188 1.27 thorpej
1189 1.27 thorpej /*
1190 1.27 thorpej * Get the default socket buffer sizes if we don't already
1191 1.27 thorpej * have them. It doesn't matter which socket we do this
1192 1.27 thorpej * to, because on the first call no socket buffer sizes
1193 1.27 thorpej * will have been modified, so we are guaranteed to get
1194 1.27 thorpej * the system defaults.
1195 1.27 thorpej */
1196 1.27 thorpej if (sndbuf_default == 0) {
1197 1.27 thorpej len = sizeof(sndbuf_default);
1198 1.27 thorpej if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf_default,
1199 1.27 thorpej &len) < 0)
1200 1.27 thorpej err(1, "unable to get default sndbuf size");
1201 1.27 thorpej len = sizeof(rcvbuf_default);
1202 1.27 thorpej if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf_default,
1203 1.27 thorpej &len) < 0)
1204 1.27 thorpej err(1, "unable to get default rcvbuf size");
1205 1.27 thorpej
1206 1.27 thorpej }
1207 1.27 thorpej
1208 1.27 thorpej size = sndbuf_size ? sndbuf_size : sndbuf_default;
1209 1.27 thorpej if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0)
1210 1.27 thorpej warn("unable to set sndbuf size %d", size);
1211 1.27 thorpej
1212 1.27 thorpej size = rcvbuf_size ? rcvbuf_size : rcvbuf_default;
1213 1.27 thorpej if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) < 0)
1214 1.27 thorpej warn("unable to set rcvbuf size %d", size);
1215 1.27 thorpej }
1216 1.27 thorpej
1217 1.27 thorpej /*
1218 1.27 thorpej * If the socket buffer sizes were not set manually (i.e. came from a
1219 1.27 thorpej * configuration file), reset them so the right thing will happen on
1220 1.27 thorpej * subsequent connections.
1221 1.27 thorpej */
1222 1.27 thorpej void
1223 1.27 thorpej resetsockbufsize()
1224 1.27 thorpej {
1225 1.27 thorpej
1226 1.27 thorpej if (sndbuf_manual == 0)
1227 1.27 thorpej sndbuf_size = 0;
1228 1.27 thorpej if (rcvbuf_manual == 0)
1229 1.27 thorpej rcvbuf_size = 0;
1230 1.43 lukem }
1231 1.43 lukem
1232 1.43 lukem void
1233 1.43 lukem ftpvis(dst, dstlen, src, srclen)
1234 1.43 lukem char *dst;
1235 1.43 lukem size_t dstlen;
1236 1.43 lukem const char *src;
1237 1.43 lukem size_t srclen;
1238 1.43 lukem {
1239 1.43 lukem int di, si;
1240 1.43 lukem
1241 1.43 lukem for (di = si = 0;
1242 1.43 lukem src[si] != '\0' && di < dstlen && si < srclen;
1243 1.43 lukem di++, si++) {
1244 1.43 lukem switch (src[si]) {
1245 1.43 lukem case '\\':
1246 1.43 lukem case ' ':
1247 1.43 lukem case '\t':
1248 1.43 lukem case '\r':
1249 1.44 lukem case '\n':
1250 1.43 lukem case '"':
1251 1.43 lukem dst[di++] = '\\';
1252 1.43 lukem if (di >= dstlen)
1253 1.43 lukem break;
1254 1.43 lukem /* FALLTHROUGH */
1255 1.43 lukem default:
1256 1.43 lukem dst[di] = src[si];
1257 1.43 lukem }
1258 1.43 lukem }
1259 1.43 lukem dst[di] = '\0';
1260 1.27 thorpej }
1261 1.27 thorpej
1262 1.27 thorpej /*
1263 1.27 thorpej * Internal version of connect(2); sets socket buffer sizes first.
1264 1.27 thorpej */
1265 1.27 thorpej int
1266 1.27 thorpej xconnect(sock, name, namelen)
1267 1.27 thorpej int sock;
1268 1.27 thorpej const struct sockaddr *name;
1269 1.27 thorpej int namelen;
1270 1.27 thorpej {
1271 1.27 thorpej
1272 1.27 thorpej setupsockbufsize(sock);
1273 1.27 thorpej return (connect(sock, name, namelen));
1274 1.27 thorpej }
1275 1.27 thorpej
1276 1.27 thorpej /*
1277 1.27 thorpej * Internal version of listen(2); sets socket buffer sizes first.
1278 1.27 thorpej */
1279 1.27 thorpej int
1280 1.27 thorpej xlisten(sock, backlog)
1281 1.27 thorpej int sock, backlog;
1282 1.27 thorpej {
1283 1.27 thorpej
1284 1.27 thorpej setupsockbufsize(sock);
1285 1.27 thorpej return (listen(sock, backlog));
1286 1.31 lukem }
1287 1.31 lukem
1288 1.31 lukem void *
1289 1.31 lukem xmalloc(size)
1290 1.31 lukem size_t size;
1291 1.31 lukem {
1292 1.31 lukem void *p;
1293 1.31 lukem
1294 1.31 lukem p = malloc(size);
1295 1.31 lukem if (p == NULL)
1296 1.31 lukem err(1, "Unable to allocate %ld bytes of memory", (long)size);
1297 1.31 lukem return (p);
1298 1.31 lukem }
1299 1.31 lukem
1300 1.31 lukem char *
1301 1.31 lukem xstrdup(str)
1302 1.31 lukem const char *str;
1303 1.31 lukem {
1304 1.31 lukem char *s;
1305 1.31 lukem
1306 1.31 lukem if (str == NULL)
1307 1.31 lukem errx(1, "xstrdup() called with NULL argument");
1308 1.31 lukem s = strdup(str);
1309 1.31 lukem if (s == NULL)
1310 1.31 lukem err(1, "Unable to allocate memory for string copy");
1311 1.31 lukem return (s);
1312 1.33 lukem }
1313 1.33 lukem
1314 1.33 lukem sig_t
1315 1.33 lukem xsignal(sig, func)
1316 1.33 lukem int sig;
1317 1.33 lukem void (*func) __P((int));
1318 1.33 lukem {
1319 1.33 lukem struct sigaction act, oact;
1320 1.33 lukem
1321 1.33 lukem act.sa_handler = func;
1322 1.33 lukem sigemptyset(&act.sa_mask);
1323 1.33 lukem act.sa_flags = SA_RESTART;
1324 1.33 lukem if (sigaction(sig, &act, &oact) < 0)
1325 1.33 lukem return (SIG_ERR);
1326 1.33 lukem return (oact.sa_handler);
1327 1.27 thorpej }
1328