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

Annotation of src/usr.bin/ssh/scp.c, Revision 1.11

1.1       deraadt     1: /*
                      2:
                      3: scp - secure remote copy.  This is basically patched BSD rcp which uses ssh
                      4: to do the data transfer (instead of using rcmd).
                      5:
                      6: NOTE: This version should NOT be suid root.  (This uses ssh to do the transfer
                      7: and ssh has the necessary privileges.)
                      8:
                      9: 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
                     10:
                     11: */
                     12:
                     13: /*
                     14:  * Copyright (c) 1983, 1990, 1992, 1993, 1995
                     15:  *     The Regents of the University of California.  All rights reserved.
                     16:  *
                     17:  * Redistribution and use in source and binary forms, with or without
                     18:  * modification, are permitted provided that the following conditions
                     19:  * are met:
                     20:  * 1. Redistributions of source code must retain the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer.
                     22:  * 2. Redistributions in binary form must reproduce the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer in the
                     24:  *    documentation and/or other materials provided with the distribution.
                     25:  * 3. All advertising materials mentioning features or use of this software
                     26:  *    must display the following acknowledgement:
                     27:  *     This product includes software developed by the University of
                     28:  *     California, Berkeley and its contributors.
                     29:  * 4. Neither the name of the University nor the names of its contributors
                     30:  *    may be used to endorse or promote products derived from this software
                     31:  *    without specific prior written permission.
                     32:  *
                     33:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     34:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     35:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     36:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     37:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     38:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     39:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     41:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     42:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGE.
                     44:  *
1.11    ! aaron      45:  *     $Id: scp.c,v 1.10 1999/10/04 01:59:56 deraadt Exp $
1.1       deraadt    46:  */
                     47:
                     48: #include "includes.h"
1.11    ! aaron      49: RCSID("$Id: scp.c,v 1.10 1999/10/04 01:59:56 deraadt Exp $");
1.1       deraadt    50:
                     51: #include "ssh.h"
                     52: #include "xmalloc.h"
                     53: #include <utime.h>
                     54:
                     55: #define _PATH_CP "cp"
                     56:
1.11    ! aaron      57: /* For progressmeter() -- number of seconds before xfer considered "stalled" */
1.4       aaron      58: #define STALLTIME      5
                     59:
1.11    ! aaron      60: /* Visual statistics about files as they are transferred. */
        !            61: void progressmeter(int);
        !            62:
        !            63: /* Returns width of the terminal (for progress meter calculations). */
        !            64: int getttywidth(void);
        !            65:
        !            66: /* Time a transfer started. */
1.4       aaron      67: static struct timeval start;
1.11    ! aaron      68:
        !            69: /* Number of bytes of current file transferred so far. */
        !            70: volatile unsigned long statbytes;
        !            71:
        !            72: /* Total size of current file. */
1.4       aaron      73: unsigned long totalbytes = 0;
1.11    ! aaron      74:
        !            75: /* Name of current file being transferred. */
        !            76: char *curfile;
1.4       aaron      77:
1.1       deraadt    78: /* This is set to non-zero to enable verbose mode. */
                     79: int verbose = 0;
                     80:
                     81: /* This is set to non-zero if compression is desired. */
                     82: int compress = 0;
                     83:
1.6       aaron      84: /* This is set to zero if the progressmeter is not desired. */
                     85: int showprogress = 1;
                     86:
