1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
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:
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:
42: mtrace ();
43:
44:
45:
46: fp = fopen (__FILE__, "r");
47: if (fp == NULL)
48:
49:
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:
72: free (copy);
73: }
74:
75: fclose (fp);
76:
77: fp = fopen (_PATH_DEVNULL, "w");
78: if (fp != NULL)
79: {
80:
81: twalk (root, print);
82:
83: fclose (fp);
84: }
85:
86:
87: tdestroy (root, free);
88:
89:
90: free (line);
91:
92:
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: }