xref: /titanic_44/usr/src/cmd/ssh/scp/scp.c (revision 038a8f41fc75478546db55fe98bd9c7fc5e08b96)
17c478bd9Sstevel@tonic-gate /*
2*038a8f41Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * scp - secure remote copy.  This is basically patched BSD rcp which
77c478bd9Sstevel@tonic-gate  * uses ssh to do the data transfer (instead of using rcmd).
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * NOTE: This version should NOT be suid root.  (This uses ssh to
107c478bd9Sstevel@tonic-gate  * do the transfer and ssh has the necessary privileges.)
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
157c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
167c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
177c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
187c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
197c478bd9Sstevel@tonic-gate  */
207c478bd9Sstevel@tonic-gate /*
217c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Aaron Campbell.  All rights reserved.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
257c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
267c478bd9Sstevel@tonic-gate  * are met:
277c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
287c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
297c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
307c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
317c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
347c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
357c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
367c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
377c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
387c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
397c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
407c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
417c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
427c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Parts from:
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1990, 1992, 1993, 1995
497c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
527c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
537c478bd9Sstevel@tonic-gate  * are met:
547c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
557c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
567c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
577c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
587c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
597c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
607c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
617c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
627c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
637c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
647c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
657c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
687c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
697c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
707c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
717c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
727c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
737c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
747c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
757c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
767c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
777c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #include "includes.h"
827c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: scp.c,v 1.91 2002/06/19 00:27:55 deraadt Exp $");
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include "xmalloc.h"
877c478bd9Sstevel@tonic-gate #include "atomicio.h"
887c478bd9Sstevel@tonic-gate #include "pathnames.h"
897c478bd9Sstevel@tonic-gate #include "log.h"
907c478bd9Sstevel@tonic-gate #include "misc.h"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME
937c478bd9Sstevel@tonic-gate extern char *__progname;
947c478bd9Sstevel@tonic-gate #else
957c478bd9Sstevel@tonic-gate char *__progname;
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* For progressmeter() -- number of seconds before xfer considered "stalled" */
997c478bd9Sstevel@tonic-gate #define	STALLTIME	5
1007c478bd9Sstevel@tonic-gate /* alarm() interval for updating progress meter */
1017c478bd9Sstevel@tonic-gate #define	PROGRESSTIME	1
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /* Visual statistics about files as they are transferred. */
1047c478bd9Sstevel@tonic-gate void progressmeter(int);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /* Returns width of the terminal (for progress meter calculations). */
1077c478bd9Sstevel@tonic-gate int getttywidth(void);
1087c478bd9Sstevel@tonic-gate int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout,
1097c478bd9Sstevel@tonic-gate     int argc);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* Struct for addargs */
1127c478bd9Sstevel@tonic-gate arglist args;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* Time a transfer started. */
1157c478bd9Sstevel@tonic-gate static struct timeval start;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* Number of bytes of current file transferred so far. */
1187c478bd9Sstevel@tonic-gate volatile off_t statbytes;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /* Total size of current file. */
1217c478bd9Sstevel@tonic-gate off_t totalbytes = 0;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* Name of current file being transferred. */
1247c478bd9Sstevel@tonic-gate char *curfile;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* This is set to non-zero to enable verbose mode. */
1277c478bd9Sstevel@tonic-gate int verbose_mode = 0;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /* This is set to zero if the progressmeter is not desired. */
1307c478bd9Sstevel@tonic-gate int showprogress = 1;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* This is the program to execute for the secured connection. ("ssh" or -S) */
1337c478bd9Sstevel@tonic-gate char *ssh_program = _PATH_SSH_PROGRAM;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /* This is used to store the pid of ssh_program */
136a7e50fb1Sjp161948 static pid_t do_cmd_pid = -1;
137a7e50fb1Sjp161948 
138a7e50fb1Sjp161948 static void
139a7e50fb1Sjp161948 killchild(int signo)
140a7e50fb1Sjp161948 {
141a7e50fb1Sjp161948 	if (do_cmd_pid > 1) {
142a7e50fb1Sjp161948 		kill(do_cmd_pid, signo ? signo : SIGTERM);
143a7e50fb1Sjp161948 		waitpid(do_cmd_pid, NULL, 0);
144a7e50fb1Sjp161948 	}
145a7e50fb1Sjp161948 
146a7e50fb1Sjp161948 	if (signo)
147a7e50fb1Sjp161948 		_exit(1);
148a7e50fb1Sjp161948 	exit(1);
149a7e50fb1Sjp161948 }
150a7e50fb1Sjp161948 
151a7e50fb1Sjp161948 /*
152a7e50fb1Sjp161948  * Run a command via fork(2)/exec(2). This can be a local-to-local copy via
153a7e50fb1Sjp161948  * cp(1) or one side of a remote-to-remote copy. We must not use system(3) here
154a7e50fb1Sjp161948  * because we don't want filenames to go through a command expansion in the
155a7e50fb1Sjp161948  * underlying shell. Note that the user can create a filename that is a piece of
156a7e50fb1Sjp161948  * shell code itself and this must not be executed.
157a7e50fb1Sjp161948  */
158a7e50fb1Sjp161948 static int
159a7e50fb1Sjp161948 do_local_cmd(arglist *a)
160a7e50fb1Sjp161948 {
161a7e50fb1Sjp161948 	uint_t i;
162a7e50fb1Sjp161948 	int status;
163a7e50fb1Sjp161948 	pid_t pid;
164a7e50fb1Sjp161948 
165a7e50fb1Sjp161948 	if (a->num == 0)
166a7e50fb1Sjp161948 		fatal("do_local_cmd: no arguments");
167a7e50fb1Sjp161948 
168a7e50fb1Sjp161948 	if (verbose_mode) {
169a7e50fb1Sjp161948 		fprintf(stderr, gettext("Executing:"));
170a7e50fb1Sjp161948 		for (i = 0; i < a->num; i++)
171a7e50fb1Sjp161948 			fprintf(stderr, " %s", a->list[i]);
172a7e50fb1Sjp161948 		fprintf(stderr, "\n");
173a7e50fb1Sjp161948 	}
174a7e50fb1Sjp161948 	if ((pid = fork()) == -1)
175a7e50fb1Sjp161948 		fatal("do_local_cmd: fork: %s", strerror(errno));
176a7e50fb1Sjp161948 
177a7e50fb1Sjp161948 	if (pid == 0) {
178a7e50fb1Sjp161948 		execvp(a->list[0], a->list);
179a7e50fb1Sjp161948 		perror(a->list[0]);
180a7e50fb1Sjp161948 		exit(1);
181a7e50fb1Sjp161948 	}
182a7e50fb1Sjp161948 
183a7e50fb1Sjp161948 	do_cmd_pid = pid;
184a7e50fb1Sjp161948 	signal(SIGTERM, killchild);
185a7e50fb1Sjp161948 	signal(SIGINT, killchild);
186a7e50fb1Sjp161948 	signal(SIGHUP, killchild);
187a7e50fb1Sjp161948 
188a7e50fb1Sjp161948 	while (waitpid(pid, &status, 0) == -1)
189a7e50fb1Sjp161948 		if (errno != EINTR)
190a7e50fb1Sjp161948 			fatal("do_local_cmd: waitpid: %s", strerror(errno));
191a7e50fb1Sjp161948 
192a7e50fb1Sjp161948 	do_cmd_pid = -1;
193a7e50fb1Sjp161948 
194a7e50fb1Sjp161948 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
195a7e50fb1Sjp161948 		return (-1);
196a7e50fb1Sjp161948 
197a7e50fb1Sjp161948 	return (0);
198a7e50fb1Sjp161948 }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * This function executes the given command as the specified user on the
2027c478bd9Sstevel@tonic-gate  * given host.  This returns < 0 if execution fails, and >= 0 otherwise. This
2037c478bd9Sstevel@tonic-gate  * assigns the input and output file descriptors on success.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate int
2077c478bd9Sstevel@tonic-gate do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	int pin[2], pout[2], reserved[2];
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (verbose_mode)
2127c478bd9Sstevel@tonic-gate 		fprintf(stderr,
2137c478bd9Sstevel@tonic-gate 			gettext("Executing: program %s host %s, "
2147c478bd9Sstevel@tonic-gate 				"user %s, command %s\n"),
2157c478bd9Sstevel@tonic-gate 			ssh_program, host,
2167c478bd9Sstevel@tonic-gate 			remuser ? remuser : gettext("(unspecified)"), cmd);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Reserve two descriptors so that the real pipes won't get
2207c478bd9Sstevel@tonic-gate 	 * descriptors 0 and 1 because that will screw up dup2 below.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 	pipe(reserved);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* Create a socket pair for communicating with ssh. */
2257c478bd9Sstevel@tonic-gate 	if (pipe(pin) < 0)
2267c478bd9Sstevel@tonic-gate 		fatal("pipe: %s", strerror(errno));
2277c478bd9Sstevel@tonic-gate 	if (pipe(pout) < 0)
2287c478bd9Sstevel@tonic-gate 		fatal("pipe: %s", strerror(errno));
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	/* Free the reserved descriptors. */
2317c478bd9Sstevel@tonic-gate 	close(reserved[0]);
2327c478bd9Sstevel@tonic-gate 	close(reserved[1]);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/* For a child to execute the command on the remote host using ssh. */
2357c478bd9Sstevel@tonic-gate 	if ((do_cmd_pid = fork()) == 0)  {
2367c478bd9Sstevel@tonic-gate 		/* Child. */
2377c478bd9Sstevel@tonic-gate 		close(pin[1]);
2387c478bd9Sstevel@tonic-gate 		close(pout[0]);
2397c478bd9Sstevel@tonic-gate 		dup2(pin[0], 0);
2407c478bd9Sstevel@tonic-gate 		dup2(pout[1], 1);
2417c478bd9Sstevel@tonic-gate 		close(pin[0]);
2427c478bd9Sstevel@tonic-gate 		close(pout[1]);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		args.list[0] = ssh_program;
2457c478bd9Sstevel@tonic-gate 		if (remuser != NULL)
2467c478bd9Sstevel@tonic-gate 			addargs(&args, "-l%s", remuser);
2477c478bd9Sstevel@tonic-gate 		addargs(&args, "%s", host);
2487c478bd9Sstevel@tonic-gate 		addargs(&args, "%s", cmd);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 		execvp(ssh_program, args.list);
2517c478bd9Sstevel@tonic-gate 		perror(ssh_program);
2527c478bd9Sstevel@tonic-gate 		exit(1);
2537c478bd9Sstevel@tonic-gate 	} else if (do_cmd_pid == (pid_t)-1) {
2547c478bd9Sstevel@tonic-gate 		/* fork() failed */
2557c478bd9Sstevel@tonic-gate 		fatal("fork: %s", strerror(errno));
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* Parent.  Close the other side, and return the local side. */
2597c478bd9Sstevel@tonic-gate 	close(pin[0]);
2607c478bd9Sstevel@tonic-gate 	*fdout = pin[1];
2617c478bd9Sstevel@tonic-gate 	close(pout[1]);
2627c478bd9Sstevel@tonic-gate 	*fdin = pout[0];
2637c478bd9Sstevel@tonic-gate 	return (0);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate typedef struct {
2677c478bd9Sstevel@tonic-gate 	int cnt;
2687c478bd9Sstevel@tonic-gate 	char *buf;
2697c478bd9Sstevel@tonic-gate } BUF;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate BUF *allocbuf(BUF *, int, int);
2727c478bd9Sstevel@tonic-gate void lostconn(int);
2737c478bd9Sstevel@tonic-gate void nospace(void);
2747c478bd9Sstevel@tonic-gate int okname(char *);
2757c478bd9Sstevel@tonic-gate void run_err(const char *, ...);
2767c478bd9Sstevel@tonic-gate void verifydir(char *);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate struct passwd *pwd;
2797c478bd9Sstevel@tonic-gate uid_t userid;
2807c478bd9Sstevel@tonic-gate int errs, remin, remout;
2817c478bd9Sstevel@tonic-gate int pflag, iamremote, iamrecursive, targetshouldbedirectory;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate #define	CMDNEEDS	64
2847c478bd9Sstevel@tonic-gate char cmd[CMDNEEDS];		/* must hold "rcp -r -p -d\0" */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate int response(void);
2877c478bd9Sstevel@tonic-gate void rsource(char *, struct stat *);
2887c478bd9Sstevel@tonic-gate void sink(int, char *[]);
2897c478bd9Sstevel@tonic-gate void source(int, char *[]);
2907c478bd9Sstevel@tonic-gate void tolocal(int, char *[]);
2917c478bd9Sstevel@tonic-gate void toremote(char *, int, char *[]);
2927c478bd9Sstevel@tonic-gate void usage(void);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate int
2957c478bd9Sstevel@tonic-gate main(argc, argv)
2967c478bd9Sstevel@tonic-gate 	int argc;
2977c478bd9Sstevel@tonic-gate 	char *argv[];
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	int ch, fflag, tflag, status;
3007c478bd9Sstevel@tonic-gate 	char *targ;
3017c478bd9Sstevel@tonic-gate 	extern char *optarg;
3027c478bd9Sstevel@tonic-gate 	extern int optind;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	__progname = get_progname(argv[0]);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	g11n_setlocale(LC_ALL, "");
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	args.list = NULL;
3097c478bd9Sstevel@tonic-gate 	addargs(&args, "ssh");		/* overwritten with ssh_program */
3107c478bd9Sstevel@tonic-gate 	addargs(&args, "-x");
3117c478bd9Sstevel@tonic-gate 	addargs(&args, "-oForwardAgent no");
3127c478bd9Sstevel@tonic-gate 	addargs(&args, "-oClearAllForwardings yes");
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	fflag = tflag = 0;
3157c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:F:")) != -1)
3167c478bd9Sstevel@tonic-gate 		switch (ch) {
3177c478bd9Sstevel@tonic-gate 		/* User-visible flags. */
3187c478bd9Sstevel@tonic-gate 		case '4':
3197c478bd9Sstevel@tonic-gate 		case '6':
3207c478bd9Sstevel@tonic-gate 		case 'C':
3217c478bd9Sstevel@tonic-gate 			addargs(&args, "-%c", ch);
3227c478bd9Sstevel@tonic-gate 			break;
3237c478bd9Sstevel@tonic-gate 		case 'o':
3247c478bd9Sstevel@tonic-gate 		case 'c':
3257c478bd9Sstevel@tonic-gate 		case 'i':
3267c478bd9Sstevel@tonic-gate 		case 'F':
3277c478bd9Sstevel@tonic-gate 			addargs(&args, "-%c%s", ch, optarg);
3287c478bd9Sstevel@tonic-gate 			break;
3297c478bd9Sstevel@tonic-gate 		case 'P':
3307c478bd9Sstevel@tonic-gate 			addargs(&args, "-p%s", optarg);
3317c478bd9Sstevel@tonic-gate 			break;
3327c478bd9Sstevel@tonic-gate 		case 'B':
3337c478bd9Sstevel@tonic-gate 			addargs(&args, "-oBatchmode yes");
3347c478bd9Sstevel@tonic-gate 			break;
3357c478bd9Sstevel@tonic-gate 		case 'p':
3367c478bd9Sstevel@tonic-gate 			pflag = 1;
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 		case 'r':
3397c478bd9Sstevel@tonic-gate 			iamrecursive = 1;
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 		case 'S':
3427c478bd9Sstevel@tonic-gate 			ssh_program = xstrdup(optarg);
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		case 'v':
3457c478bd9Sstevel@tonic-gate 			addargs(&args, "-v");
3467c478bd9Sstevel@tonic-gate 			verbose_mode = 1;
3477c478bd9Sstevel@tonic-gate 			break;
3487c478bd9Sstevel@tonic-gate 		case 'q':
3497c478bd9Sstevel@tonic-gate 			showprogress = 0;
3507c478bd9Sstevel@tonic-gate 			break;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		/* Server options. */
3537c478bd9Sstevel@tonic-gate 		case 'd':
3547c478bd9Sstevel@tonic-gate 			targetshouldbedirectory = 1;
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 		case 'f':	/* "from" */
3577c478bd9Sstevel@tonic-gate 			iamremote = 1;
3587c478bd9Sstevel@tonic-gate 			fflag = 1;
3597c478bd9Sstevel@tonic-gate 			break;
3607c478bd9Sstevel@tonic-gate 		case 't':	/* "to" */
3617c478bd9Sstevel@tonic-gate 			iamremote = 1;
3627c478bd9Sstevel@tonic-gate 			tflag = 1;
3637c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
3647c478bd9Sstevel@tonic-gate 			setmode(0, O_BINARY);
3657c478bd9Sstevel@tonic-gate #endif
3667c478bd9Sstevel@tonic-gate 			break;
3677c478bd9Sstevel@tonic-gate 		default:
3687c478bd9Sstevel@tonic-gate 			usage();
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 	argc -= optind;
3717c478bd9Sstevel@tonic-gate 	argv += optind;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if ((pwd = getpwuid(userid = getuid())) == NULL)
3747c478bd9Sstevel@tonic-gate 		fatal("unknown user %d", (int)userid);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (!isatty(STDERR_FILENO))
3777c478bd9Sstevel@tonic-gate 		showprogress = 0;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	remin = STDIN_FILENO;
3807c478bd9Sstevel@tonic-gate 	remout = STDOUT_FILENO;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (fflag) {
3837c478bd9Sstevel@tonic-gate 		/* Follow "protocol", send data. */
3847c478bd9Sstevel@tonic-gate 		(void) response();
3857c478bd9Sstevel@tonic-gate 		source(argc, argv);
3867c478bd9Sstevel@tonic-gate 		exit(errs != 0);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 	if (tflag) {
3897c478bd9Sstevel@tonic-gate 		/* Receive data. */
3907c478bd9Sstevel@tonic-gate 		sink(argc, argv);
3917c478bd9Sstevel@tonic-gate 		exit(errs != 0);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 	if (argc < 2)
3947c478bd9Sstevel@tonic-gate 		usage();
3957c478bd9Sstevel@tonic-gate 	if (argc > 2)
3967c478bd9Sstevel@tonic-gate 		targetshouldbedirectory = 1;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	remin = remout = -1;
3997c478bd9Sstevel@tonic-gate 	do_cmd_pid = (pid_t)-1;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/* Command to be executed on remote system using "ssh". */
4027c478bd9Sstevel@tonic-gate 	(void) snprintf(cmd, sizeof (cmd), "scp%s%s%s%s",
4037c478bd9Sstevel@tonic-gate 	    verbose_mode ? " -v" : "",
4047c478bd9Sstevel@tonic-gate 	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
4057c478bd9Sstevel@tonic-gate 	    targetshouldbedirectory ? " -d" : "");
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, lostconn);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if ((targ = colon(argv[argc - 1])))	/* Dest is remote host. */
4107c478bd9Sstevel@tonic-gate 		toremote(targ, argc, argv);
4117c478bd9Sstevel@tonic-gate 	else {
4127c478bd9Sstevel@tonic-gate 		if (targetshouldbedirectory)
4137c478bd9Sstevel@tonic-gate 			verifydir(argv[argc - 1]);
41426ba1984Sjp161948 		tolocal(argc, argv);	/* Dest is local host. */
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * Finally check the exit status of the ssh process, if one was forked
4187c478bd9Sstevel@tonic-gate 	 * and no error has occurred yet
4197c478bd9Sstevel@tonic-gate 	 */
4207c478bd9Sstevel@tonic-gate 	if (do_cmd_pid != (pid_t)-1 && errs == 0) {
4217c478bd9Sstevel@tonic-gate 		if (remin != -1) {
4227c478bd9Sstevel@tonic-gate 			(void) close(remin);
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		if (remout != -1) {
4257c478bd9Sstevel@tonic-gate 			(void) close(remout);
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 		if (waitpid(do_cmd_pid, &status, 0) == -1) {
4287c478bd9Sstevel@tonic-gate 			errs = 1;
4297c478bd9Sstevel@tonic-gate 		} else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
4307c478bd9Sstevel@tonic-gate 			errs = 1;
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	return (errs != 0);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate void
4387c478bd9Sstevel@tonic-gate toremote(targ, argc, argv)
4397c478bd9Sstevel@tonic-gate 	char *targ, *argv[];
4407c478bd9Sstevel@tonic-gate 	int argc;
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	int i, len;
443a7e50fb1Sjp161948 	char *bp, *host, *src, *suser, *thost, *tuser, *arg;
444a7e50fb1Sjp161948 	arglist alist;
445a7e50fb1Sjp161948 
446a7e50fb1Sjp161948 	memset(&alist, '\0', sizeof (alist));
447a7e50fb1Sjp161948 	alist.list = NULL;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	*targ++ = 0;
4507c478bd9Sstevel@tonic-gate 	if (*targ == 0)
4517c478bd9Sstevel@tonic-gate 		targ = ".";
4527c478bd9Sstevel@tonic-gate 
453a7e50fb1Sjp161948 	arg = xstrdup(argv[argc - 1]);
454a7e50fb1Sjp161948 	if ((thost = strchr(arg, '@'))) {
4557c478bd9Sstevel@tonic-gate 		/* user@host */
4567c478bd9Sstevel@tonic-gate 		*thost++ = 0;
457a7e50fb1Sjp161948 		tuser = arg;
4587c478bd9Sstevel@tonic-gate 		if (*tuser == '\0')
4597c478bd9Sstevel@tonic-gate 			tuser = NULL;
4607c478bd9Sstevel@tonic-gate 		else if (!okname(tuser))
4617c478bd9Sstevel@tonic-gate 			exit(1);
4627c478bd9Sstevel@tonic-gate 	} else {
463a7e50fb1Sjp161948 		thost = arg;
4647c478bd9Sstevel@tonic-gate 		tuser = NULL;
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
467a7e50fb1Sjp161948 	if (tuser != NULL && !okname(tuser)) {
468a7e50fb1Sjp161948 		xfree(arg);
469a7e50fb1Sjp161948 		return;
470a7e50fb1Sjp161948 	}
471a7e50fb1Sjp161948 
4727c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc - 1; i++) {
4737c478bd9Sstevel@tonic-gate 		src = colon(argv[i]);
4747c478bd9Sstevel@tonic-gate 		if (src) {	/* remote to remote */
475a7e50fb1Sjp161948 			freeargs(&alist);
476a7e50fb1Sjp161948 			addargs(&alist, "%s", ssh_program);
477a7e50fb1Sjp161948 			if (verbose_mode)
478a7e50fb1Sjp161948 				addargs(&alist, "-v");
479a7e50fb1Sjp161948 			addargs(&alist, "-x");
480a7e50fb1Sjp161948 			addargs(&alist, "-oClearAllForwardings yes");
481a7e50fb1Sjp161948 			addargs(&alist, "-n");
482a7e50fb1Sjp161948 
4837c478bd9Sstevel@tonic-gate 			*src++ = 0;
4847c478bd9Sstevel@tonic-gate 			if (*src == 0)
4857c478bd9Sstevel@tonic-gate 				src = ".";
4867c478bd9Sstevel@tonic-gate 			host = strchr(argv[i], '@');
487a7e50fb1Sjp161948 
4887c478bd9Sstevel@tonic-gate 			if (host) {
4897c478bd9Sstevel@tonic-gate 				*host++ = 0;
4907c478bd9Sstevel@tonic-gate 				host = cleanhostname(host);
4917c478bd9Sstevel@tonic-gate 				suser = argv[i];
4927c478bd9Sstevel@tonic-gate 				if (*suser == '\0')
4937c478bd9Sstevel@tonic-gate 					suser = pwd->pw_name;
4947c478bd9Sstevel@tonic-gate 				else if (!okname(suser))
4957c478bd9Sstevel@tonic-gate 					continue;
496a7e50fb1Sjp161948 				addargs(&alist, "-l");
497a7e50fb1Sjp161948 				addargs(&alist, "%s", suser);
4987c478bd9Sstevel@tonic-gate 			} else {
4997c478bd9Sstevel@tonic-gate 				host = cleanhostname(argv[i]);
500a7e50fb1Sjp161948 			}
501a7e50fb1Sjp161948 			addargs(&alist, "%s", host);
502a7e50fb1Sjp161948 			addargs(&alist, "%s", cmd);
503a7e50fb1Sjp161948 			addargs(&alist, "%s", src);
504a7e50fb1Sjp161948 			addargs(&alist, "%s%s%s:%s",
5057c478bd9Sstevel@tonic-gate 				    tuser ? tuser : "", tuser ? "@" : "",
5067c478bd9Sstevel@tonic-gate 				    thost, targ);
507a7e50fb1Sjp161948 			if (do_local_cmd(&alist) != 0)
508a7e50fb1Sjp161948 				errs = 1;
5097c478bd9Sstevel@tonic-gate 		} else {	/* local to remote */
5107c478bd9Sstevel@tonic-gate 			if (remin == -1) {
5117c478bd9Sstevel@tonic-gate 				len = strlen(targ) + CMDNEEDS + 20;
5127c478bd9Sstevel@tonic-gate 				bp = xmalloc(len);
5137c478bd9Sstevel@tonic-gate 				(void) snprintf(bp, len, "%s -t %s", cmd, targ);
5147c478bd9Sstevel@tonic-gate 				host = cleanhostname(thost);
5157c478bd9Sstevel@tonic-gate 				if (do_cmd(host, tuser, bp, &remin,
5167c478bd9Sstevel@tonic-gate 				    &remout, argc) < 0)
5177c478bd9Sstevel@tonic-gate 					exit(1);
5187c478bd9Sstevel@tonic-gate 				if (response() < 0)
5197c478bd9Sstevel@tonic-gate 					exit(1);
5207c478bd9Sstevel@tonic-gate 				(void) xfree(bp);
5217c478bd9Sstevel@tonic-gate 			}
5227c478bd9Sstevel@tonic-gate 			source(1, argv + i);
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate void
5287c478bd9Sstevel@tonic-gate tolocal(argc, argv)
5297c478bd9Sstevel@tonic-gate 	int argc;
5307c478bd9Sstevel@tonic-gate 	char *argv[];
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	int i, len;
5337c478bd9Sstevel@tonic-gate 	char *bp, *host, *src, *suser;
534a7e50fb1Sjp161948 	arglist alist;
535a7e50fb1Sjp161948 
536a7e50fb1Sjp161948 	memset(&alist, '\0', sizeof (alist));
537a7e50fb1Sjp161948 	alist.list = NULL;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc - 1; i++) {
5407c478bd9Sstevel@tonic-gate 		if (!(src = colon(argv[i]))) {	/* Local to local. */
541a7e50fb1Sjp161948 			freeargs(&alist);
542a7e50fb1Sjp161948 			addargs(&alist, "%s", _PATH_CP);
543a7e50fb1Sjp161948 			if (iamrecursive)
544a7e50fb1Sjp161948 				addargs(&alist, "-r");
545a7e50fb1Sjp161948 			if (pflag)
546a7e50fb1Sjp161948 				addargs(&alist, "-p");
547a7e50fb1Sjp161948 			addargs(&alist, "%s", argv[i]);
548a7e50fb1Sjp161948 			addargs(&alist, "%s", argv[argc-1]);
549a7e50fb1Sjp161948 			if (do_local_cmd(&alist))
5507c478bd9Sstevel@tonic-gate 				++errs;
5517c478bd9Sstevel@tonic-gate 			continue;
5527c478bd9Sstevel@tonic-gate 		}
5537c478bd9Sstevel@tonic-gate 		*src++ = 0;
5547c478bd9Sstevel@tonic-gate 		if (*src == 0)
5557c478bd9Sstevel@tonic-gate 			src = ".";
5567c478bd9Sstevel@tonic-gate 		if ((host = strchr(argv[i], '@')) == NULL) {
5577c478bd9Sstevel@tonic-gate 			host = argv[i];
5587c478bd9Sstevel@tonic-gate 			suser = NULL;
5597c478bd9Sstevel@tonic-gate 		} else {
5607c478bd9Sstevel@tonic-gate 			*host++ = 0;
5617c478bd9Sstevel@tonic-gate 			suser = argv[i];
5627c478bd9Sstevel@tonic-gate 			if (*suser == '\0')
5637c478bd9Sstevel@tonic-gate 				suser = pwd->pw_name;
5647c478bd9Sstevel@tonic-gate 			else if (!okname(suser))
5657c478bd9Sstevel@tonic-gate 				continue;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 		host = cleanhostname(host);
5687c478bd9Sstevel@tonic-gate 		len = strlen(src) + CMDNEEDS + 20;
5697c478bd9Sstevel@tonic-gate 		bp = xmalloc(len);
5707c478bd9Sstevel@tonic-gate 		(void) snprintf(bp, len, "%s -f %s", cmd, src);
5717c478bd9Sstevel@tonic-gate 		if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
5727c478bd9Sstevel@tonic-gate 			(void) xfree(bp);
5737c478bd9Sstevel@tonic-gate 			++errs;
5747c478bd9Sstevel@tonic-gate 			continue;
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 		xfree(bp);
5777c478bd9Sstevel@tonic-gate 		sink(1, argv + argc - 1);
5787c478bd9Sstevel@tonic-gate 		(void) close(remin);
5797c478bd9Sstevel@tonic-gate 		remin = remout = -1;
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate void
5847c478bd9Sstevel@tonic-gate source(argc, argv)
5857c478bd9Sstevel@tonic-gate 	int argc;
5867c478bd9Sstevel@tonic-gate 	char *argv[];
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	struct stat stb;
5897c478bd9Sstevel@tonic-gate 	static BUF buffer;
5907c478bd9Sstevel@tonic-gate 	BUF *bp;
5917c478bd9Sstevel@tonic-gate 	off_t i, amt, result;
5927c478bd9Sstevel@tonic-gate 	int fd, haderr, indx;
5937c478bd9Sstevel@tonic-gate 	char *last, *name, buf[2048];
5947c478bd9Sstevel@tonic-gate 	int len;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	for (indx = 0; indx < argc; ++indx) {
5977c478bd9Sstevel@tonic-gate 		name = argv[indx];
5987c478bd9Sstevel@tonic-gate 		statbytes = 0;
5997c478bd9Sstevel@tonic-gate 		len = strlen(name);
6007c478bd9Sstevel@tonic-gate 		while (len > 1 && name[len-1] == '/')
6017c478bd9Sstevel@tonic-gate 			name[--len] = '\0';
6027c478bd9Sstevel@tonic-gate 		if (strchr(name, '\n') != NULL) {
6037c478bd9Sstevel@tonic-gate 			run_err("%s: skipping, filename contains a newline",
6047c478bd9Sstevel@tonic-gate 			    name);
6057c478bd9Sstevel@tonic-gate 			goto next;
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		if ((fd = open(name, O_RDONLY, 0)) < 0)
6087c478bd9Sstevel@tonic-gate 			goto syserr;
6097c478bd9Sstevel@tonic-gate 		if (fstat(fd, &stb) < 0) {
6107c478bd9Sstevel@tonic-gate syserr:			run_err("%s: %s", name, strerror(errno));
6117c478bd9Sstevel@tonic-gate 			goto next;
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 		switch (stb.st_mode & S_IFMT) {
6147c478bd9Sstevel@tonic-gate 		case S_IFREG:
6157c478bd9Sstevel@tonic-gate 			break;
6167c478bd9Sstevel@tonic-gate 		case S_IFDIR:
6177c478bd9Sstevel@tonic-gate 			if (iamrecursive) {
6187c478bd9Sstevel@tonic-gate 				rsource(name, &stb);
6197c478bd9Sstevel@tonic-gate 				goto next;
6207c478bd9Sstevel@tonic-gate 			}
6217c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
6227c478bd9Sstevel@tonic-gate 		default:
6237c478bd9Sstevel@tonic-gate 			run_err("%s: not a regular file", name);
6247c478bd9Sstevel@tonic-gate 			goto next;
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 		if ((last = strrchr(name, '/')) == NULL)
6277c478bd9Sstevel@tonic-gate 			last = name;
6287c478bd9Sstevel@tonic-gate 		else
6297c478bd9Sstevel@tonic-gate 			++last;
6307c478bd9Sstevel@tonic-gate 		curfile = last;
6317c478bd9Sstevel@tonic-gate 		if (pflag) {
6327c478bd9Sstevel@tonic-gate 			/*
6337c478bd9Sstevel@tonic-gate 			 * Make it compatible with possible future
6347c478bd9Sstevel@tonic-gate 			 * versions expecting microseconds.
6357c478bd9Sstevel@tonic-gate 			 */
6367c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "T%lu 0 %lu 0\n",
6377c478bd9Sstevel@tonic-gate 			    (ulong_t)stb.st_mtime,
6387c478bd9Sstevel@tonic-gate 			    (ulong_t)stb.st_atime);
6397c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, buf, strlen(buf));
6407c478bd9Sstevel@tonic-gate 			if (response() < 0)
6417c478bd9Sstevel@tonic-gate 				goto next;
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate #define	FILEMODEMASK	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
6447c478bd9Sstevel@tonic-gate #ifdef HAVE_LONG_LONG_INT
6457c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "C%04o %lld %s\n",
6467c478bd9Sstevel@tonic-gate 		    (uint_t)(stb.st_mode & FILEMODEMASK),
6477c478bd9Sstevel@tonic-gate 		    (long long)stb.st_size, last);
6487c478bd9Sstevel@tonic-gate #else
6497c478bd9Sstevel@tonic-gate 		/* XXX: Handle integer overflow? */
6507c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "C%04o %lu %s\n",
6517c478bd9Sstevel@tonic-gate 		    (uint_t)(stb.st_mode & FILEMODEMASK),
6527c478bd9Sstevel@tonic-gate 		    (ulong_t)stb.st_size, last);
6537c478bd9Sstevel@tonic-gate #endif
6547c478bd9Sstevel@tonic-gate 		if (verbose_mode) {
6557c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Sending file modes: %s"), buf);
6567c478bd9Sstevel@tonic-gate 			fflush(stderr);
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 		(void) atomicio(write, remout, buf, strlen(buf));
6597c478bd9Sstevel@tonic-gate 		if (response() < 0)
6607c478bd9Sstevel@tonic-gate 			goto next;
6617c478bd9Sstevel@tonic-gate 		if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
6627c478bd9Sstevel@tonic-gate next:			(void) close(fd);
6637c478bd9Sstevel@tonic-gate 			continue;
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		if (showprogress) {
6667c478bd9Sstevel@tonic-gate 			totalbytes = stb.st_size;
6677c478bd9Sstevel@tonic-gate 			progressmeter(-1);
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		/* Keep writing after an error so that we stay sync'd up. */
6707c478bd9Sstevel@tonic-gate 		for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
6717c478bd9Sstevel@tonic-gate 			amt = bp->cnt;
6727c478bd9Sstevel@tonic-gate 			if (i + amt > stb.st_size)
6737c478bd9Sstevel@tonic-gate 				amt = stb.st_size - i;
6747c478bd9Sstevel@tonic-gate 			if (!haderr) {
6757c478bd9Sstevel@tonic-gate 				result = atomicio(read, fd, bp->buf, amt);
6767c478bd9Sstevel@tonic-gate 				if (result != amt)
6777c478bd9Sstevel@tonic-gate 					haderr = result >= 0 ? EIO : errno;
6787c478bd9Sstevel@tonic-gate 			}
6797c478bd9Sstevel@tonic-gate 			if (haderr)
6807c478bd9Sstevel@tonic-gate 				(void) atomicio(write, remout, bp->buf, amt);
6817c478bd9Sstevel@tonic-gate 			else {
6827c478bd9Sstevel@tonic-gate 				result = atomicio(write, remout, bp->buf, amt);
6837c478bd9Sstevel@tonic-gate 				if (result != amt)
6847c478bd9Sstevel@tonic-gate 					haderr = result >= 0 ? EIO : errno;
6857c478bd9Sstevel@tonic-gate 				statbytes += result;
6867c478bd9Sstevel@tonic-gate 			}
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 		if (showprogress)
6897c478bd9Sstevel@tonic-gate 			progressmeter(1);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		if (close(fd) < 0 && !haderr)
6927c478bd9Sstevel@tonic-gate 			haderr = errno;
6937c478bd9Sstevel@tonic-gate 		if (!haderr)
6947c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, "", 1);
6957c478bd9Sstevel@tonic-gate 		else
6967c478bd9Sstevel@tonic-gate 			run_err("%s: %s", name, strerror(haderr));
6977c478bd9Sstevel@tonic-gate 		(void) response();
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate void
7027c478bd9Sstevel@tonic-gate rsource(name, statp)
7037c478bd9Sstevel@tonic-gate 	char *name;
7047c478bd9Sstevel@tonic-gate 	struct stat *statp;
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	DIR *dirp;
7077c478bd9Sstevel@tonic-gate 	struct dirent *dp;
7087c478bd9Sstevel@tonic-gate 	char *last, *vect[1], path[1100];
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if (!(dirp = opendir(name))) {
7117c478bd9Sstevel@tonic-gate 		run_err("%s: %s", name, strerror(errno));
7127c478bd9Sstevel@tonic-gate 		return;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	last = strrchr(name, '/');
7157c478bd9Sstevel@tonic-gate 	if (last == 0)
7167c478bd9Sstevel@tonic-gate 		last = name;
7177c478bd9Sstevel@tonic-gate 	else
7187c478bd9Sstevel@tonic-gate 		last++;
7197c478bd9Sstevel@tonic-gate 	if (pflag) {
7207c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "T%lu 0 %lu 0\n",
7217c478bd9Sstevel@tonic-gate 		    (ulong_t)statp->st_mtime,
7227c478bd9Sstevel@tonic-gate 		    (ulong_t)statp->st_atime);
7237c478bd9Sstevel@tonic-gate 		(void) atomicio(write, remout, path, strlen(path));
7247c478bd9Sstevel@tonic-gate 		if (response() < 0) {
7257c478bd9Sstevel@tonic-gate 			closedir(dirp);
7267c478bd9Sstevel@tonic-gate 			return;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "D%04o %d %.1024s\n",
7307c478bd9Sstevel@tonic-gate 	    (uint_t)(statp->st_mode & FILEMODEMASK), 0, last);
7317c478bd9Sstevel@tonic-gate 	if (verbose_mode)
7327c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Entering directory: %s"), path);
7337c478bd9Sstevel@tonic-gate 	(void) atomicio(write, remout, path, strlen(path));
7347c478bd9Sstevel@tonic-gate 	if (response() < 0) {
7357c478bd9Sstevel@tonic-gate 		closedir(dirp);
7367c478bd9Sstevel@tonic-gate 		return;
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
7397c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
7407c478bd9Sstevel@tonic-gate 			continue;
7417c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
7427c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
7437c478bd9Sstevel@tonic-gate 			continue;
7447c478bd9Sstevel@tonic-gate 		if (strlen(name) + 1 + strlen(dp->d_name) >=
7457c478bd9Sstevel@tonic-gate 		    sizeof (path) - 1) {
7467c478bd9Sstevel@tonic-gate 			run_err("%s/%s: name too long", name, dp->d_name);
7477c478bd9Sstevel@tonic-gate 			continue;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "%s/%s", name, dp->d_name);
7507c478bd9Sstevel@tonic-gate 		vect[0] = path;
7517c478bd9Sstevel@tonic-gate 		source(1, vect);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
7547c478bd9Sstevel@tonic-gate 	(void) atomicio(write, remout, "E\n", 2);
7557c478bd9Sstevel@tonic-gate 	(void) response();
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate void
7597c478bd9Sstevel@tonic-gate sink(argc, argv)
7607c478bd9Sstevel@tonic-gate 	int argc;
7617c478bd9Sstevel@tonic-gate 	char *argv[];
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	static BUF buffer;
7647c478bd9Sstevel@tonic-gate 	struct stat stb;
7657c478bd9Sstevel@tonic-gate 	enum {
7667c478bd9Sstevel@tonic-gate 		YES, NO, DISPLAYED
7677c478bd9Sstevel@tonic-gate 	} wrerr;
7687c478bd9Sstevel@tonic-gate 	BUF *bp;
7697c478bd9Sstevel@tonic-gate 	off_t i, j;
7707c478bd9Sstevel@tonic-gate 	int amt, count, exists, first, mask, mode, ofd, omode;
7717c478bd9Sstevel@tonic-gate 	off_t size;
7727c478bd9Sstevel@tonic-gate 	int setimes, targisdir, wrerrno = 0;
7737c478bd9Sstevel@tonic-gate 	char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
7747c478bd9Sstevel@tonic-gate 	struct timeval tv[2];
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate #define	atime	tv[0]
7777c478bd9Sstevel@tonic-gate #define	mtime	tv[1]
7787c478bd9Sstevel@tonic-gate #define	SCREWUP(str)	{ why = str; goto screwup; }
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	setimes = targisdir = 0;
7817c478bd9Sstevel@tonic-gate 	mask = umask(0);
7827c478bd9Sstevel@tonic-gate 	if (!pflag)
7837c478bd9Sstevel@tonic-gate 		(void) umask(mask);
7847c478bd9Sstevel@tonic-gate 	if (argc != 1) {
7857c478bd9Sstevel@tonic-gate 		run_err("ambiguous target");
7867c478bd9Sstevel@tonic-gate 		exit(1);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 	targ = *argv;
7897c478bd9Sstevel@tonic-gate 	if (targetshouldbedirectory)
7907c478bd9Sstevel@tonic-gate 		verifydir(targ);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	(void) atomicio(write, remout, "", 1);
7937c478bd9Sstevel@tonic-gate 	if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
7947c478bd9Sstevel@tonic-gate 		targisdir = 1;
7957c478bd9Sstevel@tonic-gate 	for (first = 1; ; first = 0) {
7967c478bd9Sstevel@tonic-gate 		cp = buf;
7977c478bd9Sstevel@tonic-gate 		if (atomicio(read, remin, cp, 1) <= 0)
7987c478bd9Sstevel@tonic-gate 			return;
7997c478bd9Sstevel@tonic-gate 		if (*cp++ == '\n')
8007c478bd9Sstevel@tonic-gate 			SCREWUP("unexpected <newline>")
8017c478bd9Sstevel@tonic-gate 		do {
8027c478bd9Sstevel@tonic-gate 			if (atomicio(read, remin, &ch, sizeof (ch)) !=
8037c478bd9Sstevel@tonic-gate 			    sizeof (ch))
8047c478bd9Sstevel@tonic-gate 				SCREWUP("lost connection")
8057c478bd9Sstevel@tonic-gate 			*cp++ = ch;
8067c478bd9Sstevel@tonic-gate 		} while (cp < &buf[sizeof (buf) - 1] && ch != '\n');
8077c478bd9Sstevel@tonic-gate 		*cp = 0;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		if (buf[0] == '\01' || buf[0] == '\02') {
8107c478bd9Sstevel@tonic-gate 			if (iamremote == 0)
8117c478bd9Sstevel@tonic-gate 				(void) atomicio(write, STDERR_FILENO,
8127c478bd9Sstevel@tonic-gate 				    buf + 1, strlen(buf + 1));
8137c478bd9Sstevel@tonic-gate 			if (buf[0] == '\02')
8147c478bd9Sstevel@tonic-gate 				exit(1);
8157c478bd9Sstevel@tonic-gate 			++errs;
8167c478bd9Sstevel@tonic-gate 			continue;
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 		if (buf[0] == 'E') {
8197c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, "", 1);
8207c478bd9Sstevel@tonic-gate 			return;
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 		if (ch == '\n')
8237c478bd9Sstevel@tonic-gate 			*--cp = 0;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		cp = buf;
8267c478bd9Sstevel@tonic-gate 		if (*cp == 'T') {
8277c478bd9Sstevel@tonic-gate 			setimes++;
8287c478bd9Sstevel@tonic-gate 			cp++;
8297c478bd9Sstevel@tonic-gate 			mtime.tv_sec = strtol(cp, &cp, 10);
8307c478bd9Sstevel@tonic-gate 			if (!cp || *cp++ != ' ')
8317c478bd9Sstevel@tonic-gate 				SCREWUP("mtime.sec not delimited")
8327c478bd9Sstevel@tonic-gate 			mtime.tv_usec = strtol(cp, &cp, 10);
8337c478bd9Sstevel@tonic-gate 			if (!cp || *cp++ != ' ')
8347c478bd9Sstevel@tonic-gate 				SCREWUP("mtime.usec not delimited")
8357c478bd9Sstevel@tonic-gate 			atime.tv_sec = strtol(cp, &cp, 10);
8367c478bd9Sstevel@tonic-gate 			if (!cp || *cp++ != ' ')
8377c478bd9Sstevel@tonic-gate 				SCREWUP("atime.sec not delimited")
8387c478bd9Sstevel@tonic-gate 			atime.tv_usec = strtol(cp, &cp, 10);
8397c478bd9Sstevel@tonic-gate 			if (!cp || *cp++ != '\0')
8407c478bd9Sstevel@tonic-gate 				SCREWUP("atime.usec not delimited")
8417c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, "", 1);
8427c478bd9Sstevel@tonic-gate 			continue;
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate 		if (*cp != 'C' && *cp != 'D') {
8457c478bd9Sstevel@tonic-gate 			/*
8467c478bd9Sstevel@tonic-gate 			 * Check for the case "rcp remote:foo\* local:bar".
8477c478bd9Sstevel@tonic-gate 			 * In this case, the line "No match." can be returned
8487c478bd9Sstevel@tonic-gate 			 * by the shell before the rcp command on the remote is
8497c478bd9Sstevel@tonic-gate 			 * executed so the ^Aerror_message convention isn't
8507c478bd9Sstevel@tonic-gate 			 * followed.
8517c478bd9Sstevel@tonic-gate 			 */
8527c478bd9Sstevel@tonic-gate 			if (first) {
8537c478bd9Sstevel@tonic-gate 				run_err("%s", cp);
8547c478bd9Sstevel@tonic-gate 				exit(1);
8557c478bd9Sstevel@tonic-gate 			}
8567c478bd9Sstevel@tonic-gate 			SCREWUP("expected control record")
8577c478bd9Sstevel@tonic-gate 		}
8587c478bd9Sstevel@tonic-gate 		mode = 0;
8597c478bd9Sstevel@tonic-gate 		for (++cp; cp < buf + 5; cp++) {
8607c478bd9Sstevel@tonic-gate 			if (*cp < '0' || *cp > '7')
8617c478bd9Sstevel@tonic-gate 				SCREWUP("bad mode")
8627c478bd9Sstevel@tonic-gate 			mode = (mode << 3) | (*cp - '0');
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		if (*cp++ != ' ')
8657c478bd9Sstevel@tonic-gate 			SCREWUP("mode not delimited")
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 		for (size = 0; isdigit(*cp); )
8687c478bd9Sstevel@tonic-gate 			size = size * 10 + (*cp++ - '0');
8697c478bd9Sstevel@tonic-gate 		if (*cp++ != ' ')
8707c478bd9Sstevel@tonic-gate 			SCREWUP("size not delimited")
87126ba1984Sjp161948 		if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
87226ba1984Sjp161948 			run_err("error: unexpected filename: %s", cp);
87326ba1984Sjp161948 			exit(1);
87426ba1984Sjp161948 		}
8757c478bd9Sstevel@tonic-gate 		if (targisdir) {
8767c478bd9Sstevel@tonic-gate 			static char *namebuf;
8777c478bd9Sstevel@tonic-gate 			static int cursize;
8787c478bd9Sstevel@tonic-gate 			size_t need;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 			need = strlen(targ) + strlen(cp) + 250;
8817c478bd9Sstevel@tonic-gate 			if (need > cursize) {
8827c478bd9Sstevel@tonic-gate 				if (namebuf)
8837c478bd9Sstevel@tonic-gate 					xfree(namebuf);
8847c478bd9Sstevel@tonic-gate 				namebuf = xmalloc(need);
8857c478bd9Sstevel@tonic-gate 				cursize = need;
8867c478bd9Sstevel@tonic-gate 			}
8877c478bd9Sstevel@tonic-gate 			(void) snprintf(namebuf, need, "%s%s%s", targ,
8887c478bd9Sstevel@tonic-gate 			    strcmp(targ, "/") ? "/" : "", cp);
8897c478bd9Sstevel@tonic-gate 			np = namebuf;
8907c478bd9Sstevel@tonic-gate 		} else
8917c478bd9Sstevel@tonic-gate 			np = targ;
8927c478bd9Sstevel@tonic-gate 		curfile = cp;
8937c478bd9Sstevel@tonic-gate 		exists = stat(np, &stb) == 0;
8947c478bd9Sstevel@tonic-gate 		if (buf[0] == 'D') {
8957c478bd9Sstevel@tonic-gate 			int mod_flag = pflag;
89626ba1984Sjp161948 			if (!iamrecursive)
89726ba1984Sjp161948 				SCREWUP("received directory without -r");
8987c478bd9Sstevel@tonic-gate 			if (exists) {
8997c478bd9Sstevel@tonic-gate 				if (!S_ISDIR(stb.st_mode)) {
9007c478bd9Sstevel@tonic-gate 					errno = ENOTDIR;
9017c478bd9Sstevel@tonic-gate 					goto bad;
9027c478bd9Sstevel@tonic-gate 				}
9037c478bd9Sstevel@tonic-gate 				if (pflag)
9047c478bd9Sstevel@tonic-gate 					(void) chmod(np, mode);
9057c478bd9Sstevel@tonic-gate 			} else {
9067c478bd9Sstevel@tonic-gate 				/*
9077c478bd9Sstevel@tonic-gate 				 * Handle copying from a read-only
9087c478bd9Sstevel@tonic-gate 				 * directory
9097c478bd9Sstevel@tonic-gate 				 */
9107c478bd9Sstevel@tonic-gate 				mod_flag = 1;
9117c478bd9Sstevel@tonic-gate 				if (mkdir(np, mode | S_IRWXU) < 0)
9127c478bd9Sstevel@tonic-gate 					goto bad;
9137c478bd9Sstevel@tonic-gate 			}
9147c478bd9Sstevel@tonic-gate 			vect[0] = xstrdup(np);
9157c478bd9Sstevel@tonic-gate 			sink(1, vect);
9167c478bd9Sstevel@tonic-gate 			if (setimes) {
9177c478bd9Sstevel@tonic-gate 				setimes = 0;
9187c478bd9Sstevel@tonic-gate 				if (utimes(vect[0], tv) < 0)
9197c478bd9Sstevel@tonic-gate 					run_err("%s: set times: %s",
9207c478bd9Sstevel@tonic-gate 					    vect[0], strerror(errno));
9217c478bd9Sstevel@tonic-gate 			}
9227c478bd9Sstevel@tonic-gate 			if (mod_flag)
9237c478bd9Sstevel@tonic-gate 				(void) chmod(vect[0], mode);
9247c478bd9Sstevel@tonic-gate 			if (vect[0])
9257c478bd9Sstevel@tonic-gate 				xfree(vect[0]);
9267c478bd9Sstevel@tonic-gate 			continue;
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 		omode = mode;
9297c478bd9Sstevel@tonic-gate 		mode |= S_IWRITE;
9307c478bd9Sstevel@tonic-gate 		if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
9317c478bd9Sstevel@tonic-gate bad:			run_err("%s: %s", np, strerror(errno));
9327c478bd9Sstevel@tonic-gate 			continue;
9337c478bd9Sstevel@tonic-gate 		}
9347c478bd9Sstevel@tonic-gate 		(void) atomicio(write, remout, "", 1);
9357c478bd9Sstevel@tonic-gate 		if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
9367c478bd9Sstevel@tonic-gate 			(void) close(ofd);
9377c478bd9Sstevel@tonic-gate 			continue;
9387c478bd9Sstevel@tonic-gate 		}
9397c478bd9Sstevel@tonic-gate 		wrerr = NO;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		if (showprogress) {
9427c478bd9Sstevel@tonic-gate 			totalbytes = size;
9437c478bd9Sstevel@tonic-gate 			progressmeter(-1);
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 		statbytes = 0;
94612fcfd5aSjp161948 		for (i = 0; i < size; i += bp->cnt) {
94712fcfd5aSjp161948 			amt = bp->cnt;
94812fcfd5aSjp161948 			cp = bp->buf;
9497c478bd9Sstevel@tonic-gate 			if (i + amt > size)
9507c478bd9Sstevel@tonic-gate 				amt = size - i;
95112fcfd5aSjp161948 			count = amt;
9527c478bd9Sstevel@tonic-gate 			do {
9537c478bd9Sstevel@tonic-gate 				j = read(remin, cp, amt);
9547c478bd9Sstevel@tonic-gate 				if (j == -1 && (errno == EINTR ||
9557c478bd9Sstevel@tonic-gate 				    errno == EAGAIN)) {
9567c478bd9Sstevel@tonic-gate 					continue;
9577c478bd9Sstevel@tonic-gate 				} else if (j <= 0) {
9587c478bd9Sstevel@tonic-gate 					run_err("%s", j ? strerror(errno) :
9597c478bd9Sstevel@tonic-gate 					    "dropped connection");
9607c478bd9Sstevel@tonic-gate 					exit(1);
9617c478bd9Sstevel@tonic-gate 				}
9627c478bd9Sstevel@tonic-gate 				amt -= j;
9637c478bd9Sstevel@tonic-gate 				cp += j;
9647c478bd9Sstevel@tonic-gate 				statbytes += j;
9657c478bd9Sstevel@tonic-gate 			} while (amt > 0);
9667c478bd9Sstevel@tonic-gate 			/* Keep reading so we stay sync'd up. */
9677c478bd9Sstevel@tonic-gate 			if (wrerr == NO) {
9687c478bd9Sstevel@tonic-gate 				j = atomicio(write, ofd, bp->buf,
9697c478bd9Sstevel@tonic-gate 				    count);
9707c478bd9Sstevel@tonic-gate 				if (j != count) {
9717c478bd9Sstevel@tonic-gate 					wrerr = YES;
9727c478bd9Sstevel@tonic-gate 					wrerrno = j >= 0 ? EIO : errno;
9737c478bd9Sstevel@tonic-gate 				}
9747c478bd9Sstevel@tonic-gate 			}
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 		if (showprogress)
9777c478bd9Sstevel@tonic-gate 			progressmeter(1);
9787c478bd9Sstevel@tonic-gate 		if (ftruncate(ofd, size)) {
9797c478bd9Sstevel@tonic-gate 			run_err("%s: truncate: %s", np, strerror(errno));
9807c478bd9Sstevel@tonic-gate 			wrerr = DISPLAYED;
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 		if (pflag) {
9837c478bd9Sstevel@tonic-gate 			if (exists || omode != mode)
9847c478bd9Sstevel@tonic-gate #ifdef HAVE_FCHMOD
985*038a8f41Sjp161948 				if (fchmod(ofd, omode)) {
9867c478bd9Sstevel@tonic-gate #else /* HAVE_FCHMOD */
987*038a8f41Sjp161948 				if (chmod(np, omode)) {
9887c478bd9Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
9897c478bd9Sstevel@tonic-gate 					run_err("%s: set mode: %s",
9907c478bd9Sstevel@tonic-gate 					    np, strerror(errno));
991*038a8f41Sjp161948 					wrerr = DISPLAYED;
992*038a8f41Sjp161948 				}
9937c478bd9Sstevel@tonic-gate 		} else {
9947c478bd9Sstevel@tonic-gate 			if (!exists && omode != mode)
9957c478bd9Sstevel@tonic-gate #ifdef HAVE_FCHMOD
996*038a8f41Sjp161948 				if (fchmod(ofd, omode & ~mask)) {
9977c478bd9Sstevel@tonic-gate #else /* HAVE_FCHMOD */
998*038a8f41Sjp161948 				if (chmod(np, omode & ~mask)) {
9997c478bd9Sstevel@tonic-gate #endif /* HAVE_FCHMOD */
10007c478bd9Sstevel@tonic-gate 					run_err("%s: set mode: %s",
10017c478bd9Sstevel@tonic-gate 					    np, strerror(errno));
1002*038a8f41Sjp161948 					wrerr = DISPLAYED;
1003*038a8f41Sjp161948 				}
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 		if (close(ofd) == -1) {
10067c478bd9Sstevel@tonic-gate 			wrerr = YES;
10077c478bd9Sstevel@tonic-gate 			wrerrno = errno;
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 		(void) response();
10107c478bd9Sstevel@tonic-gate 		if (setimes && wrerr == NO) {
10117c478bd9Sstevel@tonic-gate 			setimes = 0;
10127c478bd9Sstevel@tonic-gate 			if (utimes(np, tv) < 0) {
10137c478bd9Sstevel@tonic-gate 				run_err("%s: set times: %s",
10147c478bd9Sstevel@tonic-gate 				    np, strerror(errno));
10157c478bd9Sstevel@tonic-gate 				wrerr = DISPLAYED;
10167c478bd9Sstevel@tonic-gate 			}
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 		switch (wrerr) {
10197c478bd9Sstevel@tonic-gate 		case YES:
10207c478bd9Sstevel@tonic-gate 			run_err("%s: %s", np, strerror(wrerrno));
10217c478bd9Sstevel@tonic-gate 			break;
10227c478bd9Sstevel@tonic-gate 		case NO:
10237c478bd9Sstevel@tonic-gate 			(void) atomicio(write, remout, "", 1);
10247c478bd9Sstevel@tonic-gate 			break;
10257c478bd9Sstevel@tonic-gate 		case DISPLAYED:
10267c478bd9Sstevel@tonic-gate 			break;
10277c478bd9Sstevel@tonic-gate 		}
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate screwup:
10307c478bd9Sstevel@tonic-gate 	run_err("protocol error: %s", why);
10317c478bd9Sstevel@tonic-gate 	exit(1);
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate int
10357c478bd9Sstevel@tonic-gate response(void)
10367c478bd9Sstevel@tonic-gate {
10377c478bd9Sstevel@tonic-gate 	char ch, *cp, resp, rbuf[2048];
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if (atomicio(read, remin, &resp, sizeof (resp)) != sizeof (resp))
10407c478bd9Sstevel@tonic-gate 		lostconn(0);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	cp = rbuf;
10437c478bd9Sstevel@tonic-gate 	switch (resp) {
10447c478bd9Sstevel@tonic-gate 	case 0:		/* ok */
10457c478bd9Sstevel@tonic-gate 		return (0);
10467c478bd9Sstevel@tonic-gate 	default:
10477c478bd9Sstevel@tonic-gate 		*cp++ = resp;
10487c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
10497c478bd9Sstevel@tonic-gate 	case 1:		/* error, followed by error msg */
10507c478bd9Sstevel@tonic-gate 	case 2:		/* fatal error, "" */
10517c478bd9Sstevel@tonic-gate 		do {
10527c478bd9Sstevel@tonic-gate 			if (atomicio(read, remin, &ch, sizeof (ch)) !=
10537c478bd9Sstevel@tonic-gate 			    sizeof (ch))
10547c478bd9Sstevel@tonic-gate 				lostconn(0);
10557c478bd9Sstevel@tonic-gate 			*cp++ = ch;
10567c478bd9Sstevel@tonic-gate 		} while (cp < &rbuf[sizeof (rbuf) - 1] && ch != '\n');
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		if (!iamremote)
10597c478bd9Sstevel@tonic-gate 			(void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
10607c478bd9Sstevel@tonic-gate 		++errs;
10617c478bd9Sstevel@tonic-gate 		if (resp == 1)
10627c478bd9Sstevel@tonic-gate 			return (-1);
10637c478bd9Sstevel@tonic-gate 		exit(1);
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate void
10697c478bd9Sstevel@tonic-gate usage(void)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
10727c478bd9Sstevel@tonic-gate 	    gettext(
10737c478bd9Sstevel@tonic-gate 		"Usage: scp [-pqrvBC46] [-F config] [-S program] [-P port]\n"
10747c478bd9Sstevel@tonic-gate 		"           [-c cipher] [-i identity] [-o option]\n"
10757c478bd9Sstevel@tonic-gate 		"           [[user@]host1:]file1 [...] "
10767c478bd9Sstevel@tonic-gate 		"[[user@]host2:]file2\n"));
10777c478bd9Sstevel@tonic-gate 	exit(1);
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
10817c478bd9Sstevel@tonic-gate void
10827c478bd9Sstevel@tonic-gate run_err(const char *fmt, ...)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate 	static FILE *fp;
10857c478bd9Sstevel@tonic-gate 	va_list ap;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	++errs;
108826ba1984Sjp161948 
108926ba1984Sjp161948 	if (!iamremote) {
109026ba1984Sjp161948 		va_start(ap, fmt);
109126ba1984Sjp161948 		vfprintf(stderr, fmt, ap);
109226ba1984Sjp161948 		va_end(ap);
109326ba1984Sjp161948 		fprintf(stderr, "\n");
109426ba1984Sjp161948 	}
109526ba1984Sjp161948 
10967c478bd9Sstevel@tonic-gate 	if (fp == NULL && !(fp = fdopen(remout, "w")))
10977c478bd9Sstevel@tonic-gate 		return;
109826ba1984Sjp161948 
10997c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%c", 0x01);
11007c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "scp: ");
11017c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
11027c478bd9Sstevel@tonic-gate 	(void) vfprintf(fp, fmt, ap);
11037c478bd9Sstevel@tonic-gate 	va_end(ap);
11047c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\n");
11057c478bd9Sstevel@tonic-gate 	(void) fflush(fp);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate void
11107c478bd9Sstevel@tonic-gate verifydir(cp)
11117c478bd9Sstevel@tonic-gate 	char *cp;
11127c478bd9Sstevel@tonic-gate {
11137c478bd9Sstevel@tonic-gate 	struct stat stb;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	if (!stat(cp, &stb)) {
11167c478bd9Sstevel@tonic-gate 		if (S_ISDIR(stb.st_mode))
11177c478bd9Sstevel@tonic-gate 			return;
11187c478bd9Sstevel@tonic-gate 		errno = ENOTDIR;
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 	run_err("%s: %s", cp, strerror(errno));
11217c478bd9Sstevel@tonic-gate 	exit(1);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate int
11257c478bd9Sstevel@tonic-gate okname(cp0)
11267c478bd9Sstevel@tonic-gate 	char *cp0;
11277c478bd9Sstevel@tonic-gate {
11287c478bd9Sstevel@tonic-gate 	int c;
11297c478bd9Sstevel@tonic-gate 	char *cp;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	cp = cp0;
11327c478bd9Sstevel@tonic-gate 	do {
11337c478bd9Sstevel@tonic-gate 		c = (int)*cp;
11347c478bd9Sstevel@tonic-gate 		if (c & 0200)
11357c478bd9Sstevel@tonic-gate 			goto bad;
11367c478bd9Sstevel@tonic-gate 		if (!isalpha(c) && !isdigit(c) &&
11377c478bd9Sstevel@tonic-gate 		    c != '_' && c != '-' && c != '.' && c != '+')
11387c478bd9Sstevel@tonic-gate 			goto bad;
11397c478bd9Sstevel@tonic-gate 	} while (*++cp);
11407c478bd9Sstevel@tonic-gate 	return (1);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate bad:	fprintf(stderr, gettext("%s: invalid user name\n"), cp0);
11437c478bd9Sstevel@tonic-gate 	return (0);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate BUF *
11477c478bd9Sstevel@tonic-gate allocbuf(bp, fd, blksize)
11487c478bd9Sstevel@tonic-gate 	BUF *bp;
11497c478bd9Sstevel@tonic-gate 	int fd, blksize;
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	size_t size;
11527c478bd9Sstevel@tonic-gate #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
11537c478bd9Sstevel@tonic-gate 	struct stat stb;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	if (fstat(fd, &stb) < 0) {
11567c478bd9Sstevel@tonic-gate 		run_err("fstat: %s", strerror(errno));
11577c478bd9Sstevel@tonic-gate 		return (0);
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 	if (stb.st_blksize == 0)
11607c478bd9Sstevel@tonic-gate 		size = blksize;
11617c478bd9Sstevel@tonic-gate 	else
11627c478bd9Sstevel@tonic-gate 		size = blksize + (stb.st_blksize - blksize % stb.st_blksize) %
11637c478bd9Sstevel@tonic-gate 		    stb.st_blksize;
11647c478bd9Sstevel@tonic-gate #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
11657c478bd9Sstevel@tonic-gate 	size = blksize;
11667c478bd9Sstevel@tonic-gate #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
11677c478bd9Sstevel@tonic-gate 	if (bp->cnt >= size)
11687c478bd9Sstevel@tonic-gate 		return (bp);
11697c478bd9Sstevel@tonic-gate 	if (bp->buf == NULL)
11707c478bd9Sstevel@tonic-gate 		bp->buf = xmalloc(size);
11717c478bd9Sstevel@tonic-gate 	else
11727c478bd9Sstevel@tonic-gate 		bp->buf = xrealloc(bp->buf, size);
11737c478bd9Sstevel@tonic-gate 	memset(bp->buf, 0, size);
11747c478bd9Sstevel@tonic-gate 	bp->cnt = size;
11757c478bd9Sstevel@tonic-gate 	return (bp);
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate void
11797c478bd9Sstevel@tonic-gate lostconn(signo)
11807c478bd9Sstevel@tonic-gate 	int signo;
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	if (!iamremote)
11837c478bd9Sstevel@tonic-gate 		write(STDERR_FILENO, "lost connection\n", 16);
11847c478bd9Sstevel@tonic-gate 	if (signo)
11857c478bd9Sstevel@tonic-gate 		_exit(1);
11867c478bd9Sstevel@tonic-gate 	else
11877c478bd9Sstevel@tonic-gate 		exit(1);
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate static void
11917c478bd9Sstevel@tonic-gate updateprogressmeter(int ignore)
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate 	int save_errno = errno;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	progressmeter(0);
11967c478bd9Sstevel@tonic-gate 	signal(SIGALRM, updateprogressmeter);
11977c478bd9Sstevel@tonic-gate 	alarm(PROGRESSTIME);
11987c478bd9Sstevel@tonic-gate 	errno = save_errno;
11997c478bd9Sstevel@tonic-gate }
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate static int
12027c478bd9Sstevel@tonic-gate foregroundproc(void)
12037c478bd9Sstevel@tonic-gate {
12047c478bd9Sstevel@tonic-gate 	static pid_t pgrp = -1;
12057c478bd9Sstevel@tonic-gate 	int ctty_pgrp;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	if (pgrp == -1)
12087c478bd9Sstevel@tonic-gate 		pgrp = getpgrp();
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate #ifdef HAVE_TCGETPGRP
12117c478bd9Sstevel@tonic-gate 	return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 &&
12127c478bd9Sstevel@tonic-gate 		ctty_pgrp == pgrp);
12137c478bd9Sstevel@tonic-gate #else
12147c478bd9Sstevel@tonic-gate 	return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
12157c478bd9Sstevel@tonic-gate 	    ctty_pgrp == pgrp));
12167c478bd9Sstevel@tonic-gate #endif
12177c478bd9Sstevel@tonic-gate }
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate void
12207c478bd9Sstevel@tonic-gate progressmeter(int flag)
12217c478bd9Sstevel@tonic-gate {
12227c478bd9Sstevel@tonic-gate 	static const char prefixes[] = " KMGTP";
12237c478bd9Sstevel@tonic-gate 	static struct timeval lastupdate;
12247c478bd9Sstevel@tonic-gate 	static off_t lastsize;
12257c478bd9Sstevel@tonic-gate 	struct timeval now, td, wait;
12267c478bd9Sstevel@tonic-gate 	off_t cursize, abbrevsize;
12277c478bd9Sstevel@tonic-gate 	double elapsed;
12287c478bd9Sstevel@tonic-gate 	int ratio, barlength, i, remaining;
12297c478bd9Sstevel@tonic-gate 	char buf[512];
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	if (flag == -1) {
12327c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&start, (struct timezone *)0);
12337c478bd9Sstevel@tonic-gate 		lastupdate = start;
12347c478bd9Sstevel@tonic-gate 		lastsize = 0;
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 	if (foregroundproc() == 0)
12377c478bd9Sstevel@tonic-gate 		return;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&now, (struct timezone *)0);
12407c478bd9Sstevel@tonic-gate 	cursize = statbytes;
12417c478bd9Sstevel@tonic-gate 	if (totalbytes != 0) {
12427c478bd9Sstevel@tonic-gate 		ratio = (int)(100.0 * cursize / totalbytes);
12437c478bd9Sstevel@tonic-gate 		ratio = MAX(ratio, 0);
12447c478bd9Sstevel@tonic-gate 		ratio = MIN(ratio, 100);
12457c478bd9Sstevel@tonic-gate 	} else
12467c478bd9Sstevel@tonic-gate 		ratio = 100;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	snprintf(buf, sizeof (buf), "\r%-20.20s %3d%% ", curfile, ratio);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	barlength = getttywidth() - 51;
12517c478bd9Sstevel@tonic-gate 	if (barlength > 0) {
12527c478bd9Sstevel@tonic-gate 		i = barlength * ratio / 100;
12537c478bd9Sstevel@tonic-gate 		snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12547c478bd9Sstevel@tonic-gate 		    "|%.*s%*s|", i,
12557c478bd9Sstevel@tonic-gate 		    "*******************************************************"
12567c478bd9Sstevel@tonic-gate 		    "*******************************************************"
12577c478bd9Sstevel@tonic-gate 		    "*******************************************************"
12587c478bd9Sstevel@tonic-gate 		    "*******************************************************"
12597c478bd9Sstevel@tonic-gate 		    "*******************************************************"
12607c478bd9Sstevel@tonic-gate 		    "*******************************************************"
12617c478bd9Sstevel@tonic-gate 		    "*******************************************************",
12627c478bd9Sstevel@tonic-gate 		    barlength - i, "");
12637c478bd9Sstevel@tonic-gate 	}
12647c478bd9Sstevel@tonic-gate 	i = 0;
12657c478bd9Sstevel@tonic-gate 	abbrevsize = cursize;
12667c478bd9Sstevel@tonic-gate 	while (abbrevsize >= 100000 && i < sizeof (prefixes)) {
12677c478bd9Sstevel@tonic-gate 		i++;
12687c478bd9Sstevel@tonic-gate 		abbrevsize >>= 10;
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 	snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf), " %5lu %c%c ",
12717c478bd9Sstevel@tonic-gate 	    (unsigned long) abbrevsize, prefixes[i],
12727c478bd9Sstevel@tonic-gate 	    prefixes[i] == ' ' ? ' ' : 'B');
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	timersub(&now, &lastupdate, &wait);
12757c478bd9Sstevel@tonic-gate 	if (cursize > lastsize) {
12767c478bd9Sstevel@tonic-gate 		lastupdate = now;
12777c478bd9Sstevel@tonic-gate 		lastsize = cursize;
12787c478bd9Sstevel@tonic-gate 		if (wait.tv_sec >= STALLTIME) {
12797c478bd9Sstevel@tonic-gate 			start.tv_sec += wait.tv_sec;
12807c478bd9Sstevel@tonic-gate 			start.tv_usec += wait.tv_usec;
12817c478bd9Sstevel@tonic-gate 		}
12827c478bd9Sstevel@tonic-gate 		wait.tv_sec = 0;
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 	timersub(&now, &start, &td);
12857c478bd9Sstevel@tonic-gate 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if (flag != 1 &&
12887c478bd9Sstevel@tonic-gate 	    (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) {
12897c478bd9Sstevel@tonic-gate 		snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12907c478bd9Sstevel@tonic-gate 		    "   --:-- ETA");
12917c478bd9Sstevel@tonic-gate 	} else if (wait.tv_sec >= STALLTIME) {
12927c478bd9Sstevel@tonic-gate 		snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
12937c478bd9Sstevel@tonic-gate 		    " - stalled -");
12947c478bd9Sstevel@tonic-gate 	} else {
12957c478bd9Sstevel@tonic-gate 		if (flag != 1)
12967c478bd9Sstevel@tonic-gate 			remaining = (int)(totalbytes / (statbytes / elapsed) -
12977c478bd9Sstevel@tonic-gate 			    elapsed);
12987c478bd9Sstevel@tonic-gate 		else
12997c478bd9Sstevel@tonic-gate 			remaining = (int)elapsed;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 		i = remaining / 3600;
13027c478bd9Sstevel@tonic-gate 		if (i)
13037c478bd9Sstevel@tonic-gate 			snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13047c478bd9Sstevel@tonic-gate 			    "%2d:", i);
13057c478bd9Sstevel@tonic-gate 		else
13067c478bd9Sstevel@tonic-gate 			snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13077c478bd9Sstevel@tonic-gate 			    "   ");
13087c478bd9Sstevel@tonic-gate 		i = remaining % 3600;
13097c478bd9Sstevel@tonic-gate 		snprintf(buf + strlen(buf), sizeof (buf) - strlen(buf),
13107c478bd9Sstevel@tonic-gate 		    "%02d:%02d%s", i / 60, i % 60,
13117c478bd9Sstevel@tonic-gate 		    (flag != 1) ? " ETA" : "    ");
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 	atomicio(write, fileno(stdout), buf, strlen(buf));
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	if (flag == -1) {
13167c478bd9Sstevel@tonic-gate 		mysignal(SIGALRM, updateprogressmeter);
13177c478bd9Sstevel@tonic-gate 		alarm(PROGRESSTIME);
13187c478bd9Sstevel@tonic-gate 	} else if (flag == 1) {
13197c478bd9Sstevel@tonic-gate 		alarm(0);
13207c478bd9Sstevel@tonic-gate 		atomicio(write, fileno(stdout), "\n", 1);
13217c478bd9Sstevel@tonic-gate 		statbytes = 0;
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate }
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate int
13267c478bd9Sstevel@tonic-gate getttywidth(void)
13277c478bd9Sstevel@tonic-gate {
13287c478bd9Sstevel@tonic-gate 	struct winsize winsize;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
13317c478bd9Sstevel@tonic-gate 		return (winsize.ws_col ? winsize.ws_col : 80);
13327c478bd9Sstevel@tonic-gate 	else
13337c478bd9Sstevel@tonic-gate 		return (80);
13347c478bd9Sstevel@tonic-gate }
1335