-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_xattr.c
68 lines (54 loc) · 1.61 KB
/
view_xattr.c
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
57
58
59
60
61
62
63
64
65
66
67
68
/* view_xattr.c
Display the extended attributes of a file.
This program is Linux (2.6 and later) specific.
See also t_setxattr.c.
*/
#include <sys/xattr.h>
#include "tlpi_hdr.h"
#define XATTR_SIZE 10000
static void
usageError(char *progName)
{
fprintf(stderr, "Usage: %s [-x] file...\n", progName);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
char list[XATTR_SIZE], value[XATTR_SIZE];
ssize_t listLen, valueLen;
int ns, j, k, opt;
Boolean hexDisplay;
hexDisplay = 0;
while ((opt = getopt(argc, argv, "x")) != -1) {
switch (opt) {
case 'x': hexDisplay = 1; break;
case '?': usageError(argv[0]);
}
}
if (optind >= argc)
usageError(argv[0]);
for (j = optind; j < argc; j++) {
listLen = listxattr(argv[j], list, XATTR_SIZE);
if (listLen == -1)
errExit("listxattr");
printf("%s:\n", argv[j]);
/* Loop through all EA names, displaying name + value */
for (ns = 0; ns < listLen; ns += strlen(&list[ns]) + 1) {
printf(" name=%s; ", &list[ns]);
valueLen = getxattr(argv[j], &list[ns], value, XATTR_SIZE);
if (valueLen == -1) {
printf("couldn't get value");
} else if (!hexDisplay) {
printf("value=%.*s", (int) valueLen, value);
} else {
printf("value=");
for (k = 0; k < valueLen; k++)
printf("%02x ", (unsigned char) value[k]);
}
printf("\n");
}
printf("\n");
}
exit(EXIT_SUCCESS);
}