-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteBinary.c
30 lines (26 loc) · 880 Bytes
/
WriteBinary.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
//The program takes one argument - a file and creates a 64-byte random data file,
// xattr creates a test attribute.
#include <stdio.h>
#include <stdlib.h>
#include <sys/xattr.h>
#define XATTR_BINARY_KEY "user.test_binary"
#define XATTR_TEST_VALUE "this is a test binary custom attribute"
#define XATTR_TEST_VALUE_SIZE (sizeof(XATTR_TEST_VALUE) - 1)
int main(int argc, char *argv[]) {
(void)argc;
FILE *path = fopen(argv[1], "wb");
if (path) {
short num_bytes = 64;
unsigned char *stream = malloc(num_bytes);
for (short i = 0; i < num_bytes; i++) {
stream[i] = random();
fwrite(&stream[i], 1, 1, path);
setxattr( argv[1], XATTR_BINARY_KEY, XATTR_TEST_VALUE, XATTR_TEST_VALUE_SIZE, 0);
}
} else {
printf("Specify 1 file.\n");
return -2;
}
fclose(path);
return 0;
}