10Sstevel@tonic-gate /*
29194SHuie-Ying.Lee@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /*
60Sstevel@tonic-gate * scp - secure remote copy. This is basically patched BSD rcp which
70Sstevel@tonic-gate * uses ssh to do the data transfer (instead of using rcmd).
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * NOTE: This version should NOT be suid root. (This uses ssh to
100Sstevel@tonic-gate * do the transfer and ssh has the necessary privileges.)
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
150Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
160Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
170Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
180Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
190Sstevel@tonic-gate */
200Sstevel@tonic-gate /*
210Sstevel@tonic-gate * Copyright (c) 1999 Theo de Raadt. All rights reserved.
220Sstevel@tonic-gate * Copyright (c) 1999 Aaron Campbell. All rights reserved.
230Sstevel@tonic-gate *
240Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
250Sstevel@tonic-gate * modification, are permitted provided that the following conditions
260Sstevel@tonic-gate * are met:
270Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
280Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
290Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
300Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
310Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
340Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
350Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
360Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
370Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
380Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
390Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
400Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
410Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
420Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
430Sstevel@tonic-gate */
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Parts from:
470Sstevel@tonic-gate *
480Sstevel@tonic-gate * Copyright (c) 1983, 1990, 1992, 1993, 1995
490Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
500Sstevel@tonic-gate *
510Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
520Sstevel@tonic-gate * modification, are permitted provided that the following conditions
530Sstevel@tonic-gate * are met:
540Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
550Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
560Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
570Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
580Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
590Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
600Sstevel@tonic-gate * must display the following acknowledgement:
610Sstevel@tonic-gate * This product includes software developed by the University of
620Sstevel@tonic-gate * California, Berkeley and its contributors.
630Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
640Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
650Sstevel@tonic-gate * without specific prior written permission.
660Sstevel@tonic-gate *
670Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
680Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
690Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
700Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
710Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
720Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
730Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
740Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
750Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
760Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
770Sstevel@tonic-gate * SUCH DAMAGE.
780Sstevel@tonic-gate *
790Sstevel@tonic-gate */
800Sstevel@tonic-gate
810Sstevel@tonic-gate #include "includes.h"
820Sstevel@tonic-gate RCSID("$OpenBSD: scp.c,v 1.91 2002/06/19 00:27:55 deraadt Exp $");
830Sstevel@tonic-gate
840Sstevel@tonic-gate #include "xmalloc.h"
850Sstevel@tonic-gate #include "atomicio.h"
860Sstevel@tonic-gate #include "pathnames.h"
870Sstevel@tonic-gate #include "log.h"
880Sstevel@tonic-gate #include "misc.h"
890Sstevel@tonic-gate
900Sstevel@tonic-gate #ifdef HAVE___PROGNAME
910Sstevel@tonic-gate extern char *__progname;
920Sstevel@tonic-gate #else
930Sstevel@tonic-gate char *__progname;
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate
960Sstevel@tonic-gate /* For progressmeter() -- number of seconds before xfer considered "stalled" */
970Sstevel@tonic-gate #define STALLTIME 5
980Sstevel@tonic-gate /* alarm() interval for updating progress meter */
990Sstevel@tonic-gate #define PROGRESSTIME 1
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /* Visual statistics about files as they are transferred. */
1020Sstevel@tonic-gate void progressmeter(int);
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /* Returns width of the terminal (for progress meter calculations). */
1050Sstevel@tonic-gate int getttywidth(void);
1060Sstevel@tonic-gate int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout,
1070Sstevel@tonic-gate int argc);
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /* Struct for addargs */
1100Sstevel@tonic-gate arglist args;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /* Time a transfer started. */
1130Sstevel@tonic-gate static struct timeval start;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /* Number of bytes of current file transferred so far. */
1160Sstevel@tonic-gate volatile off_t statbytes;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /* Total size of current file. */
1190Sstevel@tonic-gate off_t totalbytes = 0;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate /* Name of current file being transferred. */
1220Sstevel@tonic-gate char *curfile;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /* This is set to non-zero to enable verbose mode. */
1250Sstevel@tonic-gate int verbose_mode = 0;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /* This is set to zero if the progressmeter is not desired. */
1280Sstevel@tonic-gate int showprogress = 1;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /* This is the program to execute for the secured connection. ("ssh" or -S) */
1310Sstevel@tonic-gate char *ssh_program = _PATH_SSH_PROGRAM;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /* This is used to store the pid of ssh_program */
1342780Sjp161948 static pid_t do_cmd_pid = -1;
1352780Sjp161948
1362780Sjp161948 static void
killchild(int signo)1372780Sjp161948 killchild(int signo)
1382780Sjp161948 {
1392780Sjp161948 if (do_cmd_pid > 1) {
1402780Sjp161948 kill(do_cmd_pid, signo ? signo : SIGTERM);
1412780Sjp161948 waitpid(do_cmd_pid, NULL, 0);
1422780Sjp161948 }
1432780Sjp161948
1442780Sjp161948 if (signo)
1452780Sjp161948 _exit(1);
1462780Sjp161948 exit(1);
1472780Sjp161948 }
1482780Sjp161948
1492780Sjp161948 /*
1502780Sjp161948 * Run a command via fork(2)/exec(2). This can be a local-to-local copy via
1512780Sjp161948 * cp(1) or one side of a remote-to-remote copy. We must not use system(3) here
1522780Sjp161948 * because we don't want filenames to go through a command expansion in the
1532780Sjp161948 * underlying shell. Note that the user can create a filename that is a piece of
1542780Sjp161948 * shell code itself and this must not be executed.
1552780Sjp161948 */
1562780Sjp161948 static int
do_local_cmd(arglist * a)1572780Sjp161948 do_local_cmd(arglist *a)
1582780Sjp161948 {
1592780Sjp161948 uint_t i;
1602780Sjp161948 int status;
1612780Sjp161948 pid_t pid;
1622780Sjp161948
1632780Sjp161948 if (a->num == 0)
1642780Sjp161948 fatal("do_local_cmd: no arguments");
1652780Sjp161948
1662780Sjp161948 if (verbose_mode) {
1672780Sjp161948 fprintf(stderr, gettext("Executing:"));
1682780Sjp161948 for (i = 0; i < a->num; i++)
1692780Sjp161948 fprintf(stderr, " %s", a->list[i]);
1702780Sjp161948 fprintf(stderr, "\n");
1712780Sjp161948 }
1722780Sjp161948 if ((pid = fork()) == -1)
1732780Sjp161948 fatal("do_local_cmd: fork: %s", strerror(errno));
1742780Sjp161948
1752780Sjp161948 if (pid == 0) {
1762780Sjp161948 execvp(a->list[0], a->list);
1772780Sjp161948 perror(a->list[0]);
1782780Sjp161948 exit(1);
1792780Sjp161948 }
1802780Sjp161948
1812780Sjp161948 do_cmd_pid = pid;
1822780Sjp161948 signal(SIGTERM, killchild);
1832780Sjp161948 signal(SIGINT, killchild);
1842780Sjp161948 signal(SIGHUP, killchild);
1852780Sjp161948
1862780Sjp161948 while (waitpid(pid, &status, 0) == -1)
1872780Sjp161948 if (errno != EINTR)
1882780Sjp161948 fatal("do_local_cmd: waitpid: %s", strerror(errno));
1892780Sjp161948
1902780Sjp161948 do_cmd_pid = -1;
1912780Sjp161948
1922780Sjp161948 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1932780Sjp161948 return (-1);
1942780Sjp161948
1952780Sjp161948 return (0);
1962780Sjp161948 }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * This function executes the given command as the specified user on the
2000Sstevel@tonic-gate * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
2010Sstevel@tonic-gate * assigns the input and output file descriptors on success.
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate int
do_cmd(char * host,char * remuser,char * cmd,int * fdin,int * fdout,int argc)2050Sstevel@tonic-gate do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate int pin[2], pout[2], reserved[2];
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate if (verbose_mode)
2100Sstevel@tonic-gate fprintf(stderr,
2110Sstevel@tonic-gate gettext("Executing: program %s host %s, "
2120Sstevel@tonic-gate "user %s, command %s\n"),
2130Sstevel@tonic-gate ssh_program, host,
2140Sstevel@tonic-gate remuser ? remuser : gettext("(unspecified)"), cmd);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * Reserve two descriptors so that the real pipes won't get
2180Sstevel@tonic-gate * descriptors 0 and 1 because that will screw up dup2 below.
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate pipe(reserved);
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /* Create a socket pair for communicating with ssh. */
2230Sstevel@tonic-gate if (pipe(pin) < 0)
2240Sstevel@tonic-gate fatal("pipe: %s", strerror(errno));
2250Sstevel@tonic-gate if (pipe(pout) < 0)
2260Sstevel@tonic-gate fatal("pipe: %s", strerror(errno));
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate /* Free the reserved descriptors. */
2290Sstevel@tonic-gate close(reserved[0]);
2300Sstevel@tonic-gate close(reserved[1]);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /* For a child to execute the command on the remote host using ssh. */
2330Sstevel@tonic-gate if ((do_cmd_pid = fork()) == 0) {
2340Sstevel@tonic-gate /* Child. */
2350Sstevel@tonic-gate close(pin[1]);
2360Sstevel@tonic-gate close(pout[0]);
2370Sstevel@tonic-gate dup2(pin[0], 0);
2380Sstevel@tonic-gate dup2(pout[1], 1);
2390Sstevel@tonic-gate close(pin[0]);
2400Sstevel@tonic-gate close(pout[1]);
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate args.list[0] = ssh_program;
2430Sstevel@tonic-gate if (remuser != NULL)
2440Sstevel@tonic-gate addargs(&args, "-l%s", remuser);
2450Sstevel@tonic-gate addargs(&args, "%s", host);
2460Sstevel@tonic-gate addargs(&args, "%s", cmd);
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate execvp(ssh_program, args.list);
2490Sstevel@tonic-gate perror(ssh_program);
2500Sstevel@tonic-gate exit(1);
2510Sstevel@tonic-gate } else if (do_cmd_pid == (pid_t)-1) {
2520Sstevel@tonic-gate /* fork() failed */
2530Sstevel@tonic-gate fatal("fork: %s", strerror(errno));
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate /* Parent. Close the other side, and return the local side. */
2570Sstevel@tonic-gate close(pin[0]);
2580Sstevel@tonic-gate *fdout = pin[1];
2590Sstevel@tonic-gate close(pout[1]);
2600Sstevel@tonic-gate *fdin = pout[0];
2610Sstevel@tonic-gate return (0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate typedef struct {
2650Sstevel@tonic-gate int cnt;
2660Sstevel@tonic-gate char *buf;
2670Sstevel@tonic-gate } BUF;
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate BUF *allocbuf(BUF *, int, int);
2700Sstevel@tonic-gate void lostconn(int);
2710Sstevel@tonic-gate void nospace(void);
2720Sstevel@tonic-gate int okname(char *);
2730Sstevel@tonic-gate void run_err(const char *, ...);
2740Sstevel@tonic-gate void verifydir(char *);
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate struct passwd *pwd;
2770Sstevel@tonic-gate uid_t userid;
2780Sstevel@tonic-gate int errs, remin, remout;
2790Sstevel@tonic-gate int pflag, iamremote, iamrecursive, targetshouldbedirectory;
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate #define CMDNEEDS 64
2820Sstevel@tonic-gate char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate int response(void);
2850Sstevel@tonic-gate void rsource(char *, struct stat *);
2860Sstevel@tonic-gate void sink(int, char *[]);
2870Sstevel@tonic-gate void source(int, char *[]);
2880Sstevel@tonic-gate void tolocal(int, char *[]);
2890Sstevel@tonic-gate void toremote(char *, int, char *[]);
2900Sstevel@tonic-gate void usage(void);
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate int
main(argc,argv)2930Sstevel@tonic-gate main(argc, argv)
2940Sstevel@tonic-gate int argc;
2950Sstevel@tonic-gate char *argv[];
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate int ch, fflag, tflag, status;
2980Sstevel@tonic-gate char *targ;
2990Sstevel@tonic-gate extern char *optarg;
3000Sstevel@tonic-gate extern int optind;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate __progname = get_progname(argv[0]);
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate g11n_setlocale(LC_ALL, "");
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate args.list = NULL;
3070Sstevel@tonic-gate addargs(&args, "ssh"); /* overwritten with ssh_program */
3080Sstevel@tonic-gate addargs(&args, "-x");
3090Sstevel@tonic-gate addargs(&args, "-oForwardAgent no");
3100Sstevel@tonic-gate addargs(&args, "-oClearAllForwardings yes");
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate fflag = tflag = 0;
3130Sstevel@tonic-gate while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
3140Sstevel@tonic-gate switch (ch) {
3150Sstevel@tonic-gate /* User-visible flags. */
3160Sstevel@tonic-gate case '4':
3170Sstevel@tonic-gate case '6':
3180Sstevel@tonic-gate case 'C':
3190Sstevel@tonic-gate addargs(&args, "-%c", ch);
3200Sstevel@tonic-gate break;
3210Sstevel@tonic-gate case 'o':
3220Sstevel@tonic-gate case 'c':
3230Sstevel@tonic-gate case 'i':
3240Sstevel@tonic-gate case 'F':
3250Sstevel@tonic-gate addargs(&args, "-%c%s", ch, optarg);
3260Sstevel@tonic-gate break;
3270Sstevel@tonic-gate case 'P':
3280Sstevel@tonic-gate addargs(&args, "-p%s", optarg);
3290Sstevel@tonic-gate break;
3300Sstevel@tonic-gate case 'B':
3310Sstevel@tonic-gate addargs(&args, "-oBatchmode yes");
3320Sstevel@tonic-gate break;
3330Sstevel@tonic-gate case 'p':
3340Sstevel@tonic-gate pflag = 1;
3350Sstevel@tonic-gate break;
3360Sstevel@tonic-gate case 'r':
3370Sstevel@tonic-gate iamrecursive = 1;
3380Sstevel@tonic-gate break;
3390Sstevel@tonic-gate case 'S':
3400Sstevel@tonic-gate ssh_program = xstrdup(optarg);
3410Sstevel@tonic-gate break;
3420Sstevel@tonic-gate case 'v':
3430Sstevel@tonic-gate addargs(&args, "-v");
3440Sstevel@tonic-gate verbose_mode = 1;
3450Sstevel@tonic-gate break;
3460Sstevel@tonic-gate case 'q':
3470Sstevel@tonic-gate showprogress = 0;
3480Sstevel@tonic-gate break;
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /* Server options. */
3510Sstevel@tonic-gate case 'd':
3520Sstevel@tonic-gate targetshouldbedirectory = 1;
3530Sstevel@tonic-gate break;
3540Sstevel@tonic-gate case 'f': /* "from" */
3550Sstevel@tonic-gate iamremote = 1;
3560Sstevel@tonic-gate fflag = 1;
3570Sstevel@tonic-gate break;
3580Sstevel@tonic-gate case 't': /* "to" */
3590Sstevel@tonic-gate iamremote = 1;
3600Sstevel@tonic-gate tflag = 1;
3610Sstevel@tonic-gate #ifdef HAVE_CYGWIN
3620Sstevel@tonic-gate setmode(0, O_BINARY);
3630Sstevel@tonic-gate #endif
3640Sstevel@tonic-gate break;
3650Sstevel@tonic-gate default:
3660Sstevel@tonic-gate usage();
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate argc -= optind;
3690Sstevel@tonic-gate argv += optind;
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate if ((pwd = getpwuid(userid = getuid())) == NULL)
3720Sstevel@tonic-gate fatal("unknown user %d", (int)userid);
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate if (!isatty(STDERR_FILENO))
3750Sstevel@tonic-gate showprogress = 0;
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate remin = STDIN_FILENO;
3780Sstevel@tonic-gate remout = STDOUT_FILENO;
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate if (fflag) {
3810Sstevel@tonic-gate /* Follow "protocol", send data. */
3820Sstevel@tonic-gate (void) response();
3830Sstevel@tonic-gate source(argc, argv);
3840Sstevel@tonic-gate exit(errs != 0);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate if (tflag) {
3870Sstevel@tonic-gate /* Receive data. */
3880Sstevel@tonic-gate sink(argc, argv);
3890Sstevel@tonic-gate exit(errs != 0);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate if (argc < 2)
3920Sstevel@tonic-gate usage();
3930Sstevel@tonic-gate if (argc > 2)
3940Sstevel@tonic-gate targetshouldbedirectory = 1;
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate remin = remout = -1;
3970Sstevel@tonic-gate do_cmd_pid = (pid_t)-1;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate /* Command to be executed on remote system using "ssh". */
4000Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd), "scp%s%s%s%s",
4010Sstevel@tonic-gate verbose_mode ? " -v" : "",
4020Sstevel@tonic-gate iamrecursive ? " -r" : "", pflag ? " -p" : "",
4030Sstevel@tonic-gate targetshouldbedirectory ? " -d" : "");
4040Sstevel@tonic-gate
4050Sstevel@tonic-gate (void) signal(SIGPIPE, lostconn);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
4080Sstevel@tonic-gate toremote(targ, argc, argv);
4090Sstevel@tonic-gate else {
4100Sstevel@tonic-gate if (targetshouldbedirectory)
4110Sstevel@tonic-gate verifydir(argv[argc - 1]);
4122757Sjp161948 tolocal(argc, argv); /* Dest is local host. */
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate * Finally check the exit status of the ssh process, if one was forked
4160Sstevel@tonic-gate * and no error has occurred yet
4170Sstevel@tonic-gate */
4180Sstevel@tonic-gate if (do_cmd_pid != (pid_t)-1 && errs == 0) {
4190Sstevel@tonic-gate if (remin != -1) {
4200Sstevel@tonic-gate (void) close(remin);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate if (remout != -1) {
4230Sstevel@tonic-gate (void) close(remout);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate if (waitpid(do_cmd_pid, &status, 0) == -1) {
4260Sstevel@tonic-gate errs = 1;
4270Sstevel@tonic-gate } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
4280Sstevel@tonic-gate errs = 1;
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate return (errs != 0);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate void
toremote(targ,argc,argv)4360Sstevel@tonic-gate toremote(targ, argc, argv)
4370Sstevel@tonic-gate char *targ, *argv[];
4380Sstevel@tonic-gate int argc;
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate int i, len;
4412780Sjp161948 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
4422780Sjp161948 arglist alist;
4432780Sjp161948
4442780Sjp161948 memset(&alist, '\0', sizeof (alist));
4452780Sjp161948 alist.list = NULL;
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate *targ++ = 0;
4480Sstevel@tonic-gate if (*targ == 0)
4490Sstevel@tonic-gate targ = ".";
4500Sstevel@tonic-gate
4512780Sjp161948 arg = xstrdup(argv[argc - 1]);
4522780Sjp161948 if ((thost = strchr(arg, '@'))) {
4530Sstevel@tonic-gate /* user@host */
4540Sstevel@tonic-gate *thost++ = 0;
4552780Sjp161948 tuser = arg;
4560Sstevel@tonic-gate if (*tuser == '\0')
4570Sstevel@tonic-gate tuser = NULL;
4580Sstevel@tonic-gate else if (!okname(tuser))
4590Sstevel@tonic-gate exit(1);
4600Sstevel@tonic-gate } else {
4612780Sjp161948 thost = arg;
4620Sstevel@tonic-gate tuser = NULL;
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4652780Sjp161948 if (tuser != NULL && !okname(tuser)) {
4662780Sjp161948 xfree(arg);
4672780Sjp161948 return;
4682780Sjp161948 }
4692780Sjp161948
4700Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) {
4710Sstevel@tonic-gate src = colon(argv[i]);
4720Sstevel@tonic-gate if (src) { /* remote to remote */
4732780Sjp161948 freeargs(&alist);
4742780Sjp161948 addargs(&alist, "%s", ssh_program);
4752780Sjp161948 if (verbose_mode)
4762780Sjp161948 addargs(&alist, "-v");
4772780Sjp161948 addargs(&alist, "-x");
4782780Sjp161948 addargs(&alist, "-oClearAllForwardings yes");
4792780Sjp161948 addargs(&alist, "-n");
4802780Sjp161948
4810Sstevel@tonic-gate *src++ = 0;
4820Sstevel@tonic-gate if (*src == 0)
4830Sstevel@tonic-gate src = ".";
4840Sstevel@tonic-gate host = strchr(argv[i], '@');
4852780Sjp161948
4860Sstevel@tonic-gate if (host) {
4870Sstevel@tonic-gate *host++ = 0;
4880Sstevel@tonic-gate host = cleanhostname(host);
4890Sstevel@tonic-gate suser = argv[i];
4900Sstevel@tonic-gate if (*suser == '\0')
4910Sstevel@tonic-gate suser = pwd->pw_name;
4920Sstevel@tonic-gate else if (!okname(suser))
4930Sstevel@tonic-gate continue;
4942780Sjp161948 addargs(&alist, "-l");
4952780Sjp161948 addargs(&alist, "%s", suser);
4960Sstevel@tonic-gate } else {
4970Sstevel@tonic-gate host = cleanhostname(argv[i]);
4982780Sjp161948 }
4992780Sjp161948 addargs(&alist, "%s", host);
5002780Sjp161948 addargs(&alist, "%s", cmd);
5012780Sjp161948 addargs(&alist, "%s", src);
5022780Sjp161948 addargs(&alist, "%s%s%s:%s",
5030Sstevel@tonic-gate tuser ? tuser : "", tuser ? "@" : "",
5040Sstevel@tonic-gate thost, targ);
5052780Sjp161948 if (do_local_cmd(&alist) != 0)
5062780Sjp161948 errs = 1;
5070Sstevel@tonic-gate } else { /* local to remote */
5080Sstevel@tonic-gate if (remin == -1) {
5090Sstevel@tonic-gate len = strlen(targ) + CMDNEEDS + 20;
5100Sstevel@tonic-gate bp = xmalloc(len);
5110Sstevel@tonic-gate (void) snprintf(bp, len, "%s -t %s", cmd, targ);
5120Sstevel@tonic-gate host = cleanhostname(thost);
5130Sstevel@tonic-gate if (do_cmd(host, tuser, bp, &remin,
5140Sstevel@tonic-gate &remout, argc) < 0)
5150Sstevel@tonic-gate exit(1);
5160Sstevel@tonic-gate if (response() < 0)
5170Sstevel@tonic-gate exit(1);
5180Sstevel@tonic-gate (void) xfree(bp);
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate source(1, argv + i);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate void
tolocal(argc,argv)5260Sstevel@tonic-gate tolocal(argc, argv)
5270Sstevel@tonic-gate int argc;
5280Sstevel@tonic-gate char *argv[];
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate int i, len;
5310Sstevel@tonic-gate char *bp, *host, *src, *suser;
5322780Sjp161948 arglist alist;
5332780Sjp161948
5342780Sjp161948 memset(&alist, '\0', sizeof (alist));
5352780Sjp161948 alist.list = NULL;
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate for (i = 0; i < argc - 1; i++) {
5380Sstevel@tonic-gate if (!(src = colon(argv[i]))) { /* Local to local. */
5392780Sjp161948 freeargs(&alist);
5402780Sjp161948 addargs(&alist, "%s", _PATH_CP);
5412780Sjp161948 if (iamrecursive)
5422780Sjp161948 addargs(&alist, "-r");
5432780Sjp161948 if (pflag)
5442780Sjp161948 addargs(&alist, "-p");
5452780Sjp161948 addargs(&alist, "%s", argv[i]);
5462780Sjp161948 addargs(&alist, "%s", argv[argc-1]);
5472780Sjp161948 if (do_local_cmd(&alist))
5480Sstevel@tonic-gate ++errs;
5490Sstevel@tonic-gate continue;
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate *src++ = 0;
5520Sstevel@tonic-gate if (*src == 0)
5530Sstevel@tonic-gate src = ".";
5540Sstevel@tonic-gate if ((host = strchr(argv[i], '@')) == NULL) {
5550Sstevel@tonic-gate host = argv[i];
5560Sstevel@tonic-gate suser = NULL;
5570Sstevel@tonic-gate } else {
5580Sstevel@tonic-gate *host++ = 0;
5590Sstevel@tonic-gate suser = argv[i];
5600Sstevel@tonic-gate if (*suser == '\0')
5610Sstevel@tonic-gate suser = pwd->pw_name;
5620Sstevel@tonic-gate else if (!okname(suser))
5630Sstevel@tonic-gate continue;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate host = cleanhostname(host);
5660Sstevel@tonic-gate len = strlen(src) + CMDNEEDS + 20;
5670Sstevel@tonic-gate bp = xmalloc(len);
5680Sstevel@tonic-gate (void) snprintf(bp, len, "%s -f %s", cmd, src);
5690Sstevel@tonic-gate if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
5700Sstevel@tonic-gate (void) xfree(bp);
5710Sstevel@tonic-gate ++errs;
5720Sstevel@tonic-gate continue;
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate xfree(bp);
5750Sstevel@tonic-gate sink(1, argv + argc - 1);
5760Sstevel@tonic-gate (void) close(remin);
5770Sstevel@tonic-gate remin = remout = -1;
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate void
source(argc,argv)5820Sstevel@tonic-gate source(argc, argv)
5830Sstevel@tonic-gate int argc;
5840Sstevel@tonic-gate char *argv[];
5850Sstevel@tonic-gate {
5860Sstevel@tonic-gate struct stat stb;
5870Sstevel@tonic-gate static BUF buffer;
5880Sstevel@tonic-gate BUF *bp;
5890Sstevel@tonic-gate off_t i, amt, result;
5900Sstevel@tonic-gate int fd, haderr, indx;
5910Sstevel@tonic-gate char *last, *name, buf[2048];
5920Sstevel@tonic-gate int len;
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate for (indx = 0; indx < argc; ++indx) {
5950Sstevel@tonic-gate name = argv[indx];
5960Sstevel@tonic-gate statbytes = 0;
5970Sstevel@tonic-gate len = strlen(name);
5980Sstevel@tonic-gate while (len > 1 && name[len-1] == '/')
5990Sstevel@tonic-gate name[--len] = '\0';
6000Sstevel@tonic-gate if (strchr(name, '\n') != NULL) {
6010Sstevel@tonic-gate run_err("%s: skipping, filename contains a newline",
6020Sstevel@tonic-gate name);
6030Sstevel@tonic-gate goto next;
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate if ((fd = open(name, O_RDONLY, 0)) < 0)
6060Sstevel@tonic-gate goto syserr;
6070Sstevel@tonic-gate if (fstat(fd, &stb) < 0) {
6080Sstevel@tonic-gate syserr: run_err("%s: %s", name, strerror(errno));
6090Sstevel@tonic-gate goto next;
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate switch (stb.st_mode & S_IFMT) {
6120Sstevel@tonic-gate case S_IFREG:
6130Sstevel@tonic-gate break;
6140Sstevel@tonic-gate case S_IFDIR:
6150Sstevel@tonic-gate if (iamrecursive) {
6160Sstevel@tonic-gate rsource(name, &stb);
6170Sstevel@tonic-gate goto next;
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate /* FALLTHROUGH */
6200Sstevel@tonic-gate default:
6210Sstevel@tonic-gate run_err("%s: not a regular file", name);
6220Sstevel@tonic-gate goto next;
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate if ((last = strrchr(name, '/')) == NULL)
6250Sstevel@tonic-gate last = name;
6260Sstevel@tonic-gate else
6270Sstevel@tonic-gate ++last;
6280Sstevel@tonic-gate curfile = last;
6290Sstevel@tonic-gate if (pflag) {
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate * Make it compatible with possible future
6320Sstevel@tonic-gate * versions expecting microseconds.
6330Sstevel@tonic-gate */
6340Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "T%lu 0 %lu 0\n",
6350Sstevel@tonic-gate (ulong_t)stb.st_mtime,
6360Sstevel@tonic-gate (ulong_t)stb.st_atime);
6370Sstevel@tonic-gate (void) atomicio(write, remout, buf, strlen(buf));
6380Sstevel@tonic-gate if (response() < 0)
6390Sstevel@tonic-gate goto next;
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
6420Sstevel@tonic-gate #ifdef HAVE_LONG_LONG_INT
6430Sstevel@tonic-gate snprintf(buf, sizeof (buf), "C%04o %lld %s\n",
6440Sstevel@tonic-gate (uint_t)(stb.st_mode & FILEMODEMASK),
6450Sstevel@tonic-gate (long long)stb.st_size, last);
6460Sstevel@tonic-gate #else
6470Sstevel@tonic-gate /* XXX: Handle integer overflow? */
6480Sstevel@tonic-gate snprintf(buf, sizeof (buf), "C%04o %lu %s\n",
6490Sstevel@tonic-gate (uint_t)(stb.st_mode & FILEMODEMASK),
6500Sstevel@tonic-gate (ulong_t)stb.st_size, last);
6510Sstevel@tonic-gate #endif
6520Sstevel@tonic-gate if (verbose_mode) {
6530Sstevel@tonic-gate fprintf(stderr, gettext("Sending file modes: %s"), buf);
6540Sstevel@tonic-gate fflush(stderr);
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate (void) atomicio(write, remout, buf, strlen(buf));
6570Sstevel@tonic-gate if (response() < 0)
6580Sstevel@tonic-gate goto next;
6590Sstevel@tonic-gate if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
6600Sstevel@tonic-gate next: (void) close(fd);
6610Sstevel@tonic-gate continue;
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate if (showprogress) {
6640Sstevel@tonic-gate totalbytes = stb.st_size;
6650Sstevel@tonic-gate progressmeter(-1);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate /* Keep writing after an error so that we stay sync'd up. */
6680Sstevel@tonic-gate for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
6690Sstevel@tonic-gate amt = bp->cnt;
6700Sstevel@tonic-gate if (i + amt > stb.st_size)
6710Sstevel@tonic-gate amt = stb.st_size - i;
6720Sstevel@tonic-gate if (!haderr) {
6730Sstevel@tonic-gate result = atomicio(read, fd, bp->buf, amt);
6740Sstevel@tonic-gate if (result != amt)
6750Sstevel@tonic-gate haderr = result >= 0 ? EIO : errno;
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate if (haderr)
6780Sstevel@tonic-gate (void) atomicio(write, remout, bp->buf, amt);
6790Sstevel@tonic-gate else {
6800Sstevel@tonic-gate result = atomicio(write, remout, bp->buf, amt);
6810Sstevel@tonic-gate if (result != amt)
6820Sstevel@tonic-gate haderr = result >= 0 ? EIO : errno;
6830Sstevel@tonic-gate statbytes += result;
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate if (showprogress)
6870Sstevel@tonic-gate progressmeter(1);
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate if (close(fd) < 0 && !haderr)
6900Sstevel@tonic-gate haderr = errno;
6910Sstevel@tonic-gate if (!haderr)
6920Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
6930Sstevel@tonic-gate else
6940Sstevel@tonic-gate run_err("%s: %s", name, strerror(haderr));
6950Sstevel@tonic-gate (void) response();
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate void
rsource(name,statp)7000Sstevel@tonic-gate rsource(name, statp)
7010Sstevel@tonic-gate char *name;
7020Sstevel@tonic-gate struct stat *statp;
7030Sstevel@tonic-gate {
7040Sstevel@tonic-gate DIR *dirp;
7050Sstevel@tonic-gate struct dirent *dp;
7060Sstevel@tonic-gate char *last, *vect[1], path[1100];
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate if (!(dirp = opendir(name))) {
7090Sstevel@tonic-gate run_err("%s: %s", name, strerror(errno));
7100Sstevel@tonic-gate return;
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate last = strrchr(name, '/');
7130Sstevel@tonic-gate if (last == 0)
7140Sstevel@tonic-gate last = name;
7150Sstevel@tonic-gate else
7160Sstevel@tonic-gate last++;
7170Sstevel@tonic-gate if (pflag) {
7180Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "T%lu 0 %lu 0\n",
7190Sstevel@tonic-gate (ulong_t)statp->st_mtime,
7200Sstevel@tonic-gate (ulong_t)statp->st_atime);
7210Sstevel@tonic-gate (void) atomicio(write, remout, path, strlen(path));
7220Sstevel@tonic-gate if (response() < 0) {
7230Sstevel@tonic-gate closedir(dirp);
7240Sstevel@tonic-gate return;
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "D%04o %d %.1024s\n",
7280Sstevel@tonic-gate (uint_t)(statp->st_mode & FILEMODEMASK), 0, last);
7290Sstevel@tonic-gate if (verbose_mode)
7300Sstevel@tonic-gate fprintf(stderr, gettext("Entering directory: %s"), path);
7310Sstevel@tonic-gate (void) atomicio(write, remout, path, strlen(path));
7320Sstevel@tonic-gate if (response() < 0) {
7330Sstevel@tonic-gate closedir(dirp);
7340Sstevel@tonic-gate return;
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
7370Sstevel@tonic-gate if (dp->d_ino == 0)
7380Sstevel@tonic-gate continue;
7390Sstevel@tonic-gate if ((strcmp(dp->d_name, ".") == 0) ||
7400Sstevel@tonic-gate (strcmp(dp->d_name, "..") == 0))
7410Sstevel@tonic-gate continue;
7420Sstevel@tonic-gate if (strlen(name) + 1 + strlen(dp->d_name) >=
7430Sstevel@tonic-gate sizeof (path) - 1) {
7440Sstevel@tonic-gate run_err("%s/%s: name too long", name, dp->d_name);
7450Sstevel@tonic-gate continue;
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s/%s", name, dp->d_name);
7480Sstevel@tonic-gate vect[0] = path;
7490Sstevel@tonic-gate source(1, vect);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate (void) closedir(dirp);
7520Sstevel@tonic-gate (void) atomicio(write, remout, "E\n", 2);
7530Sstevel@tonic-gate (void) response();
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate void
sink(argc,argv)7570Sstevel@tonic-gate sink(argc, argv)
7580Sstevel@tonic-gate int argc;
7590Sstevel@tonic-gate char *argv[];
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate static BUF buffer;
7620Sstevel@tonic-gate struct stat stb;
7630Sstevel@tonic-gate enum {
7640Sstevel@tonic-gate YES, NO, DISPLAYED
7650Sstevel@tonic-gate } wrerr;
7660Sstevel@tonic-gate BUF *bp;
7670Sstevel@tonic-gate off_t i, j;
7680Sstevel@tonic-gate int amt, count, exists, first, mask, mode, ofd, omode;
7690Sstevel@tonic-gate off_t size;
7700Sstevel@tonic-gate int setimes, targisdir, wrerrno = 0;
7710Sstevel@tonic-gate char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
7720Sstevel@tonic-gate struct timeval tv[2];
7730Sstevel@tonic-gate
7740Sstevel@tonic-gate #define atime tv[0]
7750Sstevel@tonic-gate #define mtime tv[1]
7760Sstevel@tonic-gate #define SCREWUP(str) { why = str; goto screwup; }
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate setimes = targisdir = 0;
7790Sstevel@tonic-gate mask = umask(0);
7800Sstevel@tonic-gate if (!pflag)
7810Sstevel@tonic-gate (void) umask(mask);
7820Sstevel@tonic-gate if (argc != 1) {
7830Sstevel@tonic-gate run_err("ambiguous target");
7840Sstevel@tonic-gate exit(1);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate targ = *argv;
7870Sstevel@tonic-gate if (targetshouldbedirectory)
7880Sstevel@tonic-gate verifydir(targ);
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
7910Sstevel@tonic-gate if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
7920Sstevel@tonic-gate targisdir = 1;
7930Sstevel@tonic-gate for (first = 1; ; first = 0) {
7940Sstevel@tonic-gate cp = buf;
7950Sstevel@tonic-gate if (atomicio(read, remin, cp, 1) <= 0)
7960Sstevel@tonic-gate return;
7970Sstevel@tonic-gate if (*cp++ == '\n')
7980Sstevel@tonic-gate SCREWUP("unexpected <newline>")
7990Sstevel@tonic-gate do {
8000Sstevel@tonic-gate if (atomicio(read, remin, &ch, sizeof (ch)) !=
8010Sstevel@tonic-gate sizeof (ch))
8020Sstevel@tonic-gate SCREWUP("lost connection")
8030Sstevel@tonic-gate *cp++ = ch;
8040Sstevel@tonic-gate } while (cp < &buf[sizeof (buf) - 1] && ch != '\n');
8050Sstevel@tonic-gate *cp = 0;
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate if (buf[0] == '\01' || buf[0] == '\02') {
8080Sstevel@tonic-gate if (iamremote == 0)
8090Sstevel@tonic-gate (void) atomicio(write, STDERR_FILENO,
8100Sstevel@tonic-gate buf + 1, strlen(buf + 1));
8110Sstevel@tonic-gate if (buf[0] == '\02')
8120Sstevel@tonic-gate exit(1);
8130Sstevel@tonic-gate ++errs;
8140Sstevel@tonic-gate continue;
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate if (buf[0] == 'E') {
8170Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
8180Sstevel@tonic-gate return;
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate if (ch == '\n')
8210Sstevel@tonic-gate *--cp = 0;
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate cp = buf;
8240Sstevel@tonic-gate if (*cp == 'T') {
8250Sstevel@tonic-gate setimes++;
8260Sstevel@tonic-gate cp++;
8270Sstevel@tonic-gate mtime.tv_sec = strtol(cp, &cp, 10);
8280Sstevel@tonic-gate if (!cp || *cp++ != ' ')
8290Sstevel@tonic-gate SCREWUP("mtime.sec not delimited")
8300Sstevel@tonic-gate mtime.tv_usec = strtol(cp, &cp, 10);
8310Sstevel@tonic-gate if (!cp || *cp++ != ' ')
8320Sstevel@tonic-gate SCREWUP("mtime.usec not delimited")
8330Sstevel@tonic-gate atime.tv_sec = strtol(cp, &cp, 10);
8340Sstevel@tonic-gate if (!cp || *cp++ != ' ')
8350Sstevel@tonic-gate SCREWUP("atime.sec not delimited")
8360Sstevel@tonic-gate atime.tv_usec = strtol(cp, &cp, 10);
8370Sstevel@tonic-gate if (!cp || *cp++ != '\0')
8380Sstevel@tonic-gate SCREWUP("atime.usec not delimited")
8390Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
8400Sstevel@tonic-gate continue;
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate if (*cp != 'C' && *cp != 'D') {
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate * Check for the case "rcp remote:foo\* local:bar".
8450Sstevel@tonic-gate * In this case, the line "No match." can be returned
8460Sstevel@tonic-gate * by the shell before the rcp command on the remote is
8470Sstevel@tonic-gate * executed so the ^Aerror_message convention isn't
8480Sstevel@tonic-gate * followed.
8490Sstevel@tonic-gate */
8500Sstevel@tonic-gate if (first) {
8510Sstevel@tonic-gate run_err("%s", cp);
8520Sstevel@tonic-gate exit(1);
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate SCREWUP("expected control record")
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate mode = 0;
8570Sstevel@tonic-gate for (++cp; cp < buf + 5; cp++) {
8580Sstevel@tonic-gate if (*cp < '0' || *cp > '7')
8590Sstevel@tonic-gate SCREWUP("bad mode")
8600Sstevel@tonic-gate mode = (mode << 3) | (*cp - '0');
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate if (*cp++ != ' ')
8630Sstevel@tonic-gate SCREWUP("mode not delimited")
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate for (size = 0; isdigit(*cp); )
8660Sstevel@tonic-gate size = size * 10 + (*cp++ - '0');
8670Sstevel@tonic-gate if (*cp++ != ' ')
8680Sstevel@tonic-gate SCREWUP("size not delimited")
8692757Sjp161948 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
8702757Sjp161948 run_err("error: unexpected filename: %s", cp);
8712757Sjp161948 exit(1);
8722757Sjp161948 }
8730Sstevel@tonic-gate if (targisdir) {
8740Sstevel@tonic-gate static char *namebuf;
8750Sstevel@tonic-gate static int cursize;
8760Sstevel@tonic-gate size_t need;
8770Sstevel@tonic-gate
8780Sstevel@tonic-gate need = strlen(targ) + strlen(cp) + 250;
8790Sstevel@tonic-gate if (need > cursize) {
8800Sstevel@tonic-gate if (namebuf)
8810Sstevel@tonic-gate xfree(namebuf);
8820Sstevel@tonic-gate namebuf = xmalloc(need);
8830Sstevel@tonic-gate cursize = need;
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate (void) snprintf(namebuf, need, "%s%s%s", targ,
8860Sstevel@tonic-gate strcmp(targ, "/") ? "/" : "", cp);
8870Sstevel@tonic-gate np = namebuf;
8880Sstevel@tonic-gate } else
8890Sstevel@tonic-gate np = targ;
8900Sstevel@tonic-gate curfile = cp;
8910Sstevel@tonic-gate exists = stat(np, &stb) == 0;
8920Sstevel@tonic-gate if (buf[0] == 'D') {
8930Sstevel@tonic-gate int mod_flag = pflag;
8942757Sjp161948 if (!iamrecursive)
8952757Sjp161948 SCREWUP("received directory without -r");
8960Sstevel@tonic-gate if (exists) {
8970Sstevel@tonic-gate if (!S_ISDIR(stb.st_mode)) {
8980Sstevel@tonic-gate errno = ENOTDIR;
8990Sstevel@tonic-gate goto bad;
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate if (pflag)
9020Sstevel@tonic-gate (void) chmod(np, mode);
9030Sstevel@tonic-gate } else {
9040Sstevel@tonic-gate /*
9050Sstevel@tonic-gate * Handle copying from a read-only
9060Sstevel@tonic-gate * directory
9070Sstevel@tonic-gate */
9080Sstevel@tonic-gate mod_flag = 1;
9090Sstevel@tonic-gate if (mkdir(np, mode | S_IRWXU) < 0)
9100Sstevel@tonic-gate goto bad;
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate vect[0] = xstrdup(np);
9130Sstevel@tonic-gate sink(1, vect);
9140Sstevel@tonic-gate if (setimes) {
9150Sstevel@tonic-gate setimes = 0;
9160Sstevel@tonic-gate if (utimes(vect[0], tv) < 0)
9170Sstevel@tonic-gate run_err("%s: set times: %s",
9180Sstevel@tonic-gate vect[0], strerror(errno));
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate if (mod_flag)
9210Sstevel@tonic-gate (void) chmod(vect[0], mode);
9220Sstevel@tonic-gate if (vect[0])
9230Sstevel@tonic-gate xfree(vect[0]);
9240Sstevel@tonic-gate continue;
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate omode = mode;
9270Sstevel@tonic-gate mode |= S_IWRITE;
9280Sstevel@tonic-gate if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
9290Sstevel@tonic-gate bad: run_err("%s: %s", np, strerror(errno));
9300Sstevel@tonic-gate continue;
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
9330Sstevel@tonic-gate if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
9340Sstevel@tonic-gate (void) close(ofd);
9350Sstevel@tonic-gate continue;
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate wrerr = NO;
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate if (showprogress) {
9400Sstevel@tonic-gate totalbytes = size;
9410Sstevel@tonic-gate progressmeter(-1);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate statbytes = 0;
9442628Sjp161948 for (i = 0; i < size; i += bp->cnt) {
9452628Sjp161948 amt = bp->cnt;
9462628Sjp161948 cp = bp->buf;
9470Sstevel@tonic-gate if (i + amt > size)
9480Sstevel@tonic-gate amt = size - i;
9492628Sjp161948 count = amt;
9500Sstevel@tonic-gate do {
9510Sstevel@tonic-gate j = read(remin, cp, amt);
9520Sstevel@tonic-gate if (j == -1 && (errno == EINTR ||
9530Sstevel@tonic-gate errno == EAGAIN)) {
9540Sstevel@tonic-gate continue;
9550Sstevel@tonic-gate } else if (j <= 0) {
9560Sstevel@tonic-gate run_err("%s", j ? strerror(errno) :
9570Sstevel@tonic-gate "dropped connection");
9580Sstevel@tonic-gate exit(1);
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate amt -= j;
9610Sstevel@tonic-gate cp += j;
9620Sstevel@tonic-gate statbytes += j;
9630Sstevel@tonic-gate } while (amt > 0);
9642628Sjp161948 /* Keep reading so we stay sync'd up. */
9652628Sjp161948 if (wrerr == NO) {
9662628Sjp161948 j = atomicio(write, ofd, bp->buf,
9672628Sjp161948 count);
9682628Sjp161948 if (j != count) {
9692628Sjp161948 wrerr = YES;
9702628Sjp161948 wrerrno = j >= 0 ? EIO : errno;
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate if (showprogress)
9750Sstevel@tonic-gate progressmeter(1);
9760Sstevel@tonic-gate if (ftruncate(ofd, size)) {
9770Sstevel@tonic-gate run_err("%s: truncate: %s", np, strerror(errno));
9780Sstevel@tonic-gate wrerr = DISPLAYED;
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate if (pflag) {
9810Sstevel@tonic-gate if (exists || omode != mode)
9820Sstevel@tonic-gate #ifdef HAVE_FCHMOD
9834120Sjp161948 if (fchmod(ofd, omode)) {
9840Sstevel@tonic-gate #else /* HAVE_FCHMOD */
9854120Sjp161948 if (chmod(np, omode)) {
9860Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9870Sstevel@tonic-gate run_err("%s: set mode: %s",
9880Sstevel@tonic-gate np, strerror(errno));
9894120Sjp161948 wrerr = DISPLAYED;
9904120Sjp161948 }
9910Sstevel@tonic-gate } else {
9920Sstevel@tonic-gate if (!exists && omode != mode)
9930Sstevel@tonic-gate #ifdef HAVE_FCHMOD
9944120Sjp161948 if (fchmod(ofd, omode & ~mask)) {
9950Sstevel@tonic-gate #else /* HAVE_FCHMOD */
9964120Sjp161948 if (chmod(np, omode & ~mask)) {
9970Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9980Sstevel@tonic-gate run_err("%s: set mode: %s",
9990Sstevel@tonic-gate np, strerror(errno));
10004120Sjp161948 wrerr = DISPLAYED;
10014120Sjp161948 }
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate if (close(ofd) == -1) {
10040Sstevel@tonic-gate wrerr = YES;
10050Sstevel@tonic-gate wrerrno = errno;
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate (void) response();
10080Sstevel@tonic-gate if (setimes && wrerr == NO) {
10090Sstevel@tonic-gate setimes = 0;
10100Sstevel@tonic-gate if (utimes(np, tv) < 0) {
10110Sstevel@tonic-gate run_err("%s: set times: %s",
10120Sstevel@tonic-gate np, strerror(errno));
10130Sstevel@tonic-gate wrerr = DISPLAYED;
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate switch (wrerr) {
10170Sstevel@tonic-gate case YES:
10180Sstevel@tonic-gate run_err("%s: %s", np, strerror(wrerrno));
10190Sstevel@tonic-gate break;
10200Sstevel@tonic-gate case NO:
10210Sstevel@tonic-gate (void) atomicio(write, remout, "", 1);
10220Sstevel@tonic-gate break;
10230Sstevel@tonic-gate case DISPLAYED:
10240Sstevel@tonic-gate break;
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate screwup:
10280Sstevel@tonic-gate run_err("protocol error: %s", why);
10290Sstevel@tonic-gate exit(1);
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate int
response(void)10330Sstevel@tonic-gate response(void)
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate char ch, *cp, resp, rbuf[2048];
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate if (atomicio(read, remin, &resp, sizeof (resp)) != sizeof (resp))
10380Sstevel@tonic-gate lostconn(0);
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate cp = rbuf;
10410Sstevel@tonic-gate switch (resp) {
10420Sstevel@tonic-gate case 0: /* ok */
10430Sstevel@tonic-gate return (0);
10440Sstevel@tonic-gate default:
10450Sstevel@tonic-gate *cp++ = resp;
10460Sstevel@tonic-gate /* FALLTHROUGH */
10470Sstevel@tonic-gate case 1: /* error, followed by error msg */
10480Sstevel@tonic-gate case 2: /* fatal error, "" */
10490Sstevel@tonic-gate do {
10500Sstevel@tonic-gate if (atomicio(read, remin, &ch, sizeof (ch)) !=
10510Sstevel@tonic-gate sizeof (ch))
10520Sstevel@tonic-gate lostconn(0);
10530Sstevel@tonic-gate *cp++ = ch;
10540Sstevel@tonic-gate } while (cp < &rbuf[sizeof (rbuf) - 1] && ch != '\n');
10550Sstevel@tonic-gate
10560Sstevel@tonic-gate if (!iamremote)
10570Sstevel@tonic-gate (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
10580Sstevel@tonic-gate ++errs;
10590Sstevel@tonic-gate if (resp == 1)
10600Sstevel@tonic-gate return (-1);
10610Sstevel@tonic-gate exit(1);
10620Sstevel@tonic-gate }
10630Sstevel@tonic-gate /* NOTREACHED */
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate
10660Sstevel@tonic-gate void
usage(void)10670Sstevel@tonic-gate usage(void)
10680Sstevel@tonic-gate {
10690Sstevel@tonic-gate (void) fprintf(stderr,
10700Sstevel@tonic-gate gettext(
10710Sstevel@tonic-gate "Usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
10720Sstevel@tonic-gate " [-c cipher] [-i identity] [-o option]\n"
10730Sstevel@tonic-gate " [[user@]host1:]file1 [...] "
10740Sstevel@tonic-gate "[[user@]host2:]file2\n"));
10750Sstevel@tonic-gate exit(1);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate
10780Sstevel@tonic-gate /* PRINTFLIKE1 */
10790Sstevel@tonic-gate void
run_err(const char * fmt,...)10800Sstevel@tonic-gate run_err(const char *fmt, ...)
10810Sstevel@tonic-gate {
10820Sstevel@tonic-gate static FILE *fp;
10830Sstevel@tonic-gate va_list ap;
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate ++errs;
10862757Sjp161948
10872757Sjp161948 if (!iamremote) {
10882757Sjp161948 va_start(ap, fmt);
10892757Sjp161948 vfprintf(stderr, fmt, ap);
10902757Sjp161948 va_end(ap);
10912757Sjp161948 fprintf(stderr, "\n");
10922757Sjp161948 }
10932757Sjp161948
10940Sstevel@tonic-gate if (fp == NULL && !(fp = fdopen(remout, "w")))
10950Sstevel@tonic-gate return;
10962757Sjp161948
10970Sstevel@tonic-gate (void) fprintf(fp, "%c", 0x01);
10980Sstevel@tonic-gate (void) fprintf(fp, "scp: ");
10990Sstevel@tonic-gate va_start(ap, fmt);
11000Sstevel@tonic-gate (void) vfprintf(fp, fmt, ap);
11010Sstevel@tonic-gate va_end(ap);
11020Sstevel@tonic-gate (void) fprintf(fp, "\n");
11030Sstevel@tonic-gate (void) fflush(fp);
11040Sstevel@tonic-gate
11050Sstevel@tonic-gate }
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate void
verifydir(cp)11080Sstevel@tonic-gate verifydir(cp)
11090Sstevel@tonic-gate char *cp;
11100Sstevel@tonic-gate {
11110Sstevel@tonic-gate struct stat stb;
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate if (!stat(cp, &stb)) {
11140Sstevel@tonic-gate if (S_ISDIR(stb.st_mode))
11150Sstevel@tonic-gate return;
11160Sstevel@tonic-gate errno = ENOTDIR;
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate run_err("%s: %s", cp, strerror(errno));
11190Sstevel@tonic-gate exit(1);
11200Sstevel@tonic-gate }
11210Sstevel@tonic-gate
11220Sstevel@tonic-gate int
okname(cp0)11230Sstevel@tonic-gate okname(cp0)
11240Sstevel@tonic-gate char *cp0;
11250Sstevel@tonic-gate {
11260Sstevel@tonic-gate int c;
11270Sstevel@tonic-gate char *cp;
11280Sstevel@tonic-gate
11290Sstevel@tonic-gate cp = cp0;
11300Sstevel@tonic-gate do {
11310Sstevel@tonic-gate c = (int)*cp;
11320Sstevel@tonic-gate if (c & 0200)
11330Sstevel@tonic-gate goto bad;
1134*9927Spravas@Sun.COM if (!isalpha(c) && !isdigit(c)) {
1135*9927Spravas@Sun.COM switch (c) {
1136*9927Spravas@Sun.COM case '\'':
1137*9927Spravas@Sun.COM case '"':
1138*9927Spravas@Sun.COM case '`':
1139*9927Spravas@Sun.COM case ' ':
1140*9927Spravas@Sun.COM case '#':
1141*9927Spravas@Sun.COM goto bad;
1142*9927Spravas@Sun.COM default:
1143*9927Spravas@Sun.COM break;
1144*9927Spravas@Sun.COM }
1145*9927Spravas@Sun.COM }
11460Sstevel@tonic-gate } while (*++cp);
11470Sstevel@tonic-gate return (1);
11480Sstevel@tonic-gate
11490Sstevel@tonic-gate bad: fprintf(stderr, gettext("%s: invalid user name\n"), cp0);
11500Sstevel@tonic-gate return (0);
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate
11530Sstevel@tonic-gate BUF *
allocbuf(bp,fd,blksize)11540Sstevel@tonic-gate allocbuf(bp, fd, blksize)
11550Sstevel@tonic-gate BUF *bp;
11560Sstevel@tonic-gate int fd, blksize;
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate size_t size;
11590Sstevel@tonic-gate #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
11600Sstevel@tonic-gate struct stat stb;
11610Sstevel@tonic-gate
11620Sstevel@tonic-gate if (fstat(fd, &stb) < 0) {
11630Sstevel@tonic-gate run_err("fstat: %s", strerror(errno));
11640Sstevel@tonic-gate return (0);
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate if (stb.st_blksize == 0)
11670Sstevel@tonic-gate size = blksize;
11680Sstevel@tonic-gate else
11690Sstevel@tonic-gate size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
11700Sstevel@tonic-gate stb.st_blksize;
11710Sstevel@tonic-gate #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
11720Sstevel@tonic-gate size = blksize;
11730Sstevel@tonic-gate #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
11740Sstevel@tonic-gate if (bp->cnt >= size)
11750Sstevel@tonic-gate return (bp);
11760Sstevel@tonic-gate if (bp->buf == NULL)
11770Sstevel@tonic-gate bp->buf = xmalloc(size);
11780Sstevel@tonic-gate else
11790Sstevel@tonic-gate bp->buf = xrealloc(bp->buf, size);
11800Sstevel@tonic-gate memset(bp->buf, 0, size);
11810Sstevel@tonic-gate bp->cnt = size;
11820Sstevel@tonic-gate return (bp);
11830Sstevel@tonic-gate }
11840Sstevel@tonic-gate
11850Sstevel@tonic-gate void
lostconn(signo)11860Sstevel@tonic-gate lostconn(signo)
11870Sstevel@tonic-gate int signo;
11880Sstevel@tonic-gate {
11890Sstevel@tonic-gate if (!iamremote)
11900Sstevel@tonic-gate write(STDERR_FILENO, "lost connection\n", 16);
11910Sstevel@tonic-gate if (signo)
11920Sstevel@tonic-gate _exit(1);
11930Sstevel@tonic-gate else
11940Sstevel@tonic-gate exit(1);
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate static void
updateprogressmeter(int ignore)11980Sstevel@tonic-gate updateprogressmeter(int ignore)
11990Sstevel@tonic-gate {
12000Sstevel@tonic-gate int save_errno = errno;
12010Sstevel@tonic-gate
12020Sstevel@tonic-gate progressmeter(0);
12030Sstevel@tonic-gate signal(SIGALRM, updateprogressmeter);
12040Sstevel@tonic-gate alarm(PROGRESSTIME);
12050Sstevel@tonic-gate errno = save_errno;
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate
12080Sstevel@tonic-gate static int
foregroundproc(void)12090Sstevel@tonic-gate foregroundproc(void)
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate static pid_t pgrp = -1;
12120Sstevel@tonic-gate int ctty_pgrp;
12130Sstevel@tonic-gate
12140Sstevel@tonic-gate if (pgrp == -1)
12150Sstevel@tonic-gate pgrp = getpgrp();
12160Sstevel@tonic-gate
12170Sstevel@tonic-gate #ifdef HAVE_TCGETPGRP
12180Sstevel@tonic-gate return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
12190Sstevel@tonic-gate ctty_pgrp == pgrp);
12200Sstevel@tonic-gate #else
12210Sstevel@tonic-gate return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
12220Sstevel@tonic-gate ctty_pgrp == pgrp));
12230Sstevel@tonic-gate #endif
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate void
progressmeter(int flag)12270Sstevel@tonic-gate progressmeter(int flag)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate static const char prefixes[] = " KMGTP";
12300Sstevel@tonic-gate static struct timeval lastupdate;
12310Sstevel@tonic-gate static off_t lastsize;
12320Sstevel@tonic-gate struct timeval now, td, wait;
12330Sstevel@tonic-gate off_t cursize, abbrevsize;
12340Sstevel@tonic-gate double elapsed;
12350Sstevel@tonic-gate int ratio, barlength, i, remaining;
12360Sstevel@tonic-gate char buf[512];
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate if (flag == -1) {
12390Sstevel@tonic-gate (void) gettimeofday(&start, (struct timezone *)0);
12400Sstevel@tonic-gate lastupdate = start;
12410Sstevel@tonic-gate lastsize = 0;
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate if (foregroundproc() == 0)
12440Sstevel@tonic-gate return;
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate (void) gettimeofday(&now, (struct timezone *)0);
12470Sstevel@tonic-gate cursize = statbytes;
12480Sstevel@tonic-gate if (totalbytes != 0) {
12490Sstevel@tonic-gate ratio = (int)(100.0 * cursize / totalbytes);
12500Sstevel@tonic-gate ratio = MAX(ratio, 0);
12510Sstevel@tonic-gate ratio = MIN(ratio, 100);
12520Sstevel@tonic-gate } else
12530Sstevel@tonic-gate ratio = 100;
12540Sstevel@tonic-gate
12550Sstevel@tonic-gate snprintf(buf, sizeof (buf), "\r%-20.20s %3d%% ", curfile, ratio);
12560Sstevel@tonic-gate
12570Sstevel@tonic-gate barlength = getttywidth() - 51;
12580Sstevel@tonic-gate if (barlength > 0) {
12590Sstevel@tonic-gate i = barlength * ratio / 100;
12600Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12610Sstevel@tonic-gate "|%.*s%*s|", i,
12620Sstevel@tonic-gate "*******************************************************"
12630Sstevel@tonic-gate "*******************************************************"
12640Sstevel@tonic-gate "*******************************************************"
12650Sstevel@tonic-gate "*******************************************************"
12660Sstevel@tonic-gate "*******************************************************"
12670Sstevel@tonic-gate "*******************************************************"
12680Sstevel@tonic-gate "*******************************************************",
12690Sstevel@tonic-gate barlength - i, "");
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate i = 0;
12720Sstevel@tonic-gate abbrevsize = cursize;
12739194SHuie-Ying.Lee@Sun.COM while (abbrevsize >= 100000 && i < strlen(prefixes) - 1) {
12740Sstevel@tonic-gate i++;
12750Sstevel@tonic-gate abbrevsize >>= 10;
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf), " %5lu %c%c ",
12780Sstevel@tonic-gate (unsigned long) abbrevsize, prefixes[i],
12790Sstevel@tonic-gate prefixes[i] == ' ' ? ' ' : 'B');
12800Sstevel@tonic-gate
12810Sstevel@tonic-gate timersub(&now, &lastupdate, &wait);
12820Sstevel@tonic-gate if (cursize > lastsize) {
12830Sstevel@tonic-gate lastupdate = now;
12840Sstevel@tonic-gate lastsize = cursize;
12850Sstevel@tonic-gate if (wait.tv_sec >= STALLTIME) {
12860Sstevel@tonic-gate start.tv_sec += wait.tv_sec;
12870Sstevel@tonic-gate start.tv_usec += wait.tv_usec;
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate wait.tv_sec = 0;
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate timersub(&now, &start, &td);
12920Sstevel@tonic-gate elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate if (flag != 1 &&
12950Sstevel@tonic-gate (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
12960Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12970Sstevel@tonic-gate " --:-- ETA");
12980Sstevel@tonic-gate } else if (wait.tv_sec >= STALLTIME) {
12990Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13000Sstevel@tonic-gate " - stalled -");
13010Sstevel@tonic-gate } else {
13020Sstevel@tonic-gate if (flag != 1)
13030Sstevel@tonic-gate remaining = (int)(totalbytes / (statbytes / elapsed) -
13040Sstevel@tonic-gate elapsed);
13050Sstevel@tonic-gate else
13060Sstevel@tonic-gate remaining = (int)elapsed;
13070Sstevel@tonic-gate
13080Sstevel@tonic-gate i = remaining / 3600;
13090Sstevel@tonic-gate if (i)
13100Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13110Sstevel@tonic-gate "%2d:", i);
13120Sstevel@tonic-gate else
13130Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13140Sstevel@tonic-gate " ");
13150Sstevel@tonic-gate i = remaining % 3600;
13160Sstevel@tonic-gate snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13170Sstevel@tonic-gate "%02d:%02d%s", i / 60, i % 60,
13180Sstevel@tonic-gate (flag != 1) ? " ETA" : " ");
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate atomicio(write, fileno(stdout), buf, strlen(buf));
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate if (flag == -1) {
13230Sstevel@tonic-gate mysignal(SIGALRM, updateprogressmeter);
13240Sstevel@tonic-gate alarm(PROGRESSTIME);
13250Sstevel@tonic-gate } else if (flag == 1) {
13260Sstevel@tonic-gate alarm(0);
13270Sstevel@tonic-gate atomicio(write, fileno(stdout), "\n", 1);
13280Sstevel@tonic-gate statbytes = 0;
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate
13320Sstevel@tonic-gate int
getttywidth(void)13330Sstevel@tonic-gate getttywidth(void)
13340Sstevel@tonic-gate {
13350Sstevel@tonic-gate struct winsize winsize;
13360Sstevel@tonic-gate
13370Sstevel@tonic-gate if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
13380Sstevel@tonic-gate return (winsize.ws_col ? winsize.ws_col : 80);
13390Sstevel@tonic-gate else
13400Sstevel@tonic-gate return (80);
13410Sstevel@tonic-gate }
1342