1.1       deraadt    87: /* This is set to non-zero if running in batch mode (that is, password
                     88:    and passphrase queries are not allowed). */
                     89: int batchmode = 0;
                     90:
                     91: /* This is set to the cipher type string if given on the command line. */
                     92: char *cipher = NULL;
                     93:
                     94: /* This is set to the RSA authentication identity file name if given on
                     95:    the command line. */
                     96: char *identity = NULL;
                     97:
                     98: /* This is the port to use in contacting the remote site (is non-NULL). */
                     99: char *port = NULL;
                    100:
                    101: /* This function executes the given command as the specified user on the given
                    102:    host.  This returns < 0 if execution fails, and >= 0 otherwise.
                    103:    This assigns the input and output file descriptors on success. */
                    104:
                    105: int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
                    106: {
                    107:   int pin[2], pout[2], reserved[2];
                    108:
                    109:   if (verbose)
                    110:     fprintf(stderr, "Executing: host %s, user %s, command %s\n",
                    111:            host, remuser ? remuser : "(unspecified)", cmd);
                    112:
                    113:   /* Reserve two descriptors so that the real pipes won't get descriptors
                    114:      0 and 1 because that will screw up dup2 below. */
                    115:   pipe(reserved);
                    116:
                    117:   /* Create a socket pair for communicating with ssh. */
                    118:   if (pipe(pin) < 0)
                    119:     fatal("pipe: %s", strerror(errno));
                    120:   if (pipe(pout) < 0)
                    121:     fatal("pipe: %s", strerror(errno));
                    122:
                    123:   /* Free the reserved descriptors. */
                    124:   close(reserved[0]);
                    125:   close(reserved[1]);
                    126:
                    127:   /* For a child to execute the command on the remote host using ssh. */
                    128:   if (fork() == 0)
                    129:     {
                    130:       char *args[100];
                    131:       unsigned int i;
                    132:
                    133:       /* Child. */
                    134:       close(pin[1]);
                    135:       close(pout[0]);
                    136:       dup2(pin[0], 0);
                    137:       dup2(pout[1], 1);
                    138:       close(pin[0]);
                    139:       close(pout[1]);
                    140:
                    141:       i = 0;
                    142:       args[i++] = SSH_PROGRAM;
                    143:       args[i++] = "-x";
                    144:       args[i++] = "-oFallBackToRsh no";
                    145:       if (verbose)
                    146:        args[i++] = "-v";
                    147:       if (compress)
                    148:        args[i++] = "-C";
                    149:       if (batchmode)
                    150:        args[i++] = "-oBatchMode yes";
                    151:       if (cipher != NULL)
                    152:        {
                    153:          args[i++] = "-c";
                    154:          args[i++] = cipher;
                    155:        }
                    156:       if (identity != NULL)
                    157:        {
                    158:          args[i++] = "-i";
                    159:          args[i++] = identity;
                    160:        }
                    161:       if (port != NULL)
                    162:        {
                    163:          args[i++] = "-p";
                    164:          args[i++] = port;
                    165:        }
                    166:       if (remuser != NULL)
                    167:        {
                    168:          args[i++] = "-l";
                    169:          args[i++] = remuser;
                    170:        }
                    171:       args[i++] = host;
                    172:       args[i++] = cmd;
                    173:       args[i++] = NULL;
                    174:
                    175:       execvp(SSH_PROGRAM, args);
                    176:       perror(SSH_PROGRAM);
                    177:       exit(1);
                    178:     }
                    179:   /* Parent.  Close the other side, and return the local side. */
                    180:   close(pin[0]);
                    181:   *fdout = pin[1];
                    182:   close(pout[1]);
                    183:   *fdin = pout[0];
                    184:   return 0;
                    185: }
                    186:
                    187: void fatal(const char *fmt, ...)
                    188: {
                    189:   va_list ap;
                    190:   char buf[1024];
                    191:
                    192:   va_start(ap, fmt);
                    193:   vsnprintf(buf, sizeof(buf), fmt, ap);
                    194:   va_end(ap);
                    195:   fprintf(stderr, "%s\n", buf);
                    196:   exit(255);
                    197: }
                    198:
                    199: /* This stuff used to be in BSD rcp extern.h. */
                    200:
                    201: typedef struct {
                    202:        int cnt;
                    203:        char *buf;
                    204: } BUF;
                    205:
                    206: extern int iamremote;
                    207:
                    208: BUF    *allocbuf(BUF *, int, int);
                    209: char   *colon(char *);
                    210: void    lostconn(int);
                    211: void    nospace(void);
                    212: int     okname(char *);
                    213: void    run_err(const char *, ...);
                    214: void    verifydir(char *);
                    215:
                    216: /* Stuff from BSD rcp.c continues. */
                    217:
                    218: struct passwd *pwd;
                    219: uid_t  userid;
                    220: int errs, remin, remout;
                    221: int pflag, iamremote, iamrecursive, targetshouldbedirectory;
                    222:
                    223: #define        CMDNEEDS        64
                    224: char cmd[CMDNEEDS];            /* must hold "rcp -r -p -d\0" */
                    225:
                    226: int     response(void);
                    227: void    rsource(char *, struct stat *);
                    228: void    sink(int, char *[]);
                    229: void    source(int, char *[]);
                    230: void    tolocal(int, char *[]);
                    231: void    toremote(char *, int, char *[]);
                    232: void    usage(void);
                    233:
                    234: int
                    235: main(argc, argv)
                    236:        int argc;
                    237:        char *argv[];
                    238: {
                    239:        int ch, fflag, tflag;
                    240:        char *targ;
                    241:        extern char *optarg;
                    242:        extern int optind;
                    243:
                    244:        fflag = tflag = 0;
1.6       aaron     245:        while ((ch = getopt(argc, argv,  "dfprtvBCc:i:P:q")) != EOF)
1.1       deraadt   246:                switch(ch) {                    /* User-visible flags. */
                    247:                case 'p':
                    248:                        pflag = 1;
                    249:                        break;
                    250:                case 'P':
                    251:                        port = optarg;
                    252:                        break;
                    253:                case 'r':
                    254:                        iamrecursive = 1;
                    255:                        break;
                    256:                                                /* Server options. */
                    257:                case 'd':
                    258:                        targetshouldbedirectory = 1;
                    259:                        break;
                    260:                case 'f':                       /* "from" */
                    261:                        iamremote = 1;
                    262:                        fflag = 1;
                    263:                        break;
                    264:                case 't':                       /* "to" */
                    265:                        iamremote = 1;
                    266:                        tflag = 1;
                    267:                        break;
                    268:                case 'c':
                    269:                        cipher = optarg;
                    270:                        break;
                    271:                case 'i':
                    272:                        identity = optarg;
                    273:                        break;
                    274:                case 'v':
                    275:                        verbose = 1;
                    276:                        break;
                    277:                case 'B':
                    278:                        batchmode = 1;
                    279:                        break;
                    280:                case 'C':
                    281:                        compress = 1;
                    282:                        break;
1.6       aaron     283:                case 'q':
                    284:                        showprogress = 0;
                    285:                        break;
1.1       deraadt   286:                case '?':
                    287:                default:
                    288:                        usage();
                    289:                }
                    290:        argc -= optind;
                    291:        argv += optind;
                    292:
                    293:        if ((pwd = getpwuid(userid = getuid())) == NULL)
                    294:                fatal("unknown user %d", (int)userid);
                    295:
1.7       deraadt   296:        if (! isatty(STDERR_FILENO))
                    297:                showprogress = 0;
                    298:
1.1       deraadt   299:        remin = STDIN_FILENO;
                    300:        remout = STDOUT_FILENO;
                    301:
                    302:        if (fflag) {                    /* Follow "protocol", send data. */
                    303:                (void)response();
                    304:                source(argc, argv);
                    305:                exit(errs != 0);
                    306:        }
                    307:
                    308:        if (tflag) {                    /* Receive data. */
                    309:                sink(argc, argv);
                    310:                exit(errs != 0);
                    311:        }
                    312:
                    313:        if (argc < 2)
                    314:                usage();
                    315:        if (argc > 2)
                    316:                targetshouldbedirectory = 1;
                    317:
                    318:        remin = remout = -1;
                    319:        /* Command to be executed on remote system using "ssh". */
                    320:        (void)sprintf(cmd, "scp%s%s%s%s", verbose ? " -v" : "",
                    321:            iamrecursive ? " -r" : "", pflag ? " -p" : "",
                    322:            targetshouldbedirectory ? " -d" : "");
                    323:
                    324:        (void)signal(SIGPIPE, lostconn);
                    325:
                    326:        if ((targ = colon(argv[argc - 1])))     /* Dest is remote host. */
                    327:                toremote(targ, argc, argv);
                    328:        else {
                    329:                tolocal(argc, argv);            /* Dest is local host. */
                    330:                if (targetshouldbedirectory)
                    331:                        verifydir(argv[argc - 1]);
                    332:        }
                    333:        exit(errs != 0);
                    334: }
                    335:
                    336: void
                    337: toremote(targ, argc, argv)
                    338:        char *targ, *argv[];
                    339:        int argc;
                    340: {
                    341:        int i, len;
                    342:        char *bp, *host, *src, *suser, *thost, *tuser;
                    343:
                    344:        *targ++ = 0;
                    345:        if (*targ == 0)
                    346:                targ = ".";
                    347:
                    348:        if ((thost = strchr(argv[argc - 1], '@'))) {
                    349:                /* user@host */
                    350:                *thost++ = 0;
                    351:                tuser = argv[argc - 1];
                    352:                if (*tuser == '\0')
                    353:                        tuser = NULL;
                    354:                else if (!okname(tuser))
                    355:                        exit(1);
                    356:        } else {
                    357:                thost = argv[argc - 1];
                    358:                tuser = NULL;
                    359:        }
                    360:
                    361:        for (i = 0; i < argc - 1; i++) {
                    362:                src = colon(argv[i]);
                    363:                if (src) {                      /* remote to remote */
                    364:                        *src++ = 0;
                    365:                        if (*src == 0)
                    366:                                src = ".";
                    367:                        host = strchr(argv[i], '@');
                    368:                        len = strlen(SSH_PROGRAM) + strlen(argv[i]) +
                    369:                            strlen(src) + (tuser ? strlen(tuser) : 0) +
                    370:                            strlen(thost) + strlen(targ) + CMDNEEDS + 32;
                    371:                        bp = xmalloc(len);
                    372:                        if (host) {
                    373:                                *host++ = 0;
                    374:                                suser = argv[i];
                    375:                                if (*suser == '\0')
                    376:                                        suser = pwd->pw_name;
                    377:                                else if (!okname(suser))
                    378:                                        continue;
                    379:                                (void)sprintf(bp,
                    380:                                    "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'",
                    381:                                    SSH_PROGRAM, verbose ? " -v" : "",
                    382:                                    suser, host, cmd, src,
                    383:                                    tuser ? tuser : "", tuser ? "@" : "",
                    384:                                    thost, targ);
                    385:                        } else
                    386:                                (void)sprintf(bp,
                    387:                                    "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'",
                    388:                                    SSH_PROGRAM, verbose ? " -v" : "",
                    389:                                    argv[i], cmd, src,
                    390:                                    tuser ? tuser : "", tuser ? "@" : "",
                    391:                                    thost, targ);
                    392:                        if (verbose)
                    393:                          fprintf(stderr, "Executing: %s\n", bp);
                    394:                        (void)system(bp);
                    395:                        (void)xfree(bp);
                    396:                } else {                        /* local to remote */
                    397:                        if (remin == -1) {
                    398:                                len = strlen(targ) + CMDNEEDS + 20;
                    399:                                bp = xmalloc(len);
                    400:                                (void)sprintf(bp, "%s -t %s", cmd, targ);
                    401:                                host = thost;
                    402:                                if (do_cmd(host,  tuser,
                    403:                                           bp, &remin, &remout) < 0)
                    404:                                  exit(1);
                    405:                                if (response() < 0)
                    406:                                        exit(1);
                    407:                                (void)xfree(bp);
                    408:                        }
                    409:                        source(1, argv+i);
                    410:                }
                    411:        }
                    412: }
                    413:
                    414: void
                    415: tolocal(argc, argv)
                    416:        int argc;
                    417:        char *argv[];
                    418: {
                    419:        int i, len;
                    420:        char *bp, *host, *src, *suser;
                    421:
                    422:        for (i = 0; i < argc - 1; i++) {
                    423:                if (!(src = colon(argv[i]))) {          /* Local to local. */
                    424:                        len = strlen(_PATH_CP) + strlen(argv[i]) +
                    425:                            strlen(argv[argc - 1]) + 20;
                    426:                        bp = xmalloc(len);
                    427:                        (void)sprintf(bp, "exec %s%s%s %s %s", _PATH_CP,
                    428:                            iamrecursive ? " -r" : "", pflag ? " -p" : "",
                    429:                            argv[i], argv[argc - 1]);
                    430:                        if (verbose)
                    431:                          fprintf(stderr, "Executing: %s\n", bp);
                    432:                        if (system(bp))
                    433:                                ++errs;
                    434:                        (void)xfree(bp);
                    435:                        continue;
                    436:                }
                    437:                *src++ = 0;
                    438:                if (*src == 0)
                    439:                        src = ".";
                    440:                if ((host = strchr(argv[i], '@')) == NULL) {
                    441:                        host = argv[i];
                    442:                        suser = NULL;
                    443:                } else {
                    444:                        *host++ = 0;
                    445:                        suser = argv[i];
                    446:                        if (*suser == '\0')
                    447:                                suser = pwd->pw_name;
                    448:                        else if (!okname(suser))
                    449:                                continue;
                    450:                }
                    451:                len = strlen(src) + CMDNEEDS + 20;
                    452:                bp = xmalloc(len);
                    453:                (void)sprintf(bp, "%s -f %s", cmd, src);
                    454:                if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
                    455:                  (void)xfree(bp);
                    456:                  ++errs;
                    457:                  continue;
                    458:                }
                    459:                xfree(bp);
                    460:                sink(1, argv + argc - 1);
                    461:                (void)close(remin);
                    462:                remin = remout = -1;
                    463:        }
                    464: }
                    465:
                    466: void
                    467: source(argc, argv)
                    468:        int argc;
                    469:        char *argv[];
                    470: {
                    471:        struct stat stb;
                    472:        static BUF buffer;
                    473:        BUF *bp;
                    474:        off_t i;
                    475:        int amt, fd, haderr, indx, result;
                    476:        char *last, *name, buf[2048];
                    477:
                    478:        for (indx = 0; indx < argc; ++indx) {
                    479:                 name = argv[indx];
1.11    ! aaron     480:                statbytes = 0;
1.1       deraadt   481:                if ((fd = open(name, O_RDONLY, 0)) < 0)
                    482:                        goto syserr;
                    483:                if (fstat(fd, &stb) < 0) {
                    484: syserr:                        run_err("%s: %s", name, strerror(errno));
                    485:                        goto next;
                    486:                }
                    487:                switch (stb.st_mode & S_IFMT) {
                    488:                case S_IFREG:
                    489:                        break;
                    490:                case S_IFDIR:
                    491:                        if (iamrecursive) {
                    492:                                rsource(name, &stb);
                    493:                                goto next;
                    494:                        }
                    495:                        /* FALLTHROUGH */
                    496:                default:
                    497:                        run_err("%s: not a regular file", name);
                    498:                        goto next;
                    499:                }
                    500:                if ((last = strrchr(name, '/')) == NULL)
                    501:                        last = name;
                    502:                else
                    503:                        ++last;
1.11    ! aaron     504:                curfile = last;
1.1       deraadt   505:                if (pflag) {
                    506:                        /*
                    507:                         * Make it compatible with possible future
                    508:                         * versions expecting microseconds.
                    509:                         */
                    510:                        (void)sprintf(buf, "T%lu 0 %lu 0\n",
                    511:                                      (unsigned long)stb.st_mtime,
                    512:                                      (unsigned long)stb.st_atime);
                    513:                        (void)write(remout, buf, strlen(buf));
                    514:                        if (response() < 0)
                    515:                                goto next;
                    516:                }
                    517: #define        FILEMODEMASK    (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
                    518:                (void)sprintf(buf, "C%04o %lu %s\n",
                    519:                              (unsigned int)(stb.st_mode & FILEMODEMASK),
                    520:                              (unsigned long)stb.st_size,
                    521:                              last);
                    522:                if (verbose)
                    523:                  {
                    524:                    fprintf(stderr, "Sending file modes: %s", buf);
                    525:                    fflush(stderr);
                    526:                  }
                    527:                (void)write(remout, buf, strlen(buf));
                    528:                if (response() < 0)
                    529:                        goto next;
                    530:                if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
                    531: next:                  (void)close(fd);
                    532:                        continue;
                    533:                }
                    534:
1.4       aaron     535:                totalbytes = stb.st_size;
                    536:
                    537:                /* kick-start the progress meter */
1.6       aaron     538:                if(showprogress)
                    539:                        progressmeter(-1);
1.4       aaron     540:
1.1       deraadt   541:                /* Keep writing after an error so that we stay sync'd up. */
                    542:                for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
                    543:                        amt = bp->cnt;
                    544:                        if (i + amt > stb.st_size)
                    545:                                amt = stb.st_size - i;
                    546:                        if (!haderr) {
                    547:                                result = read(fd, bp->buf, amt);
                    548:                                if (result != amt)
                    549:                                        haderr = result >= 0 ? EIO : errno;
                    550:                        }
                    551:                        if (haderr)
                    552:                                (void)write(remout, bp->buf, amt);
                    553:                        else {
                    554:                                result = write(remout, bp->buf, amt);
                    555:                                if (result != amt)
                    556:                                        haderr = result >= 0 ? EIO : errno;
1.4       aaron     557:                                statbytes += result;
1.1       deraadt   558:                        }
                    559:                }
