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 *buf, size_t len)
201 r = read(sock, d, len);
202 if (r == 0 || r == -1)
205 r = tls_read(ctx, d, len);
206 if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
209 errx(1, "tls: %s", tls_error(ctx));
223 mustread(&len, sizeof(len));
225 if (len < HEADERSIZE)
226 errx(1, "read message of invalid length %d", len);
228 len -= 4; /* skip the length just read */
231 l = MIN(len, sizeof(tmp));
234 evbuffer_add(buf, tmp, l);
238 hexdump("incoming packet", EVBUFFER_DATA(buf),
239 EVBUFFER_LENGTH(buf));
243 np_read64(struct evbuffer *buf)
247 evbuffer_remove(buf, &n, sizeof(n));
252 np_read32(struct evbuffer *buf)
256 evbuffer_remove(buf, &n, sizeof(n));
261 np_read16(struct evbuffer *buf)
265 evbuffer_remove(buf, &n, sizeof(n));
270 np_read8(struct evbuffer *buf)
274 evbuffer_remove(buf, &n, sizeof(n));
279 np_readstr(struct evbuffer *buf)
284 len = np_read16(buf);
285 assert(EVBUFFER_LENGTH(buf) >= len);
287 if ((str = calloc(1, len+1)) == NULL)
289 evbuffer_remove(buf, str, len);
294 np_read_qid(struct evbuffer *buf, struct qid *qid)
296 assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
298 qid->type = np_read8(buf);
299 qid->vers = np_read32(buf);
300 qid->path = np_read64(buf);
304 np_read_stat(struct evbuffer *buf, struct np_stat *st)
308 memset(st, 0, sizeof(*st));
310 size = np_read16(buf);
311 if (size > EVBUFFER_LENGTH(buf))
314 st->type = np_read16(buf);
315 st->dev = np_read32(buf);
316 np_read_qid(buf, &st->qid);
317 st->mode = np_read32(buf);
318 st->atime = np_read32(buf);
319 st->mtime = np_read32(buf);
320 st->length = np_read64(buf);
321 st->name = np_readstr(buf);
322 st->uid = np_readstr(buf);
323 st->gid = np_readstr(buf);
324 st->muid = np_readstr(buf);
344 err = np_readstr(buf);
345 errx(1, "expected %s, got error %s",
346 pp_msg_type(type), err);
349 errx(1, "expected %s, got msg type %s",
350 pp_msg_type(type), pp_msg_type(t));
354 expect2(uint8_t type, uint16_t tag)
364 errx(1, "expected tag 0x%x, got 0x%x", tag, t);
368 check(uint8_t type, uint16_t tag)
373 rtype = np_read8(buf);
374 rtag = np_read16(buf);
377 errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
382 return np_readstr(buf);
384 errx(1, "expected %s, got msg type %s",
385 pp_msg_type(type), pp_msg_type(rtype));
393 tversion(VERSION9P, MSIZE9P);
396 expect2(Rversion, NOTAG);
398 msize = np_read32(buf);
399 version = np_readstr(buf);
401 if (msize > MSIZE9P || msize < 256)
402 errx(1, "got unexpected msize: %d", msize);
403 if (strcmp(version, VERSION9P))
404 errx(1, "unexpected 9p version: %s", version);
411 do_attach(const char *user)
415 tattach(pwdfid, NOFID, user, "/");
418 expect2(Rattach, iota_tag);
419 np_read_qid(buf, &qid);
425 do_open(uint32_t fid, uint8_t mode)
433 expect2(Ropen, iota_tag);
435 np_read_qid(buf, &qid);
436 iounit = np_read32(buf);
444 do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
449 tcreate(fid, name, perm, mode);
452 expect2(Rcreate, iota_tag);
454 np_read_qid(buf, &qid);
455 iounit = np_read32(buf);
463 do_clunk(uint32_t fid)
468 expect2(Rclunk, iota_tag);
474 dup_fid(int fid, int nfid)
479 twalk(fid, nfid, NULL, 0);
483 if ((errstr = check(Rwalk, iota_tag)) != NULL)
486 nwqid = np_read16(buf);
495 walk_path(int fid, int newfid, const char *path, int *missing,
498 char *wnames[MAXWELEM], *p, *t, *errstr;
502 if ((p = strdup(path)) == NULL)
506 /* strip initial ./ */
507 if (t[0] == '.' && t[1] == '/')
510 for (nwname = 0; nwname < nitems(wnames) &&
511 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
512 if (*wnames[nwname] != '\0')
516 twalk(fid, newfid, (const char **)wnames, nwname);
521 if ((errstr = check(Rwalk, iota_tag)) != NULL) {
526 nwqid = np_read16(buf);
527 assert(nwqid <= nwname);
529 /* consume all qids */
530 for (i = 0; i < nwqid; ++i)
531 np_read_qid(buf, qid);
535 *missing = nwname - nwqid;
540 do_stat(int fid, struct np_stat *st)
545 expect2(Rstat, iota_tag);
547 /* eat up the first two byte length */
550 if (np_read_stat(buf, st) == -1)
551 errx(1, "invalid stat struct read");
557 do_wstat(int fid, const struct np_stat *st)
565 if ((errstr = check(Rwstat, iota_tag)) != NULL)
581 if ((errstr = check(Rremove, iota_tag)) != NULL)
590 do_read(int fid, uint64_t off, uint32_t count, void *data)
594 tread(fid, off, count);
597 expect2(Rread, iota_tag);
600 assert(r == EVBUFFER_LENGTH(buf));
602 evbuffer_remove(buf, data, r);
610 do_write(int fid, uint64_t off, uint32_t count, void *data)
614 twrite(fid, off, data, count);
617 expect2(Rwrite, iota_tag);
628 draw_progress(const char *pre, const struct progress *p)
637 perc = 100.0 * p->done / p->max;
639 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
646 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
648 tty_width = ws.ws_col;
653 ((l = fprintf(stderr, "\r%s ", pre)) == -1 || l >= w))
656 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
658 fprintf(stderr, "%4d%%\n", (int)perc);
662 fprintf(stderr, "|");
664 l = w * MIN(100.0, perc) / 100.0;
665 for (i = 0; i < l; i++)
666 fprintf(stderr, "*");
668 fprintf(stderr, " ");
669 fprintf(stderr, "|%4d%%", (int)perc);
673 fetch_fid(int fid, int fd, const char *name)
675 static char buf[MSIZE9P];
676 struct progress p = {0};
682 do_open(fid, KOREAD);
689 len = MIN(sizeof(buf), msize);
690 len -= IOHDRSZ; /* for the request' fields */
692 r = do_read(fid, p.done, len, buf);
696 for (off = 0; off < r; off += nw)
697 if ((nw = write(fd, buf + off, r - off)) == 0 ||
704 draw_progress(name, &p);
707 /* throttle, for debugging purpose */
709 struct timespec ts = { 0, 500000000 };
710 nanosleep(&ts, NULL);
723 send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
725 static char buf[MSIZE9P];
726 struct progress p = {0};
731 if (fstat(fd, &sb) == -1)
735 do_create(fid, fnam, 0644, KOWRITE);
737 do_open(fid, open_flags | KOWRITE);
741 len = MIN(sizeof(buf), msize);
742 len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
744 r = read(fd, buf, len);
750 w = do_write(fid, p.done, r, buf);
753 draw_progress(name, &p);
756 /* throttle, for debugging purpose */
758 struct timespec ts = { 0, 500000000 };
759 nanosleep(&ts, NULL);
769 woc_file(int fd, const char *prompt, const char *path)
772 const char *n = NULL;
777 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
778 if (errstr != NULL && miss > 1) {
779 fprintf(stderr, "%s: %s\n", path, errstr);
784 if (errstr != NULL || miss == 1) {
785 char p[PATH_MAX], *dn;
788 * If it's only one component missing (the file name), walk
789 * to the parent directory and try to create the file.
792 if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
793 fprintf(stderr, "path too long: %s\n", path);
798 if (!strcmp(dn, ".")) {
799 errstr = dup_fid(pwdfid, nfid);
802 errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
804 if (errstr != NULL) {
805 fprintf(stderr, "%s: %s\n", dn, errstr);
811 fprintf(stderr, "%s: not a directory\n", dn);
815 if ((n = strrchr(path, '/')) != NULL)
824 fprintf(stderr, "can't create %s: missing %d path"
825 " component(s)\n", path, miss);
829 send_fid(nfid, n, KOTRUNC, fd, prompt);
834 do_tls_connect(const char *host, const char *port)
838 if ((tlsconf = tls_config_new()) == NULL)
839 fatalx("tls_config_new");
840 tls_config_insecure_noverifycert(tlsconf);
841 tls_config_insecure_noverifyname(tlsconf);
846 if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
847 fatalx("can't load certs (%s, %s)", crtpath, keypath);
849 if ((ctx = tls_client()) == NULL)
851 if (tls_configure(ctx, tlsconf) == -1)
852 fatalx("tls_configure: %s", tls_error(ctx));
854 if (tls_connect(ctx, host, port) == -1)
855 fatalx("can't connect to %s:%s: %s", host, port,
858 for (handshake = 0; !handshake;) {
859 switch (tls_handshake(ctx)) {
861 fatalx("tls_handshake: %s", tls_error(ctx));
870 do_ctxt_connect(const char *host, const char *port)
872 struct addrinfo hints, *res, *res0;
873 int error, saved_errno;
874 const char *cause = NULL;
876 memset(&hints, 0, sizeof(hints));
877 hints.ai_family = AF_UNSPEC;
878 hints.ai_socktype = SOCK_STREAM;
879 error = getaddrinfo(host, port, &hints, &res0);
881 errx(1, "%s", gai_strerror(error));
884 for (res = res0; res != NULL; res = res->ai_next) {
885 sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
892 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
910 do_connect(const char *host, const char *port, const char *user)
912 fprintf(stderr, "connecting to %s:%s...", host, port);
915 do_tls_connect(host, port);
917 do_ctxt_connect(host, port);
919 fprintf(stderr, " done!\n");
926 tmp_file(char sfn[TMPFSTRLEN])
930 strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
931 if ((tmpfd = mkstemp(sfn)) == -1) {
932 warn("mkstemp %s", sfn);
936 /* set the close-on-exec flag */
937 if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
946 static inline const char *
967 /* unreachable, just for the compiler' happiness */
973 prepare_wstat(struct np_stat *st)
975 memset(st, 0xFF, sizeof(*st));
983 print_dirent(const struct np_stat *st)
988 char fmt[FMT_SCALED_STRSIZE], tim[13];
990 if (fmt_scaled(st->length, fmt) == -1)
991 strlcpy(fmt, "xxx", sizeof(fmt));
995 if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1000 if ((tm = localtime(&mtime)) == NULL ||
1001 strftime(tim, sizeof(tim), timfmt, tm) == 0)
1002 strlcpy(tim, "unknown", sizeof(tim));
1004 if (st->qid.type & QTDIR)
1008 printf("%s", pp_perm(st->mode >> 6));
1009 printf("%s", pp_perm(st->mode >> 3));
1010 printf("%s", pp_perm(st->mode));
1011 printf(" %8s %12s %s%s\n", fmt, tim, st->name,
1012 st->qid.type & QTDIR ? "/" : "");
1018 dir_listing(const char *path, int (*fn)(const struct np_stat *),
1021 struct qid qid = {0, 0, QTDIR};
1025 int nfid, r, miss = 0;
1031 if (!strcmp(path, "."))
1032 errstr = dup_fid(pwdfid, nfid);
1034 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1035 if (errstr != NULL) {
1037 printf("%s: %s\n", path, errstr);
1043 printf("%s: No such file or directory\n", path);
1046 if (!(qid.type & QTDIR)) {
1048 printf("%s: not a directory\n", path);
1053 do_open(nfid, KOREAD);
1054 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1057 tread(nfid, off, msize - IOHDRSZ);
1060 expect2(Rread, iota_tag);
1062 len = np_read32(buf);
1066 evbuffer_add_buffer(dirbuf, buf);
1072 while (EVBUFFER_LENGTH(dirbuf) != 0) {
1073 if (np_read_stat(dirbuf, &st) == -1)
1074 errx(1, "invalid stat struct read");
1087 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1093 cmd_bell(int argc, const char **argv)
1098 puts("bell mode enabled");
1100 puts("bell mode disabled");
1107 if (!strcmp(*argv, "on")) {
1109 puts("bell mode enabled");
1113 if (!strcmp(*argv, "off")) {
1115 puts("bell mode disabled");
1120 printf("bell [on | off]\n");
1124 cmd_bye(int argc, const char **argv)
1131 cmd_cd(int argc, const char **argv)
1138 printf("usage: cd remote-path\n");
1143 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1144 if (errstr != NULL) {
1145 printf("%s: %s\n", argv[0], errstr);
1150 if (miss != 0 || !(qid.type & QTDIR)) {
1151 printf("%s: not a directory\n", argv[0]);
1162 cmd_edit(int argc, const char **argv)
1165 int nfid, tmpfd, miss;
1166 char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1170 puts("usage: edit file");
1174 if ((ed = getenv("VISUAL")) == NULL &&
1175 (ed = getenv("EDITOR")) == NULL)
1179 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1180 if (errstr != NULL) {
1181 printf("%s: %s\n", *argv, errstr);
1186 if (miss != 0 || qid.type != 0) {
1187 printf("%s: not a file\n", *argv);
1193 if ((tmpfd = tmp_file(sfn)) == -1) {
1198 strlcpy(p, *argv, sizeof(p));
1201 if (fetch_fid(nfid, tmpfd, name)) {
1202 warn("failed fetch or can't write %s", sfn);
1207 spawn(ed, sfn, NULL);
1210 * Re-open the file because it's not guaranteed that the
1211 * file descriptor tmpfd is still associated with the file
1212 * pointed by sfn: it's not uncommon for editor to write
1213 * a backup file and then rename(2) it to the file name.
1215 if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1216 warn("can't open %s", sfn);
1220 woc_file(tmpfd, *argv, name);
1228 cmd_get(int argc, const char **argv)
1235 if (argc != 1 && argc != 2) {
1236 printf("usage: get remote-file [local-file]\n");
1242 else if ((l = strrchr(argv[0], '/')) != NULL)
1248 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1249 if (errstr != NULL) {
1250 printf("%s: %s\n", argv[0], errstr);
1255 if (miss != 0 || qid.type != 0) {
1256 printf("%s: not a file\n", argv[0]);
1262 if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1263 warn("can't open %s", l);
1268 if (fetch_fid(nfid, fd, l) == -1)
1269 warn("write %s", l);
1274 cmd_hexdump(int argc, const char **argv)
1279 puts("hexdump mode enabled");
1281 puts("hexdump mode disabled");
1288 if (!strcmp(*argv, "on")) {
1290 puts("hexdump mode enabled");
1294 if (!strcmp(*argv, "off")) {
1296 puts("hexdump mode disabled");
1301 puts("usage: hexdump [on | off]");
1305 cmd_lcd(int argc, const char **argv)
1310 printf("usage: lcd [local-directory]\n");
1317 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1318 printf("HOME is not defined\n");
1322 if (chdir(dir) == -1)
1323 printf("cd: %s: %s\n", dir, strerror(errno));
1327 cmd_lpwd(int argc, const char **argv)
1329 char path[PATH_MAX];
1332 printf("usage: lpwd\n");
1336 if (getcwd(path, sizeof(path)) == NULL) {
1337 printf("lpwd: %s\n", strerror(errno));
1341 printf("%s\n", path);
1345 cmd_ls(int argc, const char **argv)
1348 puts("usage: ls [path]");
1352 dir_listing(argc == 0 ? "." : argv[0], print_dirent, 1);
1356 cmd_page(int argc, const char **argv)
1359 int nfid, tmpfd, miss, r;
1360 char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1364 puts("usage: page file");
1368 if ((pager = getenv("PAGER")) == NULL)
1372 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1373 if (errstr != NULL) {
1374 printf("%s: %s\n", *argv, errstr);
1379 if (miss != 0 || qid.type != 0) {
1380 printf("%s: not a file\n", *argv);
1386 if ((tmpfd = tmp_file(sfn)) == -1) {
1391 strlcpy(p, *argv, sizeof(p));
1393 if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1394 warn("write %s", sfn);
1397 spawn(pager, sfn, NULL);
1402 cmd_pipe(int argc, const char **argv)
1406 int nfid, miss, status;
1407 int filedes[2]; /* read end, write end */
1411 puts("usage: pipe remote-file cmd [args...]");
1416 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1417 if (errstr != NULL) {
1418 printf("%s: %s\n", *argv, errstr);
1423 if (miss != 0 || qid.type != 0) {
1424 printf("%s: not a file\n", *argv);
1430 if (pipe(filedes) == -1)
1433 switch (pid = vfork()) {
1438 if (dup2(filedes[0], 0) == -1)
1440 execvp(argv[1], (char *const *)argv + 1);
1445 if (fetch_fid(nfid, filedes[1], *argv) == -1)
1446 warnx("failed to fetch all the file");
1449 waitpid(pid, &status, 0);
1453 cmd_put(int argc, const char **argv)
1458 if (argc != 1 && argc != 2) {
1459 printf("usage: put local-file [remote-file]\n");
1465 else if ((l = strrchr(argv[0], '/')) != NULL)
1470 if ((fd = open(argv[0], O_RDONLY)) == -1) {
1471 warn("%s", argv[0]);
1475 woc_file(fd, argv[0], l);
1480 cmd_rename(int argc, const char **argv)
1488 puts("usage: rename remote-file new-remote-name");
1493 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1494 if (errstr != NULL) {
1495 printf("%s: %s\n", argv[0], errstr);
1501 printf("%s: not such file or directory\n", argv[0]);
1506 st.name = (char *)argv[1];
1507 if ((errstr = do_wstat(nfid, &st)) != NULL) {
1508 printf("rename: %s\n", errstr);
1516 cmd_rm(int argc, const char **argv)
1523 puts("usage: rm file ...");
1527 for (; *argv; ++argv, --argc) {
1529 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1530 if (errstr != NULL) {
1531 printf("%s: %s\n", *argv, errstr);
1536 printf("%s: not such file or directory\n", *argv);
1540 if ((errstr = do_remove(nfid)) != NULL) {
1541 printf("%s: %s\n", *argv, errstr);
1549 cmd_verbose(int argc, const char **argv)
1552 log_setverbose(!log_getverbose());
1553 if (log_getverbose())
1554 puts("verbose mode enabled");
1556 puts("verbose mode disabled");
1563 if (!strcmp(*argv, "on")) {
1565 puts("verbose mode enabled");
1569 if (!strcmp(*argv, "off")) {
1571 puts("verbose mode disabled");
1576 printf("verbose [on | off]\n");
1580 excmd(int argc, const char **argv)
1587 for (i = 0; i < nitems(cmds); ++i) {
1588 if (!strcmp(cmds[i].name, *argv)) {
1589 cmds[i].fn(argc-1, argv+1);
1594 log_warnx("unknown command %s", *argv);
1598 parsecmd(char *cmd, char **argv, int len)
1603 memset(argv, 0, sizeof(*argv) * len);
1605 while (argc < len) {
1606 while (isspace((unsigned char)*cmd))
1613 for (; *cmd != '\0'; ++cmd) {
1620 memmove(cmd, cmd + 1, strlen(cmd));
1624 if (*cmd == quote) {
1626 memmove(cmd, cmd + 1, strlen(cmd));
1630 if (*cmd == '\'' || *cmd == '"') {
1632 memmove(cmd, cmd + 1, strlen(cmd));
1639 if (isspace((unsigned char)*cmd))
1643 if (*cmd == '\0' && (escape || quote)) {
1644 fprintf(stderr, "unterminated %s\n",
1645 escape ? "escape" : "quote");
1655 fprintf(stderr, "too many arguments\n");
1662 cd_or_fetch(const char *path, const char *outfile)
1668 while (*path == '/')
1674 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1676 errx(1, "walk %s: %s", path, errstr);
1678 errc(1, ENOENT, "walk %s", path);
1680 if (qid.type & QTDIR) {
1682 errx(1, "can't fetch directory %s", path);
1688 if (outfile == NULL) {
1689 if ((outfile = strrchr(path, '/')) == NULL)
1693 if (*outfile == '\0')
1694 errx(1, "invalid path: missing file name: %s",
1698 if (strcmp(outfile, "-") != 0) {
1699 fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1701 err(1, "can't open for writing %s", outfile);
1705 if (fetch_fid(nfid, fd, outfile) == -1)
1706 err(1, "write %s", outfile);
1712 parse_addr(const char *url, const char **user,
1713 const char **port, const char **path)
1715 static char buf[PATH_MAX];
1718 *user = *port = *path = NULL;
1721 if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1722 errx(1, "connection string too long");
1724 if (!strncmp(host, "9p://", 5))
1727 if ((t = strchr(host, '/')) != NULL) {
1729 errx(1, "invalid connection string: %s", url);
1735 if ((t = strchr(host, '@')) != NULL) {
1737 errx(1, "invalid connection string: %s", url);
1741 } else if ((*user = getenv("USER")) == NULL)
1742 errx(1, "USER not defined");
1744 if ((t = strchr(host, ':')) != NULL) {
1756 main(int argc, char **argv)
1758 const char *user, *host, *port, *path;
1759 const char *outfile = NULL;
1762 log_init(1, LOG_DAEMON);
1764 log_procinit(getprogname());
1766 while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1772 case 'c': /* deprecated, remove after 0.3 */
1789 if (argc == 0 || (tls && crtpath == NULL))
1792 host = parse_addr(argv[0], &user, &port, &path);
1793 if (path == NULL && argv[1] != NULL) /* drop argv[1] after 0.3 */
1795 if (outfile && path == NULL)
1798 signal(SIGPIPE, SIG_IGN);
1802 signal(SIGWINCH, tty_resized);
1805 if ((evb = evbuffer_new()) == NULL)
1806 fatal("evbuffer_new");
1808 if ((buf = evbuffer_new()) == NULL)
1809 fatal("evbuffer_new");
1811 if ((dirbuf = evbuffer_new()) == NULL)
1812 fatal("evbuferr_new");
1814 do_connect(host, port, user);
1816 cd_or_fetch(path, outfile);
1821 char *line, *argv[16] = {0};
1823 if ((line = read_line("kamiftp> ")) == NULL)
1826 if ((argc = parsecmd(line, argv, nitems(argv) - 1)) == -1) {
1832 excmd(argc, (const char **)argv);
1835 fprintf(stderr, "\a");