LCA2011 version
[tridge/junkcode.git] / uess_leak.c
1 /*
2   demonstration of a memory leak in the AIX C library UESS subsystem
3
4   tridge@au.ibm.com, January 2004
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <pwd.h>
10 #include <malloc.h>
11
12 /*
13   return the current size of the heap (in blocks)
14 */
15 static unsigned long heap_size(void)
16 {
17         struct mallinfo m = mallinfo();
18         return m.ordblks + m.smblks;
19 }
20
21
22 int main(int argc, char *argv[])
23 {
24         char *name;
25         int loops=0;
26
27         if (argc < 2) {
28                 printf("usage: uess_leak <USERNAME>\n");
29                 exit(1);
30         }
31
32         name = argv[1];
33
34         while (1) {
35                 struct passwd *pwd = getpwnam(name);
36                 if (!pwd) {
37                         perror("getpwnam");
38                         exit(1);
39                 }
40                 printf("memory blocks used %ld after %d calls\r", 
41                        heap_size(), loops++);
42                 fflush(stdout);
43         }
44         return 0;
45 }