1.6       aaron     560:                if(showprogress)
                    561:                        progressmeter(1);
1.4       aaron     562:
1.1       deraadt   563:                if (close(fd) < 0 && !haderr)
                    564:                        haderr = errno;
                    565:                if (!haderr)
                    566:                        (void)write(remout, "", 1);
                    567:                else
                    568:                        run_err("%s: %s", name, strerror(haderr));
                    569:                (void)response();
                    570:        }
                    571: }
                    572:
                    573: void
                    574: rsource(name, statp)
                    575:        char *name;
                    576:        struct stat *statp;
                    577: {
                    578:        DIR *dirp;
                    579:        struct dirent *dp;
                    580:        char *last, *vect[1], path[1100];
                    581:
                    582:        if (!(dirp = opendir(name))) {
                    583:                run_err("%s: %s", name, strerror(errno));
                    584:                return;
                    585:        }
                    586:        last = strrchr(name, '/');
                    587:        if (last == 0)
                    588:                last = name;
                    589:        else
                    590:                last++;
                    591:        if (pflag) {
                    592:                (void)sprintf(path, "T%lu 0 %lu 0\n",
                    593:                              (unsigned long)statp->st_mtime,
                    594:                              (unsigned long)statp->st_atime);
                    595:                (void)write(remout, path, strlen(path));
                    596:                if (response() < 0) {
                    597:                        closedir(dirp);
                    598:                        return;
                    599:                }
                    600:        }
                    601:        (void)sprintf(path,
                    602:            "D%04o %d %.1024s\n", (unsigned int)(statp->st_mode & FILEMODEMASK),
                    603:                      0, last);
                    604:        if (verbose)
                    605:          fprintf(stderr, "Entering directory: %s", path);
                    606:        (void)write(remout, path, strlen(path));
                    607:        if (response() < 0) {
                    608:                closedir(dirp);
                    609:                return;
                    610:        }
                    611:        while ((dp = readdir(dirp))) {
                    612:                if (dp->d_ino == 0)
                    613:                        continue;
                    614:                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                    615:                        continue;
                    616:                if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
                    617:                        run_err("%s/%s: name too long", name, dp->d_name);
                    618:                        continue;
                    619:                }
                    620:                (void)sprintf(path, "%s/%s", name, dp->d_name);
                    621:                vect[0] = path;
                    622:                source(1, vect);
                    623:        }
                    624:        (void)closedir(dirp);
                    625:        (void)write(remout, "E\n", 2);
                    626:        (void)response();
                    627: }
                    628:
                    629: void
                    630: sink(argc, argv)
                    631:        int argc;
                    632:        char *argv[];
                    633: {
                    634:        static BUF buffer;
                    635:        struct stat stb;
                    636:        enum { YES, NO, DISPLAYED } wrerr;
                    637:        BUF *bp;
                    638:        off_t i, j;
                    639:        int amt, count, exists, first, mask, mode, ofd, omode;
                    640:        int setimes, size, targisdir, wrerrno = 0;
                    641:        char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
                    642:        struct utimbuf ut;
                    643:        int dummy_usec;
                    644:
                    645: #define        SCREWUP(str)    { why = str; goto screwup; }
                    646:
                    647:        setimes = targisdir = 0;
                    648:        mask = umask(0);
                    649:        if (!pflag)
                    650:                (void)umask(mask);
                    651:        if (argc != 1) {
                    652:                run_err("ambiguous target");
                    653:                exit(1);
                    654:        }
                    655:        targ = *argv;
                    656:        if (targetshouldbedirectory)
                    657:                verifydir(targ);
                    658:
                    659:        (void)write(remout, "", 1);
                    660:        if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
                    661:                targisdir = 1;
                    662:        for (first = 1;; first = 0) {
                    663:                cp = buf;
                    664:                if (read(remin, cp, 1) <= 0)
                    665:                        return;
                    666:                if (*cp++ == '\n')
                    667:                        SCREWUP("unexpected <newline>");
                    668:                do {
                    669:                        if (read(remin, &ch, sizeof(ch)) != sizeof(ch))
                    670:                                SCREWUP("lost connection");
                    671:                        *cp++ = ch;
                    672:                } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
                    673:                *cp = 0;
                    674:
                    675:                if (buf[0] == '\01' || buf[0] == '\02') {
                    676:                        if (iamremote == 0)
                    677:                                (void)write(STDERR_FILENO,
                    678:                                    buf + 1, strlen(buf + 1));
                    679:                        if (buf[0] == '\02')
                    680:                                exit(1);
                    681:                        ++errs;
                    682:                        continue;
                    683:                }
                    684:                if (buf[0] == 'E') {
                    685:                        (void)write(remout, "", 1);
                    686:                        return;
                    687:                }
                    688:
                    689:                if (ch == '\n')
                    690:                        *--cp = 0;
                    691:
                    692: #define getnum(t) (t) = 0; \
                    693:   while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0');
                    694:                cp = buf;
                    695:                if (*cp == 'T') {
                    696:                        setimes++;
                    697:                        cp++;
                    698:                        getnum(ut.modtime);
                    699:                        if (*cp++ != ' ')
                    700:                                SCREWUP("mtime.sec not delimited");
                    701:                        getnum(dummy_usec);
                    702:                        if (*cp++ != ' ')
                    703:                                SCREWUP("mtime.usec not delimited");
                    704:                        getnum(ut.actime);
                    705:                        if (*cp++ != ' ')
                    706:                                SCREWUP("atime.sec not delimited");
                    707:                        getnum(dummy_usec);
                    708:                        if (*cp++ != '\0')
                    709:                                SCREWUP("atime.usec not delimited");
                    710:                        (void)write(remout, "", 1);
                    711:                        continue;
                    712:                }
                    713:                if (*cp != 'C' && *cp != 'D') {
                    714:                        /*
                    715:                         * Check for the case "rcp remote:foo\* local:bar".
                    716:                         * In this case, the line "No match." can be returned
                    717:                         * by the shell before the rcp command on the remote is
                    718:                         * executed so the ^Aerror_message convention isn't
                    719:                         * followed.
                    720:                         */
                    721:                        if (first) {
                    722:                                run_err("%s", cp);
                    723:                                exit(1);
                    724:                        }
                    725:                        SCREWUP("expected control record");
                    726:                }
                    727:                mode = 0;
                    728:                for (++cp; cp < buf + 5; cp++) {
                    729:                        if (*cp < '0' || *cp > '7')
                    730:                                SCREWUP("bad mode");
                    731:                        mode = (mode << 3) | (*cp - '0');
                    732:                }
                    733:                if (*cp++ != ' ')
                    734:                        SCREWUP("mode not delimited");
                    735:
                    736:                for (size = 0; *cp >= '0' && *cp <= '9';)
                    737:                        size = size * 10 + (*cp++ - '0');
                    738:                if (*cp++ != ' ')
                    739:                        SCREWUP("size not delimited");
                    740:                if (targisdir) {
                    741:                        static char *namebuf;
                    742:                        static int cursize;
                    743:                        size_t need;
                    744:
                    745:                        need = strlen(targ) + strlen(cp) + 250;
                    746:                        if (need > cursize)
                    747:                          namebuf = xmalloc(need);
                    748:                        (void)sprintf(namebuf, "%s%s%s", targ,
                    749:                            *targ ? "/" : "", cp);
                    750:                        np = namebuf;
                    751:                } else
                    752:                        np = targ;
                    753:                exists = stat(np, &stb) == 0;
                    754:                if (buf[0] == 'D') {
                    755:                        int mod_flag = pflag;
                    756:                        if (exists) {
                    757:                                if (!S_ISDIR(stb.st_mode)) {
                    758:                                        errno = ENOTDIR;
                    759:                                        goto bad;
                    760:                                }
                    761:                                if (pflag)
                    762:                                        (void)chmod(np, mode);
                    763:                        } else {
                    764:                                /* Handle copying from a read-only directory */
                    765:                                mod_flag = 1;
                    766:                                if (mkdir(np, mode | S_IRWXU) < 0)
                    767:                                        goto bad;
                    768:                        }
                    769:                        vect[0] = np;
                    770:                        sink(1, vect);
                    771:                        if (setimes) {
                    772:                                setimes = 0;
                    773:                                if (utime(np, &ut) < 0)
                    774:                                    run_err("%s: set times: %s",
                    775:                                        np, strerror(errno));
                    776:                        }
                    777:                        if (mod_flag)
                    778:                                (void)chmod(np, mode);
                    779:                        continue;
                    780:                }
                    781:                omode = mode;
                    782:                mode |= S_IWRITE;
                    783:                if ((ofd = open(np, O_WRONLY|O_CREAT|O_TRUNC, mode)) < 0) {
                    784: bad:                   run_err("%s: %s", np, strerror(errno));
                    785:                        continue;
                    786:                }
                    787:                (void)write(remout, "", 1);
                    788:                if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
                    789:                        (void)close(ofd);
                    790:                        continue;
                    791:                }
                    792:                cp = bp->buf;
                    793:                wrerr = NO;
1.7       deraadt   794:
                    795:                if (showprogress) {
                    796:                        totalbytes = size;
                    797:                        progressmeter(-1);
                    798:                }
1.1       deraadt   799:                for (count = i = 0; i < size; i += 4096) {
                    800:                        amt = 4096;
                    801:                        if (i + amt > size)
                    802:                                amt = size - i;
                    803:                        count += amt;
                    804:                        do {
                    805:                                j = read(remin, cp, amt);
                    806:                                if (j <= 0) {
                    807:                                        run_err("%s", j ? strerror(errno) :
                    808:                                            "dropped connection");
                    809:                                        exit(1);
                    810:                                }
                    811:                                amt -= j;
                    812:                                cp += j;
1.7       deraadt   813:                        statbytes += j;
1.1       deraadt   814:                        } while (amt > 0);
                    815:                        if (count == bp->cnt) {
                    816:                                /* Keep reading so we stay sync'd up. */
                    817:                                if (wrerr == NO) {
                    818:                                        j = write(ofd, bp->buf, count);
                    819:                                        if (j != count) {
                    820:                                                wrerr = YES;
                    821:                                                wrerrno = j >= 0 ? EIO : errno;
                    822:                                        }
                    823:                                }
                    824:                                count = 0;
                    825:                                cp = bp->buf;
                    826:                        }
                    827:                }
1.7       deraadt   828:                if (showprogress)
                    829:                        progressmeter(1);
1.1       deraadt   830:                if (count != 0 && wrerr == NO &&
                    831:                    (j = write(ofd, bp->buf, count)) != count) {
                    832:                        wrerr = YES;
                    833:                        wrerrno = j >= 0 ? EIO : errno;
                    834:                }
                    835: #if 0
                    836:                if (ftruncate(ofd, size)) {
                    837:                        run_err("%s: truncate: %s", np, strerror(errno));
                    838:                        wrerr = DISPLAYED;
                    839:                }
                    840: #endif
                    841:                if (pflag) {
                    842:                        if (exists || omode != mode)
                    843:                                if (fchmod(ofd, omode))
                    844:                                        run_err("%s: set mode: %s",
                    845:                                            np, strerror(errno));
                    846:                } else {
                    847:                        if (!exists && omode != mode)
                    848:                                if (fchmod(ofd, omode & ~mask))
                    849:                                        run_err("%s: set mode: %s",
                    850:                                            np, strerror(errno));
                    851:                }
                    852:                (void)close(ofd);
                    853:                (void)response();
                    854:                if (setimes && wrerr == NO) {
                    855:                        setimes = 0;
                    856:                        if (utime(np, &ut) < 0) {
                    857:                                run_err("%s: set times: %s",
                    858:                                    np, strerror(errno));
                    859:                                wrerr = DISPLAYED;
                    860:                        }
                    861:                }
                    862:                switch(wrerr) {
                    863:                case YES:
                    864:                        run_err("%s: %s", np, strerror(wrerrno));
                    865:                        break;
                    866:                case NO:
                    867:                        (void)write(remout, "", 1);
                    868:                        break;
                    869:                case DISPLAYED:
                    870:                        break;
                    871:                }
                    872:        }
                    873: screwup:
                    874:        run_err("protocol error: %s", why);
                    875:        exit(1);
                    876: }
                    877:
                    878: int
                    879: response()
                    880: {
                    881:        char ch, *cp, resp, rbuf[2048];
                    882:
                    883:        if (read(remin, &resp, sizeof(resp)) != sizeof(resp))
                    884:                lostconn(0);
                    885:
                    886:        cp = rbuf;
                    887:        switch(resp) {
                    888:        case 0:                         /* ok */
                    889:                return (0);
                    890:        default:
                    891:                *cp++ = resp;
                    892:                /* FALLTHROUGH */
                    893:        case 1:                         /* error, followed by error msg */
                    894:        case 2:                         /* fatal error, "" */
                    895:                do {
                    896:                        if (read(remin, &ch, sizeof(ch)) != sizeof(ch))
                    897:                                lostconn(0);
                    898:                        *cp++ = ch;
                    899:                } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
                    900:
                    901:                if (!iamremote)
                    902:                        (void)write(STDERR_FILENO, rbuf, cp - rbuf);
                    903:                ++errs;
                    904:                if (resp == 1)
                    905:                        return (-1);
                    906:                exit(1);
                    907:        }
                    908:        /* NOTREACHED */
                    909: }
                    910:
                    911: void
                    912: usage()
                    913: {
                    914:        (void)fprintf(stderr,
1.6       aaron     915:            "usage: scp [-pqrvC] [-P port] [-c cipher] [-i identity] f1 f2; or:\n       scp [options] f1 ... fn directory\n");
1.1       deraadt   916:        exit(1);
                    917: }
                    918:
                    919: void
                    920: run_err(const char *fmt, ...)
                    921: {
                    922:        static FILE *fp;
                    923:        va_list ap;
                    924:        va_start(ap, fmt);
                    925:
                    926:        ++errs;
                    927:        if (fp == NULL && !(fp = fdopen(remout, "w")))
                    928:                return;
                    929:        (void)fprintf(fp, "%c", 0x01);
                    930:        (void)fprintf(fp, "scp: ");
                    931:        (void)vfprintf(fp, fmt, ap);
                    932:        (void)fprintf(fp, "\n");
                    933:        (void)fflush(fp);
                    934:
                    935:        if (!iamremote)
                    936:          {
                    937:            vfprintf(stderr, fmt, ap);
                    938:            fprintf(stderr, "\n");
                    939:          }
                    940:
                    941:        va_end(ap);
                    942: }
                    943:
                    944: /* Stuff below is from BSD rcp util.c. */
                    945:
                    946: /*-
                    947:  * Copyright (c) 1992, 1993
                    948:  *     The Regents of the University of California.  All rights reserved.
                    949:  *
                    950:  * Redistribution and use in source and binary forms, with or without
                    951:  * modification, are permitted provided that the following conditions
                    952:  * are met:
                    953:  * 1. Redistributions of source code must retain the above copyright
                    954:  *    notice, this list of conditions and the following disclaimer.
                    955:  * 2. Redistributions in binary form must reproduce the above copyright
                    956:  *    notice, this list of conditions and the following disclaimer in the
                    957:  *    documentation and/or other materials provided with the distribution.
                    958:  * 3. All advertising materials mentioning features or use of this software
                    959:  *    must display the following acknowledgement:
                    960:  *     This product includes software developed by the University of
                    961:  *     California, Berkeley and its contributors.
                    962:  * 4. Neither the name of the University nor the names of its contributors
                    963:  *    may be used to endorse or promote products derived from this software
                    964:  *    without specific prior written permission.
                    965:  *
                    966:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                    967:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                    968:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                    969:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                    970:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                    971:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                    972:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                    973:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                    974:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                    975:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                    976:  * SUCH DAMAGE.
                    977:  *
1.11    ! aaron     978:  *     $Id: scp.c,v 1.10 1999/10/04 01:59:56 deraadt Exp $
1.1       deraadt   979:  */
                    980:
                    981: char *
                    982: colon(cp)
                    983:        char *cp;
                    984: {
                    985:        if (*cp == ':')         /* Leading colon is part of file name. */
                    986:                return (0);
                    987:
                    988:        for (; *cp; ++cp) {
                    989:                if (*cp == ':')
                    990:                        return (cp);
                    991:                if (*cp == '/')
                    992:                        return (0);
                    993:        }
                    994:        return (0);
                    995: }
                    996:
                    997: void
                    998: verifydir(cp)
                    999:        char *cp;
                   1000: {
                   1001:        struct stat stb;
                   1002:
                   1003:        if (!stat(cp, &stb)) {
                   1004:                if (S_ISDIR(stb.st_mode))
                   1005:                        return;
                   1006:                errno = ENOTDIR;
                   1007:        }
                   1008:        run_err("%s: %s", cp, strerror(errno));
                   1009:        exit(1);
                   1010: }
                   1011:
                   1012: int
                   1013: okname(cp0)
                   1014:        char *cp0;
                   1015: {
                   1016:        int c;
                   1017:        char *cp;
                   1018:
                   1019:        cp = cp0;
                   1020:        do {
                   1021:                c = *cp;
                   1022:                if (c & 0200)
                   1023:                        goto bad;
                   1024:                if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
                   1025:                        goto bad;
                   1026:        } while (*++cp);
                   1027:        return (1);
                   1028:
                   1029: bad:   fprintf(stderr, "%s: invalid user name", cp0);
                   1030:        return (0);
                   1031: }
                   1032:
                   1033: BUF *
                   1034: allocbuf(bp, fd, blksize)
                   1035:        BUF *bp;
                   1036:        int fd, blksize;
                   1037: {
                   1038:        size_t size;
                   1039:        struct stat stb;
                   1040:
                   1041:        if (fstat(fd, &stb) < 0) {
                   1042:                run_err("fstat: %s", strerror(errno));
                   1043:                return (0);
                   1044:        }
                   1045:         if (stb.st_blksize == 0)
                   1046:          size = blksize;
                   1047:         else
                   1048:          size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
1.3       deraadt  1049:            stb.st_blksize;
1.1       deraadt  1050:        if (bp->cnt >= size)
                   1051:                return (bp);
                   1052:        if (bp->buf == NULL)
                   1053:          bp->buf = xmalloc(size);
                   1054:        else
                   1055:          bp->buf = xrealloc(bp->buf, size);
                   1056:        bp->cnt = size;
                   1057:        return (bp);
                   1058: }
                   1059:
                   1060: void
                   1061: lostconn(signo)
                   1062:        int signo;
                   1063: {
                   1064:        if (!iamremote)
                   1065:                fprintf(stderr, "lost connection\n");
                   1066:        exit(1);
                   1067: }
