-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxattr_view.c
85 lines (68 loc) · 2.4 KB
/
xattr_view.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2022. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
/* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/xattr.h>
#include <unistd.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;
int 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) {
fprintf(stderr,"listxattr failed: %m");
exit(EXIT_FAILURE);
}
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);
}