summaryrefslogtreecommitdiff
path: root/src/cmd/getxattr.c
blob: 3a3a4f9d520779560c71d903a47ce6a8d162640e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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));
		}
	}
}