-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckBinary.c
106 lines (92 loc) · 2.35 KB
/
CheckBinary.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/xattr.h>
int main(int argc, char *argv[])
{
// FILE *input_file = fopen(argv[1], "rb");
ssize_t buflen, keylen, vallen;
char *buf, *key, *val;
if (argc != 2) {
fprintf(stderr, "Usage: %s path\n", argv[0]);
exit(EXIT_FAILURE);
}
buflen = listxattr(argv[1], NULL, 0);
if (buflen == -1) {
perror("listxattr");
exit(EXIT_FAILURE);
}
if (buflen == 0) {
printf("%s has no attributes.\n", argv[1]);
exit(EXIT_SUCCESS);
}
/*
* Allocate the buffer.
*/
buf = malloc(buflen);
if (buf == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
/*
* Copy the list of attribute keys to the buffer.
*/
buflen = listxattr(argv[1], buf, buflen);
if (buflen == -1) {
perror("listxattr");
exit(EXIT_FAILURE);
}
/*
* Loop over the list of zero terminated strings with the
* attribute keys. Use the remaining buffer length to determine
* the end of the list.
*/
key = buf;
while (buflen > 0) {
/*
* Output attribute key.
*/
printf("%s: ", key);
/*
* Determine length of the value.
*/
vallen = getxattr(argv[1], key, NULL, 0);
if (vallen == -1)
perror("getxattr");
if (vallen > 0) {
/*
* Allocate value buffer.
* One extra byte is needed to append 0x00.
*/
val = malloc(vallen + 1);
if (val == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
/*
* Copy value to buffer.
*/
vallen = getxattr(argv[1], key, val, vallen);
if (vallen == -1)
perror("getxattr");
else {
/*
* Output attribute value.
*/
val[vallen] = 0;
printf("%s", val);
}
free(val);
} else if (vallen == 0)
printf("<no value>");
printf("\n");
/*
* Forward to next attribute key.
*/
keylen = strlen(key) + 1;
buflen -= keylen;
key += keylen;
}
free(buf);
exit(EXIT_SUCCESS);
}