#include <sys/types.h>
#include <sys/xattr.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUF_LEN 1024
int main(int argc, char **argv) {
if(argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
return -1;
}
char *path = argv[1];
char *list = malloc(BUF_LEN);
ssize_t len = listxattr(path, list, BUF_LEN, 0);
if(len == -1) {
switch(errno) {
case ERANGE: {
ssize_t newlen = listxattr(path, NULL, 0, 0);
if(newlen > 0) {
list = realloc(list, newlen);
len = listxattr(path, list, newlen, 0);
if(len != -1) {
break;
}
}
}
default: perror("listxattr");
free(list);
return -1;
}
}
int begin = 0;
for(int i=0;i<len;i++) {
if(list[i] == '\0') {
printf("%s\n", list + begin);
begin = i + 1;
}
}
free(list);
return 0;
}