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 <unistd.h>
19  #include <sys/types.h>
20  #include <sys/stat.h>
21  #include <fcntl.h>
22  
23  /*
24   * Solaris Extended Attributes Example
25   *
26   * get attribute content
27   */
28  
29  int main(int argc, char **argv) {
30      if(argc < 3) {
31          fprintf(stderr, "Usage: %s <file> <attribute>\n", argv[0]);
32          return -1;
33      }
34      
35      int file = open(argv[1], O_RDONLY);
36      if(file == -1) {
37          fprintf(stderr, "Cannot open file ''%s'': ", argv[1]);
38          perror(NULL);
39          return -1;
40      }
41      
42      int attr = openat(file, argv[2], O_RDONLY|O_XATTR);
43      if(attr == -1) {
44          fprintf(stderr, "Cannot open attribute ''%s'': ", argv[2]);
45          perror(NULL);
46          return -1;
47      }
48      
49      char buf[1024];
50      ssize_t r = 0;
51      while((r = read(attr, buf, 1024)) > 0) {
52          write(STDOUT_FILENO, buf, r);
53      }
54      
55      return 0;
56  }
57