From 2ea826b428246eb62be81630f441a4367a675968 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sat, 17 Aug 2024 17:10:04 +0200 Subject: *: getxattr --- src/cmd/getxattr.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/cmd/getxattr.c (limited to 'src/cmd/getxattr.c') diff --git a/src/cmd/getxattr.c b/src/cmd/getxattr.c new file mode 100644 index 0000000..3a3a4f9 --- /dev/null +++ b/src/cmd/getxattr.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include + +ssize_t +get(hid_t h, const char *name, char *buf, size_t len) +{ + ssize_t ret = _sys_getxattr(h, name, buf, len, 0); + if (ret < 0) { + fprintf(stderr, "error when getting %s: %s\n", name, strerror(-ret)); + } else { + printf("%s: ", name); + + for (ssize_t i = 0; i < ret; i++) { + if (isprint(buf[i])) { + putchar(buf[i]); + } else { + printf("\\x%02x", (unsigned char)buf[i]); + } + } + printf("\n"); + } + return ret; +} + +int +main(int argc, char **argv) +{ + if (argc < 2) { + fprintf(stderr, "usage: getxattr file [xattrs...]\n"); + return 1; + } + hid_t h = camellia_open(argv[1], OPEN_READ); + char buf[512]; + if (h < 0) err(1, "open"); + + if (argc == 2) { + ssize_t ret = get(h, "virt.index", buf, sizeof(buf)); + if (ret < 0) return 1; + if (0 < ret && buf[ret-1] != '\0') { + fprintf(stderr, "index truncated\n"); + } else { + for (ssize_t i = 0; i < ret; i += strlen(buf+i) + 1) { + char buf2[512]; + get(h, buf+i, buf2, sizeof(buf2)); + } + } + } else { + for (int i = 2; i < argc; i++) { + get(h, argv[i], buf, sizeof(buf)); + } + } +} -- cgit v1.2.3