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