btrfs: add test for multiple fsync with adjacent preallocated extents
[xfstests-dev.git] / src / listxattr.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016 Red Hat, Inc.  All Rights Reserved.
4  */
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/xattr.h>
13
14 int main(int argc, char **argv)
15 {
16         int ret;
17         size_t bufsize = 0;
18         char *buf = NULL;
19
20         if (argc < 2) {
21                 fprintf(stderr, "usage: %s <testfile> [bufsize]\n", argv[0]);
22                 return 1;
23         };
24
25         if (argc > 2) {
26                 bufsize = strtoul(argv[2], NULL, 10);
27                 if (bufsize == -1) {
28                         perror("buffsize");
29                         return 1;
30                 }
31         }
32
33         if (bufsize == 0) {
34                 bufsize = listxattr(argv[1], NULL, 0);
35                 if (bufsize == -1) {
36                         perror("listxattr");
37                         return 1;
38                 }
39         }
40
41         buf = malloc(bufsize);
42         if (buf == NULL) {
43                 perror("buf alloc");
44                 return 1;
45         }
46
47         ret = listxattr(argv[1], buf, bufsize);
48         if (ret < 0) {
49                 perror("listxattr");
50         } else {
51                 char *l = buf;
52                 while (l < (buf + ret)) {
53                         printf("xattr: %s\n", l);
54                         l = strchr(l, '\0') + 1;
55                 }
56         }
57
58         free(buf);
59
60         return 0;
61 }