2 dc8c1791 2022-01-10 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <sys/ioctl.h>
20 fb1a36c0 2022-01-09 op #include <sys/types.h>
21 fb1a36c0 2022-01-09 op #include <sys/socket.h>
22 01fb39ab 2022-01-16 op #include <sys/stat.h>
23 9e7dd230 2022-01-12 op #include <sys/wait.h>
25 fb1a36c0 2022-01-09 op #include <assert.h>
26 fb1a36c0 2022-01-09 op #include <errno.h>
27 fb1a36c0 2022-01-09 op #include <fcntl.h>
28 fb1a36c0 2022-01-09 op #include <inttypes.h>
29 fb1a36c0 2022-01-09 op #include <netdb.h>
30 9e7dd230 2022-01-12 op #include <libgen.h>
31 fb1a36c0 2022-01-09 op #include <limits.h>
32 fb1a36c0 2022-01-09 op #include <signal.h>
33 fb1a36c0 2022-01-09 op #include <stdio.h>
34 fb1a36c0 2022-01-09 op #include <stdlib.h>
35 fb1a36c0 2022-01-09 op #include <string.h>
36 fb1a36c0 2022-01-09 op #include <syslog.h>
37 fb1a36c0 2022-01-09 op #include <tls.h>
38 fb1a36c0 2022-01-09 op #include <unistd.h>
40 91cfa92a 2022-01-10 op #ifdef HAVE_READLINE
41 fb1a36c0 2022-01-09 op #include <readline/readline.h>
42 fb1a36c0 2022-01-09 op #include <readline/history.h>
45 fb1a36c0 2022-01-09 op #ifndef nitems
46 fb1a36c0 2022-01-09 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 fb1a36c0 2022-01-09 op #include "kami.h"
50 fb1a36c0 2022-01-09 op #include "utils.h"
51 fb1a36c0 2022-01-09 op #include "log.h"
52 c37e1cfe 2022-01-17 op #include "9pclib.h"
54 64d8db89 2022-01-16 op #define TMPFSTR "/tmp/kamiftp.XXXXXXXXXX"
55 64d8db89 2022-01-16 op #define TMPFSTRLEN sizeof(TMPFSTR)
59 fb1a36c0 2022-01-09 op const char *crtpath;
60 fb1a36c0 2022-01-09 op const char *keypath;
63 fb1a36c0 2022-01-09 op struct tls_config *tlsconf;
64 fb1a36c0 2022-01-09 op struct tls *ctx;
66 fb1a36c0 2022-01-09 op struct evbuffer *buf;
67 fb1a36c0 2022-01-09 op struct evbuffer *dirbuf;
68 fb1a36c0 2022-01-09 op uint32_t msize;
71 fb1a36c0 2022-01-09 op volatile sig_atomic_t resized;
73 fb1a36c0 2022-01-09 op int tty_width;
76 fb1a36c0 2022-01-09 op struct progress {
78 fb1a36c0 2022-01-09 op uint64_t done;
83 fb1a36c0 2022-01-09 op #define ASSERT_EMPTYBUF() assert(EVBUFFER_LENGTH(buf) == 0)
85 91cfa92a 2022-01-10 op #if !HAVE_READLINE
87 91cfa92a 2022-01-10 op readline(const char *prompt)
89 91cfa92a 2022-01-10 op char *ch, *line = NULL;
90 91cfa92a 2022-01-10 op size_t linesize = 0;
91 91cfa92a 2022-01-10 op ssize_t linelen;
93 91cfa92a 2022-01-10 op printf("%s", prompt);
94 91cfa92a 2022-01-10 op fflush(stdout);
96 91cfa92a 2022-01-10 op linelen = getline(&line, &linesize, stdin);
97 91cfa92a 2022-01-10 op if (linelen == -1)
100 91cfa92a 2022-01-10 op if ((ch = strchr(line, '\n')) != NULL)
106 91cfa92a 2022-01-10 op add_history(const char *line)
112 fb1a36c0 2022-01-09 op static char *
113 fb1a36c0 2022-01-09 op read_line(const char *prompt)
118 fb1a36c0 2022-01-09 op if ((line = readline(prompt)) == NULL)
120 fb1a36c0 2022-01-09 op /* XXX: trim spaces? */
121 fb1a36c0 2022-01-09 op if (*line == '\0') {
126 fb1a36c0 2022-01-09 op add_history(line);
131 9e7dd230 2022-01-12 op spawn(const char *argv0, ...)
136 9e7dd230 2022-01-12 op const char *argv[16], *last;
139 9e7dd230 2022-01-12 op memset(argv, 0, sizeof(argv));
141 9e7dd230 2022-01-12 op va_start(ap, argv0);
142 9e7dd230 2022-01-12 op argv[0] = argv0;
143 9e7dd230 2022-01-12 op for (i = 1; i < nitems(argv); ++i) {
144 9e7dd230 2022-01-12 op last = va_arg(ap, const char *);
145 9e7dd230 2022-01-12 op if (last == NULL)
147 9e7dd230 2022-01-12 op argv[i] = last;
151 9e7dd230 2022-01-12 op assert(last == NULL);
153 9e7dd230 2022-01-12 op switch (pid = fork()) {
155 9e7dd230 2022-01-12 op err(1, "fork");
156 9e7dd230 2022-01-12 op case 0: /* child */
157 9e7dd230 2022-01-12 op execvp(argv[0], (char *const *)argv);
158 9e7dd230 2022-01-12 op err(1, "execvp");
160 9e7dd230 2022-01-12 op waitpid(pid, &status, 0);
165 fb1a36c0 2022-01-09 op tty_resized(int signo)
170 fb1a36c0 2022-01-09 op static void __dead
171 fb1a36c0 2022-01-09 op usage(int ret)
173 dc8c1791 2022-01-10 op fprintf(stderr, "usage: %s [-c] [-C cert] [-K key] "
174 dc8c1791 2022-01-10 op "host[:port] [path]\n", getprogname());
175 fb1a36c0 2022-01-09 op fprintf(stderr, "kamid suite version " KAMID_VERSION "\n");
180 fb1a36c0 2022-01-09 op do_send(void)
182 fb1a36c0 2022-01-09 op const void *buf;
183 fb1a36c0 2022-01-09 op size_t nbytes;
187 d33c93c4 2022-01-17 op hexdump("outgoing message", EVBUFFER_DATA(evb),
188 5565d021 2022-01-16 op EVBUFFER_LENGTH(evb));
190 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(evb) != 0) {
191 fb1a36c0 2022-01-09 op buf = EVBUFFER_DATA(evb);
192 fb1a36c0 2022-01-09 op nbytes = EVBUFFER_LENGTH(evb);
194 fb1a36c0 2022-01-09 op if (ctx == NULL) {
195 fb1a36c0 2022-01-09 op r = write(sock, buf, nbytes);
196 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
197 fb1a36c0 2022-01-09 op errx(1, "EOF");
199 fb1a36c0 2022-01-09 op r = tls_write(ctx, buf, nbytes);
200 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
203 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
206 fb1a36c0 2022-01-09 op evbuffer_drain(evb, r);
211 fb1a36c0 2022-01-09 op mustread(void *d, size_t len)
215 fb1a36c0 2022-01-09 op while (len != 0) {
216 fb1a36c0 2022-01-09 op if (ctx == NULL) {
217 fb1a36c0 2022-01-09 op r = read(sock, d, len);
218 fb1a36c0 2022-01-09 op if (r == 0 || r == -1)
219 fb1a36c0 2022-01-09 op errx(1, "EOF");
221 fb1a36c0 2022-01-09 op r = tls_read(ctx, d, len);
222 fb1a36c0 2022-01-09 op if (r == TLS_WANT_POLLIN || r == TLS_WANT_POLLOUT)
225 fb1a36c0 2022-01-09 op errx(1, "tls: %s", tls_error(ctx));
234 fb1a36c0 2022-01-09 op recv_msg(void)
236 fb1a36c0 2022-01-09 op uint32_t len, l;
237 fb1a36c0 2022-01-09 op char tmp[BUFSIZ];
239 fb1a36c0 2022-01-09 op mustread(&len, sizeof(len));
240 fb1a36c0 2022-01-09 op len = le32toh(len);
241 fb1a36c0 2022-01-09 op if (len < HEADERSIZE)
242 fb1a36c0 2022-01-09 op errx(1, "read message of invalid length %d", len);
244 fb1a36c0 2022-01-09 op len -= 4; /* skip the length just read */
246 fb1a36c0 2022-01-09 op while (len != 0) {
247 fb1a36c0 2022-01-09 op l = MIN(len, sizeof(tmp));
248 fb1a36c0 2022-01-09 op mustread(tmp, l);
250 fb1a36c0 2022-01-09 op evbuffer_add(buf, tmp, l);
254 5565d021 2022-01-16 op hexdump("incoming packet", EVBUFFER_DATA(buf),
255 5565d021 2022-01-16 op EVBUFFER_LENGTH(buf));
258 fb1a36c0 2022-01-09 op static uint64_t
259 fb1a36c0 2022-01-09 op np_read64(struct evbuffer *buf)
263 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
264 fb1a36c0 2022-01-09 op return le64toh(n);
267 fb1a36c0 2022-01-09 op static uint32_t
268 fb1a36c0 2022-01-09 op np_read32(struct evbuffer *buf)
272 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
273 fb1a36c0 2022-01-09 op return le32toh(n);
276 fb1a36c0 2022-01-09 op static uint16_t
277 fb1a36c0 2022-01-09 op np_read16(struct evbuffer *buf)
281 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
282 fb1a36c0 2022-01-09 op return le16toh(n);
285 fb1a36c0 2022-01-09 op static uint16_t
286 fb1a36c0 2022-01-09 op np_read8(struct evbuffer *buf)
290 fb1a36c0 2022-01-09 op evbuffer_remove(buf, &n, sizeof(n));
294 fb1a36c0 2022-01-09 op static char *
295 fb1a36c0 2022-01-09 op np_readstr(struct evbuffer *buf)
297 fb1a36c0 2022-01-09 op uint16_t len;
300 fb1a36c0 2022-01-09 op len = np_read16(buf);
301 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= len);
303 fb1a36c0 2022-01-09 op if ((str = calloc(1, len+1)) == NULL)
304 fb1a36c0 2022-01-09 op err(1, "calloc");
305 fb1a36c0 2022-01-09 op evbuffer_remove(buf, str, len);
310 fb1a36c0 2022-01-09 op np_read_qid(struct evbuffer *buf, struct qid *qid)
312 fb1a36c0 2022-01-09 op assert(EVBUFFER_LENGTH(buf) >= QIDSIZE);
314 fb1a36c0 2022-01-09 op qid->type = np_read8(buf);
315 fb1a36c0 2022-01-09 op qid->vers = np_read32(buf);
316 fb1a36c0 2022-01-09 op qid->path = np_read64(buf);
320 fb1a36c0 2022-01-09 op np_read_stat(struct evbuffer *buf, struct np_stat *st)
322 fb1a36c0 2022-01-09 op uint16_t size;
324 fb1a36c0 2022-01-09 op memset(st, 0, sizeof(*st));
326 fb1a36c0 2022-01-09 op size = np_read16(buf);
327 fb1a36c0 2022-01-09 op if (size > EVBUFFER_LENGTH(buf))
330 fb1a36c0 2022-01-09 op st->type = np_read16(buf);
331 fb1a36c0 2022-01-09 op st->dev = np_read32(buf);
332 fb1a36c0 2022-01-09 op np_read_qid(buf, &st->qid);
333 fb1a36c0 2022-01-09 op st->mode = np_read32(buf);
334 fb1a36c0 2022-01-09 op st->atime = np_read32(buf);
335 fb1a36c0 2022-01-09 op st->mtime = np_read32(buf);
336 fb1a36c0 2022-01-09 op st->length = np_read64(buf);
337 fb1a36c0 2022-01-09 op st->name = np_readstr(buf);
338 fb1a36c0 2022-01-09 op st->uid = np_readstr(buf);
339 fb1a36c0 2022-01-09 op st->gid = np_readstr(buf);
340 fb1a36c0 2022-01-09 op st->muid = np_readstr(buf);
346 fb1a36c0 2022-01-09 op expect(uint8_t type)
350 fb1a36c0 2022-01-09 op t = np_read8(buf);
351 fb1a36c0 2022-01-09 op if (t == type)
354 fb1a36c0 2022-01-09 op if (t == Rerror) {
357 fb1a36c0 2022-01-09 op /* skip tag */
358 fb1a36c0 2022-01-09 op np_read16(buf);
360 fb1a36c0 2022-01-09 op err = np_readstr(buf);
361 fb1a36c0 2022-01-09 op errx(1, "expected %s, got error %s",
362 fb1a36c0 2022-01-09 op pp_msg_type(type), err);
365 fb1a36c0 2022-01-09 op errx(1, "expected %s, got msg type %s",
366 fb1a36c0 2022-01-09 op pp_msg_type(type), pp_msg_type(t));
370 fb1a36c0 2022-01-09 op expect2(uint8_t type, uint16_t tag)
374 fb1a36c0 2022-01-09 op expect(type);
376 fb1a36c0 2022-01-09 op t = np_read16(buf);
377 fb1a36c0 2022-01-09 op if (t == tag)
380 fb1a36c0 2022-01-09 op errx(1, "expected tag 0x%x, got 0x%x", tag, t);
383 8f9f99f7 2022-01-16 op static char *
384 8f9f99f7 2022-01-16 op check(uint8_t type, uint16_t tag)
386 8f9f99f7 2022-01-16 op uint16_t rtag;
387 8f9f99f7 2022-01-16 op uint8_t rtype;
389 8f9f99f7 2022-01-16 op rtype = np_read8(buf);
390 8f9f99f7 2022-01-16 op rtag = np_read16(buf);
391 8f9f99f7 2022-01-16 op if (rtype == type) {
392 8f9f99f7 2022-01-16 op if (rtag != tag)
393 8f9f99f7 2022-01-16 op errx(1, "expected tag 0x%x, got 0x%x", tag, rtag);
397 8f9f99f7 2022-01-16 op if (rtype == Rerror)
398 8f9f99f7 2022-01-16 op return np_readstr(buf);
400 8f9f99f7 2022-01-16 op errx(1, "expected %s, got msg type %s",
401 8f9f99f7 2022-01-16 op pp_msg_type(type), pp_msg_type(rtype));
405 fb1a36c0 2022-01-09 op do_version(void)
407 fb1a36c0 2022-01-09 op char *version;
409 fb1a36c0 2022-01-09 op tversion(VERSION9P, MSIZE9P);
412 fb1a36c0 2022-01-09 op expect2(Rversion, NOTAG);
414 fb1a36c0 2022-01-09 op msize = np_read32(buf);
415 fb1a36c0 2022-01-09 op version = np_readstr(buf);
417 fb1a36c0 2022-01-09 op if (msize > MSIZE9P)
418 fb1a36c0 2022-01-09 op errx(1, "got unexpected msize: %d", msize);
419 fb1a36c0 2022-01-09 op if (strcmp(version, VERSION9P))
420 fb1a36c0 2022-01-09 op errx(1, "unexpected 9p version: %s", version);
422 fb1a36c0 2022-01-09 op free(version);
423 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
427 fb1a36c0 2022-01-09 op do_attach(const char *path)
429 fb1a36c0 2022-01-09 op const char *user;
430 fb1a36c0 2022-01-09 op struct qid qid;
432 fb1a36c0 2022-01-09 op if (path == NULL)
434 fb1a36c0 2022-01-09 op if ((user = getenv("USER")) == NULL)
435 fb1a36c0 2022-01-09 op user = "flan";
437 fb1a36c0 2022-01-09 op tattach(pwdfid, NOFID, user, path);
440 fb1a36c0 2022-01-09 op expect2(Rattach, iota_tag);
441 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
443 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
446 fb1a36c0 2022-01-09 op static uint32_t
447 fb1a36c0 2022-01-09 op do_open(uint32_t fid, uint8_t mode)
449 fb1a36c0 2022-01-09 op struct qid qid;
450 fb1a36c0 2022-01-09 op uint32_t iounit;
452 fb1a36c0 2022-01-09 op topen(fid, mode);
455 fb1a36c0 2022-01-09 op expect2(Ropen, iota_tag);
457 01fb39ab 2022-01-16 op np_read_qid(buf, &qid);
458 01fb39ab 2022-01-16 op iounit = np_read32(buf);
460 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
462 01fb39ab 2022-01-16 op return iounit;
465 01fb39ab 2022-01-16 op static uint32_t
466 01fb39ab 2022-01-16 op do_create(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
468 01fb39ab 2022-01-16 op struct qid qid;
469 01fb39ab 2022-01-16 op uint32_t iounit;
471 01fb39ab 2022-01-16 op tcreate(fid, name, perm, mode);
474 01fb39ab 2022-01-16 op expect2(Rcreate, iota_tag);
476 fb1a36c0 2022-01-09 op np_read_qid(buf, &qid);
477 fb1a36c0 2022-01-09 op iounit = np_read32(buf);
479 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
481 fb1a36c0 2022-01-09 op return iounit;
485 fb1a36c0 2022-01-09 op do_clunk(uint32_t fid)
490 fb1a36c0 2022-01-09 op expect2(Rclunk, iota_tag);
492 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
495 1bb1bfca 2022-01-16 op static char *
496 fb1a36c0 2022-01-09 op dup_fid(int fid, int nfid)
498 fb1a36c0 2022-01-09 op uint16_t nwqid;
499 1bb1bfca 2022-01-16 op char *errstr;
501 fb1a36c0 2022-01-09 op twalk(fid, nfid, NULL, 0);
505 1bb1bfca 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
506 1bb1bfca 2022-01-16 op return errstr;
508 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
509 fb1a36c0 2022-01-09 op assert(nwqid == 0);
511 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
516 8f9f99f7 2022-01-16 op static char *
517 8f9f99f7 2022-01-16 op walk_path(int fid, int newfid, const char *path, int *missing,
518 8f9f99f7 2022-01-16 op struct qid *qid)
520 8f9f99f7 2022-01-16 op char *wnames[MAXWELEM], *p, *t, *errstr;
521 fb1a36c0 2022-01-09 op size_t nwname, i;
522 fb1a36c0 2022-01-09 op uint16_t nwqid;
524 fb1a36c0 2022-01-09 op if ((p = strdup(path)) == NULL)
525 fb1a36c0 2022-01-09 op err(1, "strdup");
528 fb1a36c0 2022-01-09 op /* strip initial ./ */
529 fb1a36c0 2022-01-09 op if (t[0] == '.' && t[1] == '/')
532 fb1a36c0 2022-01-09 op for (nwname = 0; nwname < nitems(wnames) &&
533 fb1a36c0 2022-01-09 op (wnames[nwname] = strsep(&t, "/")) != NULL;) {
534 fb1a36c0 2022-01-09 op if (*wnames[nwname] != '\0')
538 fb1a36c0 2022-01-09 op twalk(fid, newfid, (const char **)wnames, nwname);
542 8f9f99f7 2022-01-16 op *missing = nwname;
543 8f9f99f7 2022-01-16 op if ((errstr = check(Rwalk, iota_tag)) != NULL)
544 8f9f99f7 2022-01-16 op return errstr;
546 fb1a36c0 2022-01-09 op nwqid = np_read16(buf);
547 fb1a36c0 2022-01-09 op assert(nwqid <= nwname);
549 fb1a36c0 2022-01-09 op /* consume all qids */
550 43d117b1 2022-01-16 op for (i = 0; i < nwqid; ++i)
551 fb1a36c0 2022-01-09 op np_read_qid(buf, qid);
555 8f9f99f7 2022-01-16 op *missing = nwname - nwqid;
560 fb1a36c0 2022-01-09 op do_stat(int fid, struct np_stat *st)
565 fb1a36c0 2022-01-09 op expect2(Rstat, iota_tag);
567 fb1a36c0 2022-01-09 op if (np_read_stat(buf, st) == -1)
568 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
570 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
573 c37e1cfe 2022-01-17 op static char *
574 c37e1cfe 2022-01-17 op do_wstat(int fid, const struct np_stat *st)
576 c37e1cfe 2022-01-17 op char *errstr;
578 c37e1cfe 2022-01-17 op twstat(fid, st);
582 c37e1cfe 2022-01-17 op if ((errstr = check(Rwstat, iota_tag)) != NULL)
583 c37e1cfe 2022-01-17 op return errstr;
585 c37e1cfe 2022-01-17 op ASSERT_EMPTYBUF();
590 fb1a36c0 2022-01-09 op static size_t
591 fb1a36c0 2022-01-09 op do_read(int fid, uint64_t off, uint32_t count, void *data)
595 fb1a36c0 2022-01-09 op tread(fid, off, count);
598 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
600 fb1a36c0 2022-01-09 op r = np_read32(buf);
601 fb1a36c0 2022-01-09 op assert(r == EVBUFFER_LENGTH(buf));
602 fb1a36c0 2022-01-09 op assert(r <= count);
603 fb1a36c0 2022-01-09 op evbuffer_remove(buf, data, r);
605 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
610 01fb39ab 2022-01-16 op static size_t
611 01fb39ab 2022-01-16 op do_write(int fid, uint64_t off, uint32_t count, void *data)
615 01fb39ab 2022-01-16 op twrite(fid, off, data, count);
618 01fb39ab 2022-01-16 op expect2(Rwrite, iota_tag);
620 01fb39ab 2022-01-16 op r = np_read32(buf);
621 01fb39ab 2022-01-16 op assert(r <= count);
623 01fb39ab 2022-01-16 op ASSERT_EMPTYBUF();
629 fb1a36c0 2022-01-09 op draw_progress(const char *pre, const struct progress *p)
631 fb1a36c0 2022-01-09 op struct winsize ws;
638 fb1a36c0 2022-01-09 op perc = 100.0 * p->done / p->max;
639 fb1a36c0 2022-01-09 op if (!tty_p) {
640 fb1a36c0 2022-01-09 op fprintf(stderr, "%s: %d%%\n", pre, (int)perc);
644 fb1a36c0 2022-01-09 op if (resized) {
647 fb1a36c0 2022-01-09 op if (ioctl(0, TIOCGWINSZ, &ws) == -1)
649 fb1a36c0 2022-01-09 op tty_width = ws.ws_col;
651 fb1a36c0 2022-01-09 op w = tty_width;
653 fb1a36c0 2022-01-09 op if (pre == NULL ||
654 fb1a36c0 2022-01-09 op ((l = printf("\r%s ", pre)) == -1 || l >= w))
657 fb1a36c0 2022-01-09 op w -= l + 2 + 5; /* 2 for |, 5 for percentage + \n */
659 fb1a36c0 2022-01-09 op printf("%4d%%\n", (int)perc);
665 fb1a36c0 2022-01-09 op l = w * MIN(100.0, perc) / 100.0;
666 fb1a36c0 2022-01-09 op for (i = 0; i < l; i++)
668 fb1a36c0 2022-01-09 op for (; i < w; i++)
670 fb1a36c0 2022-01-09 op printf("|%4d%%", (int)perc);
672 fb1a36c0 2022-01-09 op fflush(stdout);
676 76521d26 2022-01-16 op fetch_fid(int fid, int fd, const char *name)
678 fb1a36c0 2022-01-09 op struct progress p = {0};
679 fb1a36c0 2022-01-09 op struct np_stat st;
681 fb1a36c0 2022-01-09 op char buf[BUFSIZ];
683 fb1a36c0 2022-01-09 op do_stat(fid, &st);
684 fb1a36c0 2022-01-09 op do_open(fid, KOREAD);
686 fb1a36c0 2022-01-09 op p.max = st.length;
691 fb1a36c0 2022-01-09 op r = do_read(fid, p.done, sizeof(buf), buf);
695 b2263c45 2022-01-16 op for (off = 0; off < r; off += nw)
696 b2263c45 2022-01-16 op if ((nw = write(fd, buf + off, r - off)) == 0 ||
698 fb1a36c0 2022-01-09 op err(1, "write");
701 9e7dd230 2022-01-12 op draw_progress(name, &p);
704 fb1a36c0 2022-01-09 op /* throttle, for debugging purpose */
706 fb1a36c0 2022-01-09 op struct timespec ts = { 0, 500000000 };
707 fb1a36c0 2022-01-09 op nanosleep(&ts, NULL);
712 fb1a36c0 2022-01-09 op putchar('\n');
714 01fb39ab 2022-01-16 op do_clunk(fid);
718 01fb39ab 2022-01-16 op send_fid(int fid, const char *fnam, int fd, const char *name)
720 01fb39ab 2022-01-16 op struct progress p = {0};
721 01fb39ab 2022-01-16 op struct stat sb;
724 01fb39ab 2022-01-16 op char buf[BUFSIZ];
726 01fb39ab 2022-01-16 op if (fstat(fd, &sb) == -1)
727 01fb39ab 2022-01-16 op err(1, "fstat");
729 01fb39ab 2022-01-16 op if (fnam != NULL)
730 01fb39ab 2022-01-16 op do_create(fid, fnam, 0644, KOWRITE);
732 01fb39ab 2022-01-16 op do_open(fid, KOWRITE);
734 01fb39ab 2022-01-16 op p.max = sb.st_size;
736 01fb39ab 2022-01-16 op r = read(fd, buf, sizeof(buf));
740 01fb39ab 2022-01-16 op err(1, "read");
742 01fb39ab 2022-01-16 op w = do_write(fid, p.done, r, buf);
745 01fb39ab 2022-01-16 op draw_progress(name, &p);
748 01fb39ab 2022-01-16 op /* throttle, for debugging purpose */
750 01fb39ab 2022-01-16 op struct timespec ts = { 0, 500000000 };
751 01fb39ab 2022-01-16 op nanosleep(&ts, NULL);
756 01fb39ab 2022-01-16 op putchar('\n');
757 fb1a36c0 2022-01-09 op do_clunk(fid);
761 9c48f915 2022-01-16 op woc_file(int fd, const char *prompt, const char *path)
763 9c48f915 2022-01-16 op struct qid qid;
764 9c48f915 2022-01-16 op const char *n = NULL;
765 9c48f915 2022-01-16 op char *errstr;
766 9c48f915 2022-01-16 op int nfid, miss;
768 9c48f915 2022-01-16 op nfid = pwdfid+1;
769 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, path, &miss, &qid);
770 9c48f915 2022-01-16 op if (errstr != NULL && miss > 1) {
771 9c48f915 2022-01-16 op printf("%s: %s\n", path, errstr);
772 9c48f915 2022-01-16 op free(errstr);
776 9c48f915 2022-01-16 op if (errstr != NULL || miss == 1) {
777 9c48f915 2022-01-16 op char p[PATH_MAX], *dn;
780 9c48f915 2022-01-16 op * If it's only one component missing (the file name), walk
781 9c48f915 2022-01-16 op * to the parent directory and try to create the file.
784 9c48f915 2022-01-16 op if (strlcpy(p, path, sizeof(p)) >= sizeof(p)) {
785 9c48f915 2022-01-16 op printf("path too long: %s\n", path);
788 9c48f915 2022-01-16 op dn = dirname(p);
790 9c48f915 2022-01-16 op if (!strcmp(dn, ".")) {
791 9c48f915 2022-01-16 op errstr = dup_fid(pwdfid, nfid);
794 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, dn, &miss, &qid);
796 9c48f915 2022-01-16 op if (errstr != NULL) {
797 9c48f915 2022-01-16 op printf("%s: %s\n", dn, errstr);
798 9c48f915 2022-01-16 op free(errstr);
802 9c48f915 2022-01-16 op if (miss != 0) {
803 9c48f915 2022-01-16 op printf("%s: not a directory\n", dn);
807 9c48f915 2022-01-16 op if ((n = strrchr(path, '/')) != NULL)
813 9c48f915 2022-01-16 op free(errstr);
815 9c48f915 2022-01-16 op if (miss > 1) {
816 9c48f915 2022-01-16 op printf("can't create %s: missing %d path component(s)\n",
821 9c48f915 2022-01-16 op send_fid(nfid, n, fd, prompt);
826 fb1a36c0 2022-01-09 op do_tls_connect(const char *host, const char *port)
828 fb1a36c0 2022-01-09 op int handshake;
830 fb1a36c0 2022-01-09 op if ((tlsconf = tls_config_new()) == NULL)
831 fb1a36c0 2022-01-09 op fatalx("tls_config_new");
832 fb1a36c0 2022-01-09 op tls_config_insecure_noverifycert(tlsconf);
833 fb1a36c0 2022-01-09 op tls_config_insecure_noverifyname(tlsconf);
834 fb1a36c0 2022-01-09 op if (tls_config_set_keypair_file(tlsconf, crtpath, keypath) == -1)
835 fb1a36c0 2022-01-09 op fatalx("can't load certs (%s, %s)", crtpath, keypath);
837 fb1a36c0 2022-01-09 op if ((ctx = tls_client()) == NULL)
838 fb1a36c0 2022-01-09 op fatal("tls_client");
839 fb1a36c0 2022-01-09 op if (tls_configure(ctx, tlsconf) == -1)
840 fb1a36c0 2022-01-09 op fatalx("tls_configure: %s", tls_error(ctx));
842 fb1a36c0 2022-01-09 op if (tls_connect(ctx, host, port) == -1)
843 fb1a36c0 2022-01-09 op fatalx("can't connect to %s:%s: %s", host, port,
844 fb1a36c0 2022-01-09 op tls_error(ctx));
846 fb1a36c0 2022-01-09 op for (handshake = 0; !handshake;) {
847 fb1a36c0 2022-01-09 op switch (tls_handshake(ctx)) {
849 fb1a36c0 2022-01-09 op fatalx("tls_handshake: %s", tls_error(ctx));
851 fb1a36c0 2022-01-09 op handshake = 1;
858 fb1a36c0 2022-01-09 op do_ctxt_connect(const char *host, const char *port)
860 fb1a36c0 2022-01-09 op struct addrinfo hints, *res, *res0;
861 fb1a36c0 2022-01-09 op int error, saved_errno;
862 fb1a36c0 2022-01-09 op const char *cause = NULL;
864 fb1a36c0 2022-01-09 op memset(&hints, 0, sizeof(hints));
865 fb1a36c0 2022-01-09 op hints.ai_family = AF_UNSPEC;
866 fb1a36c0 2022-01-09 op hints.ai_socktype = SOCK_STREAM;
867 fb1a36c0 2022-01-09 op error = getaddrinfo(host, port, &hints, &res0);
869 fb1a36c0 2022-01-09 op errx(1, "%s", gai_strerror(error));
872 fb1a36c0 2022-01-09 op for (res = res0; res != NULL; res = res->ai_next) {
873 db066f1e 2022-01-12 op sock = socket(res->ai_family, res->ai_socktype|SOCK_CLOEXEC,
874 fb1a36c0 2022-01-09 op res->ai_protocol);
875 fb1a36c0 2022-01-09 op if (sock == -1) {
876 fb1a36c0 2022-01-09 op cause = "socket";
880 fb1a36c0 2022-01-09 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
881 fb1a36c0 2022-01-09 op cause = "connect";
882 fb1a36c0 2022-01-09 op saved_errno = errno;
884 fb1a36c0 2022-01-09 op errno = saved_errno;
892 fb1a36c0 2022-01-09 op if (sock == -1)
893 fb1a36c0 2022-01-09 op err(1, "%s", cause);
894 fb1a36c0 2022-01-09 op freeaddrinfo(res0);
898 fb1a36c0 2022-01-09 op do_connect(const char *connspec, const char *path)
900 fb1a36c0 2022-01-09 op char *host, *colon;
901 fb1a36c0 2022-01-09 op const char *port;
903 fb1a36c0 2022-01-09 op host = xstrdup(connspec);
904 fb1a36c0 2022-01-09 op if ((colon = strchr(host, ':')) != NULL) {
905 fb1a36c0 2022-01-09 op *colon = '\0';
906 fb1a36c0 2022-01-09 op port = ++colon;
908 fb1a36c0 2022-01-09 op port = "1337";
910 fb1a36c0 2022-01-09 op printf("connecting to %s:%s...", host, port);
911 fb1a36c0 2022-01-09 op fflush(stdout);
914 fb1a36c0 2022-01-09 op do_tls_connect(host, port);
916 fb1a36c0 2022-01-09 op do_ctxt_connect(host, port);
918 fb1a36c0 2022-01-09 op printf(" done!\n");
920 fb1a36c0 2022-01-09 op do_version();
921 fb1a36c0 2022-01-09 op do_attach(path);
927 64d8db89 2022-01-16 op tmp_file(char sfn[TMPFSTRLEN])
931 64d8db89 2022-01-16 op strlcpy(sfn, TMPFSTR, TMPFSTRLEN);
932 64d8db89 2022-01-16 op if ((tmpfd = mkstemp(sfn)) == -1) {
933 64d8db89 2022-01-16 op warn("mkstemp %s", sfn);
937 aa76fd7f 2022-01-16 op /* set the close-on-exec flag */
938 aa76fd7f 2022-01-16 op if (fcntl(tmpfd, F_SETFD, FD_CLOEXEC) == -1) {
939 aa76fd7f 2022-01-16 op warn("fcntl");
940 aa76fd7f 2022-01-16 op close(tmpfd);
944 64d8db89 2022-01-16 op return tmpfd;
947 1f5fb843 2022-01-16 op static inline const char *
948 1f5fb843 2022-01-16 op pp_perm(uint8_t x)
950 1f5fb843 2022-01-16 op switch (x & 0x7) {
952 1f5fb843 2022-01-16 op return "---";
954 1f5fb843 2022-01-16 op return "--x";
956 1f5fb843 2022-01-16 op return "-w-";
958 1f5fb843 2022-01-16 op return "-wx";
960 1f5fb843 2022-01-16 op return "r--";
962 1f5fb843 2022-01-16 op return "r-x";
964 1f5fb843 2022-01-16 op return "rw-";
966 1f5fb843 2022-01-16 op return "rwx";
968 1f5fb843 2022-01-16 op /* unreachable, just for the compiler' happiness */
969 1f5fb843 2022-01-16 op return "???";
973 c37e1cfe 2022-01-17 op static inline void
974 c37e1cfe 2022-01-17 op prepare_wstat(struct np_stat *st)
976 c37e1cfe 2022-01-17 op memset(st, 0xFF, sizeof(*st));
977 c37e1cfe 2022-01-17 op st->name = NULL;
978 c37e1cfe 2022-01-17 op st->uid = NULL;
979 c37e1cfe 2022-01-17 op st->gid = NULL;
980 c37e1cfe 2022-01-17 op st->muid = NULL;
984 fb1a36c0 2022-01-09 op cmd_bell(int argc, const char **argv)
986 fb1a36c0 2022-01-09 op if (argc == 0) {
987 fb1a36c0 2022-01-09 op bell = !bell;
989 fb1a36c0 2022-01-09 op puts("bell mode enabled");
991 fb1a36c0 2022-01-09 op puts("bell mode disabled");
995 fb1a36c0 2022-01-09 op if (argc != 1)
998 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1000 fb1a36c0 2022-01-09 op puts("bell mode enabled");
1004 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1006 fb1a36c0 2022-01-09 op puts("bell mode disabled");
1011 fb1a36c0 2022-01-09 op printf("bell [on | off]\n");
1015 fb1a36c0 2022-01-09 op cmd_bye(int argc, const char **argv)
1017 fb1a36c0 2022-01-09 op log_warnx("bye\n");
1022 fb1a36c0 2022-01-09 op cmd_cd(int argc, const char **argv)
1024 fb1a36c0 2022-01-09 op struct qid qid;
1025 8f9f99f7 2022-01-16 op int nfid, miss;
1026 8f9f99f7 2022-01-16 op char *errstr;
1028 fb1a36c0 2022-01-09 op if (argc != 1) {
1029 fb1a36c0 2022-01-09 op printf("usage: cd remote-path\n");
1033 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
1034 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1035 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1036 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1037 8f9f99f7 2022-01-16 op free(errstr);
1041 8f9f99f7 2022-01-16 op if (miss != 0 || !(qid.type & QTDIR)) {
1042 8f9f99f7 2022-01-16 op printf("%s: not a directory\n", argv[0]);
1043 fcd9d510 2022-01-16 op if (miss == 0)
1044 fcd9d510 2022-01-16 op do_clunk(nfid);
1048 8f9f99f7 2022-01-16 op do_clunk(pwdfid);
1049 8f9f99f7 2022-01-16 op pwdfid = nfid;
1053 9c48f915 2022-01-16 op cmd_edit(int argc, const char **argv)
1055 9c48f915 2022-01-16 op struct qid qid;
1056 9c48f915 2022-01-16 op int nfid, tmpfd, miss;
1057 9c48f915 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1058 9c48f915 2022-01-16 op const char *ed;
1060 9c48f915 2022-01-16 op if (argc != 1) {
1061 9c48f915 2022-01-16 op puts("usage: edit file");
1065 9c48f915 2022-01-16 op if ((ed = getenv("VISUAL")) == NULL &&
1066 9c48f915 2022-01-16 op (ed = getenv("EDITOR")) == NULL)
1069 9c48f915 2022-01-16 op nfid = pwdfid+1;
1070 9c48f915 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1071 9c48f915 2022-01-16 op if (errstr != NULL) {
1072 9c48f915 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1073 9c48f915 2022-01-16 op free(errstr);
1077 9c48f915 2022-01-16 op if (miss != 0 || qid.type != 0) {
1078 9c48f915 2022-01-16 op printf("%s: not a file\n", *argv);
1079 9c48f915 2022-01-16 op if (miss == 0)
1080 9c48f915 2022-01-16 op do_clunk(nfid);
1084 9c48f915 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1085 9c48f915 2022-01-16 op do_clunk(nfid);
1089 9c48f915 2022-01-16 op strlcpy(p, *argv, sizeof(p));
1090 9c48f915 2022-01-16 op name = basename(p);
1092 9c48f915 2022-01-16 op fetch_fid(nfid, tmpfd, name);
1093 9c48f915 2022-01-16 op close(tmpfd);
1095 9c48f915 2022-01-16 op spawn(ed, sfn, NULL);
1098 9c48f915 2022-01-16 op * Re-open the file because it's not guaranteed that the
1099 9c48f915 2022-01-16 op * file descriptor tmpfd is still associated with the file
1100 9c48f915 2022-01-16 op * pointed by sfn: it's not uncommon for editor to write
1101 9c48f915 2022-01-16 op * a backup file and then rename(2) it to the file name.
1103 9c48f915 2022-01-16 op if ((tmpfd = open(sfn, O_RDONLY)) == -1) {
1104 9c48f915 2022-01-16 op warn("can't open %s", sfn);
1108 9c48f915 2022-01-16 op woc_file(tmpfd, *argv, name);
1109 9c48f915 2022-01-16 op close(tmpfd);
1112 9c48f915 2022-01-16 op unlink(sfn);
1116 fb1a36c0 2022-01-09 op cmd_get(int argc, const char **argv)
1118 fb1a36c0 2022-01-09 op struct qid qid;
1119 fb1a36c0 2022-01-09 op const char *l;
1120 8f9f99f7 2022-01-16 op char *errstr;
1121 8f9f99f7 2022-01-16 op int nfid, fd, miss;
1123 fb1a36c0 2022-01-09 op if (argc != 1 && argc != 2) {
1124 fb1a36c0 2022-01-09 op printf("usage: get remote-file [local-file]\n");
1128 fb1a36c0 2022-01-09 op if (argc == 2)
1129 fb1a36c0 2022-01-09 op l = argv[1];
1130 fb1a36c0 2022-01-09 op else if ((l = strrchr(argv[0], '/')) != NULL)
1131 fb1a36c0 2022-01-09 op l++; /* skip / */
1133 2ba25711 2022-01-16 op l = argv[0];
1135 fb1a36c0 2022-01-09 op nfid = pwdfid+1;
1136 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1137 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1138 8f9f99f7 2022-01-16 op printf("%s: %s\n", argv[0], errstr);
1139 8f9f99f7 2022-01-16 op free(errstr);
1143 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1144 8f9f99f7 2022-01-16 op printf("%s: not a file\n", argv[0]);
1145 fcd9d510 2022-01-16 op if (miss == 0)
1146 fcd9d510 2022-01-16 op do_clunk(nfid);
1150 76521d26 2022-01-16 op if ((fd = open(l, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644)) == -1) {
1151 76521d26 2022-01-16 op warn("can't open %s", l);
1152 76521d26 2022-01-16 op do_clunk(nfid);
1156 76521d26 2022-01-16 op fetch_fid(nfid, fd, l);
1161 5565d021 2022-01-16 op cmd_hexdump(int argc, const char **argv)
1163 5565d021 2022-01-16 op if (argc == 0) {
1164 5565d021 2022-01-16 op xdump = !xdump;
1166 5565d021 2022-01-16 op puts("hexdump mode enabled");
1168 5565d021 2022-01-16 op puts("hexdump mode disabled");
1172 5565d021 2022-01-16 op if (argc > 1)
1175 5565d021 2022-01-16 op if (!strcmp(*argv, "on")) {
1177 5565d021 2022-01-16 op puts("hexdump mode enabled");
1181 5565d021 2022-01-16 op if (!strcmp(*argv, "off")) {
1183 5565d021 2022-01-16 op puts("hexdump mode disabled");
1188 5565d021 2022-01-16 op puts("usage: hexdump [on | off]");
1192 fb1a36c0 2022-01-09 op cmd_lcd(int argc, const char **argv)
1194 fb1a36c0 2022-01-09 op const char *dir;
1196 fb1a36c0 2022-01-09 op if (argc > 1) {
1197 d166ffc0 2022-01-16 op printf("usage: lcd [local-directory]\n");
1201 fb1a36c0 2022-01-09 op if (argc == 1)
1202 fb1a36c0 2022-01-09 op dir = *argv;
1204 fb1a36c0 2022-01-09 op if (argc == 0 && (dir = getenv("HOME")) == NULL) {
1205 fb1a36c0 2022-01-09 op printf("HOME is not defined\n");
1209 fb1a36c0 2022-01-09 op if (chdir(dir) == -1)
1210 fb1a36c0 2022-01-09 op printf("cd: %s: %s\n", dir, strerror(errno));
1214 fb1a36c0 2022-01-09 op cmd_lpwd(int argc, const char **argv)
1216 fb1a36c0 2022-01-09 op char path[PATH_MAX];
1218 d166ffc0 2022-01-16 op if (argc != 0) {
1219 d166ffc0 2022-01-16 op printf("usage: lpwd\n");
1223 fb1a36c0 2022-01-09 op if (getcwd(path, sizeof(path)) == NULL) {
1224 fb1a36c0 2022-01-09 op printf("lpwd: %s\n", strerror(errno));
1228 fb1a36c0 2022-01-09 op printf("%s\n", path);
1232 fb1a36c0 2022-01-09 op cmd_ls(int argc, const char **argv)
1234 fb1a36c0 2022-01-09 op struct np_stat st;
1235 4f52d9af 2022-01-16 op time_t now, mtime;
1236 4f52d9af 2022-01-16 op struct tm *tm;
1237 fb1a36c0 2022-01-09 op uint64_t off = 0;
1238 fb1a36c0 2022-01-09 op uint32_t len;
1239 4f52d9af 2022-01-16 op const char *timfmt;
1240 4f52d9af 2022-01-16 op char fmt[FMT_SCALED_STRSIZE], tim[13], *errstr;
1242 fb1a36c0 2022-01-09 op if (argc != 0) {
1243 fb1a36c0 2022-01-09 op printf("ls don't take arguments (yet)\n");
1247 4f52d9af 2022-01-16 op now = time(NULL);
1249 1bb1bfca 2022-01-16 op if ((errstr = dup_fid(pwdfid, 1)) != NULL) {
1250 1bb1bfca 2022-01-16 op printf(".: %s\n", errstr);
1251 1bb1bfca 2022-01-16 op free(errstr);
1255 fb1a36c0 2022-01-09 op do_open(1, KOREAD);
1257 fb1a36c0 2022-01-09 op evbuffer_drain(dirbuf, EVBUFFER_LENGTH(dirbuf));
1260 fb1a36c0 2022-01-09 op tread(1, off, BUFSIZ);
1263 fb1a36c0 2022-01-09 op expect2(Rread, iota_tag);
1265 fb1a36c0 2022-01-09 op len = np_read32(buf);
1266 fb1a36c0 2022-01-09 op if (len == 0)
1269 fb1a36c0 2022-01-09 op evbuffer_add_buffer(dirbuf, buf);
1272 fb1a36c0 2022-01-09 op ASSERT_EMPTYBUF();
1275 fb1a36c0 2022-01-09 op while (EVBUFFER_LENGTH(dirbuf) != 0) {
1276 fb1a36c0 2022-01-09 op if (np_read_stat(dirbuf, &st) == -1)
1277 fb1a36c0 2022-01-09 op errx(1, "invalid stat struct read");
1279 fb1a36c0 2022-01-09 op if (fmt_scaled(st.length, fmt) == -1)
1280 fb1a36c0 2022-01-09 op strlcpy(fmt, "xxx", sizeof(fmt));
1282 4f52d9af 2022-01-16 op mtime = st.mtime;
1284 4f52d9af 2022-01-16 op if (now > mtime && (now - mtime) < 365/2 * 24 * 12 * 60)
1285 4f52d9af 2022-01-16 op timfmt = "%b %e %R";
1287 4f52d9af 2022-01-16 op timfmt = "%b %e %Y";
1289 4f52d9af 2022-01-16 op if ((tm = localtime(&mtime)) == NULL ||
1290 4f52d9af 2022-01-16 op strftime(tim, sizeof(tim), timfmt, tm) == 0)
1291 4f52d9af 2022-01-16 op strlcpy(tim, "unknown", sizeof(tim));
1293 1f5fb843 2022-01-16 op if (st.qid.type & QTDIR)
1294 1f5fb843 2022-01-16 op printf("d");
1296 1f5fb843 2022-01-16 op printf("-");
1297 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 6));
1298 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode >> 3));
1299 1f5fb843 2022-01-16 op printf("%s", pp_perm(st.mode));
1300 d33c93c4 2022-01-17 op printf(" %8s %12s %s%s\n", fmt, tim, st.name,
1301 6470594e 2022-01-16 op st.qid.type & QTDIR ? "/" : "");
1303 fb1a36c0 2022-01-09 op free(st.name);
1304 fb1a36c0 2022-01-09 op free(st.uid);
1305 fb1a36c0 2022-01-09 op free(st.gid);
1306 fb1a36c0 2022-01-09 op free(st.muid);
1309 fb1a36c0 2022-01-09 op do_clunk(1);
1313 9e7dd230 2022-01-12 op cmd_page(int argc, const char **argv)
1315 9e7dd230 2022-01-12 op struct qid qid;
1316 8f9f99f7 2022-01-16 op int nfid, tmpfd, miss;
1317 64d8db89 2022-01-16 op char sfn[TMPFSTRLEN], p[PATH_MAX], *name, *errstr;
1318 62f663bb 2022-01-16 op const char *pager;
1320 9e7dd230 2022-01-12 op if (argc != 1) {
1321 9e7dd230 2022-01-12 op puts("usage: page file");
1325 62f663bb 2022-01-16 op if ((pager = getenv("PAGER")) == NULL)
1326 62f663bb 2022-01-16 op pager = "less";
1328 9e7dd230 2022-01-12 op nfid = pwdfid+1;
1329 8f9f99f7 2022-01-16 op errstr = walk_path(pwdfid, nfid, *argv, &miss, &qid);
1330 8f9f99f7 2022-01-16 op if (errstr != NULL) {
1331 8f9f99f7 2022-01-16 op printf("%s: %s\n", *argv, errstr);
1332 8f9f99f7 2022-01-16 op free(errstr);
1336 8f9f99f7 2022-01-16 op if (miss != 0 || qid.type != 0) {
1337 8f9f99f7 2022-01-16 op printf("%s: not a file\n", *argv);
1338 fcd9d510 2022-01-16 op if (miss == 0)
1339 fcd9d510 2022-01-16 op do_clunk(nfid);
1343 64d8db89 2022-01-16 op if ((tmpfd = tmp_file(sfn)) == -1) {
1344 9e7dd230 2022-01-12 op do_clunk(nfid);
1348 9e7dd230 2022-01-12 op strlcpy(p, *argv, sizeof(p));
1349 9e7dd230 2022-01-12 op name = basename(p);
1350 76521d26 2022-01-16 op fetch_fid(nfid, tmpfd, name);
1351 9e7dd230 2022-01-12 op close(tmpfd);
1352 62f663bb 2022-01-16 op spawn(pager, sfn, NULL);
1353 9e7dd230 2022-01-12 op unlink(sfn);
1357 01fb39ab 2022-01-16 op cmd_put(int argc, const char **argv)
1359 01fb39ab 2022-01-16 op struct qid qid;
1360 9c48f915 2022-01-16 op const char *l;
1363 01fb39ab 2022-01-16 op if (argc != 1 && argc != 2) {
1364 01fb39ab 2022-01-16 op printf("usage: put local-file [remote-file]\n");
1368 01fb39ab 2022-01-16 op if (argc == 2)
1369 01fb39ab 2022-01-16 op l = argv[1];
1370 01fb39ab 2022-01-16 op else if ((l = strrchr(argv[0], '/')) != NULL)
1371 01fb39ab 2022-01-16 op l++; /* skip / */
1373 01fb39ab 2022-01-16 op l = argv[0];
1375 01fb39ab 2022-01-16 op if ((fd = open(argv[0], O_RDONLY)) == -1) {
1376 01fb39ab 2022-01-16 op warn("%s", argv[0]);
1380 9c48f915 2022-01-16 op woc_file(fd, argv[0], l);
1385 c37e1cfe 2022-01-17 op cmd_rename(int argc, const char **argv)
1387 c37e1cfe 2022-01-17 op struct np_stat st;
1388 c37e1cfe 2022-01-17 op struct qid qid;
1389 c37e1cfe 2022-01-17 op char *errstr;
1390 c37e1cfe 2022-01-17 op int nfid, miss;
1392 c37e1cfe 2022-01-17 op if (argc != 2) {
1393 c37e1cfe 2022-01-17 op puts("usage: rename remote-file new-remote-name");
1397 c37e1cfe 2022-01-17 op nfid = pwdfid+1;
1398 c37e1cfe 2022-01-17 op errstr = walk_path(pwdfid, nfid, argv[0], &miss, &qid);
1399 c37e1cfe 2022-01-17 op if (errstr != NULL) {
1400 c37e1cfe 2022-01-17 op printf("%s: %s\n", argv[0], errstr);
1401 c37e1cfe 2022-01-17 op free(errstr);
1405 c37e1cfe 2022-01-17 op if (miss != 0) {
1406 c37e1cfe 2022-01-17 op printf("%s: not such file or directory\n", argv[0]);
1410 c37e1cfe 2022-01-17 op prepare_wstat(&st);
1411 c37e1cfe 2022-01-17 op st.name = (char *)argv[1];
1412 c37e1cfe 2022-01-17 op if ((errstr = do_wstat(nfid, &st)) != NULL) {
1413 c37e1cfe 2022-01-17 op printf("rename: %s\n", errstr);
1414 c37e1cfe 2022-01-17 op free(errstr);
1417 c37e1cfe 2022-01-17 op do_clunk(nfid);
1421 fb1a36c0 2022-01-09 op cmd_verbose(int argc, const char **argv)
1423 fb1a36c0 2022-01-09 op if (argc == 0) {
1424 fb1a36c0 2022-01-09 op log_setverbose(!log_getverbose());
1425 fb1a36c0 2022-01-09 op if (log_getverbose())
1426 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1428 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1432 fb1a36c0 2022-01-09 op if (argc != 1)
1435 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "on")) {
1436 fb1a36c0 2022-01-09 op log_setverbose(1);
1437 fb1a36c0 2022-01-09 op puts("verbose mode enabled");
1441 fb1a36c0 2022-01-09 op if (!strcmp(*argv, "off")) {
1442 fb1a36c0 2022-01-09 op log_setverbose(0);
1443 fb1a36c0 2022-01-09 op puts("verbose mode disabled");
1448 fb1a36c0 2022-01-09 op printf("verbose [on | off]\n");
1452 fb1a36c0 2022-01-09 op excmd(int argc, const char **argv)
1454 fb1a36c0 2022-01-09 op struct cmd {
1455 fb1a36c0 2022-01-09 op const char *name;
1456 fb1a36c0 2022-01-09 op void (*fn)(int, const char **);
1457 fb1a36c0 2022-01-09 op } cmds[] = {
1458 fb1a36c0 2022-01-09 op {"bell", cmd_bell},
1459 fb1a36c0 2022-01-09 op {"bye", cmd_bye},
1460 fb1a36c0 2022-01-09 op {"cd", cmd_cd},
1461 9c48f915 2022-01-16 op {"edit", cmd_edit},
1462 fb1a36c0 2022-01-09 op {"get", cmd_get},
1463 5565d021 2022-01-16 op {"hexdump", cmd_hexdump},
1464 fb1a36c0 2022-01-09 op {"lcd", cmd_lcd},
1465 fb1a36c0 2022-01-09 op {"lpwd", cmd_lpwd},
1466 fb1a36c0 2022-01-09 op {"ls", cmd_ls},
1467 9e7dd230 2022-01-12 op {"page", cmd_page},
1468 01fb39ab 2022-01-16 op {"put", cmd_put},
1469 fb1a36c0 2022-01-09 op {"quit", cmd_bye},
1470 c37e1cfe 2022-01-17 op {"rename", cmd_rename},
1471 fb1a36c0 2022-01-09 op {"verbose", cmd_verbose},
1475 fb1a36c0 2022-01-09 op if (argc == 0)
1477 fb1a36c0 2022-01-09 op for (i = 0; i < nitems(cmds); ++i) {
1478 fb1a36c0 2022-01-09 op if (!strcmp(cmds[i].name, *argv)) {
1479 fb1a36c0 2022-01-09 op cmds[i].fn(argc-1, argv+1);
1484 fb1a36c0 2022-01-09 op log_warnx("unknown command %s", *argv);
1488 fb1a36c0 2022-01-09 op main(int argc, char **argv)
1492 fb1a36c0 2022-01-09 op log_init(1, LOG_DAEMON);
1493 fb1a36c0 2022-01-09 op log_setverbose(0);
1494 fb1a36c0 2022-01-09 op log_procinit(getprogname());
1496 fb1a36c0 2022-01-09 op while ((ch = getopt(argc, argv, "C:cK:")) != -1) {
1497 fb1a36c0 2022-01-09 op switch (ch) {
1499 fb1a36c0 2022-01-09 op crtpath = optarg;
1505 fb1a36c0 2022-01-09 op keypath = optarg;
1511 fb1a36c0 2022-01-09 op argc -= optind;
1512 fb1a36c0 2022-01-09 op argv += optind;
1514 fb1a36c0 2022-01-09 op if (argc == 0)
1517 fb1a36c0 2022-01-09 op if (isatty(1)) {
1519 fb1a36c0 2022-01-09 op resized = 1;
1520 fb1a36c0 2022-01-09 op signal(SIGWINCH, tty_resized);
1523 fb1a36c0 2022-01-09 op if ((evb = evbuffer_new()) == NULL)
1524 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1526 fb1a36c0 2022-01-09 op if ((buf = evbuffer_new()) == NULL)
1527 fb1a36c0 2022-01-09 op fatal("evbuffer_new");
1529 fb1a36c0 2022-01-09 op if ((dirbuf = evbuffer_new()) == NULL)
1530 fb1a36c0 2022-01-09 op fatal("evbuferr_new");
1532 fb1a36c0 2022-01-09 op do_connect(argv[0], argv[1]);
1535 fb1a36c0 2022-01-09 op int argc = 0;
1536 fb1a36c0 2022-01-09 op char *line, *argv[16] = {0}, **ap;
1538 fb1a36c0 2022-01-09 op if ((line = read_line("kamiftp> ")) == NULL)
1541 fb1a36c0 2022-01-09 op for (argc = 0, ap = argv; ap < &argv[15] &&
1542 fb1a36c0 2022-01-09 op (*ap = strsep(&line, " \t")) != NULL;) {
1543 fb1a36c0 2022-01-09 op if (**ap != '\0')
1544 fb1a36c0 2022-01-09 op ap++, argc++;
1546 fb1a36c0 2022-01-09 op excmd(argc, (const char **)argv);
1549 fb1a36c0 2022-01-09 op printf("\a");
1550 fb1a36c0 2022-01-09 op fflush(stdout);
1556 fb1a36c0 2022-01-09 op printf("\n");