Update copyright annotations and license boilerplates to correspond with SGI Legals...
[xfstests-dev.git] / dmapi / src / suite1 / cmd / get_events.c
1 /*
2  * Copyright (c) 2000-2005 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_events().  The
27 command line is:
28
29         get_events [-b buflen] [-m maxmsgs] [-f] sid
30
31 where buflen is the size of the buffer to use, maxmsgs is the number of messages
32 to read, -f, if selected, is DM_EV_WAIT, and sid is the session ID
33 whose dispositions 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] [-m maxmsgs] [-f] sid\n",
50                 Progname);
51         exit(1);
52 }
53
54
55 int
56 main(
57         int     argc, 
58         char    **argv)
59 {
60         dm_eventmsg_t   *msg;
61         dm_sessid_t     sid;
62         u_int           flags = 0;
63         void            *bufp;
64         size_t          buflen = 10000;
65         u_int           maxmsgs = 1;
66         size_t          rlenp;
67         char            *name;
68         int             opt;
69
70         if (Progname = strrchr(argv[0], '/')) {
71                 Progname++;
72         } else {
73                 Progname = argv[0];
74         }
75
76         /* Crack and validate the command line options. */
77
78         while ((opt = getopt(argc, argv, "b:m:f")) != EOF) {
79                 switch (opt) {
80                 case 'b':
81                         buflen = atol(optarg);
82                         break;
83                 case 'm':
84                         maxmsgs = atol(optarg);
85                         break;
86                 case 'f':
87                         flags = DM_EV_WAIT;
88                         break;
89                 case '?':
90                         usage();
91                 }
92         }
93         if (optind + 1 != argc)
94                 usage();
95         sid = atol(argv[optind]);
96
97         if (dm_init_service(&name) == -1)  {
98                 fprintf(stderr, "Can't initialize the DMAPI\n");
99                 exit(1);
100         }
101
102         if (buflen > 0) {
103                 if ((bufp = malloc(buflen)) == NULL) {
104                         fprintf(stderr, "malloc failed, %s\n", strerror(errno));
105                         exit(1);
106                 }
107         }
108
109         if (dm_get_events(sid, maxmsgs, flags, buflen, bufp, &rlenp)) {
110                 if (errno == E2BIG) {
111                         fprintf(stderr, "dm_get_events buffer too small, "
112                                 "should be %d bytes\n", rlenp);
113                 } else {
114                         fprintf(stderr, "dm_get_events failed, (%d)%s\n",
115                                 errno, strerror(errno));
116                 }
117                 exit(1);
118         }
119         fprintf(stdout, "rlenp=%d\n", rlenp);
120
121         if (rlenp == 0)
122                 return(0);
123
124         msg = bufp;
125         while (msg != NULL) {
126                 print_one_message(msg);
127                 msg = DM_STEP_TO_NEXT(msg, dm_eventmsg_t *);
128         }
129         return(0);
130 }