src/open_by_handle: verify dir content only with -r flag
[xfstests-dev.git] / src / listxattr.c
1 /*
2  * Copyright (c) 2016 Red Hat, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation, either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/xattr.h>
25
26 int main(int argc, char **argv)
27 {
28         int ret;
29         size_t bufsize = 0;
30         char *buf = NULL;
31
32         if (argc < 2) {
33                 fprintf(stderr, "usage: %s <testfile> [bufsize]\n", argv[0]);
34                 return 1;
35         };
36
37         if (argc > 2) {
38                 bufsize = strtoul(argv[2], NULL, 10);
39                 if (bufsize == -1) {
40                         perror("buffsize");
41                         return 1;
42                 }
43         }
44
45         if (bufsize == 0) {
46                 bufsize = listxattr(argv[1], NULL, 0);
47                 if (bufsize == -1) {
48                         perror("listxattr");
49                         return 1;
50                 }
51         }
52
53         buf = malloc(bufsize);
54         if (buf == NULL) {
55                 perror("buf alloc");
56                 return 1;
57         }
58
59         ret = listxattr(argv[1], buf, bufsize);
60         if (ret < 0) {
61                 perror("listxattr");
62         } else {
63                 char *l = buf;
64                 while (l < (buf + ret)) {
65                         printf("xattr: %s\n", l);
66                         l = strchr(l, '\0') + 1;
67                 }
68         }
69
70         free(buf);
71
72         return 0;
73 }