]> andersk Git - openssh.git/blame - sftp-int.c
- djm@cvs.openbsd.org 2001/03/13 22:42:54
[openssh.git] / sftp-int.c
CommitLineData
61e96248 1/*
2 * Copyright (c) 2001 Damien Miller. All rights reserved.
3 *
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
0426a3b4 25/* XXX: globbed ls */
61e96248 26/* XXX: recursive operations */
27
28#include "includes.h"
2e4fb373 29RCSID("$OpenBSD: sftp-int.c,v 1.27 2001/03/13 22:42:54 djm Exp $");
30
31#include <glob.h>
61e96248 32
33#include "buffer.h"
34#include "xmalloc.h"
35#include "log.h"
36#include "pathnames.h"
37
38#include "sftp.h"
39#include "sftp-common.h"
2e4fb373 40#include "sftp-glob.h"
61e96248 41#include "sftp-client.h"
42#include "sftp-int.h"
43
3a7fe5ba 44/* File to read commands from */
45extern FILE *infile;
46
47/* Version of server we are speaking to */
48int version;
a5ec8a3d 49
61e96248 50/* Seperators for interactive commands */
51#define WHITESPACE " \t\r\n"
52
53/* Commands for interactive mode */
54#define I_CHDIR 1
55#define I_CHGRP 2
56#define I_CHMOD 3
57#define I_CHOWN 4
58#define I_GET 5
59#define I_HELP 6
60#define I_LCHDIR 7
61#define I_LLS 8
62#define I_LMKDIR 9
63#define I_LPWD 10
64#define I_LS 11
65#define I_LUMASK 12
66#define I_MKDIR 13
67#define I_PUT 14
68#define I_PWD 15
69#define I_QUIT 16
70#define I_RENAME 17
71#define I_RM 18
72#define I_RMDIR 19
73#define I_SHELL 20
3a7fe5ba 74#define I_SYMLINK 21
61e96248 75
76struct CMD {
61e96248 77 const char *c;
ec2a033a 78 const int n;
61e96248 79};
80
81const struct CMD cmds[] = {
0426a3b4 82 { "cd", I_CHDIR },
83 { "chdir", I_CHDIR },
84 { "chgrp", I_CHGRP },
85 { "chmod", I_CHMOD },
86 { "chown", I_CHOWN },
87 { "dir", I_LS },
88 { "exit", I_QUIT },
89 { "get", I_GET },
90 { "help", I_HELP },
91 { "lcd", I_LCHDIR },
92 { "lchdir", I_LCHDIR },
93 { "lls", I_LLS },
94 { "lmkdir", I_LMKDIR },
3a7fe5ba 95 { "ln", I_SYMLINK },
0426a3b4 96 { "lpwd", I_LPWD },
97 { "ls", I_LS },
98 { "lumask", I_LUMASK },
99 { "mkdir", I_MKDIR },
100 { "put", I_PUT },
101 { "pwd", I_PWD },
102 { "quit", I_QUIT },
103 { "rename", I_RENAME },
104 { "rm", I_RM },
105 { "rmdir", I_RMDIR },
3a7fe5ba 106 { "symlink", I_SYMLINK },
ec2a033a 107 { "!", I_SHELL },
108 { "?", I_HELP },
109 { NULL, -1}
61e96248 110};
111
112void
113help(void)
114{
115 printf("Available commands:\n");
0426a3b4 116 printf("cd path Change remote directory to 'path'\n");
117 printf("lcd path Change local directory to 'path'\n");
118 printf("chgrp grp path Change group of file 'path' to 'grp'\n");
119 printf("chmod mode path Change permissions of file 'path' to 'mode'\n");
120 printf("chown own path Change owner of file 'path' to 'own'\n");
121 printf("help Display this help text\n");
122 printf("get remote-path [local-path] Download file\n");
123 printf("lls [ls-options [path]] Display local directory listing\n");
3a7fe5ba 124 printf("ln oldpath newpath Symlink remote file\n");
0426a3b4 125 printf("lmkdir path Create local directory\n");
126 printf("lpwd Print local working directory\n");
127 printf("ls [path] Display remote directory listing\n");
128 printf("lumask umask Set local umask to 'umask'\n");
129 printf("mkdir path Create remote directory\n");
130 printf("put local-path [remote-path] Upload file\n");
131 printf("pwd Display remote working directory\n");
132 printf("exit Quit sftp\n");
133 printf("quit Quit sftp\n");
134 printf("rename oldpath newpath Rename remote file\n");
135 printf("rmdir path Remove remote directory\n");
136 printf("rm path Delete remote file\n");
3a7fe5ba 137 printf("symlink oldpath newpath Symlink remote file\n");
61e96248 138 printf("!command Execute 'command' in local shell\n");
139 printf("! Escape to local shell\n");
0426a3b4 140 printf("? Synonym for help\n");
61e96248 141}
142
143void
144local_do_shell(const char *args)
145{
146 int ret, status;
147 char *shell;
148 pid_t pid;
2b87da3b 149
61e96248 150 if (!*args)
151 args = NULL;
2b87da3b 152
61e96248 153 if ((shell = getenv("SHELL")) == NULL)
154 shell = _PATH_BSHELL;
155
156 if ((pid = fork()) == -1)
157 fatal("Couldn't fork: %s", strerror(errno));
158
159 if (pid == 0) {
160 /* XXX: child has pipe fds to ssh subproc open - issue? */
161 if (args) {
162 debug3("Executing %s -c \"%s\"", shell, args);
163 ret = execl(shell, shell, "-c", args, NULL);
164 } else {
165 debug3("Executing %s", shell);
166 ret = execl(shell, shell, NULL);
167 }
2b87da3b 168 fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
61e96248 169 strerror(errno));
170 _exit(1);
171 }
172 if (waitpid(pid, &status, 0) == -1)
173 fatal("Couldn't wait for child: %s", strerror(errno));
174 if (!WIFEXITED(status))
175 error("Shell exited abormally");
176 else if (WEXITSTATUS(status))
177 error("Shell exited with status %d", WEXITSTATUS(status));
178}
179
2b87da3b 180void
61e96248 181local_do_ls(const char *args)
182{
183 if (!args || !*args)
0426a3b4 184 local_do_shell(_PATH_LS);
61e96248 185 else {
0426a3b4 186 int len = strlen(_PATH_LS " ") + strlen(args) + 1;
187 char *buf = xmalloc(len);
61e96248 188
189 /* XXX: quoting - rip quoting code from ftp? */
0426a3b4 190 snprintf(buf, len, _PATH_LS " %s", args);
61e96248 191 local_do_shell(buf);
0426a3b4 192 xfree(buf);
61e96248 193 }
194}
195
196char *
197make_absolute(char *p, char *pwd)
198{
199 char buf[2048];
200
201 /* Derelativise */
202 if (p && p[0] != '/') {
203 snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
204 xfree(p);
205 p = xstrdup(buf);
206 }
207
208 return(p);
209}
210
211int
212parse_getput_flags(const char **cpp, int *pflag)
213{
214 const char *cp = *cpp;
215
216 /* Check for flags */
217 if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
0426a3b4 218 switch (cp[1]) {
58c54a79 219 case 'p':
61e96248 220 case 'P':
221 *pflag = 1;
222 break;
223 default:
58c54a79 224 error("Invalid flag -%c", cp[1]);
61e96248 225 return(-1);
226 }
227 cp += 2;
228 *cpp = cp + strspn(cp, WHITESPACE);
229 }
230
231 return(0);
232}
233
234int
235get_pathname(const char **cpp, char **path)
236{
0426a3b4 237 const char *cp = *cpp, *end;
238 char quot;
61e96248 239 int i;
240
241 cp += strspn(cp, WHITESPACE);
242 if (!*cp) {
243 *cpp = cp;
244 *path = NULL;
0426a3b4 245 return (0);
61e96248 246 }
247
248 /* Check for quoted filenames */
249 if (*cp == '\"' || *cp == '\'') {
0426a3b4 250 quot = *cp++;
251
252 end = strchr(cp, quot);
253 if (end == NULL) {
61e96248 254 error("Unterminated quote");
0426a3b4 255 goto fail;
61e96248 256 }
0426a3b4 257 if (cp == end) {
61e96248 258 error("Empty quotes");
0426a3b4 259 goto fail;
61e96248 260 }
0426a3b4 261 *cpp = end + 1 + strspn(end + 1, WHITESPACE);
262 } else {
263 /* Read to end of filename */
264 end = strpbrk(cp, WHITESPACE);
265 if (end == NULL)
266 end = strchr(cp, '\0');
267 *cpp = end + strspn(end, WHITESPACE);
61e96248 268 }
269
0426a3b4 270 i = end - cp;
61e96248 271
272 *path = xmalloc(i + 1);
273 memcpy(*path, cp, i);
274 (*path)[i] = '\0';
61e96248 275 return(0);
0426a3b4 276
277 fail:
278 *path = NULL;
279 return (-1);
61e96248 280}
281
282int
283infer_path(const char *p, char **ifp)
284{
285 char *cp;
286
61e96248 287 cp = strrchr(p, '/');
61e96248 288 if (cp == NULL) {
289 *ifp = xstrdup(p);
290 return(0);
291 }
292
293 if (!cp[1]) {
294 error("Invalid path");
295 return(-1);
296 }
297
298 *ifp = xstrdup(cp + 1);
299 return(0);
300}
301
302int
303parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
304 char **path1, char **path2)
305{
306 const char *cmd, *cp = *cpp;
877546eb 307 char *cp2;
ec2a033a 308 int base = 0;
877546eb 309 long l;
61e96248 310 int i, cmdnum;
311
312 /* Skip leading whitespace */
313 cp = cp + strspn(cp, WHITESPACE);
314
315 /* Ignore blank lines */
316 if (!*cp)
317 return(-1);
318
319 /* Figure out which command we have */
320 for(i = 0; cmds[i].c; i++) {
321 int cmdlen = strlen(cmds[i].c);
322
323 /* Check for command followed by whitespace */
324 if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
325 strchr(WHITESPACE, cp[cmdlen])) {
326 cp += cmdlen;
327 cp = cp + strspn(cp, WHITESPACE);
328 break;
329 }
330 }
331 cmdnum = cmds[i].n;
332 cmd = cmds[i].c;
333
334 /* Special case */
335 if (*cp == '!') {
336 cp++;
337 cmdnum = I_SHELL;
338 } else if (cmdnum == -1) {
339 error("Invalid command.");
340 return(-1);
341 }
342
343 /* Get arguments and parse flags */
344 *pflag = *n_arg = 0;
345 *path1 = *path2 = NULL;
346 switch (cmdnum) {
347 case I_GET:
348 case I_PUT:
349 if (parse_getput_flags(&cp, pflag))
350 return(-1);
351 /* Get first pathname (mandatory) */
352 if (get_pathname(&cp, path1))
353 return(-1);
354 if (*path1 == NULL) {
355 error("You must specify at least one path after a "
356 "%s command.", cmd);
357 return(-1);
358 }
359 /* Try to get second pathname (optional) */
360 if (get_pathname(&cp, path2))
361 return(-1);
61e96248 362 break;
363 case I_RENAME:
3a7fe5ba 364 case I_SYMLINK:
61e96248 365 if (get_pathname(&cp, path1))
366 return(-1);
367 if (get_pathname(&cp, path2))
368 return(-1);
369 if (!*path1 || !*path2) {
370 error("You must specify two paths after a %s "
371 "command.", cmd);
372 return(-1);
373 }
374 break;
375 case I_RM:
376 case I_MKDIR:
377 case I_RMDIR:
378 case I_CHDIR:
379 case I_LCHDIR:
380 case I_LMKDIR:
381 /* Get pathname (mandatory) */
382 if (get_pathname(&cp, path1))
383 return(-1);
384 if (*path1 == NULL) {
2b87da3b 385 error("You must specify a path after a %s command.",
61e96248 386 cmd);
387 return(-1);
388 }
389 break;
390 case I_LS:
391 /* Path is optional */
392 if (get_pathname(&cp, path1))
393 return(-1);
394 break;
395 case I_LLS:
396 case I_SHELL:
397 /* Uses the rest of the line */
398 break;
399 case I_LUMASK:
877546eb 400 base = 8;
61e96248 401 case I_CHMOD:
ec2a033a 402 base = 8;
61e96248 403 case I_CHOWN:
404 case I_CHGRP:
405 /* Get numeric arg (mandatory) */
877546eb 406 l = strtol(cp, &cp2, base);
407 if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
408 errno == ERANGE) || l < 0) {
61e96248 409 error("You must supply a numeric argument "
410 "to the %s command.", cmd);
411 return(-1);
412 }
877546eb 413 cp = cp2;
414 *n_arg = l;
415 if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
416 break;
417 if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
61e96248 418 error("You must supply a numeric argument "
419 "to the %s command.", cmd);
420 return(-1);
421 }
422 cp += strspn(cp, WHITESPACE);
423
424 /* Get pathname (mandatory) */
425 if (get_pathname(&cp, path1))
426 return(-1);
427 if (*path1 == NULL) {
2b87da3b 428 error("You must specify a path after a %s command.",
61e96248 429 cmd);
430 return(-1);
431 }
432 break;
433 case I_QUIT:
434 case I_PWD:
435 case I_LPWD:
436 case I_HELP:
437 break;
438 default:
439 fatal("Command not implemented");
440 }
441
442 *cpp = cp;
61e96248 443 return(cmdnum);
444}
445
446int
447parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
448{
0426a3b4 449 char *path1, *path2, *tmp;
2e4fb373 450 int pflag, cmdnum, i;
61e96248 451 unsigned long n_arg;
452 Attrib a, *aa;
7cc4cf0a 453 char path_buf[MAXPATHLEN];
a5ec8a3d 454 int err = 0;
2e4fb373 455 glob_t g;
61e96248 456
457 path1 = path2 = NULL;
458 cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
459
460 /* Perform command */
461 switch (cmdnum) {
462 case -1:
463 break;
464 case I_GET:
2e4fb373 465 memset(&g, 0, sizeof(g));
466 if (!remote_glob(in, out, path1, 0, NULL, &g)) {
467 if (path2) {
468 /* XXX: target should be directory */
469 error("You cannot specify a target when "
470 "downloading multiple files");
471 err = -1;
472 break;
473 }
474 for(i = 0; g.gl_pathv[i]; i++) {
475 if (!infer_path(g.gl_pathv[i], &path2)) {
476 printf("Fetching %s\n", g.gl_pathv[i]);
477 if (do_download(in, out, g.gl_pathv[i],
478 path2, pflag) == -1)
479 err = -1;
480 free(path2);
481 path2 = NULL;
482 } else
483 err = -1;
484 }
485 } else {
486 if (!path2 && infer_path(path1, &path2)) {
487 err = -1;
488 break;
489 }
490 err = do_download(in, out, path1, path2, pflag);
491 }
61e96248 492 break;
493 case I_PUT:
2e4fb373 494 if (!glob(path1, 0, NULL, &g)) {
495 if (path2) {
496 error("You cannot specify a target when "
497 "uploading multiple files");
498 err = -1;
499 break;
500 }
501 for(i = 0; g.gl_pathv[i]; i++) {
502 if (!infer_path(g.gl_pathv[i], &path2)) {
503 path2 = make_absolute(path2, *pwd);
504 printf("Uploading %s\n", g.gl_pathv[i]);
505 if (do_upload(in, out, g.gl_pathv[i],
506 path2, pflag) == -1)
507 err = -1;
508 free(path2);
509 path2 = NULL;
510 } else
511 err = -1;
512 }
513 } else {
514 if (!path2 && infer_path(path1, &path2)) {
515 err = -1;
516 break;
517 }
518 err = do_upload(in, out, path1, path2, pflag);
519 }
520 break;
521 case I_RENAME:
61e96248 522 path1 = make_absolute(path1, *pwd);
523 path2 = make_absolute(path2, *pwd);
a5ec8a3d 524 err = do_rename(in, out, path1, path2);
61e96248 525 break;
3a7fe5ba 526 case I_SYMLINK:
527 if (version < 3) {
528 error("The server (version %d) does not support "
529 "this operation", version);
530 err = -1;
531 } else {
532 path2 = make_absolute(path2, *pwd);
533 err = do_symlink(in, out, path1, path2);
534 }
535 break;
61e96248 536 case I_RM:
537 path1 = make_absolute(path1, *pwd);
2e4fb373 538 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
539 for(i = 0; g.gl_pathv[i]; i++) {
540 printf("Removing %s\n", g.gl_pathv[i]);
541 if (do_rm(in, out, g.gl_pathv[i]) == -1)
542 err = -1;
543 }
61e96248 544 break;
545 case I_MKDIR:
546 path1 = make_absolute(path1, *pwd);
547 attrib_clear(&a);
548 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
549 a.perm = 0777;
a5ec8a3d 550 err = do_mkdir(in, out, path1, &a);
61e96248 551 break;
552 case I_RMDIR:
553 path1 = make_absolute(path1, *pwd);
a5ec8a3d 554 err = do_rmdir(in, out, path1);
61e96248 555 break;
556 case I_CHDIR:
557 path1 = make_absolute(path1, *pwd);
a5ec8a3d 558 if ((tmp = do_realpath(in, out, path1)) == NULL) {
559 err = 1;
0426a3b4 560 break;
a5ec8a3d 561 }
0426a3b4 562 if ((aa = do_stat(in, out, tmp)) == NULL) {
563 xfree(tmp);
a5ec8a3d 564 err = 1;
0426a3b4 565 break;
566 }
567 if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
568 error("Can't change directory: Can't check target");
569 xfree(tmp);
a5ec8a3d 570 err = 1;
0426a3b4 571 break;
572 }
573 if (!S_ISDIR(aa->perm)) {
574 error("Can't change directory: \"%s\" is not "
575 "a directory", tmp);
576 xfree(tmp);
a5ec8a3d 577 err = 1;
0426a3b4 578 break;
579 }
61e96248 580 xfree(*pwd);
0426a3b4 581 *pwd = tmp;
61e96248 582 break;
583 case I_LS:
0426a3b4 584 if (!path1) {
585 do_ls(in, out, *pwd);
586 break;
587 }
61e96248 588 path1 = make_absolute(path1, *pwd);
0426a3b4 589 if ((tmp = do_realpath(in, out, path1)) == NULL)
590 break;
591 xfree(path1);
592 path1 = tmp;
593 if ((aa = do_stat(in, out, path1)) == NULL)
594 break;
595 if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
596 !S_ISDIR(aa->perm)) {
597 error("Can't ls: \"%s\" is not a directory", path1);
598 break;
599 }
600 do_ls(in, out, path1);
61e96248 601 break;
602 case I_LCHDIR:
a5ec8a3d 603 if (chdir(path1) == -1) {
61e96248 604 error("Couldn't change local directory to "
605 "\"%s\": %s", path1, strerror(errno));
a5ec8a3d 606 err = 1;
607 }
61e96248 608 break;
609 case I_LMKDIR:
a5ec8a3d 610 if (mkdir(path1, 0777) == -1) {
0426a3b4 611 error("Couldn't create local directory "
61e96248 612 "\"%s\": %s", path1, strerror(errno));
a5ec8a3d 613 err = 1;
614 }
61e96248 615 break;
616 case I_LLS:
617 local_do_ls(cmd);
618 break;
619 case I_SHELL:
620 local_do_shell(cmd);
621 break;
622 case I_LUMASK:
623 umask(n_arg);
877546eb 624 printf("Local umask: %03lo\n", n_arg);
61e96248 625 break;
626 case I_CHMOD:
627 path1 = make_absolute(path1, *pwd);
628 attrib_clear(&a);
629 a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
630 a.perm = n_arg;
2e4fb373 631 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
632 for(i = 0; g.gl_pathv[i]; i++) {
633 printf("Changing mode on %s\n", g.gl_pathv[i]);
634 do_setstat(in, out, g.gl_pathv[i], &a);
635 }
ec2a033a 636 break;
61e96248 637 case I_CHOWN:
638 path1 = make_absolute(path1, *pwd);
2e4fb373 639 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
640 for(i = 0; g.gl_pathv[i]; i++) {
641 if (!(aa = do_stat(in, out, g.gl_pathv[i])))
642 continue;
643 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
644 error("Can't get current ownership of "
645 "remote file \"%s\"", g.gl_pathv[i]);
646 continue;
647 }
648 printf("Changing owner on %s\n", g.gl_pathv[i]);
649 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
650 aa->uid = n_arg;
651 do_setstat(in, out, g.gl_pathv[i], aa);
61e96248 652 }
61e96248 653 break;
654 case I_CHGRP:
655 path1 = make_absolute(path1, *pwd);
2e4fb373 656 remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
657 for(i = 0; g.gl_pathv[i]; i++) {
658 if (!(aa = do_stat(in, out, g.gl_pathv[i])))
659 continue;
660 if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
661 error("Can't get current ownership of "
662 "remote file \"%s\"", g.gl_pathv[i]);
663 continue;
664 }
665 printf("Changing group on %s\n", g.gl_pathv[i]);
666 aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
667 aa->gid = n_arg;
668 do_setstat(in, out, g.gl_pathv[i], aa);
61e96248 669 }
61e96248 670 break;
671 case I_PWD:
672 printf("Remote working directory: %s\n", *pwd);
673 break;
674 case I_LPWD:
675 if (!getcwd(path_buf, sizeof(path_buf)))
54b974dc 676 error("Couldn't get local cwd: %s",
61e96248 677 strerror(errno));
678 else
679 printf("Local working directory: %s\n",
680 path_buf);
681 break;
682 case I_QUIT:
683 return(-1);
684 case I_HELP:
685 help();
686 break;
687 default:
688 fatal("%d is not implemented", cmdnum);
689 }
690
691 if (path1)
692 xfree(path1);
693 if (path2)
694 xfree(path2);
a5ec8a3d 695
696 /* If an error occurs in batch mode we should abort. */
697 if (infile != stdin && err > 0)
698 return -1;
699
61e96248 700 return(0);
701}
702
703void
704interactive_loop(int fd_in, int fd_out)
705{
706 char *pwd;
707 char cmd[2048];
708
3a7fe5ba 709 version = do_init(fd_in, fd_out);
710 if (version == -1)
711 fatal("Couldn't initialise connection to server");
712
61e96248 713 pwd = do_realpath(fd_in, fd_out, ".");
714 if (pwd == NULL)
715 fatal("Need cwd");
716
0426a3b4 717 setvbuf(stdout, NULL, _IOLBF, 0);
a5ec8a3d 718 setvbuf(infile, NULL, _IOLBF, 0);
61e96248 719
720 for(;;) {
721 char *cp;
722
723 printf("sftp> ");
724
725 /* XXX: use libedit */
a5ec8a3d 726 if (fgets(cmd, sizeof(cmd), infile) == NULL) {
61e96248 727 printf("\n");
728 break;
a5ec8a3d 729 } else if (infile != stdin) /* Bluff typing */
730 printf("%s", cmd);
731
61e96248 732 cp = strrchr(cmd, '\n');
733 if (cp)
734 *cp = '\0';
a5ec8a3d 735
61e96248 736 if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
737 break;
738 }
739 xfree(pwd);
740}
This page took 0.166485 seconds and 5 git commands to generate.