UNIXworkcode

 1  /*
 2   *             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
 3   *                     Version 2, December 2004
 4   * 
 5   *  Copyright (C) 2016 Olaf Wintermann <olaf.wintermann@gmail.com>
 6   * 
 7   *  Everyone is permitted to copy and distribute verbatim or modified
 8   *  copies of this license document, and changing it is allowed as long
 9   *  as the name is changed.
10   * 
11   *             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
12   *    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
13   * 
14   *   0. You just DO WHAT THE FUCK YOU WANT TO.
15   */
16  
17  #include <stdio.h>
18  #include <string.h>
19  #include <unistd.h>
20  #include <sys/types.h>
21  #include <sys/stat.h>
22  #include <dirent.h>
23  #include <fcntl.h>
24  
25  /*
26   * Solaris Extended Attributes Example
27   *
28   * list all attributes
29   */
30  
31  int main(int argc, char **argv) {
32      if(argc < 2) {
33          fprintf(stderr, "Usage: %s <file>", argv[0]);
34          return -1;
35      }
36      
37      int file = open(argv[1], O_RDONLY);
38      if(file == -1) {
39          fprintf(stderr, "Cannot open file ''%s'': ", argv[1]);
40          perror(NULL);
41          return -1;
42      }
43      
44      int attrdir = openat(file, ".", O_RDONLY|O_XATTR);
45      DIR *dir = fdopendir(attrdir);
46      if(!dir) {
47          perror("fdopendir");
48          return -1;
49      }
50      
51      struct dirent *ent;
52      while((ent = readdir(dir)) != NULL) {
53          if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
54              continue;
55          }
56          printf("%s\n", ent->d_name);
57      }
58      
59      return 0;
60  }
61