[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.23

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