1.4       aaron    1068:
1.10      deraadt  1069: /*
                   1070:  * ensure all of data on socket comes through. f==read || f==write
                   1071:  */
                   1072: int
                   1073: atomicio(f, fd, s, n)
                   1074: int (*f)();
                   1075: char *s;
                   1076: {
                   1077:        int res, pos = 0;
                   1078:
                   1079:        while (n>pos) {
                   1080:                res = (f)(fd, s+pos, n-pos);
                   1081:                switch (res) {
                   1082:                case -1:
                   1083:                        if (errno==EINTR || errno==EAGAIN)
                   1084:                                continue;
                   1085:                case 0:
                   1086:                        return (res);
                   1087:                default:
                   1088:                        pos += res;
                   1089:                }
                   1090:        }
                   1091:        return (pos);
                   1092: }
                   1093:
1.8       deraadt  1094: void
                   1095: alarmtimer(int wait)
1.4       aaron    1096: {
                   1097:    struct itimerval itv;
                   1098:
                   1099:    itv.it_value.tv_sec = wait;
                   1100:    itv.it_value.tv_usec = 0;
                   1101:    itv.it_interval = itv.it_value;
                   1102:    setitimer(ITIMER_REAL, &itv, NULL);
                   1103: }
                   1104:
1.8       deraadt  1105: void
                   1106: updateprogressmeter(void)
