blob: b13e5da5d5b3e1b194faaa94ce927fa392a4df0e [file] [log] [blame]
Damien Miller33804262001-02-04 23:20:18 +11001/*
Damien Miller3db5f532002-02-13 14:10:32 +11002 * Copyright (c) 2001,2002 Damien Miller. All rights reserved.
Damien Miller33804262001-02-04 23:20:18 +11003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
Damien Millerd7686fd2001-02-10 00:40:03 +110025/* XXX: globbed ls */
Damien Miller33804262001-02-04 23:20:18 +110026/* XXX: recursive operations */
27
28#include "includes.h"
Ben Lindstromb1f483f2002-06-23 21:27:18 +000029RCSID("$OpenBSD: sftp-int.c,v 1.47 2002/06/23 09:30:14 deraadt Exp $");
Damien Miller4870afd2001-03-14 10:27:09 +110030
Damien Miller33804262001-02-04 23:20:18 +110031#include "buffer.h"
32#include "xmalloc.h"
33#include "log.h"
34#include "pathnames.h"
35
36#include "sftp.h"
37#include "sftp-common.h"
Damien Miller4870afd2001-03-14 10:27:09 +110038#include "sftp-glob.h"
Damien Miller33804262001-02-04 23:20:18 +110039#include "sftp-client.h"
40#include "sftp-int.h"
41
Damien Miller058316f2001-03-08 10:08:49 +110042/* File to read commands from */
43extern FILE *infile;
44
Damien Miller8829d362002-02-08 22:04:05 +110045/* Size of buffer used when copying files */
46extern size_t copy_buffer_len;
47
Damien Miller16a13332002-02-13 14:03:56 +110048/* Number of concurrent outstanding requests */
49extern int num_requests;
50
Damien Miller33804262001-02-04 23:20:18 +110051/* Seperators for interactive commands */
52#define WHITESPACE " \t\r\n"
53
54/* Commands for interactive mode */
55#define I_CHDIR 1
56#define I_CHGRP 2
57#define I_CHMOD 3
58#define I_CHOWN 4
59#define I_GET 5
60#define I_HELP 6
61#define I_LCHDIR 7
62#define I_LLS 8
63#define I_LMKDIR 9
64#define I_LPWD 10
65#define I_LS 11
66#define I_LUMASK 12
67#define I_MKDIR 13
68#define I_PUT 14
69#define I_PWD 15
70#define I_QUIT 16
71#define I_RENAME 17
72#define I_RM 18
73#define I_RMDIR 19
74#define I_SHELL 20
Damien Miller058316f2001-03-08 10:08:49 +110075#define I_SYMLINK 21
Ben Lindstromf78682d2001-03-14 21:26:27 +000076#define I_VERSION 22
Damien Miller33804262001-02-04 23:20:18 +110077
78struct CMD {
Damien Miller33804262001-02-04 23:20:18 +110079 const char *c;
Kevin Steves62c45db2001-02-05 13:42:43 +000080 const int n;
Damien Miller33804262001-02-04 23:20:18 +110081};
82
83const struct CMD cmds[] = {
Ben Lindstrom59e12492001-08-15 23:22:56 +000084 { "bye", I_QUIT },
Damien Millerd7686fd2001-02-10 00:40:03 +110085 { "cd", I_CHDIR },
86 { "chdir", I_CHDIR },
87 { "chgrp", I_CHGRP },
88 { "chmod", I_CHMOD },
89 { "chown", I_CHOWN },
90 { "dir", I_LS },
91 { "exit", I_QUIT },
92 { "get", I_GET },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +000093 { "mget", I_GET },
Damien Millerd7686fd2001-02-10 00:40:03 +110094 { "help", I_HELP },
95 { "lcd", I_LCHDIR },
96 { "lchdir", I_LCHDIR },
97 { "lls", I_LLS },
98 { "lmkdir", I_LMKDIR },
Damien Miller058316f2001-03-08 10:08:49 +110099 { "ln", I_SYMLINK },
Damien Millerd7686fd2001-02-10 00:40:03 +1100100 { "lpwd", I_LPWD },
101 { "ls", I_LS },
102 { "lumask", I_LUMASK },
103 { "mkdir", I_MKDIR },
104 { "put", I_PUT },
Ben Lindstrom23d9a6d2001-04-11 23:05:17 +0000105 { "mput", I_PUT },
Damien Millerd7686fd2001-02-10 00:40:03 +1100106 { "pwd", I_PWD },
107 { "quit", I_QUIT },
108 { "rename", I_RENAME },
109 { "rm", I_RM },
110 { "rmdir", I_RMDIR },
Damien Miller058316f2001-03-08 10:08:49 +1100111 { "symlink", I_SYMLINK },
Ben Lindstromf78682d2001-03-14 21:26:27 +0000112 { "version", I_VERSION },
Kevin Steves62c45db2001-02-05 13:42:43 +0000113 { "!", I_SHELL },
114 { "?", I_HELP },
115 { NULL, -1}
Damien Miller33804262001-02-04 23:20:18 +1100116};
117
Ben Lindstrombba81212001-06-25 05:01:22 +0000118static void
Damien Miller33804262001-02-04 23:20:18 +1100119help(void)
120{
121 printf("Available commands:\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100122 printf("cd path Change remote directory to 'path'\n");
123 printf("lcd path Change local directory to 'path'\n");
124 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
125 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
126 printf("chown own path Change owner of file 'path' to 'own'\n");
127 printf("help Display this help text\n");
128 printf("get remote-path [local-path] Download file\n");
129 printf("lls [ls-options [path]] Display local directory listing\n");
Damien Miller058316f2001-03-08 10:08:49 +1100130 printf("ln oldpath newpath Symlink remote file\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100131 printf("lmkdir path Create local directory\n");
132 printf("lpwd Print local working directory\n");
133 printf("ls [path] Display remote directory listing\n");
134 printf("lumask umask Set local umask to 'umask'\n");
135 printf("mkdir path Create remote directory\n");
136 printf("put local-path [remote-path] Upload file\n");
137 printf("pwd Display remote working directory\n");
138 printf("exit Quit sftp\n");
139 printf("quit Quit sftp\n");
140 printf("rename oldpath newpath Rename remote file\n");
141 printf("rmdir path Remove remote directory\n");
142 printf("rm path Delete remote file\n");
Damien Miller058316f2001-03-08 10:08:49 +1100143 printf("symlink oldpath newpath Symlink remote file\n");
Ben Lindstromf78682d2001-03-14 21:26:27 +0000144 printf("version Show SFTP version\n");
Damien Miller33804262001-02-04 23:20:18 +1100145 printf("!command Execute 'command' in local shell\n");
146 printf("! Escape to local shell\n");
Damien Millerd7686fd2001-02-10 00:40:03 +1100147 printf("? Synonym for help\n");
Damien Miller33804262001-02-04 23:20:18 +1100148}
149
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static void
Damien Miller33804262001-02-04 23:20:18 +1100151local_do_shell(const char *args)
152{
Ben Lindstrom206941f2001-04-15 14:27:16 +0000153 int status;
Damien Miller33804262001-02-04 23:20:18 +1100154 char *shell;
155 pid_t pid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000156
Damien Miller33804262001-02-04 23:20:18 +1100157 if (!*args)
158 args = NULL;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000159
Damien Miller33804262001-02-04 23:20:18 +1100160 if ((shell = getenv("SHELL")) == NULL)
161 shell = _PATH_BSHELL;
162
163 if ((pid = fork()) == -1)
164 fatal("Couldn't fork: %s", strerror(errno));
165
166 if (pid == 0) {
167 /* XXX: child has pipe fds to ssh subproc open - issue? */
168 if (args) {
169 debug3("Executing %s -c \"%s\"", shell, args);
Damien Millerefb1edf2001-07-14 12:19:36 +1000170 execl(shell, shell, "-c", args, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100171 } else {
172 debug3("Executing %s", shell);
Damien Millerefb1edf2001-07-14 12:19:36 +1000173 execl(shell, shell, (char *)NULL);
Damien Miller33804262001-02-04 23:20:18 +1100174 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000175 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
Damien Miller33804262001-02-04 23:20:18 +1100176 strerror(errno));
177 _exit(1);
178 }
Ben Lindstrom47fd8112002-04-02 20:48:19 +0000179 while (waitpid(pid, &status, 0) == -1)
180 if (errno != EINTR)
181 fatal("Couldn't wait for child: %s", strerror(errno));
Damien Miller33804262001-02-04 23:20:18 +1100182 if (!WIFEXITED(status))
183 error("Shell exited abormally");
184 else if (WEXITSTATUS(status))
185 error("Shell exited with status %d", WEXITSTATUS(status));
186}
187
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static void
Damien Miller33804262001-02-04 23:20:18 +1100189local_do_ls(const char *args)
190{
191 if (!args || !*args)
Damien Millerd7686fd2001-02-10 00:40:03 +1100192 local_do_shell(_PATH_LS);
Damien Miller33804262001-02-04 23:20:18 +1100193 else {
Damien Millerd7686fd2001-02-10 00:40:03 +1100194 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
195 char *buf = xmalloc(len);
Damien Miller33804262001-02-04 23:20:18 +1100196
197 /* XXX: quoting - rip quoting code from ftp? */
Damien Millerd7686fd2001-02-10 00:40:03 +1100198 snprintf(buf, len, _PATH_LS " %s", args);
Damien Miller33804262001-02-04 23:20:18 +1100199 local_do_shell(buf);
Damien Millerd7686fd2001-02-10 00:40:03 +1100200 xfree(buf);
Damien Miller33804262001-02-04 23:20:18 +1100201 }
202}
203
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static char *
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000205path_append(char *p1, char *p2)
206{
207 char *ret;
Ben Lindstromcf00df62001-03-17 00:37:31 +0000208 int len = strlen(p1) + strlen(p2) + 2;
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000209
Ben Lindstromcf00df62001-03-17 00:37:31 +0000210 ret = xmalloc(len);
211 strlcpy(ret, p1, len);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100212 if (strcmp(p1, "/") != 0)
Ben Lindstrom95148e32001-08-06 21:30:53 +0000213 strlcat(ret, "/", len);
Ben Lindstromcf00df62001-03-17 00:37:31 +0000214 strlcat(ret, p2, len);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000215
216 return(ret);
217}
218
Ben Lindstrombba81212001-06-25 05:01:22 +0000219static char *
Damien Miller33804262001-02-04 23:20:18 +1100220make_absolute(char *p, char *pwd)
221{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000222 char *abs;
Damien Miller33804262001-02-04 23:20:18 +1100223
224 /* Derelativise */
225 if (p && p[0] != '/') {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000226 abs = path_append(pwd, p);
Damien Miller33804262001-02-04 23:20:18 +1100227 xfree(p);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000228 return(abs);
229 } else
230 return(p);
231}
232
Ben Lindstrombba81212001-06-25 05:01:22 +0000233static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000234infer_path(const char *p, char **ifp)
235{
236 char *cp;
237
238 cp = strrchr(p, '/');
239 if (cp == NULL) {
240 *ifp = xstrdup(p);
241 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100242 }
243
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000244 if (!cp[1]) {
245 error("Invalid path");
246 return(-1);
247 }
248
249 *ifp = xstrdup(cp + 1);
250 return(0);
Damien Miller33804262001-02-04 23:20:18 +1100251}
252
Ben Lindstrombba81212001-06-25 05:01:22 +0000253static int
Damien Miller33804262001-02-04 23:20:18 +1100254parse_getput_flags(const char **cpp, int *pflag)
255{
256 const char *cp = *cpp;
257
258 /* Check for flags */
259 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100260 switch (cp[1]) {
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000261 case 'p':
Damien Miller33804262001-02-04 23:20:18 +1100262 case 'P':
263 *pflag = 1;
264 break;
265 default:
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +0000266 error("Invalid flag -%c", cp[1]);
Damien Miller33804262001-02-04 23:20:18 +1100267 return(-1);
268 }
269 cp += 2;
270 *cpp = cp + strspn(cp, WHITESPACE);
271 }
272
273 return(0);
274}
275
Ben Lindstrombba81212001-06-25 05:01:22 +0000276static int
Damien Miller33804262001-02-04 23:20:18 +1100277get_pathname(const char **cpp, char **path)
278{
Damien Millerd7686fd2001-02-10 00:40:03 +1100279 const char *cp = *cpp, *end;
280 char quot;
Damien Miller33804262001-02-04 23:20:18 +1100281 int i;
282
283 cp += strspn(cp, WHITESPACE);
284 if (!*cp) {
285 *cpp = cp;
286 *path = NULL;
Damien Millerd7686fd2001-02-10 00:40:03 +1100287 return (0);
Damien Miller33804262001-02-04 23:20:18 +1100288 }
289
290 /* Check for quoted filenames */
291 if (*cp == '\"' || *cp == '\'') {
Damien Millerd7686fd2001-02-10 00:40:03 +1100292 quot = *cp++;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000293
Damien Millerd7686fd2001-02-10 00:40:03 +1100294 end = strchr(cp, quot);
295 if (end == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100296 error("Unterminated quote");
Damien Millerd7686fd2001-02-10 00:40:03 +1100297 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100298 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100299 if (cp == end) {
Damien Miller33804262001-02-04 23:20:18 +1100300 error("Empty quotes");
Damien Millerd7686fd2001-02-10 00:40:03 +1100301 goto fail;
Damien Miller33804262001-02-04 23:20:18 +1100302 }
Damien Millerd7686fd2001-02-10 00:40:03 +1100303 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
304 } else {
305 /* Read to end of filename */
306 end = strpbrk(cp, WHITESPACE);
307 if (end == NULL)
308 end = strchr(cp, '\0');
309 *cpp = end + strspn(end, WHITESPACE);
Damien Miller33804262001-02-04 23:20:18 +1100310 }
311
Damien Millerd7686fd2001-02-10 00:40:03 +1100312 i = end - cp;
Damien Miller33804262001-02-04 23:20:18 +1100313
314 *path = xmalloc(i + 1);
315 memcpy(*path, cp, i);
316 (*path)[i] = '\0';
Damien Miller33804262001-02-04 23:20:18 +1100317 return(0);
Damien Millerd7686fd2001-02-10 00:40:03 +1100318
319 fail:
320 *path = NULL;
321 return (-1);
Damien Miller33804262001-02-04 23:20:18 +1100322}
323
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static int
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000325is_dir(char *path)
Damien Miller33804262001-02-04 23:20:18 +1100326{
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000327 struct stat sb;
Damien Miller33804262001-02-04 23:20:18 +1100328
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000329 /* XXX: report errors? */
330 if (stat(path, &sb) == -1)
Damien Miller33804262001-02-04 23:20:18 +1100331 return(0);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000332
333 return(sb.st_mode & S_IFDIR);
334}
335
Ben Lindstrombba81212001-06-25 05:01:22 +0000336static int
Damien Miller3db5f532002-02-13 14:10:32 +1100337remote_is_dir(struct sftp_conn *conn, char *path)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000338{
339 Attrib *a;
340
341 /* XXX: report errors? */
Damien Miller3db5f532002-02-13 14:10:32 +1100342 if ((a = do_stat(conn, path, 1)) == NULL)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000343 return(0);
344 if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
345 return(0);
346 return(a->perm & S_IFDIR);
347}
348
Ben Lindstrombba81212001-06-25 05:01:22 +0000349static int
Damien Miller3db5f532002-02-13 14:10:32 +1100350process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000351{
352 char *abs_src = NULL;
353 char *abs_dst = NULL;
354 char *tmp;
355 glob_t g;
356 int err = 0;
357 int i;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000358
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000359 abs_src = xstrdup(src);
360 abs_src = make_absolute(abs_src, pwd);
361
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000362 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000363 debug3("Looking up %s", abs_src);
Damien Miller3db5f532002-02-13 14:10:32 +1100364 if (remote_glob(conn, abs_src, 0, NULL, &g)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000365 error("File \"%s\" not found.", abs_src);
366 err = -1;
367 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100368 }
369
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000370 /* Only one match, dst may be file, directory or unspecified */
371 if (g.gl_pathv[0] && g.gl_matchc == 1) {
372 if (dst) {
373 /* If directory specified, append filename */
374 if (is_dir(dst)) {
375 if (infer_path(g.gl_pathv[0], &tmp)) {
376 err = 1;
377 goto out;
378 }
379 abs_dst = path_append(dst, tmp);
380 xfree(tmp);
381 } else
382 abs_dst = xstrdup(dst);
383 } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
384 err = -1;
385 goto out;
386 }
387 printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100388 err = do_download(conn, g.gl_pathv[0], abs_dst, pflag);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000389 goto out;
Damien Miller33804262001-02-04 23:20:18 +1100390 }
391
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000392 /* Multiple matches, dst may be directory or unspecified */
393 if (dst && !is_dir(dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000394 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000395 dst);
396 err = -1;
397 goto out;
398 }
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000399
Damien Miller9f0f5c62001-12-21 14:45:46 +1100400 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000401 if (infer_path(g.gl_pathv[i], &tmp)) {
402 err = -1;
403 goto out;
404 }
405 if (dst) {
406 abs_dst = path_append(dst, tmp);
407 xfree(tmp);
408 } else
409 abs_dst = tmp;
410
411 printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100412 if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000413 err = -1;
414 xfree(abs_dst);
415 abs_dst = NULL;
416 }
417
418out:
419 xfree(abs_src);
420 if (abs_dst)
421 xfree(abs_dst);
422 globfree(&g);
423 return(err);
424}
425
Ben Lindstrombba81212001-06-25 05:01:22 +0000426static int
Damien Miller3db5f532002-02-13 14:10:32 +1100427process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000428{
429 char *tmp_dst = NULL;
430 char *abs_dst = NULL;
431 char *tmp;
432 glob_t g;
433 int err = 0;
434 int i;
435
436 if (dst) {
437 tmp_dst = xstrdup(dst);
438 tmp_dst = make_absolute(tmp_dst, pwd);
439 }
440
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000441 memset(&g, 0, sizeof(g));
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000442 debug3("Looking up %s", src);
443 if (glob(src, 0, NULL, &g)) {
444 error("File \"%s\" not found.", src);
445 err = -1;
446 goto out;
447 }
448
449 /* Only one match, dst may be file, directory or unspecified */
450 if (g.gl_pathv[0] && g.gl_matchc == 1) {
451 if (tmp_dst) {
452 /* If directory specified, append filename */
Damien Miller3db5f532002-02-13 14:10:32 +1100453 if (remote_is_dir(conn, tmp_dst)) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000454 if (infer_path(g.gl_pathv[0], &tmp)) {
455 err = 1;
456 goto out;
457 }
458 abs_dst = path_append(tmp_dst, tmp);
459 xfree(tmp);
460 } else
461 abs_dst = xstrdup(tmp_dst);
Ben Lindstrom7527f8b2001-03-24 00:39:12 +0000462 } else {
463 if (infer_path(g.gl_pathv[0], &abs_dst)) {
464 err = -1;
465 goto out;
466 }
467 abs_dst = make_absolute(abs_dst, pwd);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000468 }
469 printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100470 err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag);
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000471 goto out;
472 }
473
474 /* Multiple matches, dst may be directory or unspecified */
Damien Miller3db5f532002-02-13 14:10:32 +1100475 if (tmp_dst && !remote_is_dir(conn, tmp_dst)) {
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000476 error("Multiple files match, but \"%s\" is not a directory",
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000477 tmp_dst);
478 err = -1;
479 goto out;
480 }
481
Damien Miller9f0f5c62001-12-21 14:45:46 +1100482 for (i = 0; g.gl_pathv[i]; i++) {
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000483 if (infer_path(g.gl_pathv[i], &tmp)) {
484 err = -1;
485 goto out;
486 }
487 if (tmp_dst) {
488 abs_dst = path_append(tmp_dst, tmp);
489 xfree(tmp);
490 } else
491 abs_dst = make_absolute(tmp, pwd);
492
493 printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
Damien Miller3db5f532002-02-13 14:10:32 +1100494 if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000495 err = -1;
496 }
497
498out:
499 if (abs_dst)
500 xfree(abs_dst);
501 if (tmp_dst)
502 xfree(tmp_dst);
503 return(err);
Damien Miller33804262001-02-04 23:20:18 +1100504}
505
Ben Lindstrombba81212001-06-25 05:01:22 +0000506static int
Damien Miller33804262001-02-04 23:20:18 +1100507parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
508 char **path1, char **path2)
509{
510 const char *cmd, *cp = *cpp;
Ben Lindstrom66904942001-02-15 03:19:56 +0000511 char *cp2;
Kevin Steves62c45db2001-02-05 13:42:43 +0000512 int base = 0;
Ben Lindstrom66904942001-02-15 03:19:56 +0000513 long l;
Damien Miller33804262001-02-04 23:20:18 +1100514 int i, cmdnum;
515
516 /* Skip leading whitespace */
517 cp = cp + strspn(cp, WHITESPACE);
518
519 /* Ignore blank lines */
520 if (!*cp)
521 return(-1);
522
523 /* Figure out which command we have */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100524 for (i = 0; cmds[i].c; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100525 int cmdlen = strlen(cmds[i].c);
526
527 /* Check for command followed by whitespace */
528 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
529 strchr(WHITESPACE, cp[cmdlen])) {
530 cp += cmdlen;
531 cp = cp + strspn(cp, WHITESPACE);
532 break;
533 }
534 }
535 cmdnum = cmds[i].n;
536 cmd = cmds[i].c;
537
538 /* Special case */
539 if (*cp == '!') {
540 cp++;
541 cmdnum = I_SHELL;
542 } else if (cmdnum == -1) {
543 error("Invalid command.");
544 return(-1);
545 }
546
547 /* Get arguments and parse flags */
548 *pflag = *n_arg = 0;
549 *path1 = *path2 = NULL;
550 switch (cmdnum) {
551 case I_GET:
552 case I_PUT:
553 if (parse_getput_flags(&cp, pflag))
554 return(-1);
555 /* Get first pathname (mandatory) */
556 if (get_pathname(&cp, path1))
557 return(-1);
558 if (*path1 == NULL) {
559 error("You must specify at least one path after a "
560 "%s command.", cmd);
561 return(-1);
562 }
563 /* Try to get second pathname (optional) */
564 if (get_pathname(&cp, path2))
565 return(-1);
Damien Miller33804262001-02-04 23:20:18 +1100566 break;
567 case I_RENAME:
Damien Miller058316f2001-03-08 10:08:49 +1100568 case I_SYMLINK:
Damien Miller33804262001-02-04 23:20:18 +1100569 if (get_pathname(&cp, path1))
570 return(-1);
571 if (get_pathname(&cp, path2))
572 return(-1);
573 if (!*path1 || !*path2) {
574 error("You must specify two paths after a %s "
575 "command.", cmd);
576 return(-1);
577 }
578 break;
579 case I_RM:
580 case I_MKDIR:
581 case I_RMDIR:
582 case I_CHDIR:
583 case I_LCHDIR:
584 case I_LMKDIR:
585 /* Get pathname (mandatory) */
586 if (get_pathname(&cp, path1))
587 return(-1);
588 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000589 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100590 cmd);
591 return(-1);
592 }
593 break;
594 case I_LS:
595 /* Path is optional */
596 if (get_pathname(&cp, path1))
597 return(-1);
598 break;
599 case I_LLS:
600 case I_SHELL:
601 /* Uses the rest of the line */
602 break;
603 case I_LUMASK:
Ben Lindstrom66904942001-02-15 03:19:56 +0000604 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100605 case I_CHMOD:
Kevin Steves62c45db2001-02-05 13:42:43 +0000606 base = 8;
Damien Miller33804262001-02-04 23:20:18 +1100607 case I_CHOWN:
608 case I_CHGRP:
609 /* Get numeric arg (mandatory) */
Ben Lindstrom66904942001-02-15 03:19:56 +0000610 l = strtol(cp, &cp2, base);
611 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
612 errno == ERANGE) || l < 0) {
Damien Miller33804262001-02-04 23:20:18 +1100613 error("You must supply a numeric argument "
614 "to the %s command.", cmd);
615 return(-1);
616 }
Ben Lindstrom66904942001-02-15 03:19:56 +0000617 cp = cp2;
618 *n_arg = l;
619 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
620 break;
621 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
Damien Miller33804262001-02-04 23:20:18 +1100622 error("You must supply a numeric argument "
623 "to the %s command.", cmd);
624 return(-1);
625 }
626 cp += strspn(cp, WHITESPACE);
627
628 /* Get pathname (mandatory) */
629 if (get_pathname(&cp, path1))
630 return(-1);
631 if (*path1 == NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000632 error("You must specify a path after a %s command.",
Damien Miller33804262001-02-04 23:20:18 +1100633 cmd);
634 return(-1);
635 }
636 break;
637 case I_QUIT:
638 case I_PWD:
639 case I_LPWD:
640 case I_HELP:
Ben Lindstromf78682d2001-03-14 21:26:27 +0000641 case I_VERSION:
Damien Miller33804262001-02-04 23:20:18 +1100642 break;
643 default:
644 fatal("Command not implemented");
645 }
646
647 *cpp = cp;
Damien Miller33804262001-02-04 23:20:18 +1100648 return(cmdnum);
649}
650
Ben Lindstrombba81212001-06-25 05:01:22 +0000651static int
Damien Miller3db5f532002-02-13 14:10:32 +1100652parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd)
Damien Miller33804262001-02-04 23:20:18 +1100653{
Damien Millerd7686fd2001-02-10 00:40:03 +1100654 char *path1, *path2, *tmp;
Damien Miller4870afd2001-03-14 10:27:09 +1100655 int pflag, cmdnum, i;
Damien Miller33804262001-02-04 23:20:18 +1100656 unsigned long n_arg;
657 Attrib a, *aa;
Ben Lindstrom0a7e3542001-02-15 03:50:49 +0000658 char path_buf[MAXPATHLEN];
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000659 int err = 0;
Damien Miller4870afd2001-03-14 10:27:09 +1100660 glob_t g;
Damien Miller33804262001-02-04 23:20:18 +1100661
662 path1 = path2 = NULL;
663 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
664
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000665 memset(&g, 0, sizeof(g));
666
Damien Miller33804262001-02-04 23:20:18 +1100667 /* Perform command */
668 switch (cmdnum) {
669 case -1:
670 break;
671 case I_GET:
Damien Miller3db5f532002-02-13 14:10:32 +1100672 err = process_get(conn, path1, path2, *pwd, pflag);
Damien Miller33804262001-02-04 23:20:18 +1100673 break;
674 case I_PUT:
Damien Miller3db5f532002-02-13 14:10:32 +1100675 err = process_put(conn, path1, path2, *pwd, pflag);
Ben Lindstroma3700052001-04-05 23:26:32 +0000676 break;
677 case I_RENAME:
Damien Miller33804262001-02-04 23:20:18 +1100678 path1 = make_absolute(path1, *pwd);
679 path2 = make_absolute(path2, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100680 err = do_rename(conn, path1, path2);
Damien Miller33804262001-02-04 23:20:18 +1100681 break;
Damien Miller058316f2001-03-08 10:08:49 +1100682 case I_SYMLINK:
Damien Miller3db5f532002-02-13 14:10:32 +1100683 path2 = make_absolute(path2, *pwd);
684 err = do_symlink(conn, path1, path2);
Damien Miller058316f2001-03-08 10:08:49 +1100685 break;
Damien Miller33804262001-02-04 23:20:18 +1100686 case I_RM:
687 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100688 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100689 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100690 printf("Removing %s\n", g.gl_pathv[i]);
Damien Miller3db5f532002-02-13 14:10:32 +1100691 if (do_rm(conn, g.gl_pathv[i]) == -1)
Damien Miller4870afd2001-03-14 10:27:09 +1100692 err = -1;
693 }
Damien Miller33804262001-02-04 23:20:18 +1100694 break;
695 case I_MKDIR:
696 path1 = make_absolute(path1, *pwd);
697 attrib_clear(&a);
698 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
699 a.perm = 0777;
Damien Miller3db5f532002-02-13 14:10:32 +1100700 err = do_mkdir(conn, path1, &a);
Damien Miller33804262001-02-04 23:20:18 +1100701 break;
702 case I_RMDIR:
703 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100704 err = do_rmdir(conn, path1);
Damien Miller33804262001-02-04 23:20:18 +1100705 break;
706 case I_CHDIR:
707 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100708 if ((tmp = do_realpath(conn, path1)) == NULL) {
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000709 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100710 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000711 }
Damien Miller3db5f532002-02-13 14:10:32 +1100712 if ((aa = do_stat(conn, tmp, 0)) == NULL) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100713 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000714 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100715 break;
716 }
717 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
718 error("Can't change directory: Can't check target");
719 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000720 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100721 break;
722 }
723 if (!S_ISDIR(aa->perm)) {
724 error("Can't change directory: \"%s\" is not "
725 "a directory", tmp);
726 xfree(tmp);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000727 err = 1;
Damien Millerd7686fd2001-02-10 00:40:03 +1100728 break;
729 }
Damien Miller33804262001-02-04 23:20:18 +1100730 xfree(*pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100731 *pwd = tmp;
Damien Miller33804262001-02-04 23:20:18 +1100732 break;
733 case I_LS:
Damien Millerd7686fd2001-02-10 00:40:03 +1100734 if (!path1) {
Damien Miller3db5f532002-02-13 14:10:32 +1100735 do_ls(conn, *pwd);
Damien Millerd7686fd2001-02-10 00:40:03 +1100736 break;
737 }
Damien Miller33804262001-02-04 23:20:18 +1100738 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100739 if ((tmp = do_realpath(conn, path1)) == NULL)
Damien Millerd7686fd2001-02-10 00:40:03 +1100740 break;
741 xfree(path1);
742 path1 = tmp;
Damien Miller3db5f532002-02-13 14:10:32 +1100743 if ((aa = do_stat(conn, path1, 0)) == NULL)
Damien Millerd7686fd2001-02-10 00:40:03 +1100744 break;
Ben Lindstrom5df2ffa2001-03-17 00:36:17 +0000745 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Millerd7686fd2001-02-10 00:40:03 +1100746 !S_ISDIR(aa->perm)) {
747 error("Can't ls: \"%s\" is not a directory", path1);
748 break;
749 }
Damien Miller3db5f532002-02-13 14:10:32 +1100750 do_ls(conn, path1);
Damien Miller33804262001-02-04 23:20:18 +1100751 break;
752 case I_LCHDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000753 if (chdir(path1) == -1) {
Damien Miller33804262001-02-04 23:20:18 +1100754 error("Couldn't change local directory to "
755 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000756 err = 1;
757 }
Damien Miller33804262001-02-04 23:20:18 +1100758 break;
759 case I_LMKDIR:
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000760 if (mkdir(path1, 0777) == -1) {
Damien Millerd7686fd2001-02-10 00:40:03 +1100761 error("Couldn't create local directory "
Damien Miller33804262001-02-04 23:20:18 +1100762 "\"%s\": %s", path1, strerror(errno));
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000763 err = 1;
764 }
Damien Miller33804262001-02-04 23:20:18 +1100765 break;
766 case I_LLS:
767 local_do_ls(cmd);
768 break;
769 case I_SHELL:
770 local_do_shell(cmd);
771 break;
772 case I_LUMASK:
773 umask(n_arg);
Ben Lindstrom66904942001-02-15 03:19:56 +0000774 printf("Local umask: %03lo\n", n_arg);
Damien Miller33804262001-02-04 23:20:18 +1100775 break;
776 case I_CHMOD:
777 path1 = make_absolute(path1, *pwd);
778 attrib_clear(&a);
779 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
780 a.perm = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +1100781 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100782 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller4870afd2001-03-14 10:27:09 +1100783 printf("Changing mode on %s\n", g.gl_pathv[i]);
Damien Miller3db5f532002-02-13 14:10:32 +1100784 do_setstat(conn, g.gl_pathv[i], &a);
Damien Miller4870afd2001-03-14 10:27:09 +1100785 }
Kevin Steves62c45db2001-02-05 13:42:43 +0000786 break;
Damien Miller33804262001-02-04 23:20:18 +1100787 case I_CHOWN:
788 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100789 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100790 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller3db5f532002-02-13 14:10:32 +1100791 if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100792 continue;
793 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
794 error("Can't get current ownership of "
795 "remote file \"%s\"", g.gl_pathv[i]);
796 continue;
797 }
798 printf("Changing owner on %s\n", g.gl_pathv[i]);
799 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
800 aa->uid = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +1100801 do_setstat(conn, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100802 }
Damien Miller33804262001-02-04 23:20:18 +1100803 break;
804 case I_CHGRP:
805 path1 = make_absolute(path1, *pwd);
Damien Miller3db5f532002-02-13 14:10:32 +1100806 remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100807 for (i = 0; g.gl_pathv[i]; i++) {
Damien Miller3db5f532002-02-13 14:10:32 +1100808 if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
Damien Miller4870afd2001-03-14 10:27:09 +1100809 continue;
810 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
811 error("Can't get current ownership of "
812 "remote file \"%s\"", g.gl_pathv[i]);
813 continue;
814 }
815 printf("Changing group on %s\n", g.gl_pathv[i]);
816 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
817 aa->gid = n_arg;
Damien Miller3db5f532002-02-13 14:10:32 +1100818 do_setstat(conn, g.gl_pathv[i], aa);
Damien Miller33804262001-02-04 23:20:18 +1100819 }
Damien Miller33804262001-02-04 23:20:18 +1100820 break;
821 case I_PWD:
822 printf("Remote working directory: %s\n", *pwd);
823 break;
824 case I_LPWD:
825 if (!getcwd(path_buf, sizeof(path_buf)))
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000826 error("Couldn't get local cwd: %s",
Damien Miller33804262001-02-04 23:20:18 +1100827 strerror(errno));
828 else
829 printf("Local working directory: %s\n",
830 path_buf);
831 break;
832 case I_QUIT:
833 return(-1);
834 case I_HELP:
835 help();
836 break;
Ben Lindstromf78682d2001-03-14 21:26:27 +0000837 case I_VERSION:
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000838 printf("SFTP protocol version %u\n", sftp_proto_version(conn));
Ben Lindstromf78682d2001-03-14 21:26:27 +0000839 break;
Damien Miller33804262001-02-04 23:20:18 +1100840 default:
841 fatal("%d is not implemented", cmdnum);
842 }
843
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000844 if (g.gl_pathc)
845 globfree(&g);
Damien Miller33804262001-02-04 23:20:18 +1100846 if (path1)
847 xfree(path1);
848 if (path2)
849 xfree(path2);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000850
851 /* If an error occurs in batch mode we should abort. */
852 if (infile != stdin && err > 0)
853 return -1;
854
Damien Miller33804262001-02-04 23:20:18 +1100855 return(0);
856}
857
858void
Ben Lindstrom63667f62001-04-13 00:00:14 +0000859interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
Damien Miller33804262001-02-04 23:20:18 +1100860{
861 char *pwd;
Ben Lindstrom63667f62001-04-13 00:00:14 +0000862 char *dir = NULL;
Damien Miller33804262001-02-04 23:20:18 +1100863 char cmd[2048];
Damien Miller3db5f532002-02-13 14:10:32 +1100864 struct sftp_conn *conn;
Damien Miller33804262001-02-04 23:20:18 +1100865
Damien Miller3db5f532002-02-13 14:10:32 +1100866 conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
867 if (conn == NULL)
Damien Miller058316f2001-03-08 10:08:49 +1100868 fatal("Couldn't initialise connection to server");
869
Damien Miller3db5f532002-02-13 14:10:32 +1100870 pwd = do_realpath(conn, ".");
Damien Miller33804262001-02-04 23:20:18 +1100871 if (pwd == NULL)
872 fatal("Need cwd");
873
Ben Lindstrom63667f62001-04-13 00:00:14 +0000874 if (file1 != NULL) {
875 dir = xstrdup(file1);
876 dir = make_absolute(dir, pwd);
877
Damien Miller3db5f532002-02-13 14:10:32 +1100878 if (remote_is_dir(conn, dir) && file2 == NULL) {
Ben Lindstrom63667f62001-04-13 00:00:14 +0000879 printf("Changing to: %s\n", dir);
880 snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
Damien Miller3db5f532002-02-13 14:10:32 +1100881 parse_dispatch_command(conn, cmd, &pwd);
Ben Lindstrom63667f62001-04-13 00:00:14 +0000882 } else {
883 if (file2 == NULL)
884 snprintf(cmd, sizeof cmd, "get %s", dir);
885 else
886 snprintf(cmd, sizeof cmd, "get %s %s", dir,
887 file2);
888
Damien Miller3db5f532002-02-13 14:10:32 +1100889 parse_dispatch_command(conn, cmd, &pwd);
Ben Lindstromcb1f60e2002-03-22 02:47:28 +0000890 xfree(dir);
Ben Lindstrom63667f62001-04-13 00:00:14 +0000891 return;
892 }
Ben Lindstromcb1f60e2002-03-22 02:47:28 +0000893 xfree(dir);
Ben Lindstrom63667f62001-04-13 00:00:14 +0000894 }
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000895#if HAVE_SETVBUF
Damien Millerd7686fd2001-02-10 00:40:03 +1100896 setvbuf(stdout, NULL, _IOLBF, 0);
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000897 setvbuf(infile, NULL, _IOLBF, 0);
Ben Lindstrom6aebb342001-05-09 00:38:19 +0000898#else
899 setlinebuf(stdout);
900 setlinebuf(infile);
901#endif
Damien Miller33804262001-02-04 23:20:18 +1100902
Damien Miller9f0f5c62001-12-21 14:45:46 +1100903 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +1100904 char *cp;
905
906 printf("sftp> ");
907
908 /* XXX: use libedit */
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000909 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
Damien Miller33804262001-02-04 23:20:18 +1100910 printf("\n");
911 break;
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000912 } else if (infile != stdin) /* Bluff typing */
913 printf("%s", cmd);
914
Damien Miller33804262001-02-04 23:20:18 +1100915 cp = strrchr(cmd, '\n');
916 if (cp)
917 *cp = '\0';
Ben Lindstrom562c26b2001-03-07 01:26:48 +0000918
Damien Miller3db5f532002-02-13 14:10:32 +1100919 if (parse_dispatch_command(conn, cmd, &pwd))
Damien Miller33804262001-02-04 23:20:18 +1100920 break;
921 }
922 xfree(pwd);
923}