1 | /* Test program for mtrace.
|
---|
2 | Copyright (C) 2000 Free Software Foundation, Inc.
|
---|
3 | This file is part of the GNU C Library.
|
---|
4 |
|
---|
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.
|
---|
9 |
|
---|
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.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU Lesser General Public
|
---|
16 | License along with the GNU C Library; if not, write to the Free
|
---|
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
18 | 02111-1307 USA. */
|
---|
19 |
|
---|
20 | #include <mcheck.h>
|
---|
21 | #include <paths.h>
|
---|
22 | #include <search.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <string.h>
|
---|
26 |
|
---|
27 |
|
---|
28 | static void print (const void *node, VISIT value, int level);
|
---|
29 |
|
---|
30 | /* Used for several purposes. */
|
---|
31 | static FILE *fp;
|
---|
32 |
|
---|
33 |
|
---|
34 | int
|
---|
35 | main (void)
|
---|
36 | {
|
---|
37 | void *root = NULL;
|
---|
38 | size_t linelen = 0;
|
---|
39 | char *line = NULL;
|
---|
40 |
|
---|
41 | /* Enable memory usage tracing. */
|
---|
42 | mtrace ();
|
---|
43 |
|
---|
44 | /* Perform some operations which definitely will allocate some
|
---|
45 | memory. */
|
---|
46 | fp = fopen (__FILE__, "r");
|
---|
47 | if (fp == NULL)
|
---|
48 | /* Shouldn't happen since this program is executed in the source
|
---|
49 | directory. */
|
---|
50 | abort ();
|
---|
51 |
|
---|
52 | while (!feof (fp))
|
---|
53 | {
|
---|
54 | char **p;
|
---|
55 | char *copy;
|
---|
56 | ssize_t n = getline (&line, &linelen, fp);
|
---|
57 |
|
---|
58 | if (n < 0)
|
---|
59 | break;
|
---|
60 |
|
---|
61 | if (n == 0)
|
---|
62 | continue;
|
---|
63 |
|
---|
64 | copy = strdup (line);
|
---|
65 | if (copy == NULL)
|
---|
66 | abort ();
|
---|
67 |
|
---|
68 | p = (char **) tsearch (copy, &root,
|
---|
69 | (int (*) (const void *, const void *)) strcmp);
|
---|
70 | if (*p != copy)
|
---|
71 | /* This line wasn't added. */
|
---|
72 | free (copy);
|
---|
73 | }
|
---|
74 |
|
---|
75 | fclose (fp);
|
---|
76 |
|
---|
77 | fp = fopen (_PATH_DEVNULL, "w");
|
---|
78 | if (fp != NULL)
|
---|
79 | {
|
---|
80 | /* Write something through stdout. */
|
---|
81 | twalk (root, print);
|
---|
82 |
|
---|
83 | fclose (fp);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* Free everything. */
|
---|
87 | tdestroy (root, free);
|
---|
88 |
|
---|
89 | /* Also the line buffer. */
|
---|
90 | free (line);
|
---|
91 |
|
---|
92 | /* That's it. */
|
---|
93 | return 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static void
|
---|
98 | print (const void *node, VISIT value, int level)
|
---|
99 | {
|
---|
100 | static int cnt;
|
---|
101 | if (value == postorder || value == leaf)
|
---|
102 | fprintf (fp, "%3d: %s", ++cnt, *(const char **) node);
|
---|
103 | }
|
---|