1 /*
2   Copyright (C) 2000,2004 Silicon Graphics, Inc.  All Rights Reserved.
3   Portions Copyright(C) David Anderson 2016. All Rights reserved.
4 
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of version 2 of the GNU General Public License as
7   published by the Free Software Foundation.
8 
9   This program is distributed in the hope that it would be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13   Further, this software is distributed without any warranty that it is
14   free of the rightful claim of any third person regarding infringement
15   or the like.  Any license provided herein, whether implied or
16   otherwise, applies only to this software file.  Patent licenses, if
17   any, provided herein do not apply to combinations of this program with
18   other software, or any other product whatsoever.
19 
20   You should have received a copy of the GNU General Public License along
21   with this program; if not, write the Free Software Foundation, Inc., 51
22   Franklin Street - Fifth Floor, Boston MA 02110-1301, USA.
23 
24    makename.c
25    $Revision: 1.4 $
26    $Date: 2005/11/08 21:48:42 $
27 
28    This used to be elaborate stuff.
29    Now it is trivial, as duplicating names is
30    unimportant in dwarfdump (in general).
31 
32    And in fact, this is only called for attributes and
33    tags etc whose true name is unknown. Not for
34    any normal case.
35 
36 */
37 
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include "dwarf_tsearch.h"
42 #include "makename.h"
43 #include "globals.h"
44 
45 #ifdef _WIN32
46 #pragma warning(disable:4996)    /* Warning when migrated to VS2010 */
47 #endif /* _WIN32 */
48 
49 #define TRUE 1
50 #define FALSE 0
51 
52 static void * makename_data;
53 #define VALTYPE char *
54 #define DW_TSHASHTYPE char *
55 
56 static int
value_compare_func(const void * l,const void * r)57 value_compare_func(const void *l, const void *r)
58 {
59     VALTYPE ml = (VALTYPE)l;
60     VALTYPE mr = (VALTYPE)r;
61     return strcmp(ml,mr);
62 }
63 /* Nothing to free for the 'value' example. */
64 static void
value_node_free(void * valp)65 value_node_free(void *valp)
66 {
67    VALTYPE v = (VALTYPE)valp;
68    free(v);
69 }
70 
71 void
makename_destructor(void)72 makename_destructor(void)
73 {
74     dwarf_tdestroy(makename_data,value_node_free);
75     makename_data = 0;
76 }
77 
78 /*  WARNING: the tree walk functions will, if presented **tree
79     when *tree is wanted, simply find nothing. No error,
80     just bad results. So when a walk produces nothing
81     suspect a code mistake here.
82     The basic problem is void* is a terrible way to
83     pass in a pointer. But it's how tsearch was defined
84     long ago.
85 */
86 
87 char *
makename(const char * s)88 makename(const char *s)
89 {
90     char *newstr = 0;
91     VALTYPE re = 0;
92     void *retval = 0;
93 
94     if (!s) {
95         return "";
96     }
97 #ifdef SELFTEST
98     printf("Selftest with name %s\n",s);
99 #endif
100 
101     newstr = (char *)strdup(s);
102     retval = dwarf_tfind(newstr,&makename_data, value_compare_func);
103     if (retval) {
104         /* We found our string, it existed already. */
105         re = *(VALTYPE *)retval;
106         free(newstr);
107         return re;
108     }
109     retval = dwarf_tsearch(newstr,&makename_data, value_compare_func);
110     if (!retval) {
111         /*  Out of memory, lets just use the string we dup'd and
112             let it leak. Things will surely fail anyway. */
113         return newstr;
114     }
115     re = *(VALTYPE *)retval;
116     return re;
117 }
118 
119 /*  We will make a search tree using a simple value
120     (the pointer from an strdup) */
121 
122 
123 
124 
125 #ifdef SELFTEST
126 
127 char *samples[]  = {
128 "abcd",
129 "efgh",
130 "a",
131 "abcd",
132 0
133 };
134 
main()135 int main()
136 {
137     char *e1 = 0;
138     char *e2= 0;
139     char *e3= 0;
140     char *e4= 0;
141     int j = 0;
142     int errct = 0;
143 
144 
145     e1 = makename(samples[0]);
146     e2 = makename(samples[1]);
147     e3 = makename(samples[2]);
148     e4 = makename(samples[3]);
149 
150     if (e1 != e4) {
151         printf(" FAIL. mismatch  pointers\n");
152         ++errct;
153     }
154     if (e1 == e2 ) {
155         printf(" FAIL. match  pointers\n");
156         ++errct;
157     }
158     if ( e1 == e3) {
159         printf(" FAIL. match  pointers\n");
160         ++errct;
161     }
162     if (errct) {
163         exit(1);
164     }
165     printf("PASS makename test\n");
166     return 0;
167 }
168 #endif /* SELFTEST */
169