1.4       aaron    1107: {
1.9       deraadt  1108:        int save_errno = errno;
                   1109:
1.4       aaron    1110:        progressmeter(0);
1.9       deraadt  1111:        errno = save_errno;
1.4       aaron    1112: }
                   1113:
1.8       deraadt  1114: void
                   1115: progressmeter(int flag)
1.4       aaron    1116: {
                   1117:        static const char prefixes[] = " KMGTP";
                   1118:        static struct timeval lastupdate;
1.11    ! aaron    1119:        static off_t lastsize;
1.4       aaron    1120:        struct timeval now, td, wait;
                   1121:        off_t cursize, abbrevsize;
                   1122:        double elapsed;
                   1123:        int ratio, barlength, i, remaining;
                   1124:        char buf[256];
                   1125:
                   1126:        if (flag == -1) {
                   1127:                (void)gettimeofday(&start, (struct timezone *)0);
                   1128:                lastupdate = start;
1.11    ! aaron    1129:                lastsize = 0;
1.4       aaron    1130:        }
                   1131:        (void)gettimeofday(&now, (struct timezone *)0);
                   1132:        cursize = statbytes;
                   1133:        ratio = cursize * 100 / totalbytes;
                   1134:        ratio = MAX(ratio, 0);
                   1135:        ratio = MIN(ratio, 100);
1.11    ! aaron    1136:        snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio);
1.4       aaron    1137:
1.11    ! aaron    1138:        barlength = getttywidth() - 51;
1.4       aaron    1139:        if (barlength > 0) {
                   1140:                i = barlength * ratio / 100;
                   1141:                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
                   1142:                "|%.*s%*s|", i,
                   1143: "*****************************************************************************"
                   1144: "*****************************************************************************",
                   1145:                  barlength - i, "");
                   1146:        }
                   1147:
                   1148:        i = 0;
                   1149:        abbrevsize = cursize;
                   1150:        while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
                   1151:                i++;
                   1152:                abbrevsize >>= 10;
                   1153:        }
                   1154:        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5qd %c%c ",
