UNIXworkcode

/* * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE * Version 2, December 2004 * * Copyright (C) 2017 Olaf Wintermann <olaf.wintermann@gmail.com> * * Everyone is permitted to copy and distribute verbatim or modified * copies of this license document, and changing it is allowed as long * as the name is changed. * * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION * * 0. You just DO WHAT THE FUCK YOU WANT TO. */ #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 // this example is 99.9% identical to the linux xattr example int main(int argc, char **argv) { if(argc != 2) { fprintf(stderr, "Usage: %s <file>\n", argv[0]); return -1; } char *path = argv[1]; // get list of extended attribute names char *list = malloc(BUF_LEN); ssize_t len = listxattr(path, list, BUF_LEN, 0); if(len == -1) { switch(errno) { case ERANGE: { // buffer too, get size of attribute list ssize_t newlen = listxattr(path, NULL, 0, 0); if(newlen > 0) { // second try list = realloc(list, newlen); len = listxattr(path, list, newlen, 0); if(len != -1) { // this time it worked break; } } } default: perror("listxattr"); free(list); return -1; } } // print list 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; }