util.c revision 1.9 1 1.9 lukem /* $NetBSD: util.c,v 1.9 1997/06/10 22:00:01 lukem Exp $ */
2 1.1 lukem
3 1.1 lukem /*
4 1.1 lukem * Copyright (c) 1985, 1989, 1993, 1994
5 1.1 lukem * The Regents of the University of California. All rights reserved.
6 1.1 lukem *
7 1.1 lukem * Redistribution and use in source and binary forms, with or without
8 1.1 lukem * modification, are permitted provided that the following conditions
9 1.1 lukem * are met:
10 1.1 lukem * 1. Redistributions of source code must retain the above copyright
11 1.1 lukem * notice, this list of conditions and the following disclaimer.
12 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 lukem * notice, this list of conditions and the following disclaimer in the
14 1.1 lukem * documentation and/or other materials provided with the distribution.
15 1.1 lukem * 3. All advertising materials mentioning features or use of this software
16 1.1 lukem * must display the following acknowledgement:
17 1.1 lukem * This product includes software developed by the University of
18 1.1 lukem * California, Berkeley and its contributors.
19 1.1 lukem * 4. Neither the name of the University nor the names of its contributors
20 1.1 lukem * may be used to endorse or promote products derived from this software
21 1.1 lukem * without specific prior written permission.
22 1.1 lukem *
23 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 lukem * SUCH DAMAGE.
34 1.1 lukem */
35 1.1 lukem
36 1.1 lukem #ifndef lint
37 1.9 lukem static char rcsid[] = "$NetBSD: util.c,v 1.9 1997/06/10 22:00:01 lukem Exp $";
38 1.1 lukem #endif /* not lint */
39 1.1 lukem
40 1.1 lukem /*
41 1.1 lukem * FTP User Program -- Misc support routines
42 1.1 lukem */
43 1.1 lukem #include <sys/ioctl.h>
44 1.1 lukem #include <sys/time.h>
45 1.1 lukem #include <arpa/ftp.h>
46 1.1 lukem
47 1.1 lukem #include <ctype.h>
48 1.1 lukem #include <err.h>
49 1.1 lukem #include <fcntl.h>
50 1.1 lukem #include <glob.h>
51 1.7 lukem #include <pwd.h>
52 1.1 lukem #include <stdio.h>
53 1.1 lukem #include <string.h>
54 1.1 lukem #include <time.h>
55 1.1 lukem #include <unistd.h>
56 1.1 lukem
57 1.1 lukem #include "ftp_var.h"
58 1.1 lukem #include "pathnames.h"
59 1.1 lukem
60 1.1 lukem /*
61 1.1 lukem * Connect to peer server and
62 1.1 lukem * auto-login, if possible.
63 1.1 lukem */
64 1.1 lukem void
65 1.1 lukem setpeer(argc, argv)
66 1.1 lukem int argc;
67 1.1 lukem char *argv[];
68 1.1 lukem {
69 1.1 lukem char *host;
70 1.1 lukem short port;
71 1.1 lukem
72 1.1 lukem if (connected) {
73 1.1 lukem printf("Already connected to %s, use close first.\n",
74 1.5 lukem hostname);
75 1.1 lukem code = -1;
76 1.1 lukem return;
77 1.1 lukem }
78 1.1 lukem if (argc < 2)
79 1.5 lukem (void)another(&argc, &argv, "to");
80 1.1 lukem if (argc < 2 || argc > 3) {
81 1.1 lukem printf("usage: %s host-name [port]\n", argv[0]);
82 1.1 lukem code = -1;
83 1.1 lukem return;
84 1.1 lukem }
85 1.1 lukem port = ftpport;
86 1.1 lukem if (argc > 2) {
87 1.1 lukem port = atoi(argv[2]);
88 1.1 lukem if (port <= 0) {
89 1.5 lukem printf("%s: bad port number '%s'.\n", argv[1], argv[2]);
90 1.5 lukem printf("usage: %s host-name [port]\n", argv[0]);
91 1.1 lukem code = -1;
92 1.1 lukem return;
93 1.1 lukem }
94 1.1 lukem port = htons(port);
95 1.1 lukem }
96 1.1 lukem host = hookup(argv[1], port);
97 1.1 lukem if (host) {
98 1.1 lukem int overbose;
99 1.1 lukem
100 1.1 lukem connected = 1;
101 1.1 lukem /*
102 1.1 lukem * Set up defaults for FTP.
103 1.1 lukem */
104 1.5 lukem (void)strcpy(typename, "ascii"), type = TYPE_A;
105 1.1 lukem curtype = TYPE_A;
106 1.5 lukem (void)strcpy(formname, "non-print"), form = FORM_N;
107 1.5 lukem (void)strcpy(modename, "stream"), mode = MODE_S;
108 1.5 lukem (void)strcpy(structname, "file"), stru = STRU_F;
109 1.5 lukem (void)strcpy(bytename, "8"), bytesize = 8;
110 1.1 lukem if (autologin)
111 1.7 lukem (void)login(argv[1], NULL, NULL);
112 1.1 lukem
113 1.1 lukem overbose = verbose;
114 1.1 lukem if (debug == 0)
115 1.1 lukem verbose = -1;
116 1.1 lukem if (command("SYST") == COMPLETE && overbose) {
117 1.1 lukem char *cp, c;
118 1.1 lukem c = 0;
119 1.1 lukem cp = strchr(reply_string+4, ' ');
120 1.1 lukem if (cp == NULL)
121 1.1 lukem cp = strchr(reply_string+4, '\r');
122 1.1 lukem if (cp) {
123 1.1 lukem if (cp[-1] == '.')
124 1.1 lukem cp--;
125 1.1 lukem c = *cp;
126 1.1 lukem *cp = '\0';
127 1.1 lukem }
128 1.1 lukem
129 1.5 lukem printf("Remote system type is %s.\n", reply_string + 4);
130 1.1 lukem if (cp)
131 1.1 lukem *cp = c;
132 1.1 lukem }
133 1.1 lukem if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
134 1.1 lukem if (proxy)
135 1.1 lukem unix_proxy = 1;
136 1.1 lukem else
137 1.1 lukem unix_server = 1;
138 1.1 lukem /*
139 1.1 lukem * Set type to 0 (not specified by user),
140 1.1 lukem * meaning binary by default, but don't bother
141 1.1 lukem * telling server. We can use binary
142 1.1 lukem * for text files unless changed by the user.
143 1.1 lukem */
144 1.1 lukem type = 0;
145 1.5 lukem (void)strcpy(typename, "binary");
146 1.1 lukem if (overbose)
147 1.1 lukem printf("Using %s mode to transfer files.\n",
148 1.1 lukem typename);
149 1.1 lukem } else {
150 1.1 lukem if (proxy)
151 1.1 lukem unix_proxy = 0;
152 1.1 lukem else
153 1.1 lukem unix_server = 0;
154 1.1 lukem if (overbose &&
155 1.1 lukem !strncmp(reply_string, "215 TOPS20", 10))
156 1.5 lukem puts(
157 1.5 lukem "Remember to set tenex mode when transferring binary files from this machine.");
158 1.1 lukem }
159 1.1 lukem verbose = overbose;
160 1.1 lukem }
161 1.7 lukem }
162 1.7 lukem
163 1.7 lukem
164 1.7 lukem /*
165 1.7 lukem * login to remote host, using given username & password if supplied
166 1.7 lukem */
167 1.7 lukem int
168 1.7 lukem login(host, user, pass)
169 1.7 lukem const char *host;
170 1.7 lukem char *user, *pass;
171 1.7 lukem {
172 1.7 lukem char tmp[80];
173 1.7 lukem char *acct;
174 1.7 lukem char anonpass[MAXLOGNAME + 1 + MAXHOSTNAMELEN]; /* "user@hostname" */
175 1.7 lukem char hostname[MAXHOSTNAMELEN];
176 1.7 lukem int n, aflag = 0;
177 1.7 lukem
178 1.7 lukem acct = NULL;
179 1.7 lukem if (user == NULL) {
180 1.7 lukem if (ruserpass(host, &user, &pass, &acct) < 0) {
181 1.7 lukem code = -1;
182 1.7 lukem return (0);
183 1.7 lukem }
184 1.7 lukem }
185 1.7 lukem
186 1.7 lukem /*
187 1.7 lukem * Set up arguments for an anonymous FTP session, if necessary.
188 1.7 lukem */
189 1.7 lukem if ((user == NULL || pass == NULL) && anonftp) {
190 1.7 lukem memset(anonpass, 0, sizeof(anonpass));
191 1.7 lukem memset(hostname, 0, sizeof(hostname));
192 1.7 lukem
193 1.7 lukem /*
194 1.7 lukem * Set up anonymous login password.
195 1.7 lukem */
196 1.7 lukem user = getlogin();
197 1.7 lukem gethostname(hostname, MAXHOSTNAMELEN);
198 1.7 lukem #ifndef DONT_CHEAT_ANONPASS
199 1.7 lukem /*
200 1.7 lukem * Every anonymous FTP server I've encountered
201 1.7 lukem * will accept the string "username@", and will
202 1.7 lukem * append the hostname itself. We do this by default
203 1.7 lukem * since many servers are picky about not having
204 1.7 lukem * a FQDN in the anonymous password. - thorpej (at) netbsd.org
205 1.7 lukem */
206 1.7 lukem snprintf(anonpass, sizeof(anonpass) - 1, "%s@",
207 1.7 lukem user);
208 1.7 lukem #else
209 1.7 lukem snprintf(anonpass, sizeof(anonpass) - 1, "%s@%s",
210 1.7 lukem user, hp->h_name);
211 1.7 lukem #endif
212 1.7 lukem pass = anonpass;
213 1.9 lukem user = "anonymous"; /* as per RFC 1635 */
214 1.7 lukem }
215 1.7 lukem
216 1.7 lukem while (user == NULL) {
217 1.7 lukem char *myname = getlogin();
218 1.7 lukem
219 1.7 lukem if (myname == NULL) {
220 1.7 lukem struct passwd *pp = getpwuid(getuid());
221 1.7 lukem
222 1.7 lukem if (pp != NULL)
223 1.7 lukem myname = pp->pw_name;
224 1.7 lukem }
225 1.7 lukem if (myname)
226 1.7 lukem printf("Name (%s:%s): ", host, myname);
227 1.7 lukem else
228 1.7 lukem printf("Name (%s): ", host);
229 1.7 lukem (void)fgets(tmp, sizeof(tmp) - 1, stdin);
230 1.7 lukem tmp[strlen(tmp) - 1] = '\0';
231 1.7 lukem if (*tmp == '\0')
232 1.7 lukem user = myname;
233 1.7 lukem else
234 1.7 lukem user = tmp;
235 1.7 lukem }
236 1.7 lukem n = command("USER %s", user);
237 1.7 lukem if (n == CONTINUE) {
238 1.7 lukem if (pass == NULL)
239 1.7 lukem pass = getpass("Password:");
240 1.7 lukem n = command("PASS %s", pass);
241 1.7 lukem }
242 1.7 lukem if (n == CONTINUE) {
243 1.7 lukem aflag++;
244 1.7 lukem if (acct == NULL)
245 1.7 lukem acct = getpass("Account:");
246 1.7 lukem n = command("ACCT %s", acct);
247 1.7 lukem }
248 1.7 lukem if ((n != COMPLETE) ||
249 1.7 lukem (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
250 1.7 lukem warnx("Login failed.");
251 1.7 lukem return (0);
252 1.7 lukem }
253 1.7 lukem if (proxy)
254 1.7 lukem return (1);
255 1.7 lukem connected = -1;
256 1.7 lukem for (n = 0; n < macnum; ++n) {
257 1.7 lukem if (!strcmp("init", macros[n].mac_name)) {
258 1.7 lukem (void)strcpy(line, "$init");
259 1.7 lukem makeargv();
260 1.7 lukem domacro(margc, margv);
261 1.7 lukem break;
262 1.7 lukem }
263 1.7 lukem }
264 1.7 lukem return (1);
265 1.1 lukem }
266 1.1 lukem
267 1.1 lukem /*
268 1.5 lukem * `another' gets another argument, and stores the new argc and argv.
269 1.1 lukem * It reverts to the top level (via main.c's intr()) on EOF/error.
270 1.1 lukem *
271 1.1 lukem * Returns false if no new arguments have been added.
272 1.1 lukem */
273 1.1 lukem int
274 1.1 lukem another(pargc, pargv, prompt)
275 1.1 lukem int *pargc;
276 1.1 lukem char ***pargv;
277 1.1 lukem const char *prompt;
278 1.1 lukem {
279 1.1 lukem int len = strlen(line), ret;
280 1.1 lukem
281 1.1 lukem if (len >= sizeof(line) - 3) {
282 1.5 lukem puts("sorry, arguments too long.");
283 1.1 lukem intr();
284 1.1 lukem }
285 1.1 lukem printf("(%s) ", prompt);
286 1.1 lukem line[len++] = ' ';
287 1.1 lukem if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
288 1.1 lukem intr();
289 1.1 lukem len += strlen(&line[len]);
290 1.1 lukem if (len > 0 && line[len - 1] == '\n')
291 1.1 lukem line[len - 1] = '\0';
292 1.1 lukem makeargv();
293 1.1 lukem ret = margc > *pargc;
294 1.1 lukem *pargc = margc;
295 1.1 lukem *pargv = margv;
296 1.1 lukem return (ret);
297 1.1 lukem }
298 1.1 lukem
299 1.5 lukem /*
300 1.5 lukem * glob files given in argv[] from the remote server.
301 1.5 lukem * if errbuf isn't NULL, store error messages there instead
302 1.5 lukem * of writing to the screen.
303 1.5 lukem */
304 1.1 lukem char *
305 1.5 lukem remglob(argv, doswitch, errbuf)
306 1.1 lukem char *argv[];
307 1.1 lukem int doswitch;
308 1.5 lukem char **errbuf;
309 1.1 lukem {
310 1.1 lukem char temp[MAXPATHLEN];
311 1.1 lukem static char buf[MAXPATHLEN];
312 1.1 lukem static FILE *ftemp = NULL;
313 1.1 lukem static char **args;
314 1.1 lukem int oldverbose, oldhash, fd;
315 1.1 lukem char *cp, *mode;
316 1.1 lukem
317 1.1 lukem if (!mflag) {
318 1.5 lukem if (!doglob)
319 1.1 lukem args = NULL;
320 1.1 lukem else {
321 1.1 lukem if (ftemp) {
322 1.5 lukem (void)fclose(ftemp);
323 1.1 lukem ftemp = NULL;
324 1.1 lukem }
325 1.1 lukem }
326 1.1 lukem return (NULL);
327 1.1 lukem }
328 1.1 lukem if (!doglob) {
329 1.1 lukem if (args == NULL)
330 1.1 lukem args = argv;
331 1.1 lukem if ((cp = *++args) == NULL)
332 1.1 lukem args = NULL;
333 1.1 lukem return (cp);
334 1.1 lukem }
335 1.1 lukem if (ftemp == NULL) {
336 1.5 lukem (void)snprintf(temp, sizeof(temp), "%s%s", _PATH_TMP, TMPFILE);
337 1.5 lukem if ((fd = mkstemp(temp)) < 0) {
338 1.1 lukem warn("unable to create temporary file %s", temp);
339 1.1 lukem return (NULL);
340 1.1 lukem }
341 1.1 lukem close(fd);
342 1.5 lukem oldverbose = verbose;
343 1.5 lukem verbose = (errbuf != NULL) ? -1 : 0;
344 1.5 lukem oldhash = hash;
345 1.5 lukem hash = 0;
346 1.5 lukem if (doswitch)
347 1.1 lukem pswitch(!proxy);
348 1.1 lukem for (mode = "w"; *++argv != NULL; mode = "a")
349 1.5 lukem recvrequest("NLST", temp, *argv, mode, 0);
350 1.5 lukem if ((code / 100) != COMPLETE) {
351 1.5 lukem if (errbuf != NULL)
352 1.5 lukem *errbuf = reply_string;
353 1.5 lukem }
354 1.5 lukem if (doswitch)
355 1.1 lukem pswitch(!proxy);
356 1.5 lukem verbose = oldverbose;
357 1.5 lukem hash = oldhash;
358 1.1 lukem ftemp = fopen(temp, "r");
359 1.5 lukem (void)unlink(temp);
360 1.1 lukem if (ftemp == NULL) {
361 1.5 lukem if (errbuf == NULL)
362 1.5 lukem puts("can't find list of remote files, oops.");
363 1.5 lukem else
364 1.5 lukem *errbuf =
365 1.5 lukem "can't find list of remote files, oops.";
366 1.1 lukem return (NULL);
367 1.1 lukem }
368 1.1 lukem }
369 1.5 lukem if (fgets(buf, sizeof(buf), ftemp) == NULL) {
370 1.5 lukem (void)fclose(ftemp);
371 1.5 lukem ftemp = NULL;
372 1.1 lukem return (NULL);
373 1.1 lukem }
374 1.1 lukem if ((cp = strchr(buf, '\n')) != NULL)
375 1.1 lukem *cp = '\0';
376 1.1 lukem return (buf);
377 1.1 lukem }
378 1.1 lukem
379 1.1 lukem int
380 1.1 lukem confirm(cmd, file)
381 1.1 lukem const char *cmd, *file;
382 1.1 lukem {
383 1.1 lukem char line[BUFSIZ];
384 1.1 lukem
385 1.1 lukem if (!interactive || confirmrest)
386 1.1 lukem return (1);
387 1.1 lukem printf("%s %s? ", cmd, file);
388 1.5 lukem (void)fflush(stdout);
389 1.1 lukem if (fgets(line, sizeof(line), stdin) == NULL)
390 1.1 lukem return (0);
391 1.1 lukem switch (tolower(*line)) {
392 1.1 lukem case 'n':
393 1.1 lukem return (0);
394 1.1 lukem case 'p':
395 1.1 lukem interactive = 0;
396 1.5 lukem puts("Interactive mode: off.");
397 1.1 lukem break;
398 1.1 lukem case 'a':
399 1.1 lukem confirmrest = 1;
400 1.5 lukem printf("Prompting off for duration of %s.\n", cmd);
401 1.1 lukem break;
402 1.1 lukem }
403 1.1 lukem return (1);
404 1.1 lukem }
405 1.1 lukem
406 1.1 lukem /*
407 1.1 lukem * Glob a local file name specification with
408 1.1 lukem * the expectation of a single return value.
409 1.1 lukem * Can't control multiple values being expanded
410 1.1 lukem * from the expression, we return only the first.
411 1.1 lukem */
412 1.1 lukem int
413 1.1 lukem globulize(cpp)
414 1.1 lukem char **cpp;
415 1.1 lukem {
416 1.1 lukem glob_t gl;
417 1.1 lukem int flags;
418 1.1 lukem
419 1.1 lukem if (!doglob)
420 1.1 lukem return (1);
421 1.1 lukem
422 1.1 lukem flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
423 1.1 lukem memset(&gl, 0, sizeof(gl));
424 1.1 lukem if (glob(*cpp, flags, NULL, &gl) ||
425 1.1 lukem gl.gl_pathc == 0) {
426 1.1 lukem warnx("%s: not found", *cpp);
427 1.1 lukem globfree(&gl);
428 1.1 lukem return (0);
429 1.1 lukem }
430 1.1 lukem *cpp = strdup(gl.gl_pathv[0]); /* XXX - wasted memory */
431 1.1 lukem globfree(&gl);
432 1.1 lukem return (1);
433 1.1 lukem }
434 1.1 lukem
435 1.1 lukem /*
436 1.1 lukem * determine size of remote file
437 1.1 lukem */
438 1.1 lukem off_t
439 1.1 lukem remotesize(file, noisy)
440 1.1 lukem const char *file;
441 1.1 lukem int noisy;
442 1.1 lukem {
443 1.1 lukem int overbose;
444 1.1 lukem off_t size;
445 1.1 lukem
446 1.1 lukem overbose = verbose;
447 1.1 lukem size = -1;
448 1.1 lukem if (debug == 0)
449 1.1 lukem verbose = -1;
450 1.1 lukem if (command("SIZE %s", file) == COMPLETE)
451 1.1 lukem sscanf(reply_string, "%*s %qd", &size);
452 1.1 lukem else if (noisy && debug == 0)
453 1.5 lukem puts(reply_string);
454 1.1 lukem verbose = overbose;
455 1.1 lukem return (size);
456 1.1 lukem }
457 1.1 lukem
458 1.1 lukem /*
459 1.1 lukem * determine last modification time (in GMT) of remote file
460 1.1 lukem */
461 1.1 lukem time_t
462 1.1 lukem remotemodtime(file, noisy)
463 1.1 lukem const char *file;
464 1.1 lukem int noisy;
465 1.1 lukem {
466 1.1 lukem int overbose;
467 1.1 lukem time_t rtime;
468 1.1 lukem
469 1.1 lukem overbose = verbose;
470 1.1 lukem rtime = -1;
471 1.1 lukem if (debug == 0)
472 1.1 lukem verbose = -1;
473 1.1 lukem if (command("MDTM %s", file) == COMPLETE) {
474 1.1 lukem struct tm timebuf;
475 1.1 lukem int yy, mo, day, hour, min, sec;
476 1.1 lukem sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
477 1.1 lukem &day, &hour, &min, &sec);
478 1.1 lukem memset(&timebuf, 0, sizeof(timebuf));
479 1.1 lukem timebuf.tm_sec = sec;
480 1.1 lukem timebuf.tm_min = min;
481 1.1 lukem timebuf.tm_hour = hour;
482 1.1 lukem timebuf.tm_mday = day;
483 1.1 lukem timebuf.tm_mon = mo - 1;
484 1.1 lukem timebuf.tm_year = yy - 1900;
485 1.1 lukem timebuf.tm_isdst = -1;
486 1.1 lukem rtime = mktime(&timebuf);
487 1.1 lukem if (rtime == -1 && (noisy || debug != 0))
488 1.5 lukem printf("Can't convert %s to a time.\n", reply_string);
489 1.1 lukem else
490 1.1 lukem rtime += timebuf.tm_gmtoff; /* conv. local -> GMT */
491 1.1 lukem } else if (noisy && debug == 0)
492 1.5 lukem puts(reply_string);
493 1.1 lukem verbose = overbose;
494 1.1 lukem return (rtime);
495 1.1 lukem }
496 1.1 lukem
497 1.3 lukem void
498 1.3 lukem updateprogressmeter()
499 1.3 lukem {
500 1.9 lukem static pid_t pgrp = -1;
501 1.9 lukem int ctty_pgrp;
502 1.3 lukem
503 1.9 lukem if (pgrp == -1)
504 1.9 lukem pgrp = getpgrp();
505 1.9 lukem
506 1.9 lukem /*
507 1.9 lukem * print progress bar only if we are foreground process.
508 1.9 lukem */
509 1.9 lukem if (ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
510 1.9 lukem ctty_pgrp == (int)pgrp)
511 1.9 lukem progressmeter(0);
512 1.3 lukem }
513 1.3 lukem
514 1.1 lukem /*
515 1.1 lukem * Display a transfer progress bar if progress is non-zero.
516 1.3 lukem * SIGALRM is hijacked for use by this function.
517 1.3 lukem * - Before the transfer, set filesize to size of file (or -1 if unknown),
518 1.3 lukem * and call with flag = -1. This starts the once per second timer,
519 1.3 lukem * and a call to updateprogressmeter() upon SIGALRM.
520 1.3 lukem * - During the transfer, updateprogressmeter will call progressmeter
521 1.3 lukem * with flag = 0
522 1.3 lukem * - After the transfer, call with flag = 1
523 1.1 lukem */
524 1.1 lukem static struct timeval start;
525 1.1 lukem
526 1.1 lukem void
527 1.1 lukem progressmeter(flag)
528 1.1 lukem int flag;
529 1.1 lukem {
530 1.3 lukem /*
531 1.3 lukem * List of order of magnitude prefixes.
532 1.3 lukem * The last is `P', as 2^64 = 16384 Petabytes
533 1.3 lukem */
534 1.3 lukem static const char prefixes[] = " KMGTP";
535 1.3 lukem
536 1.3 lukem static struct timeval lastupdate;
537 1.3 lukem static off_t lastsize;
538 1.3 lukem struct timeval now, td, wait;
539 1.1 lukem off_t cursize, abbrevsize;
540 1.1 lukem double elapsed;
541 1.1 lukem int ratio, barlength, i, remaining;
542 1.3 lukem char buf[256];
543 1.1 lukem
544 1.3 lukem if (flag == -1) {
545 1.5 lukem (void)gettimeofday(&start, (struct timezone *)0);
546 1.3 lukem lastupdate = start;
547 1.3 lukem lastsize = restart_point;
548 1.3 lukem }
549 1.5 lukem (void)gettimeofday(&now, (struct timezone *)0);
550 1.2 thorpej if (!progress || filesize <= 0)
551 1.1 lukem return;
552 1.1 lukem cursize = bytes + restart_point;
553 1.1 lukem
554 1.1 lukem ratio = cursize * 100 / filesize;
555 1.1 lukem ratio = MAX(ratio, 0);
556 1.1 lukem ratio = MIN(ratio, 100);
557 1.3 lukem snprintf(buf, sizeof(buf), "\r%3d%% ", ratio);
558 1.1 lukem
559 1.1 lukem barlength = ttywidth - 30;
560 1.1 lukem if (barlength > 0) {
561 1.1 lukem i = barlength * ratio / 100;
562 1.3 lukem snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
563 1.3 lukem "|%.*s%*s|", i,
564 1.1 lukem "*****************************************************************************"
565 1.1 lukem "*****************************************************************************",
566 1.1 lukem barlength - i, "");
567 1.1 lukem }
568 1.1 lukem
569 1.1 lukem i = 0;
570 1.1 lukem abbrevsize = cursize;
571 1.1 lukem while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
572 1.1 lukem i++;
573 1.1 lukem abbrevsize >>= 10;
574 1.1 lukem }
575 1.3 lukem snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
576 1.3 lukem " %5qd %c%c ", abbrevsize, prefixes[i],
577 1.1 lukem prefixes[i] == ' ' ? ' ' : 'B');
578 1.1 lukem
579 1.3 lukem timersub(&now, &lastupdate, &wait);
580 1.3 lukem if (cursize > lastsize) {
581 1.3 lukem lastupdate = now;
582 1.3 lukem lastsize = cursize;
583 1.3 lukem if (wait.tv_sec >= STALLTIME) { /* fudge out stalled time */
584 1.3 lukem start.tv_sec += wait.tv_sec;
585 1.3 lukem start.tv_usec += wait.tv_usec;
586 1.3 lukem }
587 1.3 lukem wait.tv_sec = 0;
588 1.3 lukem }
589 1.3 lukem
590 1.1 lukem timersub(&now, &start, &td);
591 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
592 1.3 lukem
593 1.8 lukem if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
594 1.3 lukem snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
595 1.3 lukem " --:-- ETA");
596 1.3 lukem } else if (wait.tv_sec >= STALLTIME) {
597 1.3 lukem snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
598 1.3 lukem " - stalled -");
599 1.1 lukem } else {
600 1.1 lukem remaining = (int)((filesize - restart_point) /
601 1.1 lukem (bytes / elapsed) - elapsed);
602 1.1 lukem i = remaining / 3600;
603 1.1 lukem if (i)
604 1.3 lukem snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
605 1.3 lukem "%2d:", i);
606 1.1 lukem else
607 1.3 lukem snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
608 1.3 lukem " ");
609 1.1 lukem i = remaining % 3600;
610 1.3 lukem snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
611 1.3 lukem "%02d:%02d ETA", i / 60, i % 60);
612 1.1 lukem }
613 1.3 lukem (void)write(STDOUT_FILENO, buf, strlen(buf));
614 1.1 lukem
615 1.3 lukem if (flag == -1) {
616 1.5 lukem (void)signal(SIGALRM, updateprogressmeter);
617 1.3 lukem alarmtimer(1); /* set alarm timer for 1 Hz */
618 1.3 lukem } else if (flag == 1) {
619 1.3 lukem alarmtimer(0);
620 1.5 lukem (void)putchar('\n');
621 1.3 lukem }
622 1.1 lukem fflush(stdout);
623 1.1 lukem }
624 1.1 lukem
625 1.1 lukem /*
626 1.1 lukem * Display transfer statistics.
627 1.1 lukem * Requires start to be initialised by progressmeter(-1),
628 1.1 lukem * direction to be defined by xfer routines, and filesize and bytes
629 1.1 lukem * to be updated by xfer routines
630 1.1 lukem * If siginfo is nonzero, an ETA is displayed, and the output goes to STDERR
631 1.1 lukem * instead of STDOUT.
632 1.1 lukem */
633 1.1 lukem void
634 1.1 lukem ptransfer(siginfo)
635 1.1 lukem int siginfo;
636 1.1 lukem {
637 1.1 lukem struct timeval now, td;
638 1.1 lukem double elapsed;
639 1.1 lukem off_t bs;
640 1.1 lukem int meg, remaining, hh;
641 1.1 lukem char buf[100];
642 1.1 lukem
643 1.1 lukem if (!verbose && !siginfo)
644 1.1 lukem return;
645 1.1 lukem
646 1.5 lukem (void)gettimeofday(&now, (struct timezone *)0);
647 1.1 lukem timersub(&now, &start, &td);
648 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
649 1.1 lukem bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
650 1.1 lukem meg = 0;
651 1.1 lukem if (bs > (1024 * 1024))
652 1.1 lukem meg = 1;
653 1.1 lukem (void)snprintf(buf, sizeof(buf),
654 1.1 lukem "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n",
655 1.1 lukem bytes, bytes == 1 ? "" : "s", direction, elapsed,
656 1.1 lukem bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K");
657 1.8 lukem if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
658 1.8 lukem && bytes + restart_point <= filesize) {
659 1.1 lukem remaining = (int)((filesize - restart_point) /
660 1.1 lukem (bytes / elapsed) - elapsed);
661 1.1 lukem hh = remaining / 3600;
662 1.1 lukem remaining %= 3600;
663 1.8 lukem /* "buf+len(buf) -1" to overwrite \n */
664 1.1 lukem snprintf(buf + strlen(buf) - 1, sizeof(buf) - strlen(buf),
665 1.1 lukem " ETA: %02d:%02d:%02d\n", hh, remaining / 60,
666 1.1 lukem remaining % 60);
667 1.1 lukem }
668 1.1 lukem (void)write(siginfo ? STDERR_FILENO : STDOUT_FILENO, buf, strlen(buf));
669 1.1 lukem }
670 1.1 lukem
671 1.1 lukem /*
672 1.1 lukem * List words in stringlist, vertically arranged
673 1.1 lukem */
674 1.1 lukem void
675 1.1 lukem list_vertical(sl)
676 1.1 lukem StringList *sl;
677 1.1 lukem {
678 1.1 lukem int i, j, w;
679 1.1 lukem int columns, width, lines, items;
680 1.1 lukem char *p;
681 1.1 lukem
682 1.1 lukem width = items = 0;
683 1.1 lukem
684 1.1 lukem for (i = 0 ; i < sl->sl_cur ; i++) {
685 1.1 lukem w = strlen(sl->sl_str[i]);
686 1.1 lukem if (w > width)
687 1.1 lukem width = w;
688 1.1 lukem }
689 1.1 lukem width = (width + 8) &~ 7;
690 1.1 lukem
691 1.1 lukem columns = ttywidth / width;
692 1.1 lukem if (columns == 0)
693 1.1 lukem columns = 1;
694 1.1 lukem lines = (sl->sl_cur + columns - 1) / columns;
695 1.1 lukem for (i = 0; i < lines; i++) {
696 1.1 lukem for (j = 0; j < columns; j++) {
697 1.1 lukem p = sl->sl_str[j * lines + i];
698 1.1 lukem if (p)
699 1.5 lukem fputs(p, stdout);
700 1.1 lukem if (j * lines + i + lines >= sl->sl_cur) {
701 1.5 lukem putchar('\n');
702 1.1 lukem break;
703 1.1 lukem }
704 1.1 lukem w = strlen(p);
705 1.1 lukem while (w < width) {
706 1.1 lukem w = (w + 8) &~ 7;
707 1.5 lukem (void)putchar('\t');
708 1.1 lukem }
709 1.1 lukem }
710 1.1 lukem }
711 1.3 lukem }
712 1.3 lukem
713 1.3 lukem /*
714 1.3 lukem * Update the global ttywidth value, using TIOCGWINSZ.
715 1.3 lukem */
716 1.3 lukem void
717 1.3 lukem setttywidth(a)
718 1.3 lukem int a;
719 1.3 lukem {
720 1.3 lukem struct winsize winsize;
721 1.3 lukem
722 1.3 lukem if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
723 1.3 lukem ttywidth = winsize.ws_col;
724 1.3 lukem else
725 1.3 lukem ttywidth = 80;
726 1.3 lukem }
727 1.3 lukem
728 1.3 lukem /*
729 1.3 lukem * Set the SIGALRM interval timer for wait seconds, 0 to disable.
730 1.3 lukem */
731 1.3 lukem void
732 1.3 lukem alarmtimer(wait)
733 1.3 lukem int wait;
734 1.3 lukem {
735 1.3 lukem struct itimerval itv;
736 1.3 lukem
737 1.3 lukem itv.it_value.tv_sec = wait;
738 1.3 lukem itv.it_value.tv_usec = 0;
739 1.3 lukem itv.it_interval = itv.it_value;
740 1.3 lukem setitimer(ITIMER_REAL, &itv, NULL);
741 1.1 lukem }
742 1.6 lukem
743 1.6 lukem /*
744 1.6 lukem * Setup or cleanup EditLine structures
745 1.6 lukem */
746 1.6 lukem #ifndef SMALL
747 1.6 lukem void
748 1.6 lukem controlediting()
749 1.6 lukem {
750 1.6 lukem if (editing && el == NULL && hist == NULL) {
751 1.6 lukem el = el_init(__progname, stdin, stdout); /* init editline */
752 1.6 lukem hist = history_init(); /* init the builtin history */
753 1.6 lukem history(hist, H_EVENT, 100); /* remember 100 events */
754 1.6 lukem el_set(el, EL_HIST, history, hist); /* use history */
755 1.6 lukem
756 1.6 lukem el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
757 1.6 lukem el_set(el, EL_PROMPT, prompt); /* set the prompt function */
758 1.6 lukem
759 1.6 lukem /* add local file completion, bind to TAB */
760 1.6 lukem el_set(el, EL_ADDFN, "ftp-complete",
761 1.6 lukem "Context sensitive argument completion",
762 1.6 lukem complete);
763 1.6 lukem el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
764 1.6 lukem
765 1.6 lukem el_source(el, NULL); /* read ~/.editrc */
766 1.6 lukem el_set(el, EL_SIGNAL, 1);
767 1.6 lukem } else if (!editing) {
768 1.6 lukem if (hist) {
769 1.6 lukem history_end(hist);
770 1.6 lukem hist = NULL;
771 1.6 lukem }
772 1.6 lukem if (el) {
773 1.6 lukem el_end(el);
774 1.6 lukem el = NULL;
775 1.6 lukem }
776 1.6 lukem }
777 1.6 lukem }
778 1.6 lukem #endif /* !SMALL */
779