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>
46 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
55 #define TMPFSTRLEN sizeof(TMPFSTR)
65 struct evbuffer *dirbuf;
69 volatile sig_atomic_t resized;
81 #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
85 readline(const char *prompt)
87 char *ch, *line = NULL;
94 linelen = getline(&line, &linesize, stdin);
98 if ((ch = strchr(line, '\n')) != NULL)
104 add_history(const char *line)
111 read_line(const char *prompt)
116 if ((line = readline(prompt)) == NULL)
118 /* XXX: trim spaces? */
129 spawn(const char *argv0, ...)
134 const char *argv[16], *last;
137 memset(argv, 0, sizeof(argv));
141 for (i = 1; i < nitems(argv); ++i) {
142 last = va_arg(ap, const char *);
149 assert(last == NULL);
151 switch (pid = fork()) {
155 execvp(argv[0], (char *const *)argv);
158 waitpid(pid, &status, 0);
163 stdio_tls_write(void *arg, const char *buf, int len)
165 struct tls *ctx = arg;
169 ret = tls_write(ctx, buf, len);
170 } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
173 warn("tls_write: %s", tls_error(ctx));
179 stdio_tls_read(void *arg, char *buf, int len)
181 struct tls *ctx = arg;
185 ret = tls_read(ctx, buf, len);
186 } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
189 warn("tls_read: %s", tls_error(ctx));
195 stdio_tls_close(void *arg)
197 struct tls *ctx = arg;
201 ret = tls_close(ctx);
202 } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT);
208 tty_resized(int signo)
216 fprintf(stderr, "usage: %s [-C cert] [-K key] [-o output] "
217 "[user@]host[:port][/path]\n", getprogname());
218 fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
239 hexdump("outgoing message", EVBUFFER_DATA(evb),
240 EVBUFFER_LENGTH(evb));
242 while (EVBUFFER_LENGTH(evb) != 0) {
243 r = fwrite(EVBUFFER_DATA(evb), 1, EVBUFFER_LENGTH(evb), fp);
245 fatalx("unexpected EOF");
246 evbuffer_drain(evb, r);
251 mustread(void *d, size_t len)
255 r = fread(d, 1, len, fp);
257 errx(1, "unexpected EOF");
267 r = fread(&len, 1, sizeof(len), fp);
268 if (r != sizeof(len))
269 errx(1, "unexpected EOF");
272 if (len < HEADERSIZE)
273 errx(1, "read message of invalid length %d", len);
275 len -= 4; /* skip the length just read */
278 l = MIN(len, sizeof(tmp));
280 r = fread(tmp, 1, l, fp);
282 errx(1, "unexpected EOF");
284 evbuffer_add(buf, tmp, r);
288 hexdump("incoming packet", EVBUFFER_DATA(buf),
289 EVBUFFER_LENGTH(buf));
293 np_read64(struct evbuffer *buf)
297 evbuffer_remove(buf, &n, sizeof(n));
302 np_read32(struct evbuffer *buf)
306 evbuffer_remove(buf, &n, sizeof(n));
311 np_read16(struct evbuffer *buf)
315 evbuffer_remove(buf, &n, sizeof(n));
320 np_read8(struct evbuffer *buf)
324 evbuffer_remove(buf, &n, sizeof(n));
329 np_readstr(struct evbuffer *buf)
334 len = np_read16(buf);
335 assert(EVBUFFER_LENGTH(buf) >= len);
337 if ((str = calloc(1, len+1)) == NULL)
339 evbuffer_remove(buf, str, len);
344 np_read_qid(struct evbuffer *buf, struct qid *qid)
346 assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
348 qid->type = np_read8(buf);
349 qid->vers = np_read32(buf);
350 qid->path = np_read64(buf);
354 np_read_stat(struct evbuffer *buf, struct np_stat *st)
358 memset(st, 0, sizeof(*st));
360 size = np_read16(buf);
361 if (size > EVBUFFER_LENGTH(buf))
364 st->type = np_read16(buf);
365 st->dev = np_read32(buf);
366 np_read_qid(buf, &st->qid);
367 st->mode = np_read32(buf);
368 st->atime = np_read32(buf);
369 st->mtime = np_read32(buf);
370 st->length = np_read64(buf);
371 st->name = np_readstr(buf);
372 st->uid = np_readstr(buf);
373 st->gid = np_readstr(buf);
374 st->muid = np_readstr(buf);
394 err = np_readstr(buf);
395 errx(1, "expected %s, got error %s",
396 pp_msg_type(type), err);
399 errx(1, "expected %s, got msg type %s",
400 pp_msg_type(type), pp_msg_type(t));
404 expect2(uint8_t type, uint16_t tag)
414 errx(1, "expected tag 0x%x, got 0x%x", tag, t);
418 check(uint8_t type, uint16_t tag)
423 rtype = np_read8(buf);
424 rtag = np_read16(buf);
427 errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
432 return np_readstr(buf);
434 errx(1, "expected %s, got msg type %s",
435 pp_msg_type(type), pp_msg_type(rtype));
443 tversion(VERSION9P, MSIZE9P);
446 expect2(Rversion, NOTAG);
448 msize = np_read32(buf);
449 version = np_readstr(buf);
451 if (msize > MSIZE9P || msize < 256)
452 errx(1, "got unexpected msize: %d", msize);
453 if (strcmp(version, VERSION9P))
454 errx(1, "unexpected 9p version: %s", version);
461 do_attach(const char *user)
465 tattach(pwdfid, NOFID, user, "/");
468 expect2(Rattach, iota_tag);
469 np_read_qid(buf, &qid);
475 do_open(uint32_t fid, uint8_t mode)
483 expect2(Ropen, iota_tag);
485 np_read_qid(buf, &qid);
486 iounit = np_read32(buf);
494 do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
499 tcreate(fid, name, perm, mode);
502 expect2(Rcreate, iota_tag);
504 np_read_qid(buf, &qid);
505 iounit = np_read32(buf);
513 do_clunk(uint32_t fid)
518 expect2(Rclunk, iota_tag);
524 dup_fid(int fid, int nfid)
529 twalk(fid, nfid, NULL, 0);
533 if ((errstr = check(Rwalk, iota_tag)) != NULL)
536 nwqid = np_read16(buf);
545 walk_path(int fid, int newfid, const char *path, int *missing,
548 char *wnames[MAXWELEM], *p, *t, *errstr;
552 if ((p = strdup(path)) == NULL)
556 /* strip initial ./ */
557 if (t[0] == '.' && t[1] == '/')
560 for (nwname = 0; nwname < nitems(wnames) &&
561 (wnames[nwname] = strsep(&t, "/")) != NULL;) {
562 if (*wnames[nwname] != '\0')
566 twalk(fid, newfid, (const char **)wnames, nwname);
571 if ((errstr = check(Rwalk, iota_tag)) != NULL) {
576 nwqid = np_read16(buf);
577 assert(nwqid <= nwname);
579 /* consume all qids */
580 for (i = 0; i < nwqid; ++i)
581 np_read_qid(buf, qid);
585 *missing = nwname - nwqid;
590 do_stat(int fid, struct np_stat *st)
595 expect2(Rstat, iota_tag);
597 /* eat up the first two byte length */
600 if (np_read_stat(buf, st) == -1)
601 errx(1, "invalid stat struct read");
607 do_wstat(int fid, const struct np_stat *st)
615 if ((errstr = check(Rwstat, iota_tag)) != NULL)
624 do_read(int fid, uint64_t off, uint32_t count, void *data)
628 tread(fid, off, count);
631 expect2(Rread, iota_tag);
634 assert(r == EVBUFFER_LENGTH(buf));
636 evbuffer_remove(buf, data, r);
644 do_write(int fid, uint64_t off, uint32_t count, void *data)
648 twrite(fid, off, data, count);
651 expect2(Rwrite, iota_tag);
662 draw_progress(const char *pre, const struct progress *p)
671 perc = 100.0 * p->done / p->max;
673 fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
680 if (ioctl(0, TIOCGWINSZ, &ws) == -1)
682 tty_width = ws.ws_col;
687 ((l = fprintf(stderr, "\r%s ", pre)) == -1 || l >= w))
690 w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
692 fprintf(stderr, "%4d%%\n", (int)perc);
696 fprintf(stderr, "|");
698 l = w * MIN(100.0, perc) / 100.0;
699 for (i = 0; i < l; i++)
700 fprintf(stderr, "*");
702 fprintf(stderr, " ");
703 fprintf(stderr, "|%4d%%", (int)perc);
707 fetch_fid(int fid, int fd, const char *name)
709 static char buf[MSIZE9P];
710 struct progress p = {0};
716 do_open(fid, KOREAD);
723 len = MIN(sizeof(buf), msize);
724 len -= IOHDRSZ; /* for the request' fields */
726 r = do_read(fid, p.done, len, buf);
730 for (off = 0; off < r; off += nw)
731 if ((nw = write(fd, buf + off, r - off)) == 0 ||
738 draw_progress(name, &p);
741 /* throttle, for debugging purpose */
743 struct timespec ts = { 0, 500000000 };
744 nanosleep(&ts, NULL);
757 send_fid(int fid, const char *fnam, int open_flags, int fd, const char *name)
759 static char buf[MSIZE9P];
760 struct progress p = {0};
765 if (fstat(fd, &sb) == -1)
769 do_create(fid, fnam, 0644, KOWRITE);
771 do_open(fid, open_flags | KOWRITE);
775 len = MIN(sizeof(buf), msize);
776 len -= HEADERSIZE + 4 + 4 + 8; /* for the request' fields */
778 r = read(fd, buf, len);
784 w = do_write(fid, p.done, r, buf);
787 draw_progress(name, &p);
790 /* throttle, for debugging purpose */
792 struct timespec ts = { 0, 500000000 };
793 nanosleep(&ts, NULL);
803 woc_file(int fd, const char *prompt, const char *path)
806 const char *n = NULL;
811 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
812 if (errstr != NULL && miss > 1) {
813 fprintf(stderr, "%s: %s\n", path, errstr);
818 if (errstr != NULL || miss == 1) {
819 char p[PATH_MAX], *dn;
822 * If it's only one component missing (the file name), walk
823 * to the parent directory and try to create the file.
826 if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
827 fprintf(stderr, "path too long: %s\n", path);
832 if (!strcmp(dn, ".")) {
833 errstr = dup_fid(pwdfid, nfid);
836 errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
838 if (errstr != NULL) {
839 fprintf(stderr, "%s: %s\n", dn, errstr);
845 fprintf(stderr, "%s: not a directory\n", dn);
849 if ((n = strrchr(path, '/')) != NULL)
858 fprintf(stderr, "can't create %s: missing %d path"
859 " component(s)\n", path, miss);
863 send_fid(nfid, n, KOTRUNC, fd, prompt);
868 dial(const char *host, const char *port)
870 struct addrinfo hints, *res, *res0;
871 int sock, error, saved_errno;
872 const char *cause = NULL;
874 memset(&hints, 0, sizeof(hints));
875 hints.ai_family = AF_UNSPEC;
876 hints.ai_socktype = SOCK_STREAM;
877 error = getaddrinfo(host, port, &hints, &res0);
879 errx(1, "%s", gai_strerror(error));
882 for (res = res0; res != NULL; res = res->ai_next) {
883 sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
890 if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
908 do_connect(const char *host, const char *port, const char *user)
914 fprintf(stderr, "connecting to %s:%s...", host, port);
917 struct tls_config *conf;
921 if ((conf = tls_config_new()) == NULL)
922 fatalx("failed to create TLS config");
923 tls_config_insecure_noverifycert(conf);
924 tls_config_insecure_noverifyname(conf);
929 if (tls_config_set_keypair_file(conf, crtpath, keypath) == -1)
930 fatalx("failed to load certs: (%s, %s)", crtpath,
933 if ((ctx = tls_client()) == NULL)
934 fatalx("failed to create TLS client");
935 if (tls_configure(ctx, conf) == -1)
936 fatalx("failed to configure TLS client");
937 tls_config_free(conf);
939 if (tls_connect(ctx, host, port) == -1)
940 fatalx("failed to connect to %s:%s: %s", host, port,
944 r = tls_handshake(ctx);
945 } while (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT);
946 fp = funopen(ctx, stdio_tls_read, stdio_tls_write, NULL,
953 fd = dial(host, port);
954 if ((fp = fdopen(fd, "r+")) == NULL)
958 fprintf(stderr, " done!\n");
965 tmp_file(char sfn[TMPFSTRLEN])
969 strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
970 if ((tmpfd = mkstemp(sfn)) == -1) {
971 warn("mkstemp %s", sfn);
975 /* set the close-on-exec flag */
976 if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
985 static inline const char *
1006 /* unreachable, just for the compiler' happiness */
1012 prepare_wstat(struct np_stat *st)
1014 memset(st, 0xFF, sizeof(*st));
1022 cmd_bell(int argc, const char **argv)
1027 puts("bell mode enabled");
1029 puts("bell mode disabled");
1036 if (!strcmp(*argv, "on")) {
1038 puts("bell mode enabled");
1042 if (!strcmp(*argv, "off")) {
1044 puts("bell mode disabled");
1049 printf("bell [on | off]\n");
1053 cmd_bye(int argc, const char **argv)
1061 cmd_cd(int argc, const char **argv)
1068 printf("usage: cd remote-path\n");
1073 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1074 if (errstr != NULL) {
1075 printf("%s: %s\n", argv[0], errstr);
1080 if (miss != 0 || !(qid.type & QTDIR)) {
1081 printf("%s: not a directory\n", argv[0]);
1092 cmd_edit(int argc, const char **argv)
1095 int nfid, tmpfd, miss;
1096 char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1100 puts("usage: edit file");
1104 if ((ed = getenv("VISUAL")) == NULL &&
1105 (ed = getenv("EDITOR")) == NULL)
1109 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1110 if (errstr != NULL) {
1111 printf("%s: %s\n", *argv, errstr);
1116 if (miss != 0 || qid.type != 0) {
1117 printf("%s: not a file\n", *argv);
1123 if ((tmpfd = tmp_file(sfn)) == -1) {
1128 strlcpy(p, *argv, sizeof(p));
1131 if (fetch_fid(nfid, tmpfd, name)) {
1132 warn("failed fetch or can't write %s", sfn);
1137 spawn(ed, sfn, NULL);
1140 * Re-open the file because it's not guaranteed that the
1141 * file descriptor tmpfd is still associated with the file
1142 * pointed by sfn: it's not uncommon for editor to write
1143 * a backup file and then rename(2) it to the file name.
1145 if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1146 warn("can't open %s", sfn);
1150 woc_file(tmpfd, *argv, name);
1158 cmd_get(int argc, const char **argv)
1165 if (argc != 1 && argc != 2) {
1166 printf("usage: get remote-file [local-file]\n");
1172 else if ((l = strrchr(argv[0], '/')) != NULL)
1178 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1179 if (errstr != NULL) {
1180 printf("%s: %s\n", argv[0], errstr);
1185 if (miss != 0 || qid.type != 0) {
1186 printf("%s: not a file\n", argv[0]);
1192 if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1193 warn("can't open %s", l);
1198 if (fetch_fid(nfid, fd, l) == -1)
1199 warn("write %s", l);
1204 cmd_hexdump(int argc, const char **argv)
1209 puts("hexdump mode enabled");
1211 puts("hexdump mode disabled");
1218 if (!strcmp(*argv, "on")) {
1220 puts("hexdump mode enabled");
1224 if (!strcmp(*argv, "off")) {
1226 puts("hexdump mode disabled");
1231 puts("usage: hexdump [on | off]");
1235 cmd_lcd(int argc, const char **argv)
1240 printf("usage: lcd [local-directory]\n");
1247 if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1248 printf("HOME is not defined\n");
1252 if (chdir(dir) == -1)
1253 printf("cd: %s: %s\n", dir, strerror(errno));
1257 cmd_lpwd(int argc, const char **argv)
1259 char path[PATH_MAX];
1262 printf("usage: lpwd\n");
1266 if (getcwd(path, sizeof(path)) == NULL) {
1267 printf("lpwd: %s\n", strerror(errno));
1271 printf("%s\n", path);
1275 cmd_ls(int argc, const char **argv)
1284 char fmt[FMT_SCALED_STRSIZE], tim[13], *errstr;
1287 printf("ls don't take arguments (yet)\n");
1294 if ((errstr = dup_fid(pwdfid, nfid)) != NULL) {
1295 printf(".: %s\n", errstr);
1300 do_open(nfid, KOREAD);
1302 evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1305 tread(nfid, off, msize - IOHDRSZ);
1308 expect2(Rread, iota_tag);
1310 len = np_read32(buf);
1314 evbuffer_add_buffer(dirbuf, buf);
1320 while (EVBUFFER_LENGTH(dirbuf) != 0) {
1321 if (np_read_stat(dirbuf, &st) == -1)
1322 errx(1, "invalid stat struct read");
1324 if (fmt_scaled(st.length, fmt) == -1)
1325 strlcpy(fmt, "xxx", sizeof(fmt));
1329 if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1330 timfmt = "%b %e %R";
1332 timfmt = "%b %e %Y";
1334 if ((tm = localtime(&mtime)) == NULL ||
1335 strftime(tim, sizeof(tim), timfmt, tm) == 0)
1336 strlcpy(tim, "unknown", sizeof(tim));
1338 if (st.qid.type & QTDIR)
1342 printf("%s", pp_perm(st.mode >> 6));
1343 printf("%s", pp_perm(st.mode >> 3));
1344 printf("%s", pp_perm(st.mode));
1345 printf(" %8s %12s %s%s\n", fmt, tim, st.name,
1346 st.qid.type & QTDIR ? "/" : "");
1358 cmd_page(int argc, const char **argv)
1361 int nfid, tmpfd, miss, r;
1362 char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1366 puts("usage: page file");
1370 if ((pager = getenv("PAGER")) == NULL)
1374 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1375 if (errstr != NULL) {
1376 printf("%s: %s\n", *argv, errstr);
1381 if (miss != 0 || qid.type != 0) {
1382 printf("%s: not a file\n", *argv);
1388 if ((tmpfd = tmp_file(sfn)) == -1) {
1393 strlcpy(p, *argv, sizeof(p));
1395 if ((r = fetch_fid(nfid, tmpfd, name)) == -1)
1396 warn("write %s", sfn);
1399 spawn(pager, sfn, NULL);
1404 cmd_pipe(int argc, const char **argv)
1408 int nfid, tmpfd, miss, status;
1409 int filedes[2]; /* read end, write end */
1413 puts("usage: pipe remote-file cmd [args...]");
1418 errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1419 if (errstr != NULL) {
1420 printf("%s: %s\n", *argv, errstr);
1425 if (miss != 0 || qid.type != 0) {
1426 printf("%s: not a file\n", *argv);
1432 if (pipe(filedes) == -1)
1435 switch (pid = vfork()) {
1440 if (dup2(filedes[0], 0) == -1)
1442 execvp(argv[1], (char *const *)argv + 1);
1447 if (fetch_fid(nfid, filedes[1], *argv) == -1)
1448 warnx("failed to fetch all the file");
1451 waitpid(pid, &status, 0);
1455 cmd_put(int argc, const char **argv)
1461 if (argc != 1 && argc != 2) {
1462 printf("usage: put local-file [remote-file]\n");
1468 else if ((l = strrchr(argv[0], '/')) != NULL)
1473 if ((fd = open(argv[0], O_RDONLY)) == -1) {
1474 warn("%s", argv[0]);
1478 woc_file(fd, argv[0], l);
1483 cmd_rename(int argc, const char **argv)
1491 puts("usage: rename remote-file new-remote-name");
1496 errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1497 if (errstr != NULL) {
1498 printf("%s: %s\n", argv[0], errstr);
1504 printf("%s: not such file or directory\n", argv[0]);
1509 st.name = (char *)argv[1];
1510 if ((errstr = do_wstat(nfid, &st)) != NULL) {
1511 printf("rename: %s\n", errstr);
1519 cmd_verbose(int argc, const char **argv)
1522 log_setverbose(!log_getverbose());
1523 if (log_getverbose())
1524 puts("verbose mode enabled");
1526 puts("verbose mode disabled");
1533 if (!strcmp(*argv, "on")) {
1535 puts("verbose mode enabled");
1539 if (!strcmp(*argv, "off")) {
1541 puts("verbose mode disabled");
1546 printf("verbose [on | off]\n");
1550 excmd(int argc, const char **argv)
1554 void (*fn)(int, const char **);
1561 {"hexdump", cmd_hexdump},
1569 {"rename", cmd_rename},
1570 {"verbose", cmd_verbose},
1576 for (i = 0; i < nitems(cmds); ++i) {
1577 if (!strcmp(cmds[i].name, *argv)) {
1578 cmds[i].fn(argc-1, argv+1);
1583 log_warnx("unknown command %s", *argv);
1587 cd_or_fetch(const char *path, const char *outfile)
1593 while (*path == '/')
1599 errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
1601 errx(1, "walk %s: %s", path, errstr);
1603 errc(1, ENOENT, "walk %s", path);
1605 if (qid.type & QTDIR) {
1607 errx(1, "can't fetch directory %s", path);
1613 if (outfile == NULL) {
1614 if ((outfile = strrchr(path, '/')) == NULL)
1618 if (*outfile == '\0')
1619 errx(1, "invalid path: missing file name: %s",
1623 if (strcmp(outfile, "-") != 0) {
1624 fd = open(outfile, O_WRONLY|O_CREAT, 0644);
1626 err(1, "can't open for writing %s", outfile);
1630 if (fetch_fid(nfid, fd, outfile) == -1)
1631 err(1, "write %s", outfile);
1638 parse_addr(const char *url, const char **user,
1639 const char **port, const char **path)
1641 static char buf[PATH_MAX];
1644 *user = *port = *path = NULL;
1647 if (strlcpy(buf, url, sizeof(buf)) >= sizeof(buf))
1648 errx(1, "connection string too long");
1650 if ((t = strchr(host, '/')) != NULL) {
1652 errx(1, "invalid connection string: %s", url);
1658 if ((t = strchr(host, '@')) != NULL) {
1660 errx(1, "invalid connection string: %s", url);
1664 } else if ((*user = getenv("USER")) == NULL)
1665 errx(1, "USER not defined");
1667 if ((t = strchr(host, ':')) != NULL) {
1679 main(int argc, char **argv)
1681 const char *user, *host, *port, *path;
1682 const char *outfile = NULL;
1685 log_init(1, LOG_DAEMON);
1687 log_procinit(getprogname());
1689 while ((ch = getopt(argc, argv, "C:cK:o:")) != -1) {
1695 case 'c': /* deprecated, remove after 0.3 */
1712 if (argc == 0 || (tls && crtpath == NULL))
1715 host = parse_addr(argv[0], &user, &port, &path);
1716 if (path == NULL && argv[1] != NULL)
1718 if (outfile && path == NULL)
1721 signal(SIGPIPE, SIG_IGN);
1725 signal(SIGWINCH, tty_resized);
1728 if ((evb = evbuffer_new()) == NULL)
1729 fatal("evbuffer_new");
1731 if ((buf = evbuffer_new()) == NULL)
1732 fatal("evbuffer_new");
1734 if ((dirbuf = evbuffer_new()) == NULL)
1735 fatal("evbuferr_new");
1737 do_connect(host, port, user);
1739 cd_or_fetch(path, outfile);
1743 char *line, *argv[16] = {0}, **ap;
1745 if ((line = read_line("kamiftp> ")) == NULL)
1748 for (argc = 0, ap = argv; ap < &argv[15] &&
1749 (*ap = strsep(&line, " \t")) != NULL;) {
1753 excmd(argc, (const char **)argv);