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

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