Import sendmail 8.13.8
[dragonfly.git] / contrib / sendmail-8.13.8 / libsm / t-memstat.c
1 /*
2  * Copyright (c) 2005, 2006 Sendmail, Inc. and its suppliers.
3  *      All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9
10 #include <sm/gen.h>
11 SM_IDSTR(id, "@(#)$Id: t-memstat.c,v 1.6 2006/03/27 22:34:47 ca Exp $")
12
13 /*
14 **  Simple test program for memstat
15 */
16
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <strings.h>
21 #include <string.h>
22
23 extern char *optarg;
24 extern int optind;
25
26 int
27 main(argc, argv)
28         int argc;
29         char **argv;
30 {
31         int r, r2, i, l, slp, sz;
32         long v;
33         char *resource;
34
35         l = 1;
36         sz = slp = 0;
37         resource = NULL;
38         while ((r = getopt(argc, argv, "l:m:r:s:")) != -1)
39         {
40                 switch ((char) r)
41                 {
42                   case 'l':
43                         l = strtol(optarg, NULL, 0);
44                         break;
45
46                   case 'm':
47                         sz = strtol(optarg, NULL, 0);
48                         break;
49
50                   case 'r':
51                         resource = strdup(optarg);
52                         break;
53
54                   case 's':
55                         slp = strtol(optarg, NULL, 0);
56                         break;
57
58                   default:
59                         break;
60                 }
61         }
62
63         r = sm_memstat_open();
64         r2 = -1;
65         for (i = 0; i < l; i++)
66         {
67                 char *mem;
68
69                 r2 = sm_memstat_get(resource, &v);
70                 if (slp > 0 && i + 1 < l && 0 == r)
71                 {
72                         printf("open=%d, memstat=%d, %s=%ld\n", r, r2,
73                                 resource != NULL ? resource : "default-value",
74                                 v);
75                         sleep(slp);
76                         if (sz > 0)
77                         {
78                                 /*
79                                 **  Just allocate some memory to test the
80                                 **  values that are returned.
81                                 **  Note: this is a memory leak, but that
82                                 **  doesn't matter here.
83                                 */
84
85                                 mem = malloc(sz);
86                                 if (NULL == mem)
87                                         printf("malloc(%d) failed\n", sz);
88                         }
89                 }
90         }
91         printf("open=%d, memstat=%d, %s=%ld\n", r, r2,
92                 resource != NULL ? resource : "default-value", v);
93         r = sm_memstat_close();
94         return r;
95 }