Update copyright notices with scripts/update-copyrights
[glibc.git] / malloc / tst-mtrace.c
blobe750bc89678784d14f77d38df039d32fa9a5d110
1 /* Test program for mtrace.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <mcheck.h>
20 #include <paths.h>
21 #include <search.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
27 static void print (const void *node, VISIT value, int level);
29 /* Used for several purposes. */
30 static FILE *fp;
33 int
34 main (void)
36 void *root = NULL;
37 size_t linelen = 0;
38 char *line = NULL;
40 /* Enable memory usage tracing. */
41 mtrace ();
43 /* Perform some operations which definitely will allocate some
44 memory. */
45 fp = fopen (__FILE__, "r");
46 if (fp == NULL)
47 /* Shouldn't happen since this program is executed in the source
48 directory. */
49 abort ();
51 while (!feof (fp))
53 char **p;
54 char *copy;
55 ssize_t n = getline (&line, &linelen, fp);
57 if (n < 0)
58 break;
60 if (n == 0)
61 continue;
63 copy = strdup (line);
64 if (copy == NULL)
65 abort ();
67 p = (char **) tsearch (copy, &root,
68 (int (*) (const void *, const void *)) strcmp);
69 if (*p != copy)
70 /* This line wasn't added. */
71 free (copy);
74 fclose (fp);
76 fp = fopen (_PATH_DEVNULL, "w");
77 if (fp != NULL)
79 /* Write something through stdout. */
80 twalk (root, print);
82 fclose (fp);
85 /* Free everything. */
86 tdestroy (root, free);
88 /* Also the line buffer. */
89 free (line);
91 /* That's it. */
92 return 0;
96 static void
97 print (const void *node, VISIT value, int level)
99 static int cnt;
100 if (value == postorder || value == leaf)
101 fprintf (fp, "%3d: %s", ++cnt, *(const char **) node);