summaryrefslogtreecommitdiff
path: root/src/cmd/getxattr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/getxattr.c')
-rw-r--r--src/cmd/getxattr.c56
1 files changed, 56 insertions, 0 deletions
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 <camellia.h>
+#include <camellia/syscalls.h>
+#include <ctype.h>
+#include <err.h>
+#include <stdio.h>
+#include <string.h>
+
+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));
+ }
+ }
+}