1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1997-1999 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* util.c -- low-level utilities used by map*.c
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "xlator.h"
#include "util.h"
#include "errlog.h"
/*
* String tables -- WARNING! This uses realloc to recreate tables,
* so always assign table_t * return value to the current
* table pointer, lest the table address change in the
* called function.
*/
static char *strset(char *, char *);
table_t *
create_stringtable(int size)
{
table_t *t;
/* Solaris idiom: malloc && memset. TBD. */
if ((t = calloc((size_t)1, (size_t)(sizeof (table_t) +
((sizeof (char *)) * size)))) == NULL) {
errlog(FATAL,
"\nOut of memory.\n"
"We wish to hold the whole sky,\n"
"But we never will.\n");
}
t->nelem = size;
t->used = -1;
return (t);
}
table_t *
add_to_stringtable(table_t *t, char *value)
{
table_t *t2;
int i;
if (t == NULL) {
seterrline(__LINE__, __FILE__, NULL, NULL);
errlog(FATAL|PROGRAM, "programmer error: tried to add to "
"a NULL table");
}
if (in_stringtable(t, value)) {
return (t);
}
++t->used;
if (t->used >= t->nelem) {
if ((t2 = realloc(t, (size_t)(sizeof (table_t) +
((sizeof (char *)) * (t->nelem + TABLE_INCREMENT)))))
== NULL) {
print_stringtable(t);
seterrline(__LINE__, __FILE__, NULL, NULL);
errlog(FATAL|PROGRAM, "out of memory extending a "
"string table");
}
t = t2;
t->nelem += TABLE_INCREMENT;
for (i = t->used; i < t->nelem; ++i) {
t->elements[i] = NULL;
}
}
t->elements[t->used] = strset(t->elements[t->used], value);
return (t);
}
/*
* free_stringtable -- really only mark it empty for reuse.
*/
table_t *
free_stringtable(table_t *t)
{
if (t != NULL) {
t->used = -1;
}
return (t);
}
char *
get_stringtable(table_t *t, int index)
{
if (t == NULL) {
return (NULL);
} else if (index > t->used) {
return (NULL);
} else {
return (t->elements[index]);
}
}
int
in_stringtable(table_t *t, const char *value)
{
int i;
if (t == NULL) {
return (0);
}
for (i = 0; i <= t->used; ++i) {
if (strcmp(value, t->elements[i]) == 0)
return (1);
}
return (0);
}
void
print_stringtable(table_t *t)
{
int i;
if (t == NULL)
return;
errlog(VERBOSE,
"table size = %d elements out of %d elements/%d bytes\n",
t->used + 1, t->nelem,
sizeof (table_t) + (sizeof (char *) * t->nelem));
for (i = 0; i <= t->used; ++i) {
(void) fprintf(stderr, "\t%s\n",
get_stringtable(t, i));
}
}
static int
compare(const void *p, const void *q)
{
return (strcmp((char *)p, (char *)q));
}
void
sort_stringtable(table_t *t)
{
if (t && t->used > 0) {
qsort((char *)t->elements, (size_t)t->used,
sizeof (char *), compare);
}
}
/*
* strset -- update a dynamically-allocated string or die trying.
*/
/*ARGSUSED*/
static char *
strset(char *string, char *value)
{
size_t vlen;
assert(value != NULL, "passed a null value to strset");
vlen = strlen(value);
if (string == NULL) {
/* It was never allocated, so allocate it. */
if ((string = malloc(vlen + 1)) == NULL) {
seterrline(__LINE__, __FILE__, NULL, NULL);
errlog(FATAL|PROGRAM, "out of memory allocating a "
"string");
}
} else if (strlen(string) < vlen) {
/* Reallocate bigger. */
if ((string = realloc(string, vlen + 1)) == NULL) {
seterrline(__LINE__, __FILE__, NULL, NULL);
errlog(FATAL|PROGRAM, "out of memory reallocating"
"a string");
}
}
(void) strcpy(string, value);
return (string);
}
|