1.10      deraadt  1155:            (quad_t)abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' :
                   1156:            'B');
1.4       aaron    1157:
                   1158:        timersub(&now, &lastupdate, &wait);
                   1159:        if (cursize > lastsize) {
                   1160:                lastupdate = now;
                   1161:                lastsize = cursize;
                   1162:                if (wait.tv_sec >= STALLTIME) {
                   1163:                        start.tv_sec += wait.tv_sec;
                   1164:                        start.tv_usec += wait.tv_usec;
                   1165:                }
                   1166:                wait.tv_sec = 0;
                   1167:        }
                   1168:
                   1169:        timersub(&now, &start, &td);
1.6       aaron    1170:        elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
1.4       aaron    1171:
                   1172:        if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) {
                   1173:                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1.10      deraadt  1174:                    "   --:-- ETA");
1.4       aaron    1175:        } else if (wait.tv_sec >= STALLTIME) {
                   1176:                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1.10      deraadt  1177:                    " - stalled -");
1.4       aaron    1178:        } else {
                   1179:                remaining = (int)(totalbytes / (statbytes / elapsed) - elapsed);
                   1180:                i = elapsed / 3600;
                   1181:                if (i)
                   1182:                        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1.10      deraadt  1183:                            "%2d:", i);
1.4       aaron    1184:                else
                   1185:                        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1.10      deraadt  1186:                            "   ");
1.4       aaron    1187:                i = remaining % 3600;
                   1188:                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1.10      deraadt  1189:                    "%02d:%02d ETA", i / 60, i % 60);
1.4       aaron    1190:        }
1.10      deraadt  1191:        atomicio(write, fileno(stdout), buf, strlen(buf));
1.4       aaron    1192:
                   1193:        if (flag == -1) {
                   1194:                signal(SIGALRM, (void *)updateprogressmeter);
                   1195:                alarmtimer(1);
                   1196:        } else if (flag == 1) {
1.10      deraadt  1197:                alarmtimer(0);
                   1198:                write(fileno(stdout), "\n", 1);
1.4       aaron    1199:        }
                   1200: }
                   1201:
1.8       deraadt  1202: int
                   1203: getttywidth(void)
1.4       aaron    1204: {
                   1205:        struct winsize winsize;
                   1206:
                   1207:        if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
                   1208:                return(winsize.ws_col ? winsize.ws_col : 80);
                   1209:        else
                   1210:                return(80);
                   1211: }
                   1212:
                   1213: