Print this page
OS-2444 richmond hardware maps need to support ivy bridge (review fixes)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/fm/topo/libtopo/common/topo_string.c
+++ new/usr/src/lib/fm/topo/libtopo/common/topo_string.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26 /*
27 27 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 28 */
29 29
30 30 #include <strings.h>
31 31 #include <ctype.h>
32 32 #include <fm/libtopo.h>
33 33 #include <fm/topo_mod.h>
34 34 #include <topo_alloc.h>
35 35
36 36 char *
37 37 topo_hdl_strdup(topo_hdl_t *thp, const char *s)
38 38 {
39 39 char *p;
40 40
41 41 if (s != NULL)
42 42 p = topo_hdl_alloc(thp, strlen(s) + 1);
43 43 else
44 44 p = NULL;
45 45
46 46 if (p != NULL)
47 47 (void) strcpy(p, s);
48 48
49 49 return (p);
50 50 }
51 51
52 52 void
53 53 topo_hdl_strfree(topo_hdl_t *thp, char *s)
54 54 {
55 55 if (s != NULL)
56 56 topo_hdl_free(thp, s, strlen(s) + 1);
57 57 }
58 58
59 59 char *
60 60 topo_hdl_strsplit(topo_hdl_t *hdl, const char *input, const char *sep,
61 61 char **lastp)
62 62 {
↓ open down ↓ |
62 lines elided |
↑ open up ↑ |
63 63 size_t seplen = strlen(sep);
64 64 const char *scanstart;
65 65 char *token;
66 66 char *ret;
67 67
68 68 if (input != NULL) {
69 69 /*
70 70 * Start scanning at beginning of input:
71 71 */
72 72 scanstart = input;
73 - } else {
73 + } else if (*lastp == NULL) {
74 74 /*
75 75 * If we have already finished scanning, return NULL.
76 76 */
77 - if (*lastp == NULL)
78 - return (NULL);
79 -
77 + return (NULL);
78 + } else {
80 79 /*
81 80 * Otherwise, start scanning where we left off:
82 81 */
83 82 scanstart = *lastp;
84 83 }
85 84
86 85 token = strstr(scanstart, sep);
87 86 if (token != NULL) {
88 87 /*
89 88 * We still have a separator, so advance the next-start
90 89 * pointer past it:
91 90 */
92 91 *lastp = token + seplen;
93 92 /*
94 - * Copy out this element:
93 + * Copy out this element. The buffer must fit the string
94 + * exactly, so that topo_hdl_strfree() can determine its
95 + * size with strlen().
95 96 */
96 97 ret = topo_hdl_alloc(hdl, token - scanstart + 1);
97 98 (void) strncpy(ret, scanstart, token - scanstart);
98 99 ret[token - scanstart] = '\0';
99 100 } else {
100 101 /*
101 102 * We have no separator, so this is the last element:
102 103 */
103 104 *lastp = NULL;
104 105 ret = topo_hdl_strdup(hdl, scanstart);
105 106 }
106 107
107 108 return (ret);
108 109 }
109 110
110 111 char *
111 112 topo_mod_strdup(topo_mod_t *mod, const char *s)
112 113 {
113 114 return (topo_hdl_strdup(mod->tm_hdl, s));
114 115 }
115 116
116 117 void
117 118 topo_mod_strfree(topo_mod_t *mod, char *s)
118 119 {
119 120 topo_hdl_strfree(mod->tm_hdl, s);
120 121 }
121 122
122 123 char *
123 124 topo_mod_strsplit(topo_mod_t *mod, const char *input, const char *sep,
124 125 char **lastp)
125 126 {
126 127 return (topo_hdl_strsplit(mod->tm_hdl, input, sep, lastp));
127 128 }
128 129
129 130 const char *
130 131 topo_strbasename(const char *s)
131 132 {
132 133 const char *p = strrchr(s, '/');
133 134
134 135 if (p == NULL)
135 136 return (s);
136 137
137 138 return (++p);
138 139 }
139 140
140 141 char *
141 142 topo_strdirname(char *s)
142 143 {
143 144 static char slash[] = "/";
144 145 static char dot[] = ".";
145 146 char *p;
146 147
147 148 if (s == NULL || *s == '\0')
148 149 return (dot);
149 150
150 151 for (p = s + strlen(s); p != s && *--p == '/'; )
151 152 continue;
152 153
153 154 if (p == s && *p == '/')
154 155 return (slash);
155 156
156 157 while (p != s) {
157 158 if (*--p == '/') {
158 159 while (*p == '/' && p != s)
159 160 p--;
160 161 *++p = '\0';
161 162 return (s);
162 163 }
163 164 }
164 165
165 166 return (dot);
166 167 }
167 168
168 169 ulong_t
169 170 topo_strhash(const char *key)
170 171 {
171 172 ulong_t g, h = 0;
172 173 const char *p;
173 174
174 175 for (p = key; *p != '\0'; p++) {
175 176 h = (h << 4) + *p;
176 177
177 178 if ((g = (h & 0xf0000000)) != 0) {
178 179 h ^= (g >> 24);
179 180 h ^= g;
180 181 }
181 182 }
182 183
183 184 return (h);
184 185 }
185 186
186 187 /*
187 188 * Transform string s inline, converting each embedded C escape sequence string
188 189 * to the corresponding character. For example, the substring "\n" is replaced
189 190 * by an inline '\n' character. The length of the resulting string is returned.
190 191 */
191 192 size_t
192 193 topo_stresc2chr(char *s)
193 194 {
194 195 char *p, *q, c;
195 196 int esc = 0;
196 197 int x;
197 198
198 199 for (p = q = s; (c = *p) != '\0'; p++) {
199 200 if (esc) {
200 201 switch (c) {
201 202 case '0':
202 203 case '1':
203 204 case '2':
204 205 case '3':
205 206 case '4':
206 207 case '5':
207 208 case '6':
208 209 case '7':
209 210 c -= '0';
210 211 p++;
211 212
212 213 if (*p >= '0' && *p <= '7') {
213 214 c = c * 8 + *p++ - '0';
214 215
215 216 if (*p >= '0' && *p <= '7')
216 217 c = c * 8 + *p - '0';
217 218 else
218 219 p--;
219 220 } else
220 221 p--;
221 222
222 223 *q++ = c;
223 224 break;
224 225
225 226 case 'a':
226 227 *q++ = '\a';
227 228 break;
228 229 case 'b':
229 230 *q++ = '\b';
230 231 break;
231 232 case 'f':
232 233 *q++ = '\f';
233 234 break;
234 235 case 'n':
235 236 *q++ = '\n';
236 237 break;
237 238 case 'r':
238 239 *q++ = '\r';
239 240 break;
240 241 case 't':
241 242 *q++ = '\t';
242 243 break;
243 244 case 'v':
244 245 *q++ = '\v';
245 246 break;
246 247
247 248 case 'x':
248 249 for (x = 0; (c = *++p) != '\0'; ) {
249 250 if (c >= '0' && c <= '9')
250 251 x = x * 16 + c - '0';
251 252 else if (c >= 'a' && c <= 'f')
252 253 x = x * 16 + c - 'a' + 10;
253 254 else if (c >= 'A' && c <= 'F')
254 255 x = x * 16 + c - 'A' + 10;
255 256 else
256 257 break;
257 258 }
258 259 *q++ = (char)x;
259 260 p--;
260 261 break;
261 262
262 263 case '"':
263 264 case '\\':
264 265 *q++ = c;
265 266 break;
266 267 default:
267 268 *q++ = '\\';
268 269 *q++ = c;
269 270 }
270 271
271 272 esc = 0;
272 273
273 274 } else {
274 275 if ((esc = c == '\\') == 0)
275 276 *q++ = c;
276 277 }
277 278 }
278 279
279 280 *q = '\0';
280 281 return ((size_t)(q - s));
281 282 }
282 283
283 284 int
284 285 topo_strmatch(const char *s, const char *p)
285 286 {
286 287 char c;
287 288
288 289 if (p == NULL)
289 290 return (0);
290 291
291 292 if (s == NULL)
292 293 s = ""; /* treat NULL string as the empty string */
293 294
294 295 do {
295 296 if ((c = *p++) == '\0')
296 297 return (*s == '\0');
297 298
298 299 if (c == '*') {
299 300 while (*p == '*')
300 301 p++; /* consecutive *'s can be collapsed */
301 302
302 303 if (*p == '\0')
303 304 return (1);
304 305
305 306 while (*s != '\0') {
306 307 if (topo_strmatch(s++, p) != 0)
307 308 return (1);
308 309 }
309 310
310 311 return (0);
311 312 }
312 313 } while (c == *s++);
313 314
314 315 return (0);
315 316 }
↓ open down ↓ |
211 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX