Home | History | Annotate | Line # | Download | only in dist
sftp.c revision 1.36
      1  1.27  christos /*	$NetBSD: sftp.c,v 1.36 2022/10/05 22:39:36 christos Exp $	*/
      2  1.36  christos /* $OpenBSD: sftp.c,v 1.222 2022/09/19 10:46:00 djm Exp $ */
      3  1.36  christos 
      4   1.1  christos /*
      5   1.1  christos  * Copyright (c) 2001-2004 Damien Miller <djm (at) openbsd.org>
      6   1.1  christos  *
      7   1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8   1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9   1.1  christos  * copyright notice and this permission notice appear in all copies.
     10   1.1  christos  *
     11   1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1  christos  */
     19   1.1  christos 
     20   1.2  christos #include "includes.h"
     21  1.27  christos __RCSID("$NetBSD: sftp.c,v 1.36 2022/10/05 22:39:36 christos Exp $");
     22  1.20  christos 
     23  1.14  christos #include <sys/param.h>	/* MIN MAX */
     24   1.1  christos #include <sys/types.h>
     25   1.1  christos #include <sys/ioctl.h>
     26   1.1  christos #include <sys/wait.h>
     27   1.1  christos #include <sys/stat.h>
     28   1.1  christos #include <sys/socket.h>
     29   1.1  christos #include <sys/statvfs.h>
     30   1.1  christos 
     31   1.1  christos #include <ctype.h>
     32   1.1  christos #include <errno.h>
     33   1.1  christos #include <glob.h>
     34   1.1  christos #include <histedit.h>
     35   1.1  christos #include <paths.h>
     36   1.4      adam #include <libgen.h>
     37  1.12  christos #include <locale.h>
     38   1.1  christos #include <signal.h>
     39  1.19  christos #include <stdarg.h>
     40   1.1  christos #include <stdlib.h>
     41   1.1  christos #include <stdio.h>
     42   1.1  christos #include <string.h>
     43   1.1  christos #include <unistd.h>
     44  1.14  christos #include <limits.h>
     45   1.1  christos #include <util.h>
     46   1.1  christos 
     47   1.1  christos #include "xmalloc.h"
     48   1.1  christos #include "log.h"
     49   1.1  christos #include "pathnames.h"
     50   1.1  christos #include "misc.h"
     51  1.19  christos #include "utf8.h"
     52   1.1  christos 
     53   1.1  christos #include "sftp.h"
     54  1.14  christos #include "ssherr.h"
     55  1.14  christos #include "sshbuf.h"
     56   1.1  christos #include "sftp-common.h"
     57   1.1  christos #include "sftp-client.h"
     58  1.36  christos #include "sftp-usergroup.h"
     59  1.36  christos 
     60   1.2  christos #include "fmt_scaled.h"
     61   1.1  christos 
     62   1.1  christos /* File to read commands from */
     63   1.1  christos FILE* infile;
     64   1.1  christos 
     65   1.1  christos /* Are we in batchfile mode? */
     66   1.1  christos int batchmode = 0;
     67   1.1  christos 
     68   1.1  christos /* PID of ssh transport process */
     69  1.24  christos static volatile pid_t sshpid = -1;
     70   1.1  christos 
     71  1.32  christos /* Suppress diagnostic messages */
     72  1.12  christos int quiet = 0;
     73  1.12  christos 
     74   1.1  christos /* This is set to 0 if the progressmeter is not desired. */
     75   1.1  christos int showprogress = 1;
     76   1.1  christos 
     77   1.4      adam /* When this option is set, we always recursively download/upload directories */
     78   1.4      adam int global_rflag = 0;
     79   1.4      adam 
     80  1.13  christos /* When this option is set, we resume download or upload if possible */
     81  1.12  christos int global_aflag = 0;
     82  1.12  christos 
     83   1.4      adam /* When this option is set, the file transfers will always preserve times */
     84   1.4      adam int global_pflag = 0;
     85   1.4      adam 
     86  1.13  christos /* When this option is set, transfers will have fsync() called on each file */
     87  1.13  christos int global_fflag = 0;
     88  1.13  christos 
     89   1.1  christos /* SIGINT received during command processing */
     90   1.1  christos volatile sig_atomic_t interrupted = 0;
     91   1.1  christos 
     92   1.1  christos /* I wish qsort() took a separate ctx for the comparison function...*/
     93   1.1  christos int sort_flag;
     94  1.22  christos glob_t *sort_glob;
     95   1.1  christos 
     96   1.4      adam /* Context used for commandline completion */
     97   1.4      adam struct complete_ctx {
     98   1.4      adam 	struct sftp_conn *conn;
     99   1.4      adam 	char **remote_pathp;
    100   1.4      adam };
    101   1.4      adam 
    102   1.1  christos int remote_glob(struct sftp_conn *, const char *, int,
    103   1.1  christos     int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
    104   1.1  christos 
    105   1.1  christos /* Separators for interactive commands */
    106   1.1  christos #define WHITESPACE " \t\r\n"
    107   1.1  christos 
    108   1.1  christos /* ls flags */
    109   1.4      adam #define LS_LONG_VIEW	0x0001	/* Full view ala ls -l */
    110   1.4      adam #define LS_SHORT_VIEW	0x0002	/* Single row view ala ls -1 */
    111   1.4      adam #define LS_NUMERIC_VIEW	0x0004	/* Long view with numeric uid/gid */
    112   1.4      adam #define LS_NAME_SORT	0x0008	/* Sort by name (default) */
    113   1.4      adam #define LS_TIME_SORT	0x0010	/* Sort by mtime */
    114   1.4      adam #define LS_SIZE_SORT	0x0020	/* Sort by file size */
    115   1.4      adam #define LS_REVERSE_SORT	0x0040	/* Reverse sort order */
    116   1.4      adam #define LS_SHOW_ALL	0x0080	/* Don't skip filenames starting with '.' */
    117   1.4      adam #define LS_SI_UNITS	0x0100	/* Display sizes as K, M, G, etc. */
    118   1.1  christos 
    119   1.4      adam #define VIEW_FLAGS	(LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW|LS_SI_UNITS)
    120   1.1  christos #define SORT_FLAGS	(LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
    121   1.1  christos 
    122   1.1  christos /* Commands for interactive mode */
    123  1.13  christos enum sftp_command {
    124  1.13  christos 	I_CHDIR = 1,
    125  1.13  christos 	I_CHGRP,
    126  1.13  christos 	I_CHMOD,
    127  1.13  christos 	I_CHOWN,
    128  1.35  christos 	I_COPY,
    129  1.13  christos 	I_DF,
    130  1.13  christos 	I_GET,
    131  1.13  christos 	I_HELP,
    132  1.13  christos 	I_LCHDIR,
    133  1.13  christos 	I_LINK,
    134  1.13  christos 	I_LLS,
    135  1.13  christos 	I_LMKDIR,
    136  1.13  christos 	I_LPWD,
    137  1.13  christos 	I_LS,
    138  1.13  christos 	I_LUMASK,
    139  1.13  christos 	I_MKDIR,
    140  1.13  christos 	I_PUT,
    141  1.13  christos 	I_PWD,
    142  1.13  christos 	I_QUIT,
    143  1.13  christos 	I_REGET,
    144  1.13  christos 	I_RENAME,
    145  1.13  christos 	I_REPUT,
    146  1.13  christos 	I_RM,
    147  1.13  christos 	I_RMDIR,
    148  1.13  christos 	I_SHELL,
    149  1.13  christos 	I_SYMLINK,
    150  1.13  christos 	I_VERSION,
    151  1.13  christos 	I_PROGRESS,
    152  1.13  christos };
    153   1.1  christos 
    154   1.1  christos struct CMD {
    155   1.1  christos 	const char *c;
    156   1.1  christos 	const int n;
    157  1.36  christos 	const int t;	/* Completion type for the first argument */
    158  1.36  christos 	const int t2;	/* completion type for the optional second argument */
    159   1.1  christos };
    160   1.1  christos 
    161   1.4      adam /* Type of completion */
    162   1.4      adam #define NOARGS	0
    163   1.4      adam #define REMOTE	1
    164   1.4      adam #define LOCAL	2
    165   1.4      adam 
    166   1.1  christos static const struct CMD cmds[] = {
    167  1.36  christos 	{ "bye",	I_QUIT,		NOARGS, 	NOARGS	},
    168  1.36  christos 	{ "cd",		I_CHDIR,	REMOTE, 	NOARGS	},
    169  1.36  christos 	{ "chdir",	I_CHDIR,	REMOTE, 	NOARGS	},
    170  1.36  christos 	{ "chgrp",	I_CHGRP,	REMOTE, 	NOARGS	},
    171  1.36  christos 	{ "chmod",	I_CHMOD,	REMOTE, 	NOARGS	},
    172  1.36  christos 	{ "chown",	I_CHOWN,	REMOTE, 	NOARGS	},
    173  1.36  christos 	{ "copy",	I_COPY,		REMOTE, 	LOCAL	},
    174  1.36  christos 	{ "cp",		I_COPY,		REMOTE, 	LOCAL	},
    175  1.36  christos 	{ "df",		I_DF,		REMOTE, 	NOARGS	},
    176  1.36  christos 	{ "dir",	I_LS,		REMOTE, 	NOARGS	},
    177  1.36  christos 	{ "exit",	I_QUIT,		NOARGS, 	NOARGS	},
    178  1.36  christos 	{ "get",	I_GET,		REMOTE, 	LOCAL	},
    179  1.36  christos 	{ "help",	I_HELP,		NOARGS, 	NOARGS	},
    180  1.36  christos 	{ "lcd",	I_LCHDIR,	LOCAL,		NOARGS	},
    181  1.36  christos 	{ "lchdir",	I_LCHDIR,	LOCAL,		NOARGS	},
    182  1.36  christos 	{ "lls",	I_LLS,		LOCAL,		NOARGS	},
    183  1.36  christos 	{ "lmkdir",	I_LMKDIR,	LOCAL,		NOARGS	},
    184  1.36  christos 	{ "ln",		I_LINK,		REMOTE, 	REMOTE	},
    185  1.36  christos 	{ "lpwd",	I_LPWD,		LOCAL,		NOARGS	},
    186  1.36  christos 	{ "ls",		I_LS,		REMOTE,		NOARGS	},
    187  1.36  christos 	{ "lumask",	I_LUMASK,	NOARGS,		NOARGS	},
    188  1.36  christos 	{ "mkdir",	I_MKDIR,	REMOTE,		NOARGS	},
    189  1.36  christos 	{ "mget",	I_GET,		REMOTE,		LOCAL	},
    190  1.36  christos 	{ "mput",	I_PUT,		LOCAL,		REMOTE	},
    191  1.36  christos 	{ "progress",	I_PROGRESS,	NOARGS,		NOARGS	},
    192  1.36  christos 	{ "put",	I_PUT,		LOCAL,		REMOTE	},
    193  1.36  christos 	{ "pwd",	I_PWD,		REMOTE, 	NOARGS	},
    194  1.36  christos 	{ "quit",	I_QUIT,		NOARGS, 	NOARGS	},
    195  1.36  christos 	{ "reget",	I_REGET,	REMOTE, 	LOCAL	},
    196  1.36  christos 	{ "rename",	I_RENAME,	REMOTE, 	REMOTE	},
    197  1.36  christos 	{ "reput",	I_REPUT,	LOCAL,		REMOTE	},
    198  1.36  christos 	{ "rm",		I_RM,		REMOTE,		NOARGS	},
    199  1.36  christos 	{ "rmdir",	I_RMDIR,	REMOTE,		NOARGS	},
    200  1.36  christos 	{ "symlink",	I_SYMLINK,	REMOTE,		REMOTE	},
    201  1.36  christos 	{ "version",	I_VERSION,	NOARGS, 	NOARGS	},
    202  1.36  christos 	{ "!",		I_SHELL,	NOARGS, 	NOARGS	},
    203  1.36  christos 	{ "?",		I_HELP,		NOARGS, 	NOARGS	},
    204  1.36  christos 	{ NULL,		-1,		-1,		-1	}
    205   1.1  christos };
    206   1.1  christos 
    207   1.1  christos /* ARGSUSED */
    208   1.8     joerg __dead static void
    209   1.1  christos killchild(int signo)
    210   1.1  christos {
    211  1.28  christos 	pid_t pid;
    212  1.28  christos 
    213  1.28  christos 	pid = sshpid;
    214  1.28  christos 	if (pid > 1) {
    215  1.28  christos 		kill(pid, SIGTERM);
    216  1.28  christos 		waitpid(pid, NULL, 0);
    217   1.1  christos 	}
    218   1.1  christos 
    219   1.1  christos 	_exit(1);
    220   1.1  christos }
    221   1.1  christos 
    222   1.1  christos /* ARGSUSED */
    223   1.1  christos static void
    224  1.20  christos suspchild(int signo)
    225  1.20  christos {
    226  1.20  christos 	if (sshpid > 1) {
    227  1.20  christos 		kill(sshpid, signo);
    228  1.20  christos 		while (waitpid(sshpid, NULL, WUNTRACED) == -1 && errno == EINTR)
    229  1.20  christos 			continue;
    230  1.20  christos 	}
    231  1.20  christos 	kill(getpid(), SIGSTOP);
    232  1.20  christos }
    233  1.20  christos 
    234  1.20  christos /* ARGSUSED */
    235  1.20  christos static void
    236   1.1  christos cmd_interrupt(int signo)
    237   1.1  christos {
    238   1.1  christos 	const char msg[] = "\rInterrupt  \n";
    239   1.1  christos 	int olderrno = errno;
    240   1.1  christos 
    241  1.12  christos 	(void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
    242   1.1  christos 	interrupted = 1;
    243   1.1  christos 	errno = olderrno;
    244   1.1  christos }
    245   1.1  christos 
    246  1.33  christos /* ARGSUSED */
    247  1.33  christos static void
    248  1.33  christos read_interrupt(int signo)
    249  1.33  christos {
    250  1.33  christos 	interrupted = 1;
    251  1.33  christos }
    252  1.33  christos 
    253  1.24  christos /*ARGSUSED*/
    254  1.24  christos static void
    255  1.24  christos sigchld_handler(int sig)
    256  1.24  christos {
    257  1.24  christos 	int save_errno = errno;
    258  1.24  christos 	pid_t pid;
    259  1.24  christos 	const char msg[] = "\rConnection closed.  \n";
    260  1.24  christos 
    261  1.24  christos 	/* Report if ssh transport process dies. */
    262  1.24  christos 	while ((pid = waitpid(sshpid, NULL, WNOHANG)) == -1 && errno == EINTR)
    263  1.24  christos 		continue;
    264  1.24  christos 	if (pid == sshpid) {
    265  1.24  christos 		(void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
    266  1.24  christos 		sshpid = -1;
    267  1.24  christos 	}
    268  1.24  christos 
    269  1.24  christos 	errno = save_errno;
    270  1.24  christos }
    271  1.24  christos 
    272   1.1  christos static void
    273   1.1  christos help(void)
    274   1.1  christos {
    275   1.1  christos 	printf("Available commands:\n"
    276   1.1  christos 	    "bye                                Quit sftp\n"
    277   1.1  christos 	    "cd path                            Change remote directory to 'path'\n"
    278  1.26  christos 	    "chgrp [-h] grp path                Change group of file 'path' to 'grp'\n"
    279  1.26  christos 	    "chmod [-h] mode path               Change permissions of file 'path' to 'mode'\n"
    280  1.26  christos 	    "chown [-h] own path                Change owner of file 'path' to 'own'\n"
    281  1.35  christos 	    "copy oldpath newpath               Copy remote file\n"
    282  1.35  christos 	    "cp oldpath newpath                 Copy remote file\n"
    283   1.1  christos 	    "df [-hi] [path]                    Display statistics for current directory or\n"
    284   1.1  christos 	    "                                   filesystem containing 'path'\n"
    285   1.1  christos 	    "exit                               Quit sftp\n"
    286  1.27  christos 	    "get [-afpR] remote [local]         Download file\n"
    287   1.1  christos 	    "help                               Display this help text\n"
    288   1.1  christos 	    "lcd path                           Change local directory to 'path'\n"
    289   1.1  christos 	    "lls [ls-options [path]]            Display local directory listing\n"
    290   1.1  christos 	    "lmkdir path                        Create local directory\n"
    291   1.7  christos 	    "ln [-s] oldpath newpath            Link remote file (-s for symlink)\n"
    292   1.1  christos 	    "lpwd                               Print local working directory\n"
    293   1.4      adam 	    "ls [-1afhlnrSt] [path]             Display remote directory listing\n"
    294   1.1  christos 	    "lumask umask                       Set local umask to 'umask'\n"
    295   1.1  christos 	    "mkdir path                         Create remote directory\n"
    296   1.1  christos 	    "progress                           Toggle display of progress meter\n"
    297  1.27  christos 	    "put [-afpR] local [remote]         Upload file\n"
    298   1.1  christos 	    "pwd                                Display remote working directory\n"
    299   1.1  christos 	    "quit                               Quit sftp\n"
    300  1.27  christos 	    "reget [-fpR] remote [local]        Resume download file\n"
    301   1.1  christos 	    "rename oldpath newpath             Rename remote file\n"
    302  1.27  christos 	    "reput [-fpR] local [remote]        Resume upload file\n"
    303   1.1  christos 	    "rm path                            Delete remote file\n"
    304   1.1  christos 	    "rmdir path                         Remove remote directory\n"
    305   1.1  christos 	    "symlink oldpath newpath            Symlink remote file\n"
    306   1.1  christos 	    "version                            Show SFTP version\n"
    307   1.1  christos 	    "!command                           Execute 'command' in local shell\n"
    308   1.1  christos 	    "!                                  Escape to local shell\n"
    309   1.1  christos 	    "?                                  Synonym for help\n");
    310   1.1  christos }
    311   1.1  christos 
    312   1.1  christos static void
    313   1.1  christos local_do_shell(const char *args)
    314   1.1  christos {
    315   1.1  christos 	int status;
    316   1.7  christos 	const char *shell;
    317   1.1  christos 	pid_t pid;
    318   1.1  christos 
    319   1.1  christos 	if (!*args)
    320   1.1  christos 		args = NULL;
    321   1.1  christos 
    322   1.7  christos 	if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
    323   1.1  christos 		shell = _PATH_BSHELL;
    324   1.1  christos 
    325   1.1  christos 	if ((pid = fork()) == -1)
    326   1.1  christos 		fatal("Couldn't fork: %s", strerror(errno));
    327   1.1  christos 
    328   1.1  christos 	if (pid == 0) {
    329   1.1  christos 		/* XXX: child has pipe fds to ssh subproc open - issue? */
    330   1.1  christos 		if (args) {
    331   1.1  christos 			debug3("Executing %s -c \"%s\"", shell, args);
    332   1.1  christos 			execl(shell, shell, "-c", args, (char *)NULL);
    333   1.1  christos 		} else {
    334   1.1  christos 			debug3("Executing %s", shell);
    335   1.1  christos 			execl(shell, shell, (char *)NULL);
    336   1.1  christos 		}
    337   1.1  christos 		fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
    338   1.1  christos 		    strerror(errno));
    339   1.1  christos 		_exit(1);
    340   1.1  christos 	}
    341   1.1  christos 	while (waitpid(pid, &status, 0) == -1)
    342   1.1  christos 		if (errno != EINTR)
    343   1.1  christos 			fatal("Couldn't wait for child: %s", strerror(errno));
    344   1.1  christos 	if (!WIFEXITED(status))
    345   1.1  christos 		error("Shell exited abnormally");
    346   1.1  christos 	else if (WEXITSTATUS(status))
    347   1.1  christos 		error("Shell exited with status %d", WEXITSTATUS(status));
    348   1.1  christos }
    349   1.1  christos 
    350   1.1  christos static void
    351   1.1  christos local_do_ls(const char *args)
    352   1.1  christos {
    353   1.1  christos 	if (!args || !*args)
    354   1.1  christos 		local_do_shell(_PATH_LS);
    355   1.1  christos 	else {
    356   1.1  christos 		int len = strlen(_PATH_LS " ") + strlen(args) + 1;
    357   1.1  christos 		char *buf = xmalloc(len);
    358   1.1  christos 
    359   1.1  christos 		/* XXX: quoting - rip quoting code from ftp? */
    360   1.1  christos 		snprintf(buf, len, _PATH_LS " %s", args);
    361   1.1  christos 		local_do_shell(buf);
    362  1.12  christos 		free(buf);
    363   1.1  christos 	}
    364   1.1  christos }
    365   1.1  christos 
    366   1.1  christos /* Strip one path (usually the pwd) from the start of another */
    367   1.1  christos static char *
    368  1.19  christos path_strip(const char *path, const char *strip)
    369   1.1  christos {
    370   1.1  christos 	size_t len;
    371   1.1  christos 
    372   1.1  christos 	if (strip == NULL)
    373   1.1  christos 		return (xstrdup(path));
    374   1.1  christos 
    375   1.1  christos 	len = strlen(strip);
    376   1.1  christos 	if (strncmp(path, strip, len) == 0) {
    377   1.1  christos 		if (strip[len - 1] != '/' && path[len] == '/')
    378   1.1  christos 			len++;
    379   1.1  christos 		return (xstrdup(path + len));
    380   1.1  christos 	}
    381   1.1  christos 
    382   1.1  christos 	return (xstrdup(path));
    383   1.1  christos }
    384   1.1  christos 
    385   1.1  christos static int
    386  1.12  christos parse_getput_flags(const char *cmd, char **argv, int argc,
    387  1.13  christos     int *aflag, int *fflag, int *pflag, int *rflag)
    388   1.1  christos {
    389   1.1  christos 	extern int opterr, optind, optopt, optreset;
    390   1.1  christos 	int ch;
    391   1.1  christos 
    392   1.1  christos 	optind = optreset = 1;
    393   1.1  christos 	opterr = 0;
    394   1.1  christos 
    395  1.13  christos 	*aflag = *fflag = *rflag = *pflag = 0;
    396  1.13  christos 	while ((ch = getopt(argc, argv, "afPpRr")) != -1) {
    397   1.1  christos 		switch (ch) {
    398  1.12  christos 		case 'a':
    399  1.12  christos 			*aflag = 1;
    400  1.12  christos 			break;
    401  1.13  christos 		case 'f':
    402  1.13  christos 			*fflag = 1;
    403  1.13  christos 			break;
    404   1.1  christos 		case 'p':
    405   1.1  christos 		case 'P':
    406   1.1  christos 			*pflag = 1;
    407   1.1  christos 			break;
    408   1.4      adam 		case 'r':
    409   1.4      adam 		case 'R':
    410   1.4      adam 			*rflag = 1;
    411   1.4      adam 			break;
    412   1.1  christos 		default:
    413   1.1  christos 			error("%s: Invalid flag -%c", cmd, optopt);
    414   1.1  christos 			return -1;
    415   1.1  christos 		}
    416   1.1  christos 	}
    417   1.1  christos 
    418   1.1  christos 	return optind;
    419   1.1  christos }
    420   1.1  christos 
    421   1.1  christos static int
    422   1.7  christos parse_link_flags(const char *cmd, char **argv, int argc, int *sflag)
    423   1.7  christos {
    424   1.7  christos 	extern int opterr, optind, optopt, optreset;
    425   1.7  christos 	int ch;
    426   1.7  christos 
    427   1.7  christos 	optind = optreset = 1;
    428   1.7  christos 	opterr = 0;
    429   1.7  christos 
    430   1.7  christos 	*sflag = 0;
    431   1.7  christos 	while ((ch = getopt(argc, argv, "s")) != -1) {
    432   1.7  christos 		switch (ch) {
    433   1.7  christos 		case 's':
    434   1.7  christos 			*sflag = 1;
    435   1.7  christos 			break;
    436   1.7  christos 		default:
    437   1.7  christos 			error("%s: Invalid flag -%c", cmd, optopt);
    438   1.7  christos 			return -1;
    439   1.7  christos 		}
    440   1.7  christos 	}
    441   1.7  christos 
    442   1.7  christos 	return optind;
    443   1.7  christos }
    444   1.7  christos 
    445   1.7  christos static int
    446  1.13  christos parse_rename_flags(const char *cmd, char **argv, int argc, int *lflag)
    447  1.13  christos {
    448  1.13  christos 	extern int opterr, optind, optopt, optreset;
    449  1.13  christos 	int ch;
    450  1.13  christos 
    451  1.13  christos 	optind = optreset = 1;
    452  1.13  christos 	opterr = 0;
    453  1.13  christos 
    454  1.13  christos 	*lflag = 0;
    455  1.13  christos 	while ((ch = getopt(argc, argv, "l")) != -1) {
    456  1.13  christos 		switch (ch) {
    457  1.13  christos 		case 'l':
    458  1.13  christos 			*lflag = 1;
    459  1.13  christos 			break;
    460  1.13  christos 		default:
    461  1.13  christos 			error("%s: Invalid flag -%c", cmd, optopt);
    462  1.13  christos 			return -1;
    463  1.13  christos 		}
    464  1.13  christos 	}
    465  1.13  christos 
    466  1.13  christos 	return optind;
    467  1.13  christos }
    468  1.13  christos 
    469  1.13  christos static int
    470   1.1  christos parse_ls_flags(char **argv, int argc, int *lflag)
    471   1.1  christos {
    472   1.1  christos 	extern int opterr, optind, optopt, optreset;
    473   1.1  christos 	int ch;
    474   1.1  christos 
    475   1.1  christos 	optind = optreset = 1;
    476   1.1  christos 	opterr = 0;
    477   1.1  christos 
    478   1.1  christos 	*lflag = LS_NAME_SORT;
    479   1.4      adam 	while ((ch = getopt(argc, argv, "1Safhlnrt")) != -1) {
    480   1.1  christos 		switch (ch) {
    481   1.1  christos 		case '1':
    482   1.1  christos 			*lflag &= ~VIEW_FLAGS;
    483   1.1  christos 			*lflag |= LS_SHORT_VIEW;
    484   1.1  christos 			break;
    485   1.1  christos 		case 'S':
    486   1.1  christos 			*lflag &= ~SORT_FLAGS;
    487   1.1  christos 			*lflag |= LS_SIZE_SORT;
    488   1.1  christos 			break;
    489   1.1  christos 		case 'a':
    490   1.1  christos 			*lflag |= LS_SHOW_ALL;
    491   1.1  christos 			break;
    492   1.1  christos 		case 'f':
    493   1.1  christos 			*lflag &= ~SORT_FLAGS;
    494   1.1  christos 			break;
    495   1.4      adam 		case 'h':
    496   1.4      adam 			*lflag |= LS_SI_UNITS;
    497   1.4      adam 			break;
    498   1.1  christos 		case 'l':
    499   1.4      adam 			*lflag &= ~LS_SHORT_VIEW;
    500   1.1  christos 			*lflag |= LS_LONG_VIEW;
    501   1.1  christos 			break;
    502   1.1  christos 		case 'n':
    503   1.4      adam 			*lflag &= ~LS_SHORT_VIEW;
    504   1.1  christos 			*lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
    505   1.1  christos 			break;
    506   1.1  christos 		case 'r':
    507   1.1  christos 			*lflag |= LS_REVERSE_SORT;
    508   1.1  christos 			break;
    509   1.1  christos 		case 't':
    510   1.1  christos 			*lflag &= ~SORT_FLAGS;
    511   1.1  christos 			*lflag |= LS_TIME_SORT;
    512   1.1  christos 			break;
    513   1.1  christos 		default:
    514   1.1  christos 			error("ls: Invalid flag -%c", optopt);
    515   1.1  christos 			return -1;
    516   1.1  christos 		}
    517   1.1  christos 	}
    518   1.1  christos 
    519   1.1  christos 	return optind;
    520   1.1  christos }
    521   1.1  christos 
    522   1.1  christos static int
    523   1.1  christos parse_df_flags(const char *cmd, char **argv, int argc, int *hflag, int *iflag)
    524   1.1  christos {
    525   1.1  christos 	extern int opterr, optind, optopt, optreset;
    526   1.1  christos 	int ch;
    527   1.1  christos 
    528   1.1  christos 	optind = optreset = 1;
    529   1.1  christos 	opterr = 0;
    530   1.1  christos 
    531   1.1  christos 	*hflag = *iflag = 0;
    532   1.1  christos 	while ((ch = getopt(argc, argv, "hi")) != -1) {
    533   1.1  christos 		switch (ch) {
    534   1.1  christos 		case 'h':
    535   1.1  christos 			*hflag = 1;
    536   1.1  christos 			break;
    537   1.1  christos 		case 'i':
    538   1.1  christos 			*iflag = 1;
    539   1.1  christos 			break;
    540   1.1  christos 		default:
    541   1.1  christos 			error("%s: Invalid flag -%c", cmd, optopt);
    542   1.1  christos 			return -1;
    543   1.1  christos 		}
    544   1.1  christos 	}
    545   1.1  christos 
    546   1.1  christos 	return optind;
    547   1.1  christos }
    548   1.1  christos 
    549   1.1  christos static int
    550  1.26  christos parse_ch_flags(const char *cmd, char **argv, int argc, int *hflag)
    551  1.26  christos {
    552  1.26  christos 	extern int opterr, optind, optopt, optreset;
    553  1.26  christos 	int ch;
    554  1.26  christos 
    555  1.26  christos 	optind = optreset = 1;
    556  1.26  christos 	opterr = 0;
    557  1.26  christos 
    558  1.26  christos 	*hflag = 0;
    559  1.26  christos 	while ((ch = getopt(argc, argv, "h")) != -1) {
    560  1.26  christos 		switch (ch) {
    561  1.26  christos 		case 'h':
    562  1.26  christos 			*hflag = 1;
    563  1.26  christos 			break;
    564  1.26  christos 		default:
    565  1.26  christos 			error("%s: Invalid flag -%c", cmd, optopt);
    566  1.26  christos 			return -1;
    567  1.26  christos 		}
    568  1.26  christos 	}
    569  1.26  christos 
    570  1.26  christos 	return optind;
    571  1.26  christos }
    572  1.26  christos 
    573  1.26  christos static int
    574  1.13  christos parse_no_flags(const char *cmd, char **argv, int argc)
    575  1.13  christos {
    576  1.13  christos 	extern int opterr, optind, optopt, optreset;
    577  1.13  christos 	int ch;
    578  1.13  christos 
    579  1.13  christos 	optind = optreset = 1;
    580  1.13  christos 	opterr = 0;
    581  1.13  christos 
    582  1.13  christos 	while ((ch = getopt(argc, argv, "")) != -1) {
    583  1.13  christos 		switch (ch) {
    584  1.13  christos 		default:
    585  1.13  christos 			error("%s: Invalid flag -%c", cmd, optopt);
    586  1.13  christos 			return -1;
    587  1.13  christos 		}
    588  1.13  christos 	}
    589  1.13  christos 
    590  1.13  christos 	return optind;
    591  1.13  christos }
    592  1.13  christos 
    593  1.36  christos static char *
    594  1.36  christos escape_glob(const char *s)
    595  1.36  christos {
    596  1.36  christos 	size_t i, o, len;
    597  1.36  christos 	char *ret;
    598  1.36  christos 
    599  1.36  christos 	len = strlen(s);
    600  1.36  christos 	ret = xcalloc(2, len + 1);
    601  1.36  christos 	for (i = o = 0; i < len; i++) {
    602  1.36  christos 		if (strchr("[]?*\\", s[i]) != NULL)
    603  1.36  christos 			ret[o++] = '\\';
    604  1.36  christos 		ret[o++] = s[i];
    605  1.36  christos 	}
    606  1.36  christos 	ret[o++] = '\0';
    607  1.36  christos 	return ret;
    608  1.36  christos }
    609  1.36  christos 
    610  1.36  christos static char *
    611  1.36  christos make_absolute_pwd_glob(const char *p, const char *pwd)
    612  1.36  christos {
    613  1.36  christos 	char *ret, *escpwd;
    614  1.36  christos 
    615  1.36  christos 	escpwd = escape_glob(pwd);
    616  1.36  christos 	if (p == NULL)
    617  1.36  christos 		return escpwd;
    618  1.36  christos 	ret = make_absolute(xstrdup(p), escpwd);
    619  1.36  christos 	free(escpwd);
    620  1.36  christos 	return ret;
    621  1.36  christos }
    622  1.36  christos 
    623  1.13  christos static int
    624  1.19  christos process_get(struct sftp_conn *conn, const char *src, const char *dst,
    625  1.19  christos     const char *pwd, int pflag, int rflag, int resume, int fflag)
    626   1.1  christos {
    627  1.36  christos 	char *filename, *abs_src = NULL, *abs_dst = NULL, *tmp = NULL;
    628   1.1  christos 	glob_t g;
    629  1.13  christos 	int i, r, err = 0;
    630   1.1  christos 
    631  1.36  christos 	abs_src = make_absolute_pwd_glob(src, pwd);
    632   1.4      adam 	memset(&g, 0, sizeof(g));
    633   1.1  christos 
    634   1.1  christos 	debug3("Looking up %s", abs_src);
    635  1.13  christos 	if ((r = remote_glob(conn, abs_src, GLOB_MARK, NULL, &g)) != 0) {
    636  1.13  christos 		if (r == GLOB_NOSPACE) {
    637  1.13  christos 			error("Too many matches for \"%s\".", abs_src);
    638  1.13  christos 		} else {
    639  1.13  christos 			error("File \"%s\" not found.", abs_src);
    640  1.13  christos 		}
    641   1.1  christos 		err = -1;
    642   1.1  christos 		goto out;
    643   1.1  christos 	}
    644   1.1  christos 
    645   1.4      adam 	/*
    646   1.4      adam 	 * If multiple matches then dst must be a directory or
    647   1.4      adam 	 * unspecified.
    648   1.4      adam 	 */
    649  1.31  christos 	if (g.gl_matchc > 1 && dst != NULL && !local_is_dir(dst)) {
    650   1.4      adam 		error("Multiple source paths, but destination "
    651   1.4      adam 		    "\"%s\" is not a directory", dst);
    652   1.1  christos 		err = -1;
    653   1.1  christos 		goto out;
    654   1.1  christos 	}
    655   1.1  christos 
    656   1.1  christos 	for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
    657   1.4      adam 		tmp = xstrdup(g.gl_pathv[i]);
    658   1.4      adam 		if ((filename = basename(tmp)) == NULL) {
    659   1.4      adam 			error("basename %s: %s", tmp, strerror(errno));
    660  1.12  christos 			free(tmp);
    661   1.1  christos 			err = -1;
    662   1.1  christos 			goto out;
    663   1.1  christos 		}
    664   1.1  christos 
    665   1.1  christos 		if (g.gl_matchc == 1 && dst) {
    666  1.31  christos 			if (local_is_dir(dst)) {
    667   1.4      adam 				abs_dst = path_append(dst, filename);
    668   1.4      adam 			} else {
    669   1.1  christos 				abs_dst = xstrdup(dst);
    670   1.4      adam 			}
    671   1.1  christos 		} else if (dst) {
    672   1.4      adam 			abs_dst = path_append(dst, filename);
    673   1.4      adam 		} else {
    674   1.4      adam 			abs_dst = xstrdup(filename);
    675   1.4      adam 		}
    676  1.12  christos 		free(tmp);
    677   1.1  christos 
    678  1.12  christos 		resume |= global_aflag;
    679  1.12  christos 		if (!quiet && resume)
    680  1.19  christos 			mprintf("Resuming %s to %s\n",
    681  1.19  christos 			    g.gl_pathv[i], abs_dst);
    682  1.12  christos 		else if (!quiet && !resume)
    683  1.19  christos 			mprintf("Fetching %s to %s\n",
    684  1.19  christos 			    g.gl_pathv[i], abs_dst);
    685  1.33  christos 		/* XXX follow link flag */
    686  1.31  christos 		if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
    687  1.12  christos 			if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
    688  1.13  christos 			    pflag || global_pflag, 1, resume,
    689  1.36  christos 			    fflag || global_fflag, 0, 0) == -1)
    690   1.4      adam 				err = -1;
    691   1.4      adam 		} else {
    692   1.4      adam 			if (do_download(conn, g.gl_pathv[i], abs_dst, NULL,
    693  1.13  christos 			    pflag || global_pflag, resume,
    694  1.36  christos 			    fflag || global_fflag, 0) == -1)
    695   1.4      adam 				err = -1;
    696   1.4      adam 		}
    697  1.12  christos 		free(abs_dst);
    698   1.1  christos 		abs_dst = NULL;
    699   1.1  christos 	}
    700   1.1  christos 
    701   1.1  christos out:
    702  1.12  christos 	free(abs_src);
    703   1.1  christos 	globfree(&g);
    704   1.1  christos 	return(err);
    705   1.1  christos }
    706   1.1  christos 
    707   1.1  christos static int
    708  1.19  christos process_put(struct sftp_conn *conn, const char *src, const char *dst,
    709  1.19  christos     const char *pwd, int pflag, int rflag, int resume, int fflag)
    710   1.1  christos {
    711   1.1  christos 	char *tmp_dst = NULL;
    712   1.1  christos 	char *abs_dst = NULL;
    713   1.4      adam 	char *tmp = NULL, *filename = NULL;
    714   1.1  christos 	glob_t g;
    715   1.1  christos 	int err = 0;
    716   1.4      adam 	int i, dst_is_dir = 1;
    717   1.1  christos 	struct stat sb;
    718   1.1  christos 
    719   1.1  christos 	if (dst) {
    720   1.1  christos 		tmp_dst = xstrdup(dst);
    721   1.1  christos 		tmp_dst = make_absolute(tmp_dst, pwd);
    722   1.1  christos 	}
    723   1.1  christos 
    724   1.1  christos 	memset(&g, 0, sizeof(g));
    725   1.1  christos 	debug3("Looking up %s", src);
    726   1.4      adam 	if (glob(src, GLOB_NOCHECK | GLOB_LIMIT | GLOB_MARK, NULL, &g)) {
    727   1.1  christos 		error("File \"%s\" not found.", src);
    728   1.1  christos 		err = -1;
    729   1.1  christos 		goto out;
    730   1.1  christos 	}
    731   1.1  christos 
    732   1.4      adam 	/* If we aren't fetching to pwd then stash this status for later */
    733   1.4      adam 	if (tmp_dst != NULL)
    734   1.4      adam 		dst_is_dir = remote_is_dir(conn, tmp_dst);
    735   1.4      adam 
    736   1.1  christos 	/* If multiple matches, dst may be directory or unspecified */
    737   1.4      adam 	if (g.gl_matchc > 1 && tmp_dst && !dst_is_dir) {
    738   1.4      adam 		error("Multiple paths match, but destination "
    739   1.4      adam 		    "\"%s\" is not a directory", tmp_dst);
    740   1.1  christos 		err = -1;
    741   1.1  christos 		goto out;
    742   1.1  christos 	}
    743   1.1  christos 
    744   1.1  christos 	for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
    745   1.1  christos 		if (stat(g.gl_pathv[i], &sb) == -1) {
    746   1.1  christos 			err = -1;
    747   1.1  christos 			error("stat %s: %s", g.gl_pathv[i], strerror(errno));
    748   1.1  christos 			continue;
    749   1.1  christos 		}
    750  1.13  christos 
    751   1.4      adam 		tmp = xstrdup(g.gl_pathv[i]);
    752   1.4      adam 		if ((filename = basename(tmp)) == NULL) {
    753   1.4      adam 			error("basename %s: %s", tmp, strerror(errno));
    754  1.12  christos 			free(tmp);
    755   1.1  christos 			err = -1;
    756   1.1  christos 			goto out;
    757   1.1  christos 		}
    758   1.1  christos 
    759   1.1  christos 		if (g.gl_matchc == 1 && tmp_dst) {
    760   1.1  christos 			/* If directory specified, append filename */
    761   1.4      adam 			if (dst_is_dir)
    762   1.4      adam 				abs_dst = path_append(tmp_dst, filename);
    763   1.4      adam 			else
    764   1.1  christos 				abs_dst = xstrdup(tmp_dst);
    765   1.1  christos 		} else if (tmp_dst) {
    766   1.4      adam 			abs_dst = path_append(tmp_dst, filename);
    767   1.4      adam 		} else {
    768   1.4      adam 			abs_dst = make_absolute(xstrdup(filename), pwd);
    769   1.4      adam 		}
    770  1.12  christos 		free(tmp);
    771   1.1  christos 
    772  1.32  christos 		resume |= global_aflag;
    773  1.13  christos 		if (!quiet && resume)
    774  1.19  christos 			mprintf("Resuming upload of %s to %s\n",
    775  1.19  christos 			    g.gl_pathv[i], abs_dst);
    776  1.13  christos 		else if (!quiet && !resume)
    777  1.19  christos 			mprintf("Uploading %s to %s\n",
    778  1.19  christos 			    g.gl_pathv[i], abs_dst);
    779  1.33  christos 		/* XXX follow_link_flag */
    780  1.31  christos 		if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
    781   1.4      adam 			if (upload_dir(conn, g.gl_pathv[i], abs_dst,
    782  1.13  christos 			    pflag || global_pflag, 1, resume,
    783  1.36  christos 			    fflag || global_fflag, 0, 0) == -1)
    784   1.4      adam 				err = -1;
    785   1.4      adam 		} else {
    786   1.4      adam 			if (do_upload(conn, g.gl_pathv[i], abs_dst,
    787  1.13  christos 			    pflag || global_pflag, resume,
    788  1.36  christos 			    fflag || global_fflag, 0) == -1)
    789   1.4      adam 				err = -1;
    790   1.4      adam 		}
    791  1.15  christos 		free(abs_dst);
    792  1.15  christos 		abs_dst = NULL;
    793   1.1  christos 	}
    794   1.1  christos 
    795   1.1  christos out:
    796  1.12  christos 	free(abs_dst);
    797  1.12  christos 	free(tmp_dst);
    798   1.1  christos 	globfree(&g);
    799   1.1  christos 	return(err);
    800   1.1  christos }
    801   1.1  christos 
    802   1.1  christos static int
    803   1.1  christos sdirent_comp(const void *aa, const void *bb)
    804   1.1  christos {
    805   1.7  christos 	const SFTP_DIRENT *a = *(const SFTP_DIRENT * const *)aa;
    806   1.7  christos 	const SFTP_DIRENT *b = *(const SFTP_DIRENT * const *)bb;
    807   1.1  christos 	int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
    808   1.1  christos 
    809   1.1  christos #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
    810   1.1  christos 	if (sort_flag & LS_NAME_SORT)
    811   1.1  christos 		return (rmul * strcmp(a->filename, b->filename));
    812   1.1  christos 	else if (sort_flag & LS_TIME_SORT)
    813   1.1  christos 		return (rmul * NCMP(a->a.mtime, b->a.mtime));
    814   1.1  christos 	else if (sort_flag & LS_SIZE_SORT)
    815   1.1  christos 		return (rmul * NCMP(a->a.size, b->a.size));
    816   1.1  christos 
    817   1.1  christos 	fatal("Unknown ls sort type");
    818   1.2  christos 	/*NOTREACHED*/
    819   1.2  christos 	return 0;
    820   1.1  christos }
    821   1.1  christos 
    822   1.1  christos /* sftp ls.1 replacement for directories */
    823   1.1  christos static int
    824  1.19  christos do_ls_dir(struct sftp_conn *conn, const char *path,
    825  1.19  christos     const char *strip_path, int lflag)
    826   1.1  christos {
    827   1.1  christos 	int n;
    828   1.1  christos 	u_int c = 1, colspace = 0, columns = 1;
    829   1.1  christos 	SFTP_DIRENT **d;
    830   1.1  christos 
    831   1.1  christos 	if ((n = do_readdir(conn, path, &d)) != 0)
    832   1.1  christos 		return (n);
    833   1.1  christos 
    834   1.1  christos 	if (!(lflag & LS_SHORT_VIEW)) {
    835   1.1  christos 		u_int m = 0, width = 80;
    836   1.1  christos 		struct winsize ws;
    837   1.1  christos 		char *tmp;
    838   1.1  christos 
    839   1.1  christos 		/* Count entries for sort and find longest filename */
    840   1.1  christos 		for (n = 0; d[n] != NULL; n++) {
    841   1.1  christos 			if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
    842  1.20  christos 				m = MAXIMUM(m, strlen(d[n]->filename));
    843   1.1  christos 		}
    844   1.1  christos 
    845   1.1  christos 		/* Add any subpath that also needs to be counted */
    846   1.1  christos 		tmp = path_strip(path, strip_path);
    847   1.1  christos 		m += strlen(tmp);
    848  1.12  christos 		free(tmp);
    849   1.1  christos 
    850   1.1  christos 		if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
    851   1.1  christos 			width = ws.ws_col;
    852   1.1  christos 
    853   1.1  christos 		columns = width / (m + 2);
    854  1.20  christos 		columns = MAXIMUM(columns, 1);
    855   1.1  christos 		colspace = width / columns;
    856  1.20  christos 		colspace = MINIMUM(colspace, width);
    857   1.1  christos 	}
    858   1.1  christos 
    859   1.1  christos 	if (lflag & SORT_FLAGS) {
    860   1.1  christos 		for (n = 0; d[n] != NULL; n++)
    861   1.1  christos 			;	/* count entries */
    862   1.1  christos 		sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
    863   1.1  christos 		qsort(d, n, sizeof(*d), sdirent_comp);
    864   1.1  christos 	}
    865   1.1  christos 
    866  1.36  christos 	get_remote_user_groups_from_dirents(conn, d);
    867   1.1  christos 	for (n = 0; d[n] != NULL && !interrupted; n++) {
    868   1.1  christos 		char *tmp, *fname;
    869   1.1  christos 
    870   1.1  christos 		if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
    871   1.1  christos 			continue;
    872   1.1  christos 
    873   1.1  christos 		tmp = path_append(path, d[n]->filename);
    874   1.1  christos 		fname = path_strip(tmp, strip_path);
    875  1.12  christos 		free(tmp);
    876   1.1  christos 
    877   1.1  christos 		if (lflag & LS_LONG_VIEW) {
    878  1.36  christos 			if ((lflag & (LS_NUMERIC_VIEW|LS_SI_UNITS)) != 0 ||
    879  1.36  christos 			    can_get_users_groups_by_id(conn)) {
    880   1.1  christos 				char *lname;
    881   1.1  christos 				struct stat sb;
    882   1.1  christos 
    883   1.1  christos 				memset(&sb, 0, sizeof(sb));
    884   1.1  christos 				attrib_to_stat(&d[n]->a, &sb);
    885   1.4      adam 				lname = ls_file(fname, &sb, 1,
    886  1.36  christos 				    (lflag & LS_SI_UNITS),
    887  1.36  christos 				    ruser_name(sb.st_uid),
    888  1.36  christos 				    rgroup_name(sb.st_gid));
    889  1.19  christos 				mprintf("%s\n", lname);
    890  1.12  christos 				free(lname);
    891   1.1  christos 			} else
    892  1.19  christos 				mprintf("%s\n", d[n]->longname);
    893   1.1  christos 		} else {
    894  1.19  christos 			mprintf("%-*s", colspace, fname);
    895   1.1  christos 			if (c >= columns) {
    896   1.1  christos 				printf("\n");
    897   1.1  christos 				c = 1;
    898   1.1  christos 			} else
    899   1.1  christos 				c++;
    900   1.1  christos 		}
    901   1.1  christos 
    902  1.12  christos 		free(fname);
    903   1.1  christos 	}
    904   1.1  christos 
    905   1.1  christos 	if (!(lflag & LS_LONG_VIEW) && (c != 1))
    906   1.1  christos 		printf("\n");
    907   1.1  christos 
    908   1.1  christos 	free_sftp_dirents(d);
    909   1.1  christos 	return (0);
    910   1.1  christos }
    911   1.1  christos 
    912  1.22  christos static int
    913  1.22  christos sglob_comp(const void *aa, const void *bb)
    914  1.22  christos {
    915  1.22  christos 	u_int a = *(const u_int *)aa;
    916  1.22  christos 	u_int b = *(const u_int *)bb;
    917  1.22  christos 	const char *ap = sort_glob->gl_pathv[a];
    918  1.22  christos 	const char *bp = sort_glob->gl_pathv[b];
    919  1.22  christos #if 0
    920  1.22  christos 	const struct stat *as = sort_glob->gl_statv[a];
    921  1.22  christos 	const struct stat *bs = sort_glob->gl_statv[b];
    922  1.22  christos #else
    923  1.22  christos 	struct stat ass, bss;
    924  1.22  christos 	const struct stat *as = &ass;
    925  1.22  christos 	const struct stat *bs = &bss;
    926  1.22  christos #endif
    927  1.22  christos 	int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
    928  1.22  christos 
    929  1.22  christos #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
    930  1.22  christos #if 1
    931  1.22  christos 	if (stat(ap, &ass) == -1 || stat(bp, &bss) == -1)
    932  1.22  christos 		return 0;
    933  1.22  christos #endif
    934  1.31  christos 	if (sort_flag & LS_NAME_SORT)
    935  1.31  christos 		return (rmul * strcmp(ap, bp));
    936  1.31  christos 	else if (sort_flag & LS_TIME_SORT) {
    937  1.31  christos 		if (timespeccmp(&as->st_mtim, &bs->st_mtim, ==))
    938  1.31  christos 			return 0;
    939  1.31  christos 		return timespeccmp(&as->st_mtim, &bs->st_mtim, <) ?
    940  1.31  christos 		    rmul : -rmul;
    941  1.31  christos 	} else if (sort_flag & LS_SIZE_SORT)
    942  1.22  christos 		return (rmul * NCMP(as->st_size, bs->st_size));
    943  1.22  christos 
    944  1.22  christos 	fatal("Unknown ls sort type");
    945  1.22  christos }
    946  1.22  christos 
    947   1.1  christos /* sftp ls.1 replacement which handles path globs */
    948   1.1  christos static int
    949  1.19  christos do_globbed_ls(struct sftp_conn *conn, const char *path,
    950  1.19  christos     const char *strip_path, int lflag)
    951   1.1  christos {
    952   1.7  christos 	char *fname, *lname;
    953   1.1  christos 	glob_t g;
    954  1.13  christos 	int err, r;
    955   1.7  christos 	struct winsize ws;
    956  1.22  christos 	u_int i, j, nentries, *indices = NULL, c = 1;
    957  1.22  christos 	u_int colspace = 0, columns = 1, m = 0, width = 80;
    958   1.7  christos 	struct stat *stp;
    959   1.7  christos #ifndef GLOB_KEEPSTAT
    960   1.7  christos 	struct stat st;
    961   1.7  christos #define GLOB_KEEPSTAT	0
    962   1.7  christos #endif
    963   1.1  christos 
    964   1.1  christos 	memset(&g, 0, sizeof(g));
    965   1.1  christos 
    966  1.13  christos 	if ((r = remote_glob(conn, path,
    967   1.9  christos 	    GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE|GLOB_KEEPSTAT|GLOB_NOSORT,
    968  1.13  christos 	    NULL, &g)) != 0 ||
    969   1.7  christos 	    (g.gl_pathc && !g.gl_matchc)) {
    970   1.1  christos 		if (g.gl_pathc)
    971   1.1  christos 			globfree(&g);
    972  1.13  christos 		if (r == GLOB_NOSPACE) {
    973  1.13  christos 			error("Can't ls: Too many matches for \"%s\"", path);
    974  1.13  christos 		} else {
    975  1.13  christos 			error("Can't ls: \"%s\" not found", path);
    976  1.13  christos 		}
    977   1.7  christos 		return -1;
    978   1.1  christos 	}
    979   1.1  christos 
    980   1.1  christos 	if (interrupted)
    981   1.1  christos 		goto out;
    982   1.1  christos 
    983   1.1  christos 	/*
    984   1.1  christos 	 * If the glob returns a single match and it is a directory,
    985   1.1  christos 	 * then just list its contents.
    986   1.1  christos 	 */
    987   1.7  christos 	if (g.gl_matchc == 1 &&
    988   1.7  christos #if GLOB_KEEPSTAT != 0
    989   1.7  christos 	    (stp = g.gl_statv[0]) != NULL &&
    990   1.7  christos #else
    991   1.7  christos 	    lstat(g.gl_pathv[0], stp = &st) != -1 &&
    992   1.7  christos #endif
    993   1.7  christos 	    S_ISDIR(stp->st_mode)) {
    994   1.7  christos 		err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
    995   1.7  christos 		globfree(&g);
    996   1.7  christos 		return err;
    997   1.7  christos 	}
    998   1.1  christos 
    999   1.7  christos 	if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
   1000   1.7  christos 		width = ws.ws_col;
   1001   1.1  christos 
   1002   1.1  christos 	if (!(lflag & LS_SHORT_VIEW)) {
   1003   1.1  christos 		/* Count entries for sort and find longest filename */
   1004   1.1  christos 		for (i = 0; g.gl_pathv[i]; i++)
   1005  1.20  christos 			m = MAXIMUM(m, strlen(g.gl_pathv[i]));
   1006   1.1  christos 
   1007   1.1  christos 		columns = width / (m + 2);
   1008  1.20  christos 		columns = MAXIMUM(columns, 1);
   1009   1.1  christos 		colspace = width / columns;
   1010   1.1  christos 	}
   1011   1.1  christos 
   1012  1.22  christos 	/*
   1013  1.22  christos 	 * Sorting: rather than mess with the contents of glob_t, prepare
   1014  1.22  christos 	 * an array of indices into it and sort that. For the usual
   1015  1.22  christos 	 * unsorted case, the indices are just the identity 1=1, 2=2, etc.
   1016  1.22  christos 	 */
   1017  1.22  christos 	for (nentries = 0; g.gl_pathv[nentries] != NULL; nentries++)
   1018  1.22  christos 		;	/* count entries */
   1019  1.22  christos 	indices = calloc(nentries, sizeof(*indices));
   1020  1.22  christos 	for (i = 0; i < nentries; i++)
   1021  1.22  christos 		indices[i] = i;
   1022  1.22  christos 
   1023  1.22  christos 	if (lflag & SORT_FLAGS) {
   1024  1.22  christos 		sort_glob = &g;
   1025  1.22  christos 		sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
   1026  1.22  christos 		qsort(indices, nentries, sizeof(*indices), sglob_comp);
   1027  1.22  christos 		sort_glob = NULL;
   1028  1.22  christos 	}
   1029  1.22  christos 
   1030  1.36  christos 	get_remote_user_groups_from_glob(conn, &g);
   1031  1.22  christos 	for (j = 0; j < nentries && !interrupted; j++) {
   1032  1.22  christos 		i = indices[j];
   1033   1.1  christos 		fname = path_strip(g.gl_pathv[i], strip_path);
   1034   1.1  christos 		if (lflag & LS_LONG_VIEW) {
   1035   1.7  christos #if GLOB_KEEPSTAT != 0
   1036   1.7  christos 			stp = g.gl_statv[i];
   1037   1.7  christos #else
   1038   1.7  christos 			if (lstat(g.gl_pathv[i], stp = &st) == -1)
   1039   1.7  christos 				stp = NULL;
   1040   1.7  christos #endif
   1041   1.7  christos 			if (stp == NULL) {
   1042   1.7  christos 				error("no stat information for %s", fname);
   1043   1.7  christos 				continue;
   1044   1.7  christos 			}
   1045  1.36  christos 			lname = ls_file(fname, stp, 1,
   1046  1.36  christos 			    (lflag & LS_SI_UNITS),
   1047  1.36  christos 			    ruser_name(stp->st_uid),
   1048  1.36  christos 			    rgroup_name(stp->st_gid));
   1049  1.19  christos 			mprintf("%s\n", lname);
   1050  1.12  christos 			free(lname);
   1051   1.1  christos 		} else {
   1052  1.19  christos 			mprintf("%-*s", colspace, fname);
   1053   1.1  christos 			if (c >= columns) {
   1054   1.1  christos 				printf("\n");
   1055   1.1  christos 				c = 1;
   1056   1.1  christos 			} else
   1057   1.1  christos 				c++;
   1058   1.1  christos 		}
   1059  1.12  christos 		free(fname);
   1060   1.1  christos 	}
   1061   1.1  christos 
   1062   1.1  christos 	if (!(lflag & LS_LONG_VIEW) && (c != 1))
   1063   1.1  christos 		printf("\n");
   1064   1.1  christos 
   1065   1.1  christos  out:
   1066   1.1  christos 	if (g.gl_pathc)
   1067   1.1  christos 		globfree(&g);
   1068  1.22  christos 	free(indices);
   1069   1.1  christos 
   1070   1.7  christos 	return 0;
   1071   1.1  christos }
   1072   1.1  christos 
   1073   1.1  christos static int
   1074  1.19  christos do_df(struct sftp_conn *conn, const char *path, int hflag, int iflag)
   1075   1.1  christos {
   1076   1.1  christos 	struct sftp_statvfs st;
   1077  1.21  christos 	char s_used[FMT_SCALED_STRSIZE], s_avail[FMT_SCALED_STRSIZE];
   1078  1.21  christos 	char s_root[FMT_SCALED_STRSIZE], s_total[FMT_SCALED_STRSIZE];
   1079  1.21  christos 	char s_icapacity[16], s_dcapacity[16];
   1080   1.1  christos 
   1081   1.1  christos 	if (do_statvfs(conn, path, &st, 1) == -1)
   1082   1.1  christos 		return -1;
   1083  1.21  christos 	if (st.f_files == 0)
   1084  1.21  christos 		strlcpy(s_icapacity, "ERR", sizeof(s_icapacity));
   1085  1.21  christos 	else {
   1086  1.21  christos 		snprintf(s_icapacity, sizeof(s_icapacity), "%3llu%%",
   1087  1.21  christos 		    (unsigned long long)(100 * (st.f_files - st.f_ffree) /
   1088  1.21  christos 		    st.f_files));
   1089  1.21  christos 	}
   1090  1.21  christos 	if (st.f_blocks == 0)
   1091  1.21  christos 		strlcpy(s_dcapacity, "ERR", sizeof(s_dcapacity));
   1092  1.21  christos 	else {
   1093  1.21  christos 		snprintf(s_dcapacity, sizeof(s_dcapacity), "%3llu%%",
   1094  1.21  christos 		    (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
   1095  1.21  christos 		    st.f_blocks));
   1096  1.21  christos 	}
   1097   1.1  christos 	if (iflag) {
   1098   1.1  christos 		printf("     Inodes        Used       Avail      "
   1099   1.1  christos 		    "(root)    %%Capacity\n");
   1100  1.21  christos 		printf("%11llu %11llu %11llu %11llu         %s\n",
   1101   1.1  christos 		    (unsigned long long)st.f_files,
   1102   1.1  christos 		    (unsigned long long)(st.f_files - st.f_ffree),
   1103   1.1  christos 		    (unsigned long long)st.f_favail,
   1104  1.21  christos 		    (unsigned long long)st.f_ffree, s_icapacity);
   1105   1.1  christos 	} else if (hflag) {
   1106   1.1  christos 		strlcpy(s_used, "error", sizeof(s_used));
   1107   1.1  christos 		strlcpy(s_avail, "error", sizeof(s_avail));
   1108   1.1  christos 		strlcpy(s_root, "error", sizeof(s_root));
   1109   1.1  christos 		strlcpy(s_total, "error", sizeof(s_total));
   1110   1.1  christos 		fmt_scaled((st.f_blocks - st.f_bfree) * st.f_frsize, s_used);
   1111   1.1  christos 		fmt_scaled(st.f_bavail * st.f_frsize, s_avail);
   1112   1.1  christos 		fmt_scaled(st.f_bfree * st.f_frsize, s_root);
   1113   1.1  christos 		fmt_scaled(st.f_blocks * st.f_frsize, s_total);
   1114   1.1  christos 		printf("    Size     Used    Avail   (root)    %%Capacity\n");
   1115  1.21  christos 		printf("%7sB %7sB %7sB %7sB         %s\n",
   1116  1.21  christos 		    s_total, s_used, s_avail, s_root, s_dcapacity);
   1117   1.1  christos 	} else {
   1118   1.1  christos 		printf("        Size         Used        Avail       "
   1119   1.1  christos 		    "(root)    %%Capacity\n");
   1120  1.21  christos 		printf("%12llu %12llu %12llu %12llu         %s\n",
   1121   1.1  christos 		    (unsigned long long)(st.f_frsize * st.f_blocks / 1024),
   1122   1.1  christos 		    (unsigned long long)(st.f_frsize *
   1123   1.1  christos 		    (st.f_blocks - st.f_bfree) / 1024),
   1124   1.1  christos 		    (unsigned long long)(st.f_frsize * st.f_bavail / 1024),
   1125   1.1  christos 		    (unsigned long long)(st.f_frsize * st.f_bfree / 1024),
   1126  1.21  christos 		    s_dcapacity);
   1127   1.1  christos 	}
   1128   1.1  christos 	return 0;
   1129   1.1  christos }
   1130   1.1  christos 
   1131   1.1  christos /*
   1132   1.1  christos  * Undo escaping of glob sequences in place. Used to undo extra escaping
   1133   1.1  christos  * applied in makeargv() when the string is destined for a function that
   1134   1.1  christos  * does not glob it.
   1135   1.1  christos  */
   1136   1.1  christos static void
   1137   1.1  christos undo_glob_escape(char *s)
   1138   1.1  christos {
   1139   1.1  christos 	size_t i, j;
   1140   1.1  christos 
   1141   1.1  christos 	for (i = j = 0;;) {
   1142   1.1  christos 		if (s[i] == '\0') {
   1143   1.1  christos 			s[j] = '\0';
   1144   1.1  christos 			return;
   1145   1.1  christos 		}
   1146   1.1  christos 		if (s[i] != '\\') {
   1147   1.1  christos 			s[j++] = s[i++];
   1148   1.1  christos 			continue;
   1149   1.1  christos 		}
   1150   1.1  christos 		/* s[i] == '\\' */
   1151   1.1  christos 		++i;
   1152   1.1  christos 		switch (s[i]) {
   1153   1.1  christos 		case '?':
   1154   1.1  christos 		case '[':
   1155   1.1  christos 		case '*':
   1156   1.1  christos 		case '\\':
   1157   1.1  christos 			s[j++] = s[i++];
   1158   1.1  christos 			break;
   1159   1.1  christos 		case '\0':
   1160   1.1  christos 			s[j++] = '\\';
   1161   1.1  christos 			s[j] = '\0';
   1162   1.1  christos 			return;
   1163   1.1  christos 		default:
   1164   1.1  christos 			s[j++] = '\\';
   1165   1.1  christos 			s[j++] = s[i++];
   1166   1.1  christos 			break;
   1167   1.1  christos 		}
   1168   1.1  christos 	}
   1169   1.1  christos }
   1170   1.1  christos 
   1171   1.1  christos /*
   1172   1.1  christos  * Split a string into an argument vector using sh(1)-style quoting,
   1173   1.1  christos  * comment and escaping rules, but with some tweaks to handle glob(3)
   1174   1.1  christos  * wildcards.
   1175   1.4      adam  * The "sloppy" flag allows for recovery from missing terminating quote, for
   1176   1.4      adam  * use in parsing incomplete commandlines during tab autocompletion.
   1177   1.4      adam  *
   1178   1.1  christos  * Returns NULL on error or a NULL-terminated array of arguments.
   1179   1.4      adam  *
   1180   1.4      adam  * If "lastquote" is not NULL, the quoting character used for the last
   1181   1.4      adam  * argument is placed in *lastquote ("\0", "'" or "\"").
   1182  1.13  christos  *
   1183   1.4      adam  * If "terminated" is not NULL, *terminated will be set to 1 when the
   1184   1.4      adam  * last argument's quote has been properly terminated or 0 otherwise.
   1185   1.4      adam  * This parameter is only of use if "sloppy" is set.
   1186   1.1  christos  */
   1187  1.31  christos #define MAXARGS		128
   1188   1.1  christos #define MAXARGLEN	8192
   1189   1.1  christos static char **
   1190   1.4      adam makeargv(const char *arg, int *argcp, int sloppy, char *lastquote,
   1191   1.4      adam     u_int *terminated)
   1192   1.1  christos {
   1193   1.1  christos 	int argc, quot;
   1194   1.1  christos 	size_t i, j;
   1195   1.1  christos 	static char argvs[MAXARGLEN];
   1196   1.1  christos 	static char *argv[MAXARGS + 1];
   1197   1.1  christos 	enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q;
   1198   1.1  christos 
   1199   1.1  christos 	*argcp = argc = 0;
   1200   1.1  christos 	if (strlen(arg) > sizeof(argvs) - 1) {
   1201   1.1  christos  args_too_longs:
   1202   1.1  christos 		error("string too long");
   1203   1.1  christos 		return NULL;
   1204   1.1  christos 	}
   1205   1.4      adam 	if (terminated != NULL)
   1206   1.4      adam 		*terminated = 1;
   1207   1.4      adam 	if (lastquote != NULL)
   1208   1.4      adam 		*lastquote = '\0';
   1209   1.1  christos 	state = MA_START;
   1210   1.1  christos 	i = j = 0;
   1211   1.1  christos 	for (;;) {
   1212  1.11  christos 		if ((size_t)argc >= sizeof(argv) / sizeof(*argv)){
   1213  1.11  christos 			error("Too many arguments.");
   1214  1.11  christos 			return NULL;
   1215  1.11  christos 		}
   1216   1.2  christos 		if (isspace((unsigned char)arg[i])) {
   1217   1.1  christos 			if (state == MA_UNQUOTED) {
   1218   1.1  christos 				/* Terminate current argument */
   1219   1.1  christos 				argvs[j++] = '\0';
   1220   1.1  christos 				argc++;
   1221   1.1  christos 				state = MA_START;
   1222   1.1  christos 			} else if (state != MA_START)
   1223   1.1  christos 				argvs[j++] = arg[i];
   1224   1.1  christos 		} else if (arg[i] == '"' || arg[i] == '\'') {
   1225   1.1  christos 			q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE;
   1226   1.1  christos 			if (state == MA_START) {
   1227   1.1  christos 				argv[argc] = argvs + j;
   1228   1.1  christos 				state = q;
   1229   1.4      adam 				if (lastquote != NULL)
   1230   1.4      adam 					*lastquote = arg[i];
   1231  1.13  christos 			} else if (state == MA_UNQUOTED)
   1232   1.1  christos 				state = q;
   1233   1.1  christos 			else if (state == q)
   1234   1.1  christos 				state = MA_UNQUOTED;
   1235   1.1  christos 			else
   1236   1.1  christos 				argvs[j++] = arg[i];
   1237   1.1  christos 		} else if (arg[i] == '\\') {
   1238   1.1  christos 			if (state == MA_SQUOTE || state == MA_DQUOTE) {
   1239   1.1  christos 				quot = state == MA_SQUOTE ? '\'' : '"';
   1240   1.1  christos 				/* Unescape quote we are in */
   1241   1.1  christos 				/* XXX support \n and friends? */
   1242   1.1  christos 				if (arg[i + 1] == quot) {
   1243   1.1  christos 					i++;
   1244   1.1  christos 					argvs[j++] = arg[i];
   1245   1.1  christos 				} else if (arg[i + 1] == '?' ||
   1246   1.1  christos 				    arg[i + 1] == '[' || arg[i + 1] == '*') {
   1247   1.1  christos 					/*
   1248   1.1  christos 					 * Special case for sftp: append
   1249   1.1  christos 					 * double-escaped glob sequence -
   1250   1.1  christos 					 * glob will undo one level of
   1251   1.1  christos 					 * escaping. NB. string can grow here.
   1252   1.1  christos 					 */
   1253   1.1  christos 					if (j >= sizeof(argvs) - 5)
   1254   1.1  christos 						goto args_too_longs;
   1255   1.1  christos 					argvs[j++] = '\\';
   1256   1.1  christos 					argvs[j++] = arg[i++];
   1257   1.1  christos 					argvs[j++] = '\\';
   1258   1.1  christos 					argvs[j++] = arg[i];
   1259   1.1  christos 				} else {
   1260   1.1  christos 					argvs[j++] = arg[i++];
   1261   1.1  christos 					argvs[j++] = arg[i];
   1262   1.1  christos 				}
   1263   1.1  christos 			} else {
   1264   1.1  christos 				if (state == MA_START) {
   1265   1.1  christos 					argv[argc] = argvs + j;
   1266   1.1  christos 					state = MA_UNQUOTED;
   1267   1.4      adam 					if (lastquote != NULL)
   1268   1.4      adam 						*lastquote = '\0';
   1269   1.1  christos 				}
   1270   1.1  christos 				if (arg[i + 1] == '?' || arg[i + 1] == '[' ||
   1271   1.1  christos 				    arg[i + 1] == '*' || arg[i + 1] == '\\') {
   1272   1.1  christos 					/*
   1273   1.1  christos 					 * Special case for sftp: append
   1274   1.1  christos 					 * escaped glob sequence -
   1275   1.1  christos 					 * glob will undo one level of
   1276   1.1  christos 					 * escaping.
   1277   1.1  christos 					 */
   1278   1.1  christos 					argvs[j++] = arg[i++];
   1279   1.1  christos 					argvs[j++] = arg[i];
   1280   1.1  christos 				} else {
   1281   1.1  christos 					/* Unescape everything */
   1282   1.1  christos 					/* XXX support \n and friends? */
   1283   1.1  christos 					i++;
   1284   1.1  christos 					argvs[j++] = arg[i];
   1285   1.1  christos 				}
   1286   1.1  christos 			}
   1287   1.1  christos 		} else if (arg[i] == '#') {
   1288   1.1  christos 			if (state == MA_SQUOTE || state == MA_DQUOTE)
   1289   1.1  christos 				argvs[j++] = arg[i];
   1290   1.1  christos 			else
   1291   1.1  christos 				goto string_done;
   1292   1.1  christos 		} else if (arg[i] == '\0') {
   1293   1.1  christos 			if (state == MA_SQUOTE || state == MA_DQUOTE) {
   1294   1.4      adam 				if (sloppy) {
   1295   1.4      adam 					state = MA_UNQUOTED;
   1296   1.4      adam 					if (terminated != NULL)
   1297   1.4      adam 						*terminated = 0;
   1298   1.4      adam 					goto string_done;
   1299   1.4      adam 				}
   1300   1.1  christos 				error("Unterminated quoted argument");
   1301   1.1  christos 				return NULL;
   1302   1.1  christos 			}
   1303   1.1  christos  string_done:
   1304   1.1  christos 			if (state == MA_UNQUOTED) {
   1305   1.1  christos 				argvs[j++] = '\0';
   1306   1.1  christos 				argc++;
   1307   1.1  christos 			}
   1308   1.1  christos 			break;
   1309   1.1  christos 		} else {
   1310   1.1  christos 			if (state == MA_START) {
   1311   1.1  christos 				argv[argc] = argvs + j;
   1312   1.1  christos 				state = MA_UNQUOTED;
   1313   1.4      adam 				if (lastquote != NULL)
   1314   1.4      adam 					*lastquote = '\0';
   1315   1.1  christos 			}
   1316   1.1  christos 			if ((state == MA_SQUOTE || state == MA_DQUOTE) &&
   1317   1.1  christos 			    (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) {
   1318   1.1  christos 				/*
   1319   1.1  christos 				 * Special case for sftp: escape quoted
   1320   1.1  christos 				 * glob(3) wildcards. NB. string can grow
   1321   1.1  christos 				 * here.
   1322   1.1  christos 				 */
   1323   1.1  christos 				if (j >= sizeof(argvs) - 3)
   1324   1.1  christos 					goto args_too_longs;
   1325   1.1  christos 				argvs[j++] = '\\';
   1326   1.1  christos 				argvs[j++] = arg[i];
   1327   1.1  christos 			} else
   1328   1.1  christos 				argvs[j++] = arg[i];
   1329   1.1  christos 		}
   1330   1.1  christos 		i++;
   1331   1.1  christos 	}
   1332   1.1  christos 	*argcp = argc;
   1333   1.1  christos 	return argv;
   1334   1.1  christos }
   1335   1.1  christos 
   1336   1.1  christos static int
   1337  1.26  christos parse_args(const char **cpp, int *ignore_errors, int *disable_echo, int *aflag,
   1338  1.19  christos 	  int *fflag, int *hflag, int *iflag, int *lflag, int *pflag,
   1339  1.13  christos 	  int *rflag, int *sflag,
   1340  1.13  christos     unsigned long *n_arg, char **path1, char **path2)
   1341   1.1  christos {
   1342   1.1  christos 	const char *cmd, *cp = *cpp;
   1343   1.1  christos 	char *cp2, **argv;
   1344   1.1  christos 	int base = 0;
   1345  1.31  christos 	long long ll;
   1346  1.23  christos 	int path1_mandatory = 0, i, cmdnum, optidx, argc;
   1347   1.1  christos 
   1348   1.1  christos 	/* Skip leading whitespace */
   1349   1.1  christos 	cp = cp + strspn(cp, WHITESPACE);
   1350   1.1  christos 
   1351  1.26  christos 	/*
   1352  1.26  christos 	 * Check for leading '-' (disable error processing) and '@' (suppress
   1353  1.26  christos 	 * command echo)
   1354  1.26  christos 	 */
   1355  1.13  christos 	*ignore_errors = 0;
   1356  1.26  christos 	*disable_echo = 0;
   1357  1.26  christos 	for (;*cp != '\0'; cp++) {
   1358  1.26  christos 		if (*cp == '-') {
   1359  1.26  christos 			*ignore_errors = 1;
   1360  1.26  christos 		} else if (*cp == '@') {
   1361  1.26  christos 			*disable_echo = 1;
   1362  1.26  christos 		} else {
   1363  1.26  christos 			/* all other characters terminate prefix processing */
   1364  1.26  christos 			break;
   1365  1.26  christos 		}
   1366   1.1  christos 	}
   1367  1.26  christos 	cp = cp + strspn(cp, WHITESPACE);
   1368   1.1  christos 
   1369   1.4      adam 	/* Ignore blank lines and lines which begin with comment '#' char */
   1370   1.4      adam 	if (*cp == '\0' || *cp == '#')
   1371   1.4      adam 		return (0);
   1372   1.4      adam 
   1373   1.4      adam 	if ((argv = makeargv(cp, &argc, 0, NULL, NULL)) == NULL)
   1374   1.1  christos 		return -1;
   1375   1.1  christos 
   1376   1.1  christos 	/* Figure out which command we have */
   1377   1.1  christos 	for (i = 0; cmds[i].c != NULL; i++) {
   1378  1.11  christos 		if (argv[0] != NULL && strcasecmp(cmds[i].c, argv[0]) == 0)
   1379   1.1  christos 			break;
   1380   1.1  christos 	}
   1381   1.1  christos 	cmdnum = cmds[i].n;
   1382   1.1  christos 	cmd = cmds[i].c;
   1383   1.1  christos 
   1384   1.1  christos 	/* Special case */
   1385   1.1  christos 	if (*cp == '!') {
   1386   1.1  christos 		cp++;
   1387   1.1  christos 		cmdnum = I_SHELL;
   1388   1.1  christos 	} else if (cmdnum == -1) {
   1389   1.1  christos 		error("Invalid command.");
   1390   1.1  christos 		return -1;
   1391   1.1  christos 	}
   1392   1.1  christos 
   1393   1.1  christos 	/* Get arguments and parse flags */
   1394  1.13  christos 	*aflag = *fflag = *hflag = *iflag = *lflag = *pflag = 0;
   1395  1.13  christos 	*rflag = *sflag = 0;
   1396   1.1  christos 	*path1 = *path2 = NULL;
   1397   1.1  christos 	optidx = 1;
   1398   1.1  christos 	switch (cmdnum) {
   1399   1.1  christos 	case I_GET:
   1400  1.12  christos 	case I_REGET:
   1401  1.13  christos 	case I_REPUT:
   1402   1.1  christos 	case I_PUT:
   1403   1.7  christos 		if ((optidx = parse_getput_flags(cmd, argv, argc,
   1404  1.13  christos 		    aflag, fflag, pflag, rflag)) == -1)
   1405   1.1  christos 			return -1;
   1406   1.1  christos 		/* Get first pathname (mandatory) */
   1407   1.1  christos 		if (argc - optidx < 1) {
   1408   1.1  christos 			error("You must specify at least one path after a "
   1409   1.1  christos 			    "%s command.", cmd);
   1410   1.1  christos 			return -1;
   1411   1.1  christos 		}
   1412   1.1  christos 		*path1 = xstrdup(argv[optidx]);
   1413   1.1  christos 		/* Get second pathname (optional) */
   1414   1.1  christos 		if (argc - optidx > 1) {
   1415   1.1  christos 			*path2 = xstrdup(argv[optidx + 1]);
   1416   1.1  christos 			/* Destination is not globbed */
   1417   1.1  christos 			undo_glob_escape(*path2);
   1418   1.1  christos 		}
   1419   1.1  christos 		break;
   1420   1.7  christos 	case I_LINK:
   1421   1.7  christos 		if ((optidx = parse_link_flags(cmd, argv, argc, sflag)) == -1)
   1422   1.7  christos 			return -1;
   1423  1.13  christos 		goto parse_two_paths;
   1424  1.35  christos 	case I_COPY:
   1425  1.35  christos 		if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
   1426  1.35  christos 			return -1;
   1427  1.35  christos 		goto parse_two_paths;
   1428  1.13  christos 	case I_RENAME:
   1429  1.13  christos 		if ((optidx = parse_rename_flags(cmd, argv, argc, lflag)) == -1)
   1430  1.13  christos 			return -1;
   1431  1.13  christos 		goto parse_two_paths;
   1432   1.7  christos 	case I_SYMLINK:
   1433  1.13  christos 		if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
   1434  1.13  christos 			return -1;
   1435  1.13  christos  parse_two_paths:
   1436   1.1  christos 		if (argc - optidx < 2) {
   1437   1.1  christos 			error("You must specify two paths after a %s "
   1438   1.1  christos 			    "command.", cmd);
   1439   1.1  christos 			return -1;
   1440   1.1  christos 		}
   1441   1.1  christos 		*path1 = xstrdup(argv[optidx]);
   1442   1.1  christos 		*path2 = xstrdup(argv[optidx + 1]);
   1443   1.1  christos 		/* Paths are not globbed */
   1444   1.1  christos 		undo_glob_escape(*path1);
   1445   1.1  christos 		undo_glob_escape(*path2);
   1446   1.1  christos 		break;
   1447   1.1  christos 	case I_RM:
   1448   1.1  christos 	case I_MKDIR:
   1449   1.1  christos 	case I_RMDIR:
   1450  1.23  christos 	case I_LMKDIR:
   1451  1.23  christos 		path1_mandatory = 1;
   1452  1.23  christos 		/* FALLTHROUGH */
   1453   1.1  christos 	case I_CHDIR:
   1454   1.1  christos 	case I_LCHDIR:
   1455  1.13  christos 		if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
   1456  1.13  christos 			return -1;
   1457   1.1  christos 		/* Get pathname (mandatory) */
   1458   1.1  christos 		if (argc - optidx < 1) {
   1459  1.23  christos 			if (!path1_mandatory)
   1460  1.23  christos 				break; /* return a NULL path1 */
   1461   1.1  christos 			error("You must specify a path after a %s command.",
   1462   1.1  christos 			    cmd);
   1463   1.1  christos 			return -1;
   1464   1.1  christos 		}
   1465   1.1  christos 		*path1 = xstrdup(argv[optidx]);
   1466   1.1  christos 		/* Only "rm" globs */
   1467   1.1  christos 		if (cmdnum != I_RM)
   1468   1.1  christos 			undo_glob_escape(*path1);
   1469   1.1  christos 		break;
   1470   1.1  christos 	case I_DF:
   1471   1.1  christos 		if ((optidx = parse_df_flags(cmd, argv, argc, hflag,
   1472   1.1  christos 		    iflag)) == -1)
   1473   1.1  christos 			return -1;
   1474   1.1  christos 		/* Default to current directory if no path specified */
   1475   1.1  christos 		if (argc - optidx < 1)
   1476   1.1  christos 			*path1 = NULL;
   1477   1.1  christos 		else {
   1478   1.1  christos 			*path1 = xstrdup(argv[optidx]);
   1479   1.1  christos 			undo_glob_escape(*path1);
   1480   1.1  christos 		}
   1481   1.1  christos 		break;
   1482   1.1  christos 	case I_LS:
   1483   1.1  christos 		if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1)
   1484   1.1  christos 			return(-1);
   1485   1.1  christos 		/* Path is optional */
   1486   1.1  christos 		if (argc - optidx > 0)
   1487   1.1  christos 			*path1 = xstrdup(argv[optidx]);
   1488   1.1  christos 		break;
   1489   1.1  christos 	case I_LLS:
   1490   1.1  christos 		/* Skip ls command and following whitespace */
   1491   1.1  christos 		cp = cp + strlen(cmd) + strspn(cp, WHITESPACE);
   1492   1.1  christos 	case I_SHELL:
   1493   1.1  christos 		/* Uses the rest of the line */
   1494   1.1  christos 		break;
   1495   1.1  christos 	case I_LUMASK:
   1496   1.1  christos 	case I_CHMOD:
   1497   1.1  christos 		base = 8;
   1498  1.26  christos 		/* FALLTHROUGH */
   1499   1.1  christos 	case I_CHOWN:
   1500   1.1  christos 	case I_CHGRP:
   1501  1.26  christos 		if ((optidx = parse_ch_flags(cmd, argv, argc, hflag)) == -1)
   1502  1.13  christos 			return -1;
   1503   1.1  christos 		/* Get numeric arg (mandatory) */
   1504   1.1  christos 		if (argc - optidx < 1)
   1505   1.1  christos 			goto need_num_arg;
   1506   1.1  christos 		errno = 0;
   1507  1.31  christos 		ll = strtoll(argv[optidx], &cp2, base);
   1508   1.1  christos 		if (cp2 == argv[optidx] || *cp2 != '\0' ||
   1509  1.31  christos 		    ((ll == LLONG_MIN || ll == LLONG_MAX) && errno == ERANGE) ||
   1510  1.31  christos 		    ll < 0 || ll > UINT32_MAX) {
   1511   1.1  christos  need_num_arg:
   1512   1.1  christos 			error("You must supply a numeric argument "
   1513   1.1  christos 			    "to the %s command.", cmd);
   1514   1.1  christos 			return -1;
   1515   1.1  christos 		}
   1516  1.31  christos 		*n_arg = ll;
   1517   1.1  christos 		if (cmdnum == I_LUMASK)
   1518   1.1  christos 			break;
   1519   1.1  christos 		/* Get pathname (mandatory) */
   1520   1.1  christos 		if (argc - optidx < 2) {
   1521   1.1  christos 			error("You must specify a path after a %s command.",
   1522   1.1  christos 			    cmd);
   1523   1.1  christos 			return -1;
   1524   1.1  christos 		}
   1525   1.1  christos 		*path1 = xstrdup(argv[optidx + 1]);
   1526   1.1  christos 		break;
   1527   1.1  christos 	case I_QUIT:
   1528   1.1  christos 	case I_PWD:
   1529   1.1  christos 	case I_LPWD:
   1530   1.1  christos 	case I_HELP:
   1531   1.1  christos 	case I_VERSION:
   1532   1.1  christos 	case I_PROGRESS:
   1533  1.13  christos 		if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
   1534  1.13  christos 			return -1;
   1535   1.1  christos 		break;
   1536   1.1  christos 	default:
   1537   1.1  christos 		fatal("Command not implemented");
   1538   1.1  christos 	}
   1539   1.1  christos 
   1540   1.1  christos 	*cpp = cp;
   1541   1.1  christos 	return(cmdnum);
   1542   1.1  christos }
   1543   1.1  christos 
   1544   1.1  christos static int
   1545   1.1  christos parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
   1546  1.26  christos     const char *startdir, int err_abort, int echo_command)
   1547   1.1  christos {
   1548  1.26  christos 	const char *ocmd = cmd;
   1549   1.1  christos 	char *path1, *path2, *tmp;
   1550  1.26  christos 	int ignore_errors = 0, disable_echo = 1;
   1551  1.26  christos 	int aflag = 0, fflag = 0, hflag = 0, iflag = 0;
   1552  1.13  christos 	int lflag = 0, pflag = 0, rflag = 0, sflag = 0;
   1553   1.7  christos 	int cmdnum, i;
   1554   1.1  christos 	unsigned long n_arg = 0;
   1555   1.1  christos 	Attrib a, *aa;
   1556  1.14  christos 	char path_buf[PATH_MAX];
   1557   1.1  christos 	int err = 0;
   1558   1.1  christos 	glob_t g;
   1559   1.1  christos 
   1560   1.2  christos 	pflag = 0;	/* XXX gcc */
   1561   1.2  christos 	lflag = 0;	/* XXX gcc */
   1562   1.2  christos 	iflag = 0;	/* XXX gcc */
   1563   1.2  christos 	hflag = 0;	/* XXX gcc */
   1564   1.2  christos 	n_arg = 0;	/* XXX gcc */
   1565   1.2  christos 
   1566   1.1  christos 	path1 = path2 = NULL;
   1567  1.26  christos 	cmdnum = parse_args(&cmd, &ignore_errors, &disable_echo, &aflag, &fflag,
   1568  1.26  christos 	    &hflag, &iflag, &lflag, &pflag, &rflag, &sflag, &n_arg,
   1569  1.26  christos 	    &path1, &path2);
   1570  1.13  christos 	if (ignore_errors != 0)
   1571   1.1  christos 		err_abort = 0;
   1572   1.1  christos 
   1573  1.26  christos 	if (echo_command && !disable_echo)
   1574  1.26  christos 		mprintf("sftp> %s\n", ocmd);
   1575  1.26  christos 
   1576   1.1  christos 	memset(&g, 0, sizeof(g));
   1577   1.1  christos 
   1578   1.1  christos 	/* Perform command */
   1579   1.1  christos 	switch (cmdnum) {
   1580   1.1  christos 	case 0:
   1581   1.1  christos 		/* Blank line */
   1582   1.1  christos 		break;
   1583   1.1  christos 	case -1:
   1584   1.1  christos 		/* Unrecognized command */
   1585   1.1  christos 		err = -1;
   1586   1.1  christos 		break;
   1587  1.12  christos 	case I_REGET:
   1588  1.12  christos 		aflag = 1;
   1589  1.12  christos 		/* FALLTHROUGH */
   1590   1.1  christos 	case I_GET:
   1591  1.12  christos 		err = process_get(conn, path1, path2, *pwd, pflag,
   1592  1.13  christos 		    rflag, aflag, fflag);
   1593   1.1  christos 		break;
   1594  1.13  christos 	case I_REPUT:
   1595  1.13  christos 		aflag = 1;
   1596  1.13  christos 		/* FALLTHROUGH */
   1597   1.1  christos 	case I_PUT:
   1598  1.13  christos 		err = process_put(conn, path1, path2, *pwd, pflag,
   1599  1.13  christos 		    rflag, aflag, fflag);
   1600   1.1  christos 		break;
   1601  1.35  christos 	case I_COPY:
   1602  1.35  christos 		path1 = make_absolute(path1, *pwd);
   1603  1.35  christos 		path2 = make_absolute(path2, *pwd);
   1604  1.35  christos 		err = do_copy(conn, path1, path2);
   1605  1.35  christos 		break;
   1606   1.1  christos 	case I_RENAME:
   1607   1.1  christos 		path1 = make_absolute(path1, *pwd);
   1608   1.1  christos 		path2 = make_absolute(path2, *pwd);
   1609  1.13  christos 		err = do_rename(conn, path1, path2, lflag);
   1610   1.1  christos 		break;
   1611   1.1  christos 	case I_SYMLINK:
   1612   1.7  christos 		sflag = 1;
   1613  1.26  christos 		/* FALLTHROUGH */
   1614   1.7  christos 	case I_LINK:
   1615  1.13  christos 		if (!sflag)
   1616  1.13  christos 			path1 = make_absolute(path1, *pwd);
   1617   1.1  christos 		path2 = make_absolute(path2, *pwd);
   1618   1.7  christos 		err = (sflag ? do_symlink : do_hardlink)(conn, path1, path2);
   1619   1.1  christos 		break;
   1620   1.1  christos 	case I_RM:
   1621  1.36  christos 		path1 = make_absolute_pwd_glob(path1, *pwd);
   1622   1.1  christos 		remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
   1623   1.1  christos 		for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
   1624  1.12  christos 			if (!quiet)
   1625  1.19  christos 				mprintf("Removing %s\n", g.gl_pathv[i]);
   1626   1.1  christos 			err = do_rm(conn, g.gl_pathv[i]);
   1627   1.1  christos 			if (err != 0 && err_abort)
   1628   1.1  christos 				break;
   1629   1.1  christos 		}
   1630   1.1  christos 		break;
   1631   1.1  christos 	case I_MKDIR:
   1632   1.1  christos 		path1 = make_absolute(path1, *pwd);
   1633   1.1  christos 		attrib_clear(&a);
   1634   1.1  christos 		a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
   1635   1.1  christos 		a.perm = 0777;
   1636   1.4      adam 		err = do_mkdir(conn, path1, &a, 1);
   1637   1.1  christos 		break;
   1638   1.1  christos 	case I_RMDIR:
   1639   1.1  christos 		path1 = make_absolute(path1, *pwd);
   1640   1.1  christos 		err = do_rmdir(conn, path1);
   1641   1.1  christos 		break;
   1642   1.1  christos 	case I_CHDIR:
   1643  1.23  christos 		if (path1 == NULL || *path1 == '\0')
   1644  1.23  christos 			path1 = xstrdup(startdir);
   1645   1.1  christos 		path1 = make_absolute(path1, *pwd);
   1646   1.1  christos 		if ((tmp = do_realpath(conn, path1)) == NULL) {
   1647   1.1  christos 			err = 1;
   1648   1.1  christos 			break;
   1649   1.1  christos 		}
   1650   1.1  christos 		if ((aa = do_stat(conn, tmp, 0)) == NULL) {
   1651  1.12  christos 			free(tmp);
   1652   1.1  christos 			err = 1;
   1653   1.1  christos 			break;
   1654   1.1  christos 		}
   1655   1.1  christos 		if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
   1656   1.1  christos 			error("Can't change directory: Can't check target");
   1657  1.12  christos 			free(tmp);
   1658   1.1  christos 			err = 1;
   1659   1.1  christos 			break;
   1660   1.1  christos 		}
   1661   1.1  christos 		if (!S_ISDIR(aa->perm)) {
   1662   1.1  christos 			error("Can't change directory: \"%s\" is not "
   1663   1.1  christos 			    "a directory", tmp);
   1664  1.12  christos 			free(tmp);
   1665   1.1  christos 			err = 1;
   1666   1.1  christos 			break;
   1667   1.1  christos 		}
   1668  1.12  christos 		free(*pwd);
   1669   1.1  christos 		*pwd = tmp;
   1670   1.1  christos 		break;
   1671   1.1  christos 	case I_LS:
   1672   1.1  christos 		if (!path1) {
   1673   1.4      adam 			do_ls_dir(conn, *pwd, *pwd, lflag);
   1674   1.1  christos 			break;
   1675   1.1  christos 		}
   1676   1.1  christos 
   1677   1.1  christos 		/* Strip pwd off beginning of non-absolute paths */
   1678   1.1  christos 		tmp = NULL;
   1679  1.26  christos 		if (!path_absolute(path1))
   1680   1.1  christos 			tmp = *pwd;
   1681   1.1  christos 
   1682  1.36  christos 		path1 = make_absolute_pwd_glob(path1, *pwd);
   1683   1.1  christos 		err = do_globbed_ls(conn, path1, tmp, lflag);
   1684   1.1  christos 		break;
   1685   1.1  christos 	case I_DF:
   1686   1.1  christos 		/* Default to current directory if no path specified */
   1687   1.1  christos 		if (path1 == NULL)
   1688   1.1  christos 			path1 = xstrdup(*pwd);
   1689   1.1  christos 		path1 = make_absolute(path1, *pwd);
   1690   1.1  christos 		err = do_df(conn, path1, hflag, iflag);
   1691   1.1  christos 		break;
   1692   1.1  christos 	case I_LCHDIR:
   1693  1.23  christos 		if (path1 == NULL || *path1 == '\0')
   1694  1.23  christos 			path1 = xstrdup("~");
   1695  1.14  christos 		tmp = tilde_expand_filename(path1, getuid());
   1696  1.14  christos 		free(path1);
   1697  1.14  christos 		path1 = tmp;
   1698   1.1  christos 		if (chdir(path1) == -1) {
   1699   1.1  christos 			error("Couldn't change local directory to "
   1700   1.1  christos 			    "\"%s\": %s", path1, strerror(errno));
   1701   1.1  christos 			err = 1;
   1702   1.1  christos 		}
   1703   1.1  christos 		break;
   1704   1.1  christos 	case I_LMKDIR:
   1705   1.1  christos 		if (mkdir(path1, 0777) == -1) {
   1706   1.1  christos 			error("Couldn't create local directory "
   1707   1.1  christos 			    "\"%s\": %s", path1, strerror(errno));
   1708   1.1  christos 			err = 1;
   1709   1.1  christos 		}
   1710   1.1  christos 		break;
   1711   1.1  christos 	case I_LLS:
   1712   1.1  christos 		local_do_ls(cmd);
   1713   1.1  christos 		break;
   1714   1.1  christos 	case I_SHELL:
   1715   1.1  christos 		local_do_shell(cmd);
   1716   1.1  christos 		break;
   1717   1.1  christos 	case I_LUMASK:
   1718   1.1  christos 		umask(n_arg);
   1719   1.1  christos 		printf("Local umask: %03lo\n", n_arg);
   1720   1.1  christos 		break;
   1721   1.1  christos 	case I_CHMOD:
   1722  1.36  christos 		path1 = make_absolute_pwd_glob(path1, *pwd);
   1723   1.1  christos 		attrib_clear(&a);
   1724   1.1  christos 		a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
   1725   1.1  christos 		a.perm = n_arg;
   1726   1.1  christos 		remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
   1727   1.1  christos 		for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
   1728  1.12  christos 			if (!quiet)
   1729  1.19  christos 				mprintf("Changing mode on %s\n",
   1730  1.19  christos 				    g.gl_pathv[i]);
   1731  1.26  christos 			err = (hflag ? do_lsetstat : do_setstat)(conn,
   1732  1.26  christos 			    g.gl_pathv[i], &a);
   1733   1.1  christos 			if (err != 0 && err_abort)
   1734   1.1  christos 				break;
   1735   1.1  christos 		}
   1736   1.1  christos 		break;
   1737   1.1  christos 	case I_CHOWN:
   1738   1.1  christos 	case I_CHGRP:
   1739  1.36  christos 		path1 = make_absolute_pwd_glob(path1, *pwd);
   1740   1.1  christos 		remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
   1741   1.1  christos 		for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
   1742  1.26  christos 			if (!(aa = (hflag ? do_lstat : do_stat)(conn,
   1743  1.26  christos 			    g.gl_pathv[i], 0))) {
   1744   1.1  christos 				if (err_abort) {
   1745   1.1  christos 					err = -1;
   1746   1.1  christos 					break;
   1747   1.1  christos 				} else
   1748   1.1  christos 					continue;
   1749   1.1  christos 			}
   1750   1.1  christos 			if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
   1751   1.1  christos 				error("Can't get current ownership of "
   1752   1.1  christos 				    "remote file \"%s\"", g.gl_pathv[i]);
   1753   1.1  christos 				if (err_abort) {
   1754   1.1  christos 					err = -1;
   1755   1.1  christos 					break;
   1756   1.1  christos 				} else
   1757   1.1  christos 					continue;
   1758   1.1  christos 			}
   1759   1.1  christos 			aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
   1760   1.1  christos 			if (cmdnum == I_CHOWN) {
   1761  1.12  christos 				if (!quiet)
   1762  1.19  christos 					mprintf("Changing owner on %s\n",
   1763  1.12  christos 					    g.gl_pathv[i]);
   1764   1.1  christos 				aa->uid = n_arg;
   1765   1.1  christos 			} else {
   1766  1.12  christos 				if (!quiet)
   1767  1.19  christos 					mprintf("Changing group on %s\n",
   1768  1.12  christos 					    g.gl_pathv[i]);
   1769   1.1  christos 				aa->gid = n_arg;
   1770   1.1  christos 			}
   1771  1.26  christos 			err = (hflag ? do_lsetstat : do_setstat)(conn,
   1772  1.26  christos 			    g.gl_pathv[i], aa);
   1773   1.1  christos 			if (err != 0 && err_abort)
   1774   1.1  christos 				break;
   1775   1.1  christos 		}
   1776   1.1  christos 		break;
   1777   1.1  christos 	case I_PWD:
   1778  1.19  christos 		mprintf("Remote working directory: %s\n", *pwd);
   1779   1.1  christos 		break;
   1780   1.1  christos 	case I_LPWD:
   1781   1.1  christos 		if (!getcwd(path_buf, sizeof(path_buf))) {
   1782   1.1  christos 			error("Couldn't get local cwd: %s", strerror(errno));
   1783   1.1  christos 			err = -1;
   1784   1.1  christos 			break;
   1785   1.1  christos 		}
   1786  1.19  christos 		mprintf("Local working directory: %s\n", path_buf);
   1787   1.1  christos 		break;
   1788   1.1  christos 	case I_QUIT:
   1789   1.1  christos 		/* Processed below */
   1790   1.1  christos 		break;
   1791   1.1  christos 	case I_HELP:
   1792   1.1  christos 		help();
   1793   1.1  christos 		break;
   1794   1.1  christos 	case I_VERSION:
   1795   1.1  christos 		printf("SFTP protocol version %u\n", sftp_proto_version(conn));
   1796   1.1  christos 		break;
   1797   1.1  christos 	case I_PROGRESS:
   1798   1.1  christos 		showprogress = !showprogress;
   1799   1.1  christos 		if (showprogress)
   1800   1.1  christos 			printf("Progress meter enabled\n");
   1801   1.1  christos 		else
   1802   1.1  christos 			printf("Progress meter disabled\n");
   1803   1.1  christos 		break;
   1804   1.1  christos 	default:
   1805   1.1  christos 		fatal("%d is not implemented", cmdnum);
   1806   1.1  christos 	}
   1807   1.1  christos 
   1808   1.1  christos 	if (g.gl_pathc)
   1809   1.1  christos 		globfree(&g);
   1810  1.12  christos 	free(path1);
   1811  1.12  christos 	free(path2);
   1812   1.1  christos 
   1813   1.1  christos 	/* If an unignored error occurs in batch mode we should abort. */
   1814   1.1  christos 	if (err_abort && err != 0)
   1815   1.1  christos 		return (-1);
   1816   1.1  christos 	else if (cmdnum == I_QUIT)
   1817   1.1  christos 		return (1);
   1818   1.1  christos 
   1819   1.1  christos 	return (0);
   1820   1.1  christos }
   1821   1.1  christos 
   1822   1.7  christos static const char *
   1823   1.1  christos prompt(EditLine *el)
   1824   1.1  christos {
   1825   1.1  christos 	return ("sftp> ");
   1826   1.1  christos }
   1827   1.1  christos 
   1828   1.4      adam /* Display entries in 'list' after skipping the first 'len' chars */
   1829   1.4      adam static void
   1830   1.4      adam complete_display(char **list, u_int len)
   1831   1.4      adam {
   1832   1.4      adam 	u_int y, m = 0, width = 80, columns = 1, colspace = 0, llen;
   1833   1.4      adam 	struct winsize ws;
   1834   1.7  christos 	const char *tmp;
   1835   1.4      adam 
   1836   1.4      adam 	/* Count entries for sort and find longest */
   1837  1.13  christos 	for (y = 0; list[y]; y++)
   1838  1.20  christos 		m = MAXIMUM(m, strlen(list[y]));
   1839   1.4      adam 
   1840   1.4      adam 	if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
   1841   1.4      adam 		width = ws.ws_col;
   1842   1.4      adam 
   1843   1.4      adam 	m = m > len ? m - len : 0;
   1844   1.4      adam 	columns = width / (m + 2);
   1845  1.20  christos 	columns = MAXIMUM(columns, 1);
   1846   1.4      adam 	colspace = width / columns;
   1847  1.20  christos 	colspace = MINIMUM(colspace, width);
   1848   1.4      adam 
   1849   1.4      adam 	printf("\n");
   1850   1.4      adam 	m = 1;
   1851   1.4      adam 	for (y = 0; list[y]; y++) {
   1852   1.4      adam 		llen = strlen(list[y]);
   1853   1.4      adam 		tmp = llen > len ? list[y] + len : "";
   1854  1.19  christos 		mprintf("%-*s", colspace, tmp);
   1855   1.4      adam 		if (m >= columns) {
   1856   1.4      adam 			printf("\n");
   1857   1.4      adam 			m = 1;
   1858   1.4      adam 		} else
   1859   1.4      adam 			m++;
   1860   1.4      adam 	}
   1861   1.4      adam 	printf("\n");
   1862   1.4      adam }
   1863   1.4      adam 
   1864   1.4      adam /*
   1865   1.4      adam  * Given a "list" of words that begin with a common prefix of "word",
   1866   1.4      adam  * attempt to find an autocompletion to extends "word" by the next
   1867   1.4      adam  * characters common to all entries in "list".
   1868   1.4      adam  */
   1869   1.4      adam static char *
   1870   1.4      adam complete_ambiguous(const char *word, char **list, size_t count)
   1871   1.4      adam {
   1872   1.4      adam 	if (word == NULL)
   1873   1.4      adam 		return NULL;
   1874   1.4      adam 
   1875   1.4      adam 	if (count > 0) {
   1876   1.4      adam 		u_int y, matchlen = strlen(list[0]);
   1877   1.4      adam 
   1878   1.4      adam 		/* Find length of common stem */
   1879   1.4      adam 		for (y = 1; list[y]; y++) {
   1880   1.4      adam 			u_int x;
   1881   1.4      adam 
   1882  1.13  christos 			for (x = 0; x < matchlen; x++)
   1883  1.13  christos 				if (list[0][x] != list[y][x])
   1884   1.4      adam 					break;
   1885   1.4      adam 
   1886   1.4      adam 			matchlen = x;
   1887   1.4      adam 		}
   1888   1.4      adam 
   1889   1.4      adam 		if (matchlen > strlen(word)) {
   1890   1.4      adam 			char *tmp = xstrdup(list[0]);
   1891   1.4      adam 
   1892   1.4      adam 			tmp[matchlen] = '\0';
   1893   1.4      adam 			return tmp;
   1894   1.4      adam 		}
   1895  1.13  christos 	}
   1896   1.4      adam 
   1897   1.4      adam 	return xstrdup(word);
   1898   1.4      adam }
   1899   1.4      adam 
   1900   1.4      adam /* Autocomplete a sftp command */
   1901   1.4      adam static int
   1902   1.4      adam complete_cmd_parse(EditLine *el, char *cmd, int lastarg, char quote,
   1903   1.4      adam     int terminated)
   1904   1.4      adam {
   1905   1.4      adam 	u_int y, count = 0, cmdlen, tmplen;
   1906   1.4      adam 	char *tmp, **list, argterm[3];
   1907   1.4      adam 	const LineInfo *lf;
   1908   1.4      adam 
   1909   1.4      adam 	list = xcalloc((sizeof(cmds) / sizeof(*cmds)) + 1, sizeof(char *));
   1910   1.4      adam 
   1911   1.4      adam 	/* No command specified: display all available commands */
   1912   1.4      adam 	if (cmd == NULL) {
   1913   1.4      adam 		for (y = 0; cmds[y].c; y++)
   1914   1.4      adam 			list[count++] = xstrdup(cmds[y].c);
   1915  1.13  christos 
   1916   1.4      adam 		list[count] = NULL;
   1917   1.4      adam 		complete_display(list, 0);
   1918   1.4      adam 
   1919  1.13  christos 		for (y = 0; list[y] != NULL; y++)
   1920  1.13  christos 			free(list[y]);
   1921  1.12  christos 		free(list);
   1922   1.4      adam 		return count;
   1923   1.4      adam 	}
   1924   1.4      adam 
   1925   1.4      adam 	/* Prepare subset of commands that start with "cmd" */
   1926   1.4      adam 	cmdlen = strlen(cmd);
   1927   1.4      adam 	for (y = 0; cmds[y].c; y++)  {
   1928  1.13  christos 		if (!strncasecmp(cmd, cmds[y].c, cmdlen))
   1929   1.4      adam 			list[count++] = xstrdup(cmds[y].c);
   1930   1.4      adam 	}
   1931   1.4      adam 	list[count] = NULL;
   1932   1.4      adam 
   1933   1.9  christos 	if (count == 0) {
   1934  1.12  christos 		free(list);
   1935   1.4      adam 		return 0;
   1936   1.9  christos 	}
   1937   1.4      adam 
   1938  1.24  christos 	/* Complete ambiguous command */
   1939   1.4      adam 	tmp = complete_ambiguous(cmd, list, count);
   1940   1.4      adam 	if (count > 1)
   1941   1.4      adam 		complete_display(list, 0);
   1942   1.4      adam 
   1943  1.13  christos 	for (y = 0; list[y]; y++)
   1944  1.13  christos 		free(list[y]);
   1945  1.12  christos 	free(list);
   1946   1.4      adam 
   1947   1.4      adam 	if (tmp != NULL) {
   1948   1.4      adam 		tmplen = strlen(tmp);
   1949   1.4      adam 		cmdlen = strlen(cmd);
   1950   1.4      adam 		/* If cmd may be extended then do so */
   1951   1.4      adam 		if (tmplen > cmdlen)
   1952   1.4      adam 			if (el_insertstr(el, tmp + cmdlen) == -1)
   1953   1.4      adam 				fatal("el_insertstr failed.");
   1954   1.4      adam 		lf = el_line(el);
   1955   1.4      adam 		/* Terminate argument cleanly */
   1956   1.4      adam 		if (count == 1) {
   1957   1.4      adam 			y = 0;
   1958   1.4      adam 			if (!terminated)
   1959   1.4      adam 				argterm[y++] = quote;
   1960   1.4      adam 			if (lastarg || *(lf->cursor) != ' ')
   1961   1.4      adam 				argterm[y++] = ' ';
   1962   1.4      adam 			argterm[y] = '\0';
   1963   1.4      adam 			if (y > 0 && el_insertstr(el, argterm) == -1)
   1964   1.4      adam 				fatal("el_insertstr failed.");
   1965   1.4      adam 		}
   1966  1.12  christos 		free(tmp);
   1967   1.4      adam 	}
   1968   1.4      adam 
   1969   1.4      adam 	return count;
   1970   1.4      adam }
   1971   1.4      adam 
   1972   1.4      adam /*
   1973  1.36  christos  * Determine whether a particular sftp command's arguments (if any) represent
   1974  1.36  christos  * local or remote files. The "cmdarg" argument specifies the actual argument
   1975  1.36  christos  * and accepts values 1 or 2.
   1976   1.4      adam  */
   1977   1.4      adam static int
   1978  1.36  christos complete_is_remote(char *cmd, int cmdarg) {
   1979   1.4      adam 	int i;
   1980   1.4      adam 
   1981   1.4      adam 	if (cmd == NULL)
   1982   1.4      adam 		return -1;
   1983   1.4      adam 
   1984   1.4      adam 	for (i = 0; cmds[i].c; i++) {
   1985  1.36  christos 		if (!strncasecmp(cmd, cmds[i].c, strlen(cmds[i].c))) {
   1986  1.36  christos 			if (cmdarg == 1)
   1987  1.36  christos 				return cmds[i].t;
   1988  1.36  christos 			else if (cmdarg == 2)
   1989  1.36  christos 				return cmds[i].t2;
   1990  1.36  christos 			break;
   1991  1.36  christos 		}
   1992   1.4      adam 	}
   1993   1.4      adam 
   1994   1.4      adam 	return -1;
   1995   1.4      adam }
   1996   1.4      adam 
   1997   1.4      adam /* Autocomplete a filename "file" */
   1998   1.4      adam static int
   1999   1.4      adam complete_match(EditLine *el, struct sftp_conn *conn, char *remote_path,
   2000   1.4      adam     char *file, int remote, int lastarg, char quote, int terminated)
   2001   1.4      adam {
   2002   1.4      adam 	glob_t g;
   2003  1.12  christos 	char *tmp, *tmp2, ins[8];
   2004  1.11  christos 	u_int i, hadglob, pwdlen, len, tmplen, filelen, cesc, isesc, isabs;
   2005  1.12  christos 	int clen;
   2006   1.4      adam 	const LineInfo *lf;
   2007  1.13  christos 
   2008   1.4      adam 	/* Glob from "file" location */
   2009   1.4      adam 	if (file == NULL)
   2010   1.4      adam 		tmp = xstrdup("*");
   2011   1.4      adam 	else
   2012   1.4      adam 		xasprintf(&tmp, "%s*", file);
   2013   1.4      adam 
   2014  1.11  christos 	/* Check if the path is absolute. */
   2015  1.26  christos 	isabs = path_absolute(tmp);
   2016  1.11  christos 
   2017   1.4      adam 	memset(&g, 0, sizeof(g));
   2018   1.4      adam 	if (remote != LOCAL) {
   2019  1.36  christos 		tmp = make_absolute_pwd_glob(tmp, remote_path);
   2020   1.4      adam 		remote_glob(conn, tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
   2021  1.17  christos 	} else
   2022   1.6  christos 		glob(tmp, GLOB_LIMIT|GLOB_DOOFFS|GLOB_MARK, NULL, &g);
   2023   1.4      adam 
   2024   1.4      adam 	/* Determine length of pwd so we can trim completion display */
   2025   1.4      adam 	for (hadglob = tmplen = pwdlen = 0; tmp[tmplen] != 0; tmplen++) {
   2026   1.4      adam 		/* Terminate counting on first unescaped glob metacharacter */
   2027   1.4      adam 		if (tmp[tmplen] == '*' || tmp[tmplen] == '?') {
   2028   1.4      adam 			if (tmp[tmplen] != '*' || tmp[tmplen + 1] != '\0')
   2029   1.4      adam 				hadglob = 1;
   2030   1.4      adam 			break;
   2031   1.4      adam 		}
   2032   1.4      adam 		if (tmp[tmplen] == '\\' && tmp[tmplen + 1] != '\0')
   2033   1.4      adam 			tmplen++;
   2034   1.4      adam 		if (tmp[tmplen] == '/')
   2035   1.4      adam 			pwdlen = tmplen + 1;	/* track last seen '/' */
   2036   1.4      adam 	}
   2037  1.12  christos 	free(tmp);
   2038  1.13  christos 	tmp = NULL;
   2039   1.4      adam 
   2040  1.13  christos 	if (g.gl_matchc == 0)
   2041   1.4      adam 		goto out;
   2042   1.4      adam 
   2043   1.4      adam 	if (g.gl_matchc > 1)
   2044   1.4      adam 		complete_display(g.gl_pathv, pwdlen);
   2045   1.4      adam 
   2046   1.4      adam 	/* Don't try to extend globs */
   2047   1.4      adam 	if (file == NULL || hadglob)
   2048   1.4      adam 		goto out;
   2049   1.4      adam 
   2050   1.4      adam 	tmp2 = complete_ambiguous(file, g.gl_pathv, g.gl_matchc);
   2051  1.11  christos 	tmp = path_strip(tmp2, isabs ? NULL : remote_path);
   2052  1.12  christos 	free(tmp2);
   2053   1.4      adam 
   2054   1.4      adam 	if (tmp == NULL)
   2055   1.4      adam 		goto out;
   2056   1.4      adam 
   2057   1.4      adam 	tmplen = strlen(tmp);
   2058   1.4      adam 	filelen = strlen(file);
   2059   1.4      adam 
   2060  1.11  christos 	/* Count the number of escaped characters in the input string. */
   2061  1.11  christos 	cesc = isesc = 0;
   2062  1.11  christos 	for (i = 0; i < filelen; i++) {
   2063  1.11  christos 		if (!isesc && file[i] == '\\' && i + 1 < filelen){
   2064  1.11  christos 			isesc = 1;
   2065  1.11  christos 			cesc++;
   2066  1.11  christos 		} else
   2067  1.11  christos 			isesc = 0;
   2068  1.11  christos 	}
   2069  1.11  christos 
   2070  1.11  christos 	if (tmplen > (filelen - cesc)) {
   2071  1.11  christos 		tmp2 = tmp + filelen - cesc;
   2072  1.13  christos 		len = strlen(tmp2);
   2073   1.4      adam 		/* quote argument on way out */
   2074  1.12  christos 		for (i = 0; i < len; i += clen) {
   2075  1.12  christos 			if ((clen = mblen(tmp2 + i, len - i)) < 0 ||
   2076  1.12  christos 			    (size_t)clen > sizeof(ins) - 2)
   2077  1.12  christos 				fatal("invalid multibyte character");
   2078   1.4      adam 			ins[0] = '\\';
   2079  1.12  christos 			memcpy(ins + 1, tmp2 + i, clen);
   2080  1.12  christos 			ins[clen + 1] = '\0';
   2081   1.4      adam 			switch (tmp2[i]) {
   2082   1.4      adam 			case '\'':
   2083   1.4      adam 			case '"':
   2084   1.4      adam 			case '\\':
   2085   1.4      adam 			case '\t':
   2086   1.7  christos 			case '[':
   2087   1.4      adam 			case ' ':
   2088  1.11  christos 			case '#':
   2089  1.11  christos 			case '*':
   2090   1.4      adam 				if (quote == '\0' || tmp2[i] == quote) {
   2091   1.4      adam 					if (el_insertstr(el, ins) == -1)
   2092   1.4      adam 						fatal("el_insertstr "
   2093   1.4      adam 						    "failed.");
   2094   1.4      adam 					break;
   2095   1.4      adam 				}
   2096   1.4      adam 				/* FALLTHROUGH */
   2097   1.4      adam 			default:
   2098   1.4      adam 				if (el_insertstr(el, ins + 1) == -1)
   2099   1.4      adam 					fatal("el_insertstr failed.");
   2100   1.4      adam 				break;
   2101   1.4      adam 			}
   2102   1.4      adam 		}
   2103   1.4      adam 	}
   2104   1.4      adam 
   2105   1.4      adam 	lf = el_line(el);
   2106   1.4      adam 	if (g.gl_matchc == 1) {
   2107   1.4      adam 		i = 0;
   2108  1.13  christos 		if (!terminated && quote != '\0')
   2109   1.4      adam 			ins[i++] = quote;
   2110   1.4      adam 		if (*(lf->cursor - 1) != '/' &&
   2111   1.4      adam 		    (lastarg || *(lf->cursor) != ' '))
   2112   1.4      adam 			ins[i++] = ' ';
   2113   1.4      adam 		ins[i] = '\0';
   2114   1.4      adam 		if (i > 0 && el_insertstr(el, ins) == -1)
   2115   1.4      adam 			fatal("el_insertstr failed.");
   2116   1.4      adam 	}
   2117  1.12  christos 	free(tmp);
   2118   1.4      adam 
   2119   1.4      adam  out:
   2120   1.4      adam 	globfree(&g);
   2121   1.4      adam 	return g.gl_matchc;
   2122   1.4      adam }
   2123   1.4      adam 
   2124   1.4      adam /* tab-completion hook function, called via libedit */
   2125   1.4      adam static unsigned char
   2126   1.4      adam complete(EditLine *el, int ch)
   2127   1.4      adam {
   2128  1.13  christos 	char **argv, *line, quote;
   2129  1.12  christos 	int argc, carg;
   2130  1.12  christos 	u_int cursor, len, terminated, ret = CC_ERROR;
   2131   1.4      adam 	const LineInfo *lf;
   2132   1.4      adam 	struct complete_ctx *complete_ctx;
   2133   1.4      adam 
   2134   1.4      adam 	lf = el_line(el);
   2135   1.5      adam 	if (el_get(el, EL_CLIENTDATA, &complete_ctx) != 0)
   2136  1.31  christos 		fatal_f("el_get failed");
   2137   1.4      adam 
   2138   1.4      adam 	/* Figure out which argument the cursor points to */
   2139   1.4      adam 	cursor = lf->cursor - lf->buffer;
   2140  1.16  christos 	line = xmalloc(cursor + 1);
   2141   1.4      adam 	memcpy(line, lf->buffer, cursor);
   2142   1.4      adam 	line[cursor] = '\0';
   2143   1.4      adam 	argv = makeargv(line, &carg, 1, &quote, &terminated);
   2144  1.12  christos 	free(line);
   2145   1.4      adam 
   2146   1.4      adam 	/* Get all the arguments on the line */
   2147   1.4      adam 	len = lf->lastchar - lf->buffer;
   2148  1.16  christos 	line = xmalloc(len + 1);
   2149   1.4      adam 	memcpy(line, lf->buffer, len);
   2150   1.4      adam 	line[len] = '\0';
   2151   1.4      adam 	argv = makeargv(line, &argc, 1, NULL, NULL);
   2152   1.4      adam 
   2153   1.4      adam 	/* Ensure cursor is at EOL or a argument boundary */
   2154   1.4      adam 	if (line[cursor] != ' ' && line[cursor] != '\0' &&
   2155   1.4      adam 	    line[cursor] != '\n') {
   2156  1.12  christos 		free(line);
   2157   1.4      adam 		return ret;
   2158   1.4      adam 	}
   2159   1.4      adam 
   2160   1.4      adam 	if (carg == 0) {
   2161   1.4      adam 		/* Show all available commands */
   2162   1.4      adam 		complete_cmd_parse(el, NULL, argc == carg, '\0', 1);
   2163   1.4      adam 		ret = CC_REDISPLAY;
   2164   1.4      adam 	} else if (carg == 1 && cursor > 0 && line[cursor - 1] != ' ')  {
   2165   1.4      adam 		/* Handle the command parsing */
   2166   1.4      adam 		if (complete_cmd_parse(el, argv[0], argc == carg,
   2167  1.13  christos 		    quote, terminated) != 0)
   2168   1.4      adam 			ret = CC_REDISPLAY;
   2169   1.4      adam 	} else if (carg >= 1) {
   2170   1.4      adam 		/* Handle file parsing */
   2171  1.36  christos 		int remote = 0;
   2172  1.36  christos 		int i = 0, cmdarg = 0;
   2173   1.4      adam 		char *filematch = NULL;
   2174   1.4      adam 
   2175   1.4      adam 		if (carg > 1 && line[cursor-1] != ' ')
   2176   1.4      adam 			filematch = argv[carg - 1];
   2177   1.4      adam 
   2178  1.36  christos 		for (i = 1; i < carg; i++) {
   2179  1.36  christos 			/* Skip flags */
   2180  1.36  christos 			if (argv[i][0] != '-')
   2181  1.36  christos 				cmdarg++;
   2182  1.36  christos 		}
   2183  1.36  christos 
   2184  1.36  christos 		/*
   2185  1.36  christos 		 * If previous argument is complete, then offer completion
   2186  1.36  christos 		 * on the next one.
   2187  1.36  christos 		 */
   2188  1.36  christos 		if (line[cursor - 1] == ' ')
   2189  1.36  christos 			cmdarg++;
   2190  1.36  christos 
   2191  1.36  christos 		remote = complete_is_remote(argv[0], cmdarg);
   2192  1.36  christos 
   2193  1.36  christos 		if ((remote == REMOTE || remote == LOCAL) &&
   2194   1.4      adam 		    complete_match(el, complete_ctx->conn,
   2195   1.4      adam 		    *complete_ctx->remote_pathp, filematch,
   2196  1.13  christos 		    remote, carg == argc, quote, terminated) != 0)
   2197   1.4      adam 			ret = CC_REDISPLAY;
   2198   1.4      adam 	}
   2199   1.4      adam 
   2200  1.13  christos 	free(line);
   2201   1.4      adam 	return ret;
   2202   1.4      adam }
   2203   1.4      adam 
   2204  1.23  christos static int
   2205   1.7  christos interactive_loop(struct sftp_conn *conn, const char *file1, const char *file2)
   2206   1.1  christos {
   2207   1.4      adam 	char *remote_path;
   2208  1.23  christos 	char *dir = NULL, *startdir = NULL;
   2209   1.1  christos 	char cmd[2048];
   2210   1.1  christos 	int err, interactive;
   2211   1.1  christos 	EditLine *el = NULL;
   2212   1.1  christos 	History *hl = NULL;
   2213   1.1  christos 	HistEvent hev;
   2214   1.1  christos 	extern char *__progname;
   2215   1.4      adam 	struct complete_ctx complete_ctx;
   2216   1.1  christos 
   2217   1.1  christos 	if (!batchmode && isatty(STDIN_FILENO)) {
   2218   1.1  christos 		if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
   2219   1.1  christos 			fatal("Couldn't initialise editline");
   2220   1.1  christos 		if ((hl = history_init()) == NULL)
   2221   1.1  christos 			fatal("Couldn't initialise editline history");
   2222   1.1  christos 		history(hl, &hev, H_SETSIZE, 100);
   2223   1.1  christos 		el_set(el, EL_HIST, history, hl);
   2224   1.1  christos 
   2225   1.1  christos 		el_set(el, EL_PROMPT, prompt);
   2226   1.1  christos 		el_set(el, EL_EDITOR, "emacs");
   2227   1.1  christos 		el_set(el, EL_TERMINAL, NULL);
   2228   1.1  christos 		el_set(el, EL_SIGNAL, 1);
   2229   1.1  christos 		el_source(el, NULL);
   2230   1.4      adam 
   2231   1.4      adam 		/* Tab Completion */
   2232  1.13  christos 		el_set(el, EL_ADDFN, "ftp-complete",
   2233   1.7  christos 		    "Context sensitive argument completion", complete);
   2234   1.4      adam 		complete_ctx.conn = conn;
   2235   1.4      adam 		complete_ctx.remote_pathp = &remote_path;
   2236   1.4      adam 		el_set(el, EL_CLIENTDATA, (void*)&complete_ctx);
   2237   1.4      adam 		el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
   2238  1.13  christos 		/* enable ctrl-left-arrow and ctrl-right-arrow */
   2239  1.13  christos 		el_set(el, EL_BIND, "\\e[1;5C", "em-next-word", NULL);
   2240  1.27  christos 		el_set(el, EL_BIND, "\\e\\e[C", "em-next-word", NULL);
   2241  1.13  christos 		el_set(el, EL_BIND, "\\e[1;5D", "ed-prev-word", NULL);
   2242  1.13  christos 		el_set(el, EL_BIND, "\\e\\e[D", "ed-prev-word", NULL);
   2243  1.13  christos 		/* make ^w match ksh behaviour */
   2244  1.13  christos 		el_set(el, EL_BIND, "^w", "ed-delete-prev-word", NULL);
   2245   1.1  christos 	}
   2246   1.1  christos 
   2247   1.4      adam 	remote_path = do_realpath(conn, ".");
   2248   1.4      adam 	if (remote_path == NULL)
   2249   1.1  christos 		fatal("Need cwd");
   2250  1.23  christos 	startdir = xstrdup(remote_path);
   2251   1.1  christos 
   2252   1.1  christos 	if (file1 != NULL) {
   2253   1.1  christos 		dir = xstrdup(file1);
   2254   1.4      adam 		dir = make_absolute(dir, remote_path);
   2255   1.1  christos 
   2256   1.1  christos 		if (remote_is_dir(conn, dir) && file2 == NULL) {
   2257  1.12  christos 			if (!quiet)
   2258  1.19  christos 				mprintf("Changing to: %s\n", dir);
   2259   1.1  christos 			snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
   2260   1.4      adam 			if (parse_dispatch_command(conn, cmd,
   2261  1.26  christos 			    &remote_path, startdir, 1, 0) != 0) {
   2262  1.12  christos 				free(dir);
   2263  1.23  christos 				free(startdir);
   2264  1.12  christos 				free(remote_path);
   2265  1.12  christos 				free(conn);
   2266   1.1  christos 				return (-1);
   2267   1.1  christos 			}
   2268   1.1  christos 		} else {
   2269  1.11  christos 			/* XXX this is wrong wrt quoting */
   2270  1.12  christos 			snprintf(cmd, sizeof cmd, "get%s %s%s%s",
   2271  1.12  christos 			    global_aflag ? " -a" : "", dir,
   2272  1.12  christos 			    file2 == NULL ? "" : " ",
   2273  1.12  christos 			    file2 == NULL ? "" : file2);
   2274   1.4      adam 			err = parse_dispatch_command(conn, cmd,
   2275  1.26  christos 			    &remote_path, startdir, 1, 0);
   2276  1.12  christos 			free(dir);
   2277  1.23  christos 			free(startdir);
   2278  1.12  christos 			free(remote_path);
   2279  1.12  christos 			free(conn);
   2280   1.1  christos 			return (err);
   2281   1.1  christos 		}
   2282  1.12  christos 		free(dir);
   2283   1.1  christos 	}
   2284   1.1  christos 
   2285  1.14  christos 	setvbuf(stdout, NULL, _IOLBF, 0);
   2286  1.14  christos 	setvbuf(infile, NULL, _IOLBF, 0);
   2287   1.1  christos 
   2288   1.1  christos 	interactive = !batchmode && isatty(STDIN_FILENO);
   2289   1.1  christos 	err = 0;
   2290   1.1  christos 	for (;;) {
   2291  1.34  christos 		struct sigaction sa;
   2292   1.1  christos 		const char *line;
   2293   1.1  christos 		int count = 0;
   2294   1.1  christos 
   2295  1.34  christos 		interrupted = 0;
   2296  1.34  christos 		memset(&sa, 0, sizeof(sa));
   2297  1.34  christos 		sa.sa_handler = interactive ? read_interrupt : killchild;
   2298  1.34  christos 		if (sigaction(SIGINT, &sa, NULL) == -1) {
   2299  1.34  christos 			debug3("sigaction(%s): %s", strsignal(SIGINT),
   2300  1.34  christos 			    strerror(errno));
   2301  1.34  christos 			break;
   2302  1.34  christos 		}
   2303   1.1  christos 		if (el == NULL) {
   2304   1.1  christos 			if (interactive)
   2305   1.1  christos 				printf("sftp> ");
   2306   1.1  christos 			if (fgets(cmd, sizeof(cmd), infile) == NULL) {
   2307   1.1  christos 				if (interactive)
   2308   1.1  christos 					printf("\n");
   2309  1.34  christos 				if (interrupted)
   2310  1.34  christos 					continue;
   2311   1.1  christos 				break;
   2312   1.1  christos 			}
   2313   1.1  christos 		} else {
   2314   1.4      adam 			if ((line = el_gets(el, &count)) == NULL ||
   2315   1.4      adam 			    count <= 0) {
   2316   1.1  christos 				printf("\n");
   2317  1.33  christos 				if (interrupted)
   2318  1.33  christos 					continue;
   2319   1.1  christos 				break;
   2320   1.1  christos 			}
   2321   1.1  christos 			history(hl, &hev, H_ENTER, line);
   2322   1.1  christos 			if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
   2323   1.1  christos 				fprintf(stderr, "Error: input line too long\n");
   2324   1.1  christos 				continue;
   2325   1.1  christos 			}
   2326   1.1  christos 		}
   2327   1.1  christos 
   2328  1.26  christos 		cmd[strcspn(cmd, "\n")] = '\0';
   2329   1.1  christos 
   2330   1.1  christos 		/* Handle user interrupts gracefully during commands */
   2331   1.1  christos 		interrupted = 0;
   2332  1.28  christos 		ssh_signal(SIGINT, cmd_interrupt);
   2333   1.1  christos 
   2334   1.4      adam 		err = parse_dispatch_command(conn, cmd, &remote_path,
   2335  1.26  christos 		    startdir, batchmode, !interactive && el == NULL);
   2336   1.1  christos 		if (err != 0)
   2337   1.1  christos 			break;
   2338   1.1  christos 	}
   2339  1.28  christos 	ssh_signal(SIGCHLD, SIG_DFL);
   2340  1.12  christos 	free(remote_path);
   2341  1.23  christos 	free(startdir);
   2342  1.12  christos 	free(conn);
   2343   1.1  christos 
   2344   1.1  christos 	if (el != NULL)
   2345   1.1  christos 		el_end(el);
   2346   1.1  christos 
   2347   1.1  christos 	/* err == 1 signifies normal "quit" exit */
   2348   1.1  christos 	return (err >= 0 ? 0 : -1);
   2349   1.1  christos }
   2350   1.1  christos 
   2351   1.1  christos static void
   2352   1.7  christos connect_to_server(const char *path, char **args, int *in, int *out)
   2353   1.1  christos {
   2354  1.35  christos 	int c_in, c_out, inout[2];
   2355   1.1  christos 
   2356   1.1  christos 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
   2357   1.1  christos 		fatal("socketpair: %s", strerror(errno));
   2358   1.1  christos 	*in = *out = inout[0];
   2359   1.1  christos 	c_in = c_out = inout[1];
   2360   1.1  christos 
   2361   1.1  christos 	if ((sshpid = fork()) == -1)
   2362   1.1  christos 		fatal("fork: %s", strerror(errno));
   2363   1.1  christos 	else if (sshpid == 0) {
   2364   1.1  christos 		if ((dup2(c_in, STDIN_FILENO) == -1) ||
   2365   1.1  christos 		    (dup2(c_out, STDOUT_FILENO) == -1)) {
   2366   1.1  christos 			fprintf(stderr, "dup2: %s\n", strerror(errno));
   2367   1.1  christos 			_exit(1);
   2368   1.1  christos 		}
   2369   1.1  christos 		close(*in);
   2370   1.1  christos 		close(*out);
   2371   1.1  christos 		close(c_in);
   2372   1.1  christos 		close(c_out);
   2373   1.1  christos 
   2374   1.1  christos 		/*
   2375   1.1  christos 		 * The underlying ssh is in the same process group, so we must
   2376   1.1  christos 		 * ignore SIGINT if we want to gracefully abort commands,
   2377   1.1  christos 		 * otherwise the signal will make it to the ssh process and
   2378   1.4      adam 		 * kill it too.  Contrawise, since sftp sends SIGTERMs to the
   2379   1.4      adam 		 * underlying ssh, it must *not* ignore that signal.
   2380   1.1  christos 		 */
   2381  1.28  christos 		ssh_signal(SIGINT, SIG_IGN);
   2382  1.28  christos 		ssh_signal(SIGTERM, SIG_DFL);
   2383   1.1  christos 		execvp(path, args);
   2384   1.1  christos 		fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
   2385   1.1  christos 		_exit(1);
   2386   1.1  christos 	}
   2387   1.1  christos 
   2388  1.28  christos 	ssh_signal(SIGTERM, killchild);
   2389  1.28  christos 	ssh_signal(SIGINT, killchild);
   2390  1.28  christos 	ssh_signal(SIGHUP, killchild);
   2391  1.28  christos 	ssh_signal(SIGTSTP, suspchild);
   2392  1.28  christos 	ssh_signal(SIGTTIN, suspchild);
   2393  1.28  christos 	ssh_signal(SIGTTOU, suspchild);
   2394  1.28  christos 	ssh_signal(SIGCHLD, sigchld_handler);
   2395   1.1  christos 	close(c_in);
   2396   1.1  christos 	close(c_out);
   2397   1.1  christos }
   2398   1.1  christos 
   2399   1.8     joerg __dead static void
   2400   1.1  christos usage(void)
   2401   1.1  christos {
   2402   1.1  christos 	extern char *__progname;
   2403   1.1  christos 
   2404   1.1  christos 	fprintf(stderr,
   2405  1.30  christos 	    "usage: %s [-46AaCfNpqrv] [-B buffer_size] [-b batchfile] [-c cipher]\n"
   2406  1.36  christos 	    "          [-D sftp_server_command] [-F ssh_config] [-i identity_file]\n"
   2407  1.26  christos 	    "          [-J destination] [-l limit] [-o ssh_option] [-P port]\n"
   2408  1.26  christos 	    "          [-R num_requests] [-S program] [-s subsystem | sftp_server]\n"
   2409  1.26  christos 	    "          destination\n",
   2410  1.23  christos 	    __progname);
   2411   1.1  christos 	exit(1);
   2412   1.1  christos }
   2413   1.1  christos 
   2414   1.1  christos int
   2415   1.1  christos main(int argc, char **argv)
   2416   1.1  christos {
   2417  1.36  christos 	int r, in, out, ch, err, tmp, port = -1, noisy = 0;
   2418  1.36  christos 	char *host = NULL, *user, *cp, **cpp, *file1 = NULL, *file2 = NULL;
   2419  1.29  christos 	int debug_level = 0;
   2420  1.31  christos 	const char *sftp_server = NULL;
   2421   1.7  christos 	const char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
   2422   1.7  christos 	const char *errstr;
   2423   1.1  christos 	LogLevel ll = SYSLOG_LEVEL_INFO;
   2424   1.1  christos 	arglist args;
   2425   1.1  christos 	extern int optind;
   2426   1.1  christos 	extern char *optarg;
   2427   1.4      adam 	struct sftp_conn *conn;
   2428  1.32  christos 	size_t copy_buffer_len = 0;
   2429  1.32  christos 	size_t num_requests = 0;
   2430   1.7  christos 	long long limit_kbps = 0;
   2431   1.1  christos 
   2432   1.1  christos 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
   2433   1.1  christos 	sanitise_stdfd();
   2434  1.12  christos 	setlocale(LC_CTYPE, "");
   2435   1.1  christos 
   2436   1.1  christos 	memset(&args, '\0', sizeof(args));
   2437   1.1  christos 	args.list = NULL;
   2438   1.1  christos 	addargs(&args, "%s", ssh_program);
   2439   1.1  christos 	addargs(&args, "-oForwardX11 no");
   2440   1.1  christos 	addargs(&args, "-oPermitLocalCommand no");
   2441   1.1  christos 	addargs(&args, "-oClearAllForwardings yes");
   2442   1.1  christos 
   2443   1.1  christos 	ll = SYSLOG_LEVEL_INFO;
   2444   1.1  christos 	infile = stdin;
   2445   1.1  christos 
   2446   1.4      adam 	while ((ch = getopt(argc, argv,
   2447  1.30  christos 	    "1246AafhNpqrvCc:D:i:l:o:s:S:b:B:F:J:P:R:")) != -1) {
   2448   1.1  christos 		switch (ch) {
   2449   1.4      adam 		/* Passed through to ssh(1) */
   2450  1.30  christos 		case 'A':
   2451   1.4      adam 		case '4':
   2452   1.4      adam 		case '6':
   2453   1.1  christos 		case 'C':
   2454   1.4      adam 			addargs(&args, "-%c", ch);
   2455   1.4      adam 			break;
   2456   1.4      adam 		/* Passed through to ssh(1) with argument */
   2457   1.4      adam 		case 'F':
   2458  1.26  christos 		case 'J':
   2459   1.4      adam 		case 'c':
   2460   1.4      adam 		case 'i':
   2461   1.4      adam 		case 'o':
   2462   1.4      adam 			addargs(&args, "-%c", ch);
   2463   1.4      adam 			addargs(&args, "%s", optarg);
   2464   1.4      adam 			break;
   2465   1.4      adam 		case 'q':
   2466  1.12  christos 			ll = SYSLOG_LEVEL_ERROR;
   2467  1.12  christos 			quiet = 1;
   2468   1.4      adam 			showprogress = 0;
   2469   1.4      adam 			addargs(&args, "-%c", ch);
   2470   1.4      adam 			break;
   2471   1.4      adam 		case 'P':
   2472  1.23  christos 			port = a2port(optarg);
   2473  1.23  christos 			if (port <= 0)
   2474  1.23  christos 				fatal("Bad port \"%s\"\n", optarg);
   2475   1.1  christos 			break;
   2476   1.1  christos 		case 'v':
   2477   1.1  christos 			if (debug_level < 3) {
   2478   1.1  christos 				addargs(&args, "-v");
   2479   1.1  christos 				ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
   2480   1.1  christos 			}
   2481   1.1  christos 			debug_level++;
   2482   1.1  christos 			break;
   2483   1.1  christos 		case '1':
   2484  1.29  christos 			fatal("SSH protocol v.1 is no longer supported");
   2485   1.1  christos 			break;
   2486   1.4      adam 		case '2':
   2487  1.29  christos 			/* accept silently */
   2488   1.1  christos 			break;
   2489  1.12  christos 		case 'a':
   2490  1.12  christos 			global_aflag = 1;
   2491  1.12  christos 			break;
   2492   1.4      adam 		case 'B':
   2493   1.4      adam 			copy_buffer_len = strtol(optarg, &cp, 10);
   2494   1.4      adam 			if (copy_buffer_len == 0 || *cp != '\0')
   2495   1.4      adam 				fatal("Invalid buffer size \"%s\"", optarg);
   2496   1.1  christos 			break;
   2497   1.1  christos 		case 'b':
   2498   1.1  christos 			if (batchmode)
   2499   1.1  christos 				fatal("Batch file already specified.");
   2500   1.1  christos 
   2501   1.1  christos 			/* Allow "-" as stdin */
   2502   1.1  christos 			if (strcmp(optarg, "-") != 0 &&
   2503   1.1  christos 			    (infile = fopen(optarg, "r")) == NULL)
   2504   1.1  christos 				fatal("%s (%s).", strerror(errno), optarg);
   2505   1.1  christos 			showprogress = 0;
   2506  1.12  christos 			quiet = batchmode = 1;
   2507   1.1  christos 			addargs(&args, "-obatchmode yes");
   2508   1.1  christos 			break;
   2509  1.13  christos 		case 'f':
   2510  1.13  christos 			global_fflag = 1;
   2511  1.13  christos 			break;
   2512  1.29  christos 		case 'N':
   2513  1.29  christos 			noisy = 1; /* Used to clear quiet mode after getopt */
   2514  1.29  christos 			break;
   2515   1.4      adam 		case 'p':
   2516   1.4      adam 			global_pflag = 1;
   2517   1.4      adam 			break;
   2518   1.4      adam 		case 'D':
   2519   1.1  christos 			sftp_direct = optarg;
   2520   1.1  christos 			break;
   2521   1.7  christos 		case 'l':
   2522   1.7  christos 			limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
   2523   1.7  christos 			    &errstr);
   2524   1.7  christos 			if (errstr != NULL)
   2525   1.7  christos 				usage();
   2526   1.7  christos 			limit_kbps *= 1024; /* kbps */
   2527   1.7  christos 			break;
   2528   1.4      adam 		case 'r':
   2529   1.4      adam 			global_rflag = 1;
   2530   1.1  christos 			break;
   2531   1.1  christos 		case 'R':
   2532   1.1  christos 			num_requests = strtol(optarg, &cp, 10);
   2533   1.1  christos 			if (num_requests == 0 || *cp != '\0')
   2534   1.1  christos 				fatal("Invalid number of requests \"%s\"",
   2535   1.1  christos 				    optarg);
   2536   1.1  christos 			break;
   2537   1.4      adam 		case 's':
   2538   1.4      adam 			sftp_server = optarg;
   2539   1.4      adam 			break;
   2540   1.4      adam 		case 'S':
   2541   1.4      adam 			ssh_program = optarg;
   2542   1.4      adam 			replacearg(&args, 0, "%s", ssh_program);
   2543   1.4      adam 			break;
   2544   1.1  christos 		case 'h':
   2545   1.1  christos 		default:
   2546   1.1  christos 			usage();
   2547   1.1  christos 		}
   2548   1.1  christos 	}
   2549   1.1  christos 
   2550  1.30  christos 	/* Do this last because we want the user to be able to override it */
   2551  1.30  christos 	addargs(&args, "-oForwardAgent no");
   2552  1.30  christos 
   2553   1.1  christos 	if (!isatty(STDERR_FILENO))
   2554   1.1  christos 		showprogress = 0;
   2555   1.1  christos 
   2556  1.29  christos 	if (noisy)
   2557  1.29  christos 		quiet = 0;
   2558  1.29  christos 
   2559   1.1  christos 	log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
   2560   1.1  christos 
   2561   1.1  christos 	if (sftp_direct == NULL) {
   2562   1.1  christos 		if (optind == argc || argc > (optind + 2))
   2563   1.1  christos 			usage();
   2564  1.23  christos 		argv += optind;
   2565   1.1  christos 
   2566  1.23  christos 		switch (parse_uri("sftp", *argv, &user, &host, &tmp, &file1)) {
   2567  1.23  christos 		case -1:
   2568  1.23  christos 			usage();
   2569  1.23  christos 			break;
   2570  1.23  christos 		case 0:
   2571  1.23  christos 			if (tmp != -1)
   2572  1.23  christos 				port = tmp;
   2573  1.23  christos 			break;
   2574  1.23  christos 		default:
   2575  1.27  christos 			/* Try with user, host and path. */
   2576  1.23  christos 			if (parse_user_host_path(*argv, &user, &host,
   2577  1.27  christos 			    &file1) == 0)
   2578  1.27  christos 				break;
   2579  1.27  christos 			/* Try with user and host. */
   2580  1.27  christos 			if (parse_user_host_port(*argv, &user, &host, NULL)
   2581  1.27  christos 			    == 0)
   2582  1.27  christos 				break;
   2583  1.27  christos 			/* Treat as a plain hostname. */
   2584  1.27  christos 			host = xstrdup(*argv);
   2585  1.27  christos 			host = cleanhostname(host);
   2586  1.23  christos 			break;
   2587   1.1  christos 		}
   2588  1.23  christos 		file2 = *(argv + 1);
   2589   1.1  christos 
   2590   1.1  christos 		if (!*host) {
   2591   1.1  christos 			fprintf(stderr, "Missing hostname\n");
   2592   1.1  christos 			usage();
   2593   1.1  christos 		}
   2594   1.1  christos 
   2595  1.23  christos 		if (port != -1)
   2596  1.23  christos 			addargs(&args, "-oPort %d", port);
   2597  1.23  christos 		if (user != NULL) {
   2598  1.23  christos 			addargs(&args, "-l");
   2599  1.23  christos 			addargs(&args, "%s", user);
   2600  1.23  christos 		}
   2601   1.1  christos 
   2602   1.1  christos 		/* no subsystem if the server-spec contains a '/' */
   2603   1.1  christos 		if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
   2604   1.1  christos 			addargs(&args, "-s");
   2605   1.1  christos 
   2606   1.4      adam 		addargs(&args, "--");
   2607   1.1  christos 		addargs(&args, "%s", host);
   2608   1.1  christos 		addargs(&args, "%s", (sftp_server != NULL ?
   2609   1.1  christos 		    sftp_server : "sftp"));
   2610   1.1  christos 
   2611   1.1  christos 		connect_to_server(ssh_program, args.list, &in, &out);
   2612   1.1  christos 	} else {
   2613  1.36  christos 		if ((r = argv_split(sftp_direct, &tmp, &cpp, 1)) != 0)
   2614  1.36  christos 			fatal_r(r, "Parse -D arguments");
   2615  1.36  christos 		if (cpp[0] == 0)
   2616  1.36  christos 			fatal("No sftp server specified via -D");
   2617  1.36  christos 		connect_to_server(cpp[0], cpp, &in, &out);
   2618  1.36  christos 		argv_free(cpp, tmp);
   2619   1.1  christos 	}
   2620   1.1  christos 	freeargs(&args);
   2621   1.1  christos 
   2622   1.7  christos 	conn = do_init(in, out, copy_buffer_len, num_requests, limit_kbps);
   2623   1.4      adam 	if (conn == NULL)
   2624   1.4      adam 		fatal("Couldn't initialise connection to server");
   2625   1.4      adam 
   2626  1.12  christos 	if (!quiet) {
   2627   1.4      adam 		if (sftp_direct == NULL)
   2628   1.4      adam 			fprintf(stderr, "Connected to %s.\n", host);
   2629   1.4      adam 		else
   2630   1.4      adam 			fprintf(stderr, "Attached to %s.\n", sftp_direct);
   2631   1.4      adam 	}
   2632   1.4      adam 
   2633   1.4      adam 	err = interactive_loop(conn, file1, file2);
   2634   1.1  christos 
   2635   1.1  christos 	close(in);
   2636   1.1  christos 	close(out);
   2637   1.1  christos 	if (batchmode)
   2638   1.1  christos 		fclose(infile);
   2639   1.1  christos 
   2640  1.24  christos 	while (waitpid(sshpid, NULL, 0) == -1 && sshpid > 1)
   2641   1.1  christos 		if (errno != EINTR)
   2642   1.1  christos 			fatal("Couldn't wait for ssh process: %s",
   2643   1.1  christos 			    strerror(errno));
   2644   1.1  christos 
   2645   1.1  christos 	exit(err == 0 ? 0 : 1);
   2646   1.1  christos }
   2647