[BACK]Return to util.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ftp

Annotation of src/usr.bin/ftp/util.c, Revision 1.7

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