Update copyright annotations and license boilerplates to correspond with SGI Legals...
[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;
65         size_t          buflen = 10000;
66         size_t          rlenp;
67         void            *hanp;
68         size_t           hlen;
69         char            *name;
70         int             opt;
71
72         if (Progname = strrchr(argv[0], '/')) {
73                 Progname++;
74         } else {
75                 Progname = argv[0];
76         }
77
78         /* Crack and validate the command line options. */
79
80         while ((opt = getopt(argc, argv, "b:s:t:")) != EOF) {
81                 switch (opt) {
82                 case 'b':
83                         buflen = atol(optarg);
84                         break;
85                 case 's':
86                         sid = atol(optarg);
87                         break;
88                 case 't':
89                         token = atol(optarg);
90                         break;
91                 case '?':
92                         usage();
93                 }
94         }
95         if (optind + 2 != argc)
96                 usage();
97         object = argv[optind++];
98         attrnamep = (dm_attrname_t *)argv[optind];
99
100         if (dm_init_service(&name) == -1)  {
101                 fprintf(stderr, "Can't initialize the DMAPI\n");
102                 exit(1);
103         }
104         if (sid == DM_NO_SESSION)
105                 find_test_session(&sid);
106
107         /* Get the file's handle. */
108
109         if (opaque_to_handle(object, &hanp, &hlen)) {
110                 fprintf(stderr, "can't get handle for %s\n", object);
111                 exit(1);
112         }
113
114         if (buflen > 0) {
115                 if ((bufp = malloc(buflen)) == NULL) {
116                         fprintf(stderr, "malloc failed, %s\n", strerror(errno));
117                         exit(1);
118                 }
119         }
120
121         if (dm_get_dmattr(sid, hanp, hlen, token, attrnamep, buflen,
122             bufp, &rlenp)) {
123                 if (errno == E2BIG) {
124                         fprintf(stderr, "dm_get_dmattr buffer too small, "
125                                 "should be %d bytes\n", rlenp);
126                 } else {
127                         fprintf(stderr, "dm_get_dmattr failed, %s\n",
128                                 strerror(errno));
129                 }
130                 exit(1);
131         }
132         fprintf(stdout, "rlenp is %d, value is '%s'\n", rlenp, (char*)bufp);
133
134         dm_handle_free(hanp, hlen);
135         exit(0);
136 }