2 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
41 #include <readline/readline.h>
42 #include <readline/history.h>
52 #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
53 #define TMPFSTRLEN sizeof(TMPFSTR)
61 struct tls_config *tlsconf;
65 struct evbuffer *dirbuf;
70 volatile sig_atomic_t resized;
82 #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
85 read_line(const char *prompt)
90 if ((line = readline(prompt)) == NULL)
92 /* XXX: trim spaces? */
103 spawn(const char *argv0, ...)
108 const char *argv[16], *last;
111 memset(argv, 0, sizeof(argv));
115 for (i = 1; i < nitems(argv); ++i) {
116 last = va_arg(ap, const char *);
123 assert(last == NULL);
125 switch (pid = fork()) {
129 execvp(argv[0], (char *const *)argv);
132 waitpid(pid, &status, 0);
137 tty_resized(int signo)
145 fprintf(stderr, "usage: %s [-C cert] [-K key] [-o output] "
146 "[9p://][user@]host[:port][/path]\n", getprogname());
147 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
170 hexdump("outgoing message", EVBUFFER_DATA(evb),
171 EVBUFFER_LENGTH(evb));
173 while (EVBUFFER_LENGTH(evb) != 0) {
174 buf = EVBUFFER_DATA(evb);
175 nbytes = EVBUFFER_LENGTH(evb);
178 r = write(sock, buf, nbytes);
179 if (r == 0 || r == -1)
182 r = tls_write(ctx, buf, nbytes);
183 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
186 errx(1, "tls: %s", tls_error(ctx));
189 evbuffer_drain(evb, r);
194 mustread(void *d, size_t len)
200 r = read(sock, d, len);
201 if (r == 0 || r == -1)
204 r = tls_read(ctx, d, len);
205 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
208 errx(1, "tls: %s", tls_error(ctx));
222 mustread(&len, sizeof(len));
224 if (len < HEADERSIZE)
225 errx(1, "read message of invalid length %d", len);
227 len -= 4; /* skip the length just read */
230 l = MIN(len, sizeof(tmp));
233 evbuffer_add(buf, tmp, l);
237 hexdump("incoming packet", EVBUFFER_DATA(buf),
238 EVBUFFER_LENGTH(buf));
242 np_read64(struct evbuffer *buf)
246 evbuffer_remove(buf, &n, sizeof(n));
251 np_read32(struct evbuffer *buf)
255 evbuffer_remove(buf, &n, sizeof(n));
260 np_read16(struct evbuffer *buf)
264 evbuffer_remove(buf, &n, sizeof(n));
269 np_read8(struct evbuffer *buf)
273 evbuffer_remove(buf, &n, sizeof(n));
278 np_readstr(struct evbuffer *buf)
283 len = np_read16(buf);
284 assert(EVBUFFER_LENGTH(buf) >= len);
286 if ((str = calloc(1, len+1)) == NULL)
288 evbuffer_remove(buf, str, len);
293 np_read_qid(struct evbuffer *buf, struct qid *qid)
295 assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
297 qid->type = np_read8(buf);
298 qid->vers = np_read32(buf);
299 qid->path = np_read64(buf);
303 np_read_stat(struct evbuffer *buf, struct np_stat *st)
307 memset(st, 0, sizeof(*st));
309 size = np_read16(buf);
310 if (size > EVBUFFER_LENGTH(buf))
313 st->type = np_read16(buf);
314 st->dev = np_read32(buf);
315 np_read_qid(buf, &st->qid);
316 st->mode = np_read32(buf);
317 st->atime = np_read32(buf);
318 st->mtime = np_read32(buf);
319 st->length = np_read64(buf);
320 st->name = np_readstr(buf);
321 st->uid = np_readstr(buf);
322 st->gid = np_readstr(buf);
323 st->muid = np_readstr(buf);
343 err = np_readstr(buf);
344 errx(1, "expected %s, got error %s",
345 pp_msg_type(type), err);
348 errx(1, "expected %s, got msg type %s",
349 pp_msg_type(type), pp_msg_type(t));
353 expect2(uint8_t type, uint16_t tag)
363 errx(1, "expected tag 0x%x, got 0x%x", tag, t);
367 check(uint8_t type, uint16_t tag)
372 rtype = np_read8(buf);
373 rtag = np_read16(buf);
376 errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
381 return np_readstr(buf);
383 errx(1, "expected %s, got msg type %s",
384 pp_msg_type(type), pp_msg_type(rtype));
392 tversion(VERSION9P, MSIZE9P);
395 expect2(Rversion, NOTAG);
397 msize = np_read32(buf);
398 version = np_readstr(buf);
400 if (msize > MSIZE9P || msize < 256)
401 errx(1, "got unexpected msize: %d", msize);
402 if (strcmp(version, VERSION9P))
403 errx(1, "unexpected 9p version: %s", version);
410 do_attach(const char *user)
414 tattach(pwdfid, NOFID, user, "/");
417 expect2(Rattach, iota_tag);
418 np_read_qid(buf, &qid);
424 do_open(uint32_t fid, uint8_t mode)
432 expect2(Ropen, iota_tag);
434 np_read_qid(buf, &qid);
435 iounit = np_read32(buf);
443 do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
448 tcreate(fid, name, perm, mode);
451 expect2(Rcreate, iota_tag);
453 np_read_qid(buf, &qid);
454 iounit = np_read32(buf);
462 do_clunk(uint32_t fid)
467 expect2(Rclunk, iota_tag);
473 dup_fid(int fid, int nfid)
478 twalk(fid, nfid, NULL, 0);
482 if ((errstr = check(Rwalk, iota_tag)) != NULL)
485 nwqid = np_read16(buf);
494 walk_path(int fid, int newfid, const char *path, int *missing,
497 char *wnames[MAXWELEM], *p, *t, *errstr;
501 if ((p = strdup(path)) == NULL)
505 /* strip initial ./ */
506 if (t[0] == '.' && t[1] == '/')
509 for (nwname = 0; nwname < nitems(wnames) &&
510 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
511 if (*wnames[nwname] != '\0')
515 twalk(fid, newfid, (const char **)wnames, nwname);
520 if ((errstr = check(Rwalk, iota_tag)) != NULL) {
525 nwqid = np_read16(buf);
526 assert(nwqid <= nwname);
528 /* consume all qids */
529 for (i = 0; i < nwqid; ++i)
530 np_read_qid(buf, qid);
534 *missing = nwname - nwqid;
539 do_stat(int fid, struct np_stat *st)
544 expect2(Rstat, iota_tag);
546 /* eat up the first two byte length */
549 if (np_read_stat(buf, st) == -1)
550 errx(1, "invalid stat struct read");
556 do_wstat(int fid, const struct np_stat *st)
564 if ((errstr = check(Rwstat, iota_tag)) != NULL)
580 if ((errstr = check(Rremove, iota_tag)) != NULL)
589 do_read(int fid, uint64_t off, uint32_t count, void *data)
593 tread(fid, off, count);
596 expect2(Rread, iota_tag);
599 assert(r == EVBUFFER_LENGTH(buf));
601 evbuffer_remove(buf, data, r);
609 do_write(int fid, uint64_t off, uint32_t count, void *data)
613 twrite(fid, off, data, count);
616 expect2(Rwrite, iota_tag);
627 draw_progress(const char *pre, const struct progress *p)
636 perc = 100.0 * p->done / p->max;
638 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
645 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
647 tty_width = ws.ws_col;
652 ((l = fprintf(stderr, "\r%s ", pre)) == -1 || l >= w))
655 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
657 fprintf(stderr, "%4d%%\n", (int)perc);
661 fprintf(stderr, "|");
663 l = w * MIN(100.0, perc) / 100.0;
664 for (i = 0; i < l; i++)
665 fprintf(stderr, "*");
667 fprintf(stderr, " ");
668 fprintf(stderr, "|%4d%%", (int)perc);
672 fetch_fid(int fid, int fd, const char *name)
674 static char buf[MSIZE9P];
675 struct progress p = {0};
681 do_open(fid, KOREAD);
688 len = MIN(sizeof(buf), msize);
689 len -= IOHDRSZ; /* for the request' fields */
691 r = do_read(fid, p.done, len, buf);
695 for (off = 0; off < r; off += nw)
696 if ((nw = write(fd, buf + off, r - off)) == 0 ||
703 draw_progress(name, &p);
706 /* throttle, for debugging purpose */
708 struct timespec ts = { 0, 500000000 };
709 nanosleep(&ts, NULL);
722 send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
724 static char buf[MSIZE9P];
725 struct progress p = {0};
730 if (fstat(fd, &sb) == -1)
734 do_create(fid, fnam, 0644, KOWRITE);
736 do_open(fid, open_flags | KOWRITE);
740 len = MIN(sizeof(buf), msize);
741 len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
743 r = read(fd, buf, len);
749 w = do_write(fid, p.done, r, buf);
752 draw_progress(name, &p);
755 /* throttle, for debugging purpose */
757 struct timespec ts = { 0, 500000000 };
758 nanosleep(&ts, NULL);
768 woc_file(int fd, const char *prompt, const char *path)
771 const char *n = NULL;
776 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
777 if (errstr != NULL && miss > 1) {
778 fprintf(stderr, "%s: %s\n", path, errstr);
783 if (errstr != NULL || miss == 1) {
784 char p[PATH_MAX], *dn;
787 * If it's only one component missing (the file name), walk
788 * to the parent directory and try to create the file.
791 if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
792 fprintf(stderr, "path too long: %s\n", path);
797 if (!strcmp(dn, ".")) {
798 errstr = dup_fid(pwdfid, nfid);
801 errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
803 if (errstr != NULL) {
804 fprintf(stderr, "%s: %s\n", dn, errstr);
810 fprintf(stderr, "%s: not a directory\n", dn);
814 if ((n = strrchr(path, '/')) != NULL)
823 fprintf(stderr, "can't create %s: missing %d path"
824 " component(s)\n", path, miss);
828 send_fid(nfid, n, KOTRUNC, fd, prompt);
833 do_tls_connect(const char *host, const char *port)
837 if ((tlsconf = tls_config_new()) == NULL)
838 fatalx("tls_config_new");
839 tls_config_insecure_noverifycert(tlsconf);
840 tls_config_insecure_noverifyname(tlsconf);
845 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
846 fatalx("can't load certs (%s, %s)", crtpath, keypath);
848 if ((ctx = tls_client()) == NULL)
850 if (tls_configure(ctx, tlsconf) == -1)
851 fatalx("tls_configure: %s", tls_error(ctx));
853 if (tls_connect(ctx, host, port) == -1)
854 fatalx("can't connect to %s:%s: %s", host, port,
857 for (handshake = 0; !handshake;) {
858 switch (tls_handshake(ctx)) {
860 fatalx("tls_handshake: %s", tls_error(ctx));
869 do_ctxt_connect(const char *host, const char *port)
871 struct addrinfo hints, *res, *res0;
872 int error, saved_errno;
873 const char *cause = NULL;
875 memset(&hints, 0, sizeof(hints));
876 hints.ai_family = AF_UNSPEC;
877 hints.ai_socktype = SOCK_STREAM;
878 error = getaddrinfo(host, port, &hints, &res0);
880 errx(1, "%s", gai_strerror(error));
883 for (res = res0; res != NULL; res = res->ai_next) {
884 sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
891 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
909 do_connect(const char *host, const char *port, const char *user)
915 fprintf(stderr, "connecting to %s:%s...", host, port);
918 do_tls_connect(host, port);
920 do_ctxt_connect(host, port);
922 fprintf(stderr, " done!\n");
929 tmp_file(char sfn[TMPFSTRLEN])
933 strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
934 if ((tmpfd = mkstemp(sfn)) == -1) {
935 warn("mkstemp %s", sfn);
939 /* set the close-on-exec flag */
940 if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
949 static inline const char *
970 /* unreachable, just for the compiler' happiness */
976 prepare_wstat(struct np_stat *st)
978 memset(st, 0xFF, sizeof(*st));
986 print_dirent(const struct np_stat *st)
991 char fmt[FMT_SCALED_STRSIZE], tim[13];
993 if (fmt_scaled(st->length, fmt) == -1)
994 strlcpy(fmt, "xxx", sizeof(fmt));
998 if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1001 timfmt = "%b %e %Y";
1003 if ((tm = localtime(&mtime)) == NULL ||
1004 strftime(tim, sizeof(tim), timfmt, tm) == 0)
1005 strlcpy(tim, "unknown", sizeof(tim));
1007 if (st->qid.type & QTDIR)
1011 printf("%s", pp_perm(st->mode >> 6));
1012 printf("%s", pp_perm(st->mode >> 3));
1013 printf("%s", pp_perm(st->mode));
1014 printf(" %8s %12s %s%s\n", fmt, tim, st->name,
1015 st->qid.type & QTDIR ? "/" : "");
1021 dir_listing(const char *path, int (*fn)(const struct np_stat *),
1024 struct qid qid = {0, 0, QTDIR};
1028 int nfid, r, miss = 0;
1034 if (!strcmp(path, "."))
1035 errstr = dup_fid(pwdfid, nfid);
1037 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1038 if (errstr != NULL) {
1040 printf("%s: %s\n", path, errstr);
1046 printf("%s: No such file or directory\n", path);
1049 if (!(qid.type & QTDIR)) {
1051 printf("%s: not a directory\n", path);
1056 do_open(nfid, KOREAD);
1057 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1060 tread(nfid, off, msize - IOHDRSZ);
1063 expect2(Rread, iota_tag);
1065 len = np_read32(buf);
1069 evbuffer_add_buffer(dirbuf, buf);
1075 while (EVBUFFER_LENGTH(dirbuf) != 0) {
1076 if (np_read_stat(dirbuf, &st) == -1)
1077 errx(1, "invalid stat struct read");
1090 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1096 cmd_bell(int argc, const char **argv)
1101 puts("bell mode enabled");
1103 puts("bell mode disabled");
1110 if (!strcmp(*argv, "on")) {
1112 puts("bell mode enabled");
1116 if (!strcmp(*argv, "off")) {
1118 puts("bell mode disabled");
1123 printf("bell [on | off]\n");
1127 cmd_bye(int argc, const char **argv)
1134 cmd_cd(int argc, const char **argv)
1141 printf("usage: cd remote-path\n");
1146 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1147 if (errstr != NULL) {
1148 printf("%s: %s\n", argv[0], errstr);
1153 if (miss != 0 || !(qid.type & QTDIR)) {
1154 printf("%s: not a directory\n", argv[0]);
1165 cmd_edit(int argc, const char **argv)
1168 int nfid, tmpfd, miss;
1169 char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1173 puts("usage: edit file");
1177 if ((ed = getenv("VISUAL")) == NULL &&
1178 (ed = getenv("EDITOR")) == NULL)
1182 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1183 if (errstr != NULL) {
1184 printf("%s: %s\n", *argv, errstr);
1189 if (miss != 0 || qid.type != 0) {
1190 printf("%s: not a file\n", *argv);
1196 if ((tmpfd = tmp_file(sfn)) == -1) {
1201 strlcpy(p, *argv, sizeof(p));
1204 if (fetch_fid(nfid, tmpfd, name)) {
1205 warn("failed fetch or can't write %s", sfn);
1210 spawn(ed, sfn, NULL);
1213 * Re-open the file because it's not guaranteed that the
1214 * file descriptor tmpfd is still associated with the file
1215 * pointed by sfn: it's not uncommon for editor to write
1216 * a backup file and then rename(2) it to the file name.
1218 if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1219 warn("can't open %s", sfn);
1223 woc_file(tmpfd, *argv, name);
1231 cmd_get(int argc, const char **argv)
1238 if (argc != 1 && argc != 2) {
1239 printf("usage: get remote-file [local-file]\n");
1245 else if ((l = strrchr(argv[0], '/')) != NULL)
1251 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1252 if (errstr != NULL) {
1253 printf("%s: %s\n", argv[0], errstr);
1258 if (miss != 0 || qid.type != 0) {
1259 printf("%s: not a file\n", argv[0]);
1265 if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1266 warn("can't open %s", l);
1271 if (fetch_fid(nfid, fd, l) == -1)
1272 warn("write %s", l);
1277 cmd_hexdump(int argc, const char **argv)
1282 puts("hexdump mode enabled");
1284 puts("hexdump mode disabled");
1291 if (!strcmp(*argv, "on")) {
1293 puts("hexdump mode enabled");
1297 if (!strcmp(*argv, "off")) {
1299 puts("hexdump mode disabled");
1304 puts("usage: hexdump [on | off]");
1308 cmd_lcd(int argc, const char **argv)
1313 printf("usage: lcd [local-directory]\n");
1320 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1321 printf("HOME is not defined\n");
1325 if (chdir(dir) == -1)
1326 printf("cd: %s: %s\n", dir, strerror(errno));
1330 cmd_lpwd(int argc, const char **argv)
1332 char path[PATH_MAX];
1335 printf("usage: lpwd\n");
1339 if (getcwd(path, sizeof(path)) == NULL) {
1340 printf("lpwd: %s\n", strerror(errno));
1344 printf("%s\n", path);
1348 cmd_ls(int argc, const char **argv)
1351 puts("usage: ls [path]");
1355 dir_listing(argc == 0 ? "." : argv[0], print_dirent, 1);
1359 cmd_page(int argc, const char **argv)
1362 int nfid, tmpfd, miss, r;
1363 char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1367 puts("usage: page file");
1371 if ((pager = getenv("PAGER")) == NULL)
1375 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1376 if (errstr != NULL) {
1377 printf("%s: %s\n", *argv, errstr);
1382 if (miss != 0 || qid.type != 0) {
1383 printf("%s: not a file\n", *argv);
1389 if ((tmpfd = tmp_file(sfn)) == -1) {
1394 strlcpy(p, *argv, sizeof(p));
1396 if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1397 warn("write %s", sfn);
1400 spawn(pager, sfn, NULL);
1405 cmd_pipe(int argc, const char **argv)
1409 int nfid, tmpfd, miss, status;
1410 int filedes[2]; /* read end, write end */
1414 puts("usage: pipe remote-file cmd [args...]");
1419 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1420 if (errstr != NULL) {
1421 printf("%s: %s\n", *argv, errstr);
1426 if (miss != 0 || qid.type != 0) {
1427 printf("%s: not a file\n", *argv);
1433 if (pipe(filedes) == -1)
1436 switch (pid = vfork()) {
1441 if (dup2(filedes[0], 0) == -1)
1443 execvp(argv[1], (char *const *)argv + 1);
1448 if (fetch_fid(nfid, filedes[1], *argv) == -1)
1449 warnx("failed to fetch all the file");
1452 waitpid(pid, &status, 0);
1456 cmd_put(int argc, const char **argv)
1462 if (argc != 1 && argc != 2) {
1463 printf("usage: put local-file [remote-file]\n");
1469 else if ((l = strrchr(argv[0], '/')) != NULL)
1474 if ((fd = open(argv[0], O_RDONLY)) == -1) {
1475 warn("%s", argv[0]);
1479 woc_file(fd, argv[0], l);
1484 cmd_rename(int argc, const char **argv)
1492 puts("usage: rename remote-file new-remote-name");
1497 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1498 if (errstr != NULL) {
1499 printf("%s: %s\n", argv[0], errstr);
1505 printf("%s: not such file or directory\n", argv[0]);
1510 st.name = (char *)argv[1];
1511 if ((errstr = do_wstat(nfid, &st)) != NULL) {
1512 printf("rename: %s\n", errstr);
1520 cmd_rm(int argc, const char **argv)
1527 puts("usage: rm file ...");
1531 for (; *argv; ++argv, --argc) {
1533 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1534 if (errstr != NULL) {
1535 printf("%s: %s\n", *argv, errstr);
1540 printf("%s: not such file or directory\n", *argv);
1544 if ((errstr = do_remove(nfid)) != NULL) {
1545 printf("%s: %s\n", *argv, errstr);
1553 cmd_verbose(int argc, const char **argv)
1556 log_setverbose(!log_getverbose());
1557 if (log_getverbose())
1558 puts("verbose mode enabled");
1560 puts("verbose mode disabled");
1567 if (!strcmp(*argv, "on")) {
1569 puts("verbose mode enabled");
1573 if (!strcmp(*argv, "off")) {
1575 puts("verbose mode disabled");
1580 printf("verbose [on | off]\n");
1584 excmd(int argc, const char **argv)
1591 for (i = 0; i < nitems(cmds); ++i) {
1592 if (!strcmp(cmds[i].name, *argv)) {
1593 cmds[i].fn(argc-1, argv+1);
1598 log_warnx("unknown command %s", *argv);
1602 parsecmd(char *cmd, char **argv, int len)
1607 memset(argv, 0, sizeof(*argv) * len);
1609 while (argc < len) {
1610 while (isspace((unsigned char)*cmd))
1617 for (; *cmd != '\0'; ++cmd) {
1624 memmove(cmd, cmd + 1, strlen(cmd));
1628 if (*cmd == quote) {
1630 memmove(cmd, cmd + 1, strlen(cmd));
1634 if (*cmd == '\'' || *cmd == '"') {
1636 memmove(cmd, cmd + 1, strlen(cmd));
1643 if (isspace((unsigned char)*cmd))
1647 if (*cmd == '\0' && (escape || quote)) {
1648 fprintf(stderr, "unterminated %s\n",
1649 escape ? "escape" : "quote");
1659 fprintf(stderr, "too many arguments\n");
1666 cd_or_fetch(const char *path, const char *outfile)
1672 while (*path == '/')
1678 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1680 errx(1, "walk %s: %s", path, errstr);
1682 errc(1, ENOENT, "walk %s", path);
1684 if (qid.type & QTDIR) {
1686 errx(1, "can't fetch directory %s", path);
1692 if (outfile == NULL) {
1693 if ((outfile = strrchr(path, '/')) == NULL)
1697 if (*outfile == '\0')
1698 errx(1, "invalid path: missing file name: %s",
1702 if (strcmp(outfile, "-") != 0) {
1703 fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1705 err(1, "can't open for writing %s", outfile);
1709 if (fetch_fid(nfid, fd, outfile) == -1)
1710 err(1, "write %s", outfile);
1716 parse_addr(const char *url, const char **user,
1717 const char **port, const char **path)
1719 static char buf[PATH_MAX];
1722 *user = *port = *path = NULL;
1725 if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1726 errx(1, "connection string too long");
1728 if (!strncmp(host, "9p://", 5))
1731 if ((t = strchr(host, '/')) != NULL) {
1733 errx(1, "invalid connection string: %s", url);
1739 if ((t = strchr(host, '@')) != NULL) {
1741 errx(1, "invalid connection string: %s", url);
1745 } else if ((*user = getenv("USER")) == NULL)
1746 errx(1, "USER not defined");
1748 if ((t = strchr(host, ':')) != NULL) {
1760 main(int argc, char **argv)
1762 const char *user, *host, *port, *path;
1763 const char *outfile = NULL;
1766 log_init(1, LOG_DAEMON);
1768 log_procinit(getprogname());
1770 while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1776 case 'c': /* deprecated, remove after 0.3 */
1793 if (argc == 0 || (tls && crtpath == NULL))
1796 host = parse_addr(argv[0], &user, &port, &path);
1797 if (path == NULL && argv[1] != NULL) /* drop argv[1] after 0.3 */
1799 if (outfile && path == NULL)
1802 signal(SIGPIPE, SIG_IGN);
1806 signal(SIGWINCH, tty_resized);
1809 if ((evb = evbuffer_new()) == NULL)
1810 fatal("evbuffer_new");
1812 if ((buf = evbuffer_new()) == NULL)
1813 fatal("evbuffer_new");
1815 if ((dirbuf = evbuffer_new()) == NULL)
1816 fatal("evbuferr_new");
1818 do_connect(host, port, user);
1820 cd_or_fetch(path, outfile);
1825 char *line, *argv[16] = {0}, **ap;
1827 if ((line = read_line("kamiftp> ")) == NULL)
1830 if ((argc = parsecmd(line, argv, nitems(argv) - 1)) == -1) {
1836 excmd(argc, (const char **)argv);
1839 fprintf(stderr, "\a");