On Wed, 21 Sep 2011, Loganaden Velvindron wrote:
> s/similar/A little bit like
>
> The diff has issues with stuff like sftp 127.0.0.1. I've
> fixed it.
The way I'd like to see the sftp commandline go is to become mostly
compatible with scp(1). So:
sftp local [local...] remote:/path # do an upload
sftp remote:/path [remote:path...] local # do a download
-d
> Index: src/usr.bin/ssh/sftp.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/ssh/sftp.c,v
> retrieving revision 1.132
> diff -u -p -r1.132 sftp.c
> --- src/usr.bin/ssh/sftp.c 4 Dec 2010 00:18:01 -0000 1.132
> +++ src/usr.bin/ssh/sftp.c 21 Sep 2011 07:30:08 -0000
> @@ -25,6 +25,7 @@
>
> #include <ctype.h>
> #include <errno.h>
> +#include <fcntl.h>
> #include <glob.h>
> #include <histedit.h>
> #include <paths.h>
> @@ -175,7 +176,7 @@ static const struct CMD cmds[] = {
> { NULL, -1, -1 }
> };
>
> -int interactive_loop(struct sftp_conn *, char *file1, char *file2);
> +int interactive_loop(struct sftp_conn *, char *file1, char *file2, int);
>
> /* ARGSUSED */
> static void
> @@ -1834,7 +1835,7 @@ complete(EditLine *el, int ch)
> }
>
> int
> -interactive_loop(struct sftp_conn *conn, char *file1, char *file2)
> +interactive_loop(struct sftp_conn *conn, char *file1, char *file2, int tflag)
> {
> char *remote_path;
> char *dir = NULL;
> @@ -1889,10 +1890,19 @@ interactive_loop(struct sftp_conn *conn,
> }
> } else {
> if (file2 == NULL)
> - snprintf(cmd, sizeof cmd, "get %s", dir);
> + if (tflag == 0)
> + snprintf(cmd, sizeof cmd, "get %s",
> + dir);
> + else
> + snprintf(cmd, sizeof cmd, "put %s",
> + dir);
> else
> - snprintf(cmd, sizeof cmd, "get %s %s", dir,
> - file2);
> + if (tflag == 0)
> + snprintf(cmd, sizeof cmd, "get %s %s",
> + dir, file2);
> + else
> + snprintf(cmd, sizeof cmd, "put %s %s",
> + dir, file2);
>
> err = parse_dispatch_command(conn, cmd,
> &remote_path, 1);
> @@ -2034,12 +2044,13 @@ usage(void)
> int
> main(int argc, char **argv)
> {
> - int in, out, ch, err;
> + int in, out, ch, tflag = 0, fd, err;
> char *host = NULL, *userhost, *cp, *file2 = NULL;
> int debug_level = 0, sshver = 2;
> char *file1 = NULL, *sftp_server = NULL;
> char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
> const char *errstr;
> + char path[MAXPATHLEN];
> LogLevel ll = SYSLOG_LEVEL_INFO;
> arglist args;
> extern int optind;
> @@ -2163,9 +2174,24 @@ main(int argc, char **argv)
> if (optind == argc || argc > (optind + 2))
> usage();
>
> - userhost = xstrdup(argv[optind]);
> - file2 = argv[optind+1];
> -
> + if ((strrchr(argv[optind], '@') != NULL) ||
> + (argc == optind + 1) ||
> + (strrchr(argv[optind], ':') != NULL)) {
> + userhost = xstrdup(argv[optind]);
> + file2 = argv[optind + 1];
> + }
> + else {
> + tflag = 1;
> + userhost = xstrdup(argv[optind + 1]);
> + file2 = argv[optind];
> + if ((fd = open(file2, O_RDONLY | O_NONBLOCK, 0)) < 0) {
> + fprintf(stderr, "\n%s\n",strerror(errno));
> + _exit(1);
> + }
> + if ((getcwd(path, sizeof(path)) != NULL) &&
> + (file2[0] != '/'))
> + file2 = path_append(path, file2);
> + }
> if ((host = strrchr(userhost, '@')) == NULL)
> host = userhost;
> else {
> @@ -2219,9 +2245,10 @@ main(int argc, char **argv)
> else
> fprintf(stderr, "Attached to %s.\n", sftp_direct);
> }
> -
> - err = interactive_loop(conn, file1, file2);
> -
> + if (tflag == 0)
> + err = interactive_loop(conn, file1, file2, 0);
> + else
> + err = interactive_loop(conn, file2, file1, 1);
> close(in);
> close(out);
> if (batchmode)