xfstests: eliminate warnings under dmapi/src/suite1/cmd (1)
[xfstests-dev.git] / dmapi / src / suite1 / cmd / get_dmattr.c
1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
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, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <lib/hsm.h>
20
21 #include <string.h>
22 #include <getopt.h>
23
24 /*---------------------------------------------------------------------------
25
26 Test program used to test the DMAPI function dm_get_dmattr().  The
27 command line is:
28
29         get_dmattr [-b buflen] [-s sid] [-t token] {pathname|handle} attr
30
31 where pathname is the name of a file, buflen is the size of the buffer to use
32 in the call, attr is the name of the DMAPI attribute, and sid is the session ID
33 whose attributes you you are interested in.
34
35 ----------------------------------------------------------------------------*/
36
37 #ifndef linux
38 extern  char    *sys_errlist[];
39 #endif
40 extern  int     optind;
41 extern  char    *optarg;
42
43
44 char    *Progname;
45
46 static void
47 usage(void)
48 {
49         fprintf(stderr, "usage:\t%s [-b buflen] [-s sid] [-t token] "
50                 "{pathname|handle} attr\n", Progname);
51         exit(1);
52 }
53
54
55 int
56 main(
57         int     argc, 
58         char    **argv)
59 {
60         dm_sessid_t     sid = DM_NO_SESSION;
61         dm_token_t      token = DM_NO_TOKEN;
62         char            *object;
63         dm_attrname_t   *attrnamep;
64         void            *bufp = NULL;
65         size_t          buflen = 10000;
66         size_t          rlenp;
67         void            *hanp;
68         size_t           hlen;
69         char            *name;
70         int             opt;
71
72         Progname = strrchr(argv[0], '/');
73         if (Progname) {
74                 Progname++;
75         } else {
76                 Progname = argv[0];
77         }
78
79         /* Crack and validate the command line options. */
80
81         while ((opt = getopt(argc, argv, "b:s:t:")) != EOF) {
82                 switch (opt) {
83                 case 'b':
84                         buflen = atol(optarg);
85                         break;
86                 case 's':
87                         sid = atol(optarg);
88                         break;
89                 case 't':
90                         token = atol(optarg);
91                         break;
92                 case '?':
93                         usage();
94                 }
95         }
96         if (optind + 2 != argc)
97                 usage();
98         object = argv[optind++];
99         attrnamep = (dm_attrname_t *)argv[optind];
100
101         if (dm_init_service(&name) == -1)  {
102                 fprintf(stderr, "Can't initialize the DMAPI\n");
103                 exit(1);
104         }
105         if (sid == DM_NO_SESSION)
106                 find_test_session(&sid);
107
108         /* Get the file's handle. */
109
110         if (opaque_to_handle(object, &hanp, &hlen)) {
111                 fprintf(stderr, "can't get handle for %s\n", object);
112                 exit(1);
113         }
114
115         if (buflen > 0) {
116                 if ((bufp = malloc(buflen)) == NULL) {
117                         fprintf(stderr, "malloc failed, %s\n", strerror(errno));
118                         exit(1);
119                 }
120         }
121
122         if (dm_get_dmattr(sid, hanp, hlen, token, attrnamep, buflen,
123             bufp, &rlenp)) {
124                 if (errno == E2BIG) {
125                         fprintf(stderr, "dm_get_dmattr buffer too small, "
126                                 "should be %zd bytes\n", rlenp);
127                 } else {
128                         fprintf(stderr, "dm_get_dmattr failed, %s\n",
129                                 strerror(errno));
130                 }
131                 exit(1);
132         }
133         fprintf(stdout, "rlenp is %zd, value is '%s'\n", rlenp, (char*)bufp);
134
135         dm_handle_free(hanp, hlen);
136         exit(0);
137 }