]>
Commit | Line | Data |
---|---|---|
18607dcb FB |
1 | /* |
2 | * Simple C functions to supplement the C library | |
5fafdf24 | 3 | * |
18607dcb FB |
4 | * Copyright (c) 2006 Fabrice Bellard |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
856dfd8a | 24 | |
aafd7584 | 25 | #include "qemu/osdep.h" |
1de7afc9 | 26 | #include "qemu/host-utils.h" |
9f9b17a4 | 27 | #include <math.h> |
18607dcb | 28 | |
06680b15 MAL |
29 | #ifdef __FreeBSD__ |
30 | #include <sys/sysctl.h> | |
31 | #include <sys/user.h> | |
32 | #endif | |
33 | ||
34 | #ifdef __NetBSD__ | |
35 | #include <sys/sysctl.h> | |
36 | #endif | |
37 | ||
856dfd8a | 38 | #include "qemu/ctype.h" |
f348b6d1 | 39 | #include "qemu/cutils.h" |
05cb8ed5 | 40 | #include "qemu/error-report.h" |
8c5135f9 | 41 | |
2a025ae4 DF |
42 | void strpadcpy(char *buf, int buf_size, const char *str, char pad) |
43 | { | |
44 | int len = qemu_strnlen(str, buf_size); | |
45 | memcpy(buf, str, len); | |
46 | memset(buf + len, pad, buf_size - len); | |
47 | } | |
48 | ||
18607dcb FB |
49 | void pstrcpy(char *buf, int buf_size, const char *str) |
50 | { | |
51 | int c; | |
52 | char *q = buf; | |
53 | ||
54 | if (buf_size <= 0) | |
55 | return; | |
56 | ||
57 | for(;;) { | |
58 | c = *str++; | |
59 | if (c == 0 || q >= buf + buf_size - 1) | |
60 | break; | |
61 | *q++ = c; | |
62 | } | |
63 | *q = '\0'; | |
64 | } | |
65 | ||
66 | /* strcat and truncate. */ | |
67 | char *pstrcat(char *buf, int buf_size, const char *s) | |
68 | { | |
69 | int len; | |
70 | len = strlen(buf); | |
5fafdf24 | 71 | if (len < buf_size) |
18607dcb FB |
72 | pstrcpy(buf + len, buf_size - len, s); |
73 | return buf; | |
74 | } | |
75 | ||
76 | int strstart(const char *str, const char *val, const char **ptr) | |
77 | { | |
78 | const char *p, *q; | |
79 | p = str; | |
80 | q = val; | |
81 | while (*q != '\0') { | |
82 | if (*p != *q) | |
83 | return 0; | |
84 | p++; | |
85 | q++; | |
86 | } | |
87 | if (ptr) | |
88 | *ptr = p; | |
89 | return 1; | |
90 | } | |
91 | ||
92 | int stristart(const char *str, const char *val, const char **ptr) | |
93 | { | |
94 | const char *p, *q; | |
95 | p = str; | |
96 | q = val; | |
97 | while (*q != '\0') { | |
cd390083 | 98 | if (qemu_toupper(*p) != qemu_toupper(*q)) |
18607dcb FB |
99 | return 0; |
100 | p++; | |
101 | q++; | |
102 | } | |
103 | if (ptr) | |
104 | *ptr = p; | |
105 | return 1; | |
106 | } | |
3c6b2088 | 107 | |
d43277c5 BS |
108 | /* XXX: use host strnlen if available ? */ |
109 | int qemu_strnlen(const char *s, int max_len) | |
110 | { | |
111 | int i; | |
112 | ||
113 | for(i = 0; i < max_len; i++) { | |
114 | if (s[i] == '\0') { | |
115 | break; | |
116 | } | |
117 | } | |
118 | return i; | |
119 | } | |
120 | ||
a38ed811 KW |
121 | char *qemu_strsep(char **input, const char *delim) |
122 | { | |
123 | char *result = *input; | |
124 | if (result != NULL) { | |
125 | char *p; | |
126 | ||
127 | for (p = result; *p != '\0'; p++) { | |
128 | if (strchr(delim, *p)) { | |
129 | break; | |
130 | } | |
131 | } | |
132 | if (*p == '\0') { | |
133 | *input = NULL; | |
134 | } else { | |
135 | *p = '\0'; | |
136 | *input = p + 1; | |
137 | } | |
138 | } | |
139 | return result; | |
140 | } | |
141 | ||
3c6b2088 FB |
142 | time_t mktimegm(struct tm *tm) |
143 | { | |
144 | time_t t; | |
145 | int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday; | |
146 | if (m < 3) { | |
147 | m += 12; | |
148 | y--; | |
149 | } | |
b6db4aca | 150 | t = 86400ULL * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + |
3c6b2088 FB |
151 | y / 400 - 719469); |
152 | t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; | |
153 | return t; | |
154 | } | |
b39ade83 | 155 | |
eba90e4e MA |
156 | static int64_t suffix_mul(char suffix, int64_t unit) |
157 | { | |
158 | switch (qemu_toupper(suffix)) { | |
17f94256 | 159 | case 'B': |
eba90e4e | 160 | return 1; |
17f94256 | 161 | case 'K': |
eba90e4e | 162 | return unit; |
17f94256 | 163 | case 'M': |
eba90e4e | 164 | return unit * unit; |
17f94256 | 165 | case 'G': |
eba90e4e | 166 | return unit * unit * unit; |
17f94256 | 167 | case 'T': |
eba90e4e | 168 | return unit * unit * unit * unit; |
17f94256 | 169 | case 'P': |
5e00984a | 170 | return unit * unit * unit * unit * unit; |
17f94256 | 171 | case 'E': |
5e00984a | 172 | return unit * unit * unit * unit * unit * unit; |
eba90e4e MA |
173 | } |
174 | return -1; | |
175 | } | |
176 | ||
9f9b17a4 | 177 | /* |
cf923b78 EB |
178 | * Convert size string to bytes. |
179 | * | |
180 | * The size parsing supports the following syntaxes | |
181 | * - 12345 - decimal, scale determined by @default_suffix and @unit | |
182 | * - 12345{bBkKmMgGtTpPeE} - decimal, scale determined by suffix and @unit | |
183 | * - 12345.678{kKmMgGtTpPeE} - decimal, scale determined by suffix, and | |
184 | * fractional portion is truncated to byte | |
185 | * - 0x7fEE - hexadecimal, unit determined by @default_suffix | |
186 | * | |
f174cd33 EB |
187 | * The following cause a deprecation warning, and may be removed in the future |
188 | * - 0xabc{kKmMgGtTpP} - hex with scaling suffix | |
189 | * | |
cf923b78 EB |
190 | * The following are intentionally not supported |
191 | * - octal, such as 08 | |
192 | * - fractional hex, such as 0x1.8 | |
193 | * - floating point exponents, such as 1e3 | |
194 | * | |
195 | * The end pointer will be returned in *end, if not NULL. If there is | |
196 | * no fraction, the input can be decimal or hexadecimal; if there is a | |
197 | * fraction, then the input must be decimal and there must be a suffix | |
198 | * (possibly by @default_suffix) larger than Byte, and the fractional | |
199 | * portion may suffer from precision loss or rounding. The input must | |
200 | * be positive. | |
201 | * | |
202 | * Return -ERANGE on overflow (with *@end advanced), and -EINVAL on | |
203 | * other error (with *@end left unchanged). | |
9f9b17a4 | 204 | */ |
af02f4c5 | 205 | static int do_strtosz(const char *nptr, const char **end, |
f17fd4fd | 206 | const char default_suffix, int64_t unit, |
f46bfdbf | 207 | uint64_t *result) |
9f9b17a4 | 208 | { |
f17fd4fd | 209 | int retval; |
cf923b78 | 210 | const char *endptr, *f; |
eba90e4e | 211 | unsigned char c; |
7625a1ed RH |
212 | bool hex = false; |
213 | uint64_t val, valf = 0; | |
cf923b78 | 214 | int64_t mul; |
9f9b17a4 | 215 | |
cf923b78 EB |
216 | /* Parse integral portion as decimal. */ |
217 | retval = qemu_strtou64(nptr, &endptr, 10, &val); | |
af02f4c5 | 218 | if (retval) { |
4fcdf65a | 219 | goto out; |
9f9b17a4 | 220 | } |
cf923b78 EB |
221 | if (memchr(nptr, '-', endptr - nptr) != NULL) { |
222 | endptr = nptr; | |
223 | retval = -EINVAL; | |
224 | goto out; | |
225 | } | |
226 | if (val == 0 && (*endptr == 'x' || *endptr == 'X')) { | |
227 | /* Input looks like hex, reparse, and insist on no fraction. */ | |
228 | retval = qemu_strtou64(nptr, &endptr, 16, &val); | |
229 | if (retval) { | |
230 | goto out; | |
231 | } | |
232 | if (*endptr == '.') { | |
233 | endptr = nptr; | |
234 | retval = -EINVAL; | |
235 | goto out; | |
236 | } | |
f174cd33 | 237 | hex = true; |
cf923b78 EB |
238 | } else if (*endptr == '.') { |
239 | /* | |
240 | * Input looks like a fraction. Make sure even 1.k works | |
241 | * without fractional digits. If we see an exponent, treat | |
242 | * the entire input as invalid instead. | |
243 | */ | |
7625a1ed RH |
244 | double fraction; |
245 | ||
cf923b78 EB |
246 | f = endptr; |
247 | retval = qemu_strtod_finite(f, &endptr, &fraction); | |
248 | if (retval) { | |
cf923b78 EB |
249 | endptr++; |
250 | } else if (memchr(f, 'e', endptr - f) || memchr(f, 'E', endptr - f)) { | |
251 | endptr = nptr; | |
252 | retval = -EINVAL; | |
253 | goto out; | |
7625a1ed RH |
254 | } else { |
255 | /* Extract into a 64-bit fixed-point fraction. */ | |
256 | valf = (uint64_t)(fraction * 0x1p64); | |
cf923b78 | 257 | } |
9f9b17a4 | 258 | } |
9f9b17a4 | 259 | c = *endptr; |
eba90e4e | 260 | mul = suffix_mul(c, unit); |
cf923b78 | 261 | if (mul > 0) { |
f174cd33 EB |
262 | if (hex) { |
263 | warn_report("Using a multiplier suffix on hex numbers " | |
264 | "is deprecated: %s", nptr); | |
265 | } | |
eba90e4e MA |
266 | endptr++; |
267 | } else { | |
268 | mul = suffix_mul(default_suffix, unit); | |
cf923b78 | 269 | assert(mul > 0); |
9f9b17a4 | 270 | } |
7625a1ed RH |
271 | if (mul == 1) { |
272 | /* When a fraction is present, a scale is required. */ | |
273 | if (valf != 0) { | |
274 | endptr = nptr; | |
275 | retval = -EINVAL; | |
276 | goto out; | |
277 | } | |
278 | } else { | |
279 | uint64_t valh, tmp; | |
280 | ||
281 | /* Compute exact result: 64.64 x 64.0 -> 128.64 fixed point */ | |
282 | mulu64(&val, &valh, val, mul); | |
283 | mulu64(&valf, &tmp, valf, mul); | |
284 | val += tmp; | |
285 | valh += val < tmp; | |
286 | ||
287 | /* Round 0.5 upward. */ | |
288 | tmp = valf >> 63; | |
289 | val += tmp; | |
290 | valh += val < tmp; | |
291 | ||
292 | /* Report overflow. */ | |
293 | if (valh != 0) { | |
294 | retval = -ERANGE; | |
295 | goto out; | |
296 | } | |
9f9b17a4 | 297 | } |
7625a1ed | 298 | |
f17fd4fd | 299 | retval = 0; |
9f9b17a4 | 300 | |
4fcdf65a | 301 | out: |
9f9b17a4 JS |
302 | if (end) { |
303 | *end = endptr; | |
4fcdf65a MA |
304 | } else if (*endptr) { |
305 | retval = -EINVAL; | |
9f9b17a4 | 306 | } |
061d7909 EB |
307 | if (retval == 0) { |
308 | *result = val; | |
309 | } | |
9f9b17a4 JS |
310 | |
311 | return retval; | |
312 | } | |
d8427002 | 313 | |
af02f4c5 | 314 | int qemu_strtosz(const char *nptr, const char **end, uint64_t *result) |
a732e1ba | 315 | { |
f17fd4fd | 316 | return do_strtosz(nptr, end, 'B', 1024, result); |
a732e1ba JR |
317 | } |
318 | ||
af02f4c5 | 319 | int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result) |
d8427002 | 320 | { |
f17fd4fd | 321 | return do_strtosz(nptr, end, 'M', 1024, result); |
d8427002 | 322 | } |
443916d1 | 323 | |
af02f4c5 | 324 | int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result) |
d2734d26 | 325 | { |
f17fd4fd | 326 | return do_strtosz(nptr, end, 'B', 1000, result); |
d2734d26 MA |
327 | } |
328 | ||
764e0fa4 | 329 | /** |
717adf96 | 330 | * Helper function for error checking after strtol() and the like |
764e0fa4 | 331 | */ |
717adf96 | 332 | static int check_strtox_error(const char *nptr, char *ep, |
6162f7da EB |
333 | const char **endptr, bool check_zero, |
334 | int libc_errno) | |
764e0fa4 | 335 | { |
53a90b97 | 336 | assert(ep >= nptr); |
6162f7da EB |
337 | |
338 | /* Windows has a bug in that it fails to parse 0 from "0x" in base 16 */ | |
339 | if (check_zero && ep == nptr && libc_errno == 0) { | |
340 | char *tmp; | |
341 | ||
342 | errno = 0; | |
343 | if (strtol(nptr, &tmp, 10) == 0 && errno == 0 && | |
344 | (*tmp == 'x' || *tmp == 'X')) { | |
345 | ep = tmp; | |
346 | } | |
347 | } | |
348 | ||
4baef267 MA |
349 | if (endptr) { |
350 | *endptr = ep; | |
351 | } | |
352 | ||
353 | /* Turn "no conversion" into an error */ | |
717adf96 | 354 | if (libc_errno == 0 && ep == nptr) { |
4baef267 | 355 | return -EINVAL; |
47d4be12 | 356 | } |
4baef267 MA |
357 | |
358 | /* Fail when we're expected to consume the string, but didn't */ | |
717adf96 | 359 | if (!endptr && *ep) { |
764e0fa4 CT |
360 | return -EINVAL; |
361 | } | |
4baef267 | 362 | |
717adf96 | 363 | return -libc_errno; |
764e0fa4 CT |
364 | } |
365 | ||
473a2a33 DB |
366 | /** |
367 | * Convert string @nptr to an integer, and store it in @result. | |
368 | * | |
369 | * This is a wrapper around strtol() that is harder to misuse. | |
370 | * Semantics of @nptr, @endptr, @base match strtol() with differences | |
371 | * noted below. | |
372 | * | |
373 | * @nptr may be null, and no conversion is performed then. | |
374 | * | |
375 | * If no conversion is performed, store @nptr in *@endptr and return | |
376 | * -EINVAL. | |
377 | * | |
378 | * If @endptr is null, and the string isn't fully converted, return | |
379 | * -EINVAL. This is the case when the pointer that would be stored in | |
380 | * a non-null @endptr points to a character other than '\0'. | |
381 | * | |
382 | * If the conversion overflows @result, store INT_MAX in @result, | |
383 | * and return -ERANGE. | |
384 | * | |
385 | * If the conversion underflows @result, store INT_MIN in @result, | |
386 | * and return -ERANGE. | |
387 | * | |
388 | * Else store the converted value in @result, and return zero. | |
389 | */ | |
390 | int qemu_strtoi(const char *nptr, const char **endptr, int base, | |
391 | int *result) | |
392 | { | |
393 | char *ep; | |
394 | long long lresult; | |
395 | ||
53a90b97 | 396 | assert((unsigned) base <= 36 && base != 1); |
473a2a33 DB |
397 | if (!nptr) { |
398 | if (endptr) { | |
399 | *endptr = nptr; | |
400 | } | |
401 | return -EINVAL; | |
402 | } | |
403 | ||
404 | errno = 0; | |
405 | lresult = strtoll(nptr, &ep, base); | |
406 | if (lresult < INT_MIN) { | |
407 | *result = INT_MIN; | |
408 | errno = ERANGE; | |
409 | } else if (lresult > INT_MAX) { | |
410 | *result = INT_MAX; | |
411 | errno = ERANGE; | |
412 | } else { | |
413 | *result = lresult; | |
414 | } | |
6162f7da | 415 | return check_strtox_error(nptr, ep, endptr, lresult == 0, errno); |
473a2a33 DB |
416 | } |
417 | ||
418 | /** | |
419 | * Convert string @nptr to an unsigned integer, and store it in @result. | |
420 | * | |
421 | * This is a wrapper around strtoul() that is harder to misuse. | |
422 | * Semantics of @nptr, @endptr, @base match strtoul() with differences | |
423 | * noted below. | |
424 | * | |
425 | * @nptr may be null, and no conversion is performed then. | |
426 | * | |
427 | * If no conversion is performed, store @nptr in *@endptr and return | |
428 | * -EINVAL. | |
429 | * | |
430 | * If @endptr is null, and the string isn't fully converted, return | |
431 | * -EINVAL. This is the case when the pointer that would be stored in | |
432 | * a non-null @endptr points to a character other than '\0'. | |
433 | * | |
434 | * If the conversion overflows @result, store UINT_MAX in @result, | |
435 | * and return -ERANGE. | |
436 | * | |
437 | * Else store the converted value in @result, and return zero. | |
438 | * | |
439 | * Note that a number with a leading minus sign gets converted without | |
440 | * the minus sign, checked for overflow (see above), then negated (in | |
441 | * @result's type). This is exactly how strtoul() works. | |
442 | */ | |
443 | int qemu_strtoui(const char *nptr, const char **endptr, int base, | |
444 | unsigned int *result) | |
445 | { | |
446 | char *ep; | |
447 | long long lresult; | |
448 | ||
53a90b97 | 449 | assert((unsigned) base <= 36 && base != 1); |
473a2a33 DB |
450 | if (!nptr) { |
451 | if (endptr) { | |
452 | *endptr = nptr; | |
453 | } | |
454 | return -EINVAL; | |
455 | } | |
456 | ||
457 | errno = 0; | |
458 | lresult = strtoull(nptr, &ep, base); | |
459 | ||
460 | /* Windows returns 1 for negative out-of-range values. */ | |
461 | if (errno == ERANGE) { | |
462 | *result = -1; | |
463 | } else { | |
464 | if (lresult > UINT_MAX) { | |
465 | *result = UINT_MAX; | |
466 | errno = ERANGE; | |
467 | } else if (lresult < INT_MIN) { | |
468 | *result = UINT_MAX; | |
469 | errno = ERANGE; | |
470 | } else { | |
471 | *result = lresult; | |
472 | } | |
473 | } | |
6162f7da | 474 | return check_strtox_error(nptr, ep, endptr, lresult == 0, errno); |
473a2a33 DB |
475 | } |
476 | ||
764e0fa4 | 477 | /** |
4295f879 | 478 | * Convert string @nptr to a long integer, and store it in @result. |
764e0fa4 | 479 | * |
4295f879 MA |
480 | * This is a wrapper around strtol() that is harder to misuse. |
481 | * Semantics of @nptr, @endptr, @base match strtol() with differences | |
482 | * noted below. | |
764e0fa4 | 483 | * |
4295f879 | 484 | * @nptr may be null, and no conversion is performed then. |
764e0fa4 | 485 | * |
4295f879 MA |
486 | * If no conversion is performed, store @nptr in *@endptr and return |
487 | * -EINVAL. | |
764e0fa4 | 488 | * |
4295f879 MA |
489 | * If @endptr is null, and the string isn't fully converted, return |
490 | * -EINVAL. This is the case when the pointer that would be stored in | |
491 | * a non-null @endptr points to a character other than '\0'. | |
492 | * | |
493 | * If the conversion overflows @result, store LONG_MAX in @result, | |
494 | * and return -ERANGE. | |
495 | * | |
496 | * If the conversion underflows @result, store LONG_MIN in @result, | |
497 | * and return -ERANGE. | |
498 | * | |
499 | * Else store the converted value in @result, and return zero. | |
764e0fa4 CT |
500 | */ |
501 | int qemu_strtol(const char *nptr, const char **endptr, int base, | |
502 | long *result) | |
503 | { | |
717adf96 | 504 | char *ep; |
4baef267 | 505 | |
53a90b97 | 506 | assert((unsigned) base <= 36 && base != 1); |
764e0fa4 CT |
507 | if (!nptr) { |
508 | if (endptr) { | |
509 | *endptr = nptr; | |
510 | } | |
4baef267 | 511 | return -EINVAL; |
764e0fa4 | 512 | } |
4baef267 MA |
513 | |
514 | errno = 0; | |
515 | *result = strtol(nptr, &ep, base); | |
6162f7da | 516 | return check_strtox_error(nptr, ep, endptr, *result == 0, errno); |
764e0fa4 | 517 | } |
c817c015 CT |
518 | |
519 | /** | |
4295f879 MA |
520 | * Convert string @nptr to an unsigned long, and store it in @result. |
521 | * | |
522 | * This is a wrapper around strtoul() that is harder to misuse. | |
523 | * Semantics of @nptr, @endptr, @base match strtoul() with differences | |
524 | * noted below. | |
525 | * | |
526 | * @nptr may be null, and no conversion is performed then. | |
527 | * | |
528 | * If no conversion is performed, store @nptr in *@endptr and return | |
529 | * -EINVAL. | |
530 | * | |
531 | * If @endptr is null, and the string isn't fully converted, return | |
532 | * -EINVAL. This is the case when the pointer that would be stored in | |
533 | * a non-null @endptr points to a character other than '\0'. | |
c817c015 | 534 | * |
4295f879 MA |
535 | * If the conversion overflows @result, store ULONG_MAX in @result, |
536 | * and return -ERANGE. | |
c817c015 | 537 | * |
4295f879 | 538 | * Else store the converted value in @result, and return zero. |
c817c015 | 539 | * |
4295f879 MA |
540 | * Note that a number with a leading minus sign gets converted without |
541 | * the minus sign, checked for overflow (see above), then negated (in | |
542 | * @result's type). This is exactly how strtoul() works. | |
c817c015 CT |
543 | */ |
544 | int qemu_strtoul(const char *nptr, const char **endptr, int base, | |
545 | unsigned long *result) | |
546 | { | |
717adf96 | 547 | char *ep; |
4baef267 | 548 | |
53a90b97 | 549 | assert((unsigned) base <= 36 && base != 1); |
c817c015 CT |
550 | if (!nptr) { |
551 | if (endptr) { | |
552 | *endptr = nptr; | |
553 | } | |
4baef267 MA |
554 | return -EINVAL; |
555 | } | |
556 | ||
557 | errno = 0; | |
558 | *result = strtoul(nptr, &ep, base); | |
559 | /* Windows returns 1 for negative out-of-range values. */ | |
560 | if (errno == ERANGE) { | |
561 | *result = -1; | |
c817c015 | 562 | } |
6162f7da | 563 | return check_strtox_error(nptr, ep, endptr, *result == 0, errno); |
c817c015 CT |
564 | } |
565 | ||
8ac4df40 | 566 | /** |
4295f879 | 567 | * Convert string @nptr to an int64_t. |
8ac4df40 | 568 | * |
4295f879 | 569 | * Works like qemu_strtol(), except it stores INT64_MAX on overflow, |
369276eb | 570 | * and INT64_MIN on underflow. |
8ac4df40 | 571 | */ |
b30d1886 | 572 | int qemu_strtoi64(const char *nptr, const char **endptr, int base, |
8ac4df40 CT |
573 | int64_t *result) |
574 | { | |
717adf96 | 575 | char *ep; |
4baef267 | 576 | |
53a90b97 | 577 | assert((unsigned) base <= 36 && base != 1); |
8ac4df40 CT |
578 | if (!nptr) { |
579 | if (endptr) { | |
580 | *endptr = nptr; | |
581 | } | |
4baef267 | 582 | return -EINVAL; |
8ac4df40 | 583 | } |
4baef267 | 584 | |
369276eb MA |
585 | /* This assumes int64_t is long long TODO relax */ |
586 | QEMU_BUILD_BUG_ON(sizeof(int64_t) != sizeof(long long)); | |
4baef267 | 587 | errno = 0; |
4baef267 | 588 | *result = strtoll(nptr, &ep, base); |
6162f7da | 589 | return check_strtox_error(nptr, ep, endptr, *result == 0, errno); |
8ac4df40 CT |
590 | } |
591 | ||
3904e6bf | 592 | /** |
4295f879 | 593 | * Convert string @nptr to an uint64_t. |
3904e6bf | 594 | * |
4295f879 | 595 | * Works like qemu_strtoul(), except it stores UINT64_MAX on overflow. |
3904e6bf | 596 | */ |
b30d1886 | 597 | int qemu_strtou64(const char *nptr, const char **endptr, int base, |
3904e6bf CT |
598 | uint64_t *result) |
599 | { | |
717adf96 | 600 | char *ep; |
4baef267 | 601 | |
53a90b97 | 602 | assert((unsigned) base <= 36 && base != 1); |
3904e6bf CT |
603 | if (!nptr) { |
604 | if (endptr) { | |
605 | *endptr = nptr; | |
606 | } | |
4baef267 MA |
607 | return -EINVAL; |
608 | } | |
609 | ||
369276eb MA |
610 | /* This assumes uint64_t is unsigned long long TODO relax */ |
611 | QEMU_BUILD_BUG_ON(sizeof(uint64_t) != sizeof(unsigned long long)); | |
4baef267 | 612 | errno = 0; |
4baef267 MA |
613 | *result = strtoull(nptr, &ep, base); |
614 | /* Windows returns 1 for negative out-of-range values. */ | |
615 | if (errno == ERANGE) { | |
616 | *result = -1; | |
3904e6bf | 617 | } |
6162f7da | 618 | return check_strtox_error(nptr, ep, endptr, *result == 0, errno); |
3904e6bf CT |
619 | } |
620 | ||
ca28f548 DH |
621 | /** |
622 | * Convert string @nptr to a double. | |
623 | * | |
624 | * This is a wrapper around strtod() that is harder to misuse. | |
625 | * Semantics of @nptr and @endptr match strtod() with differences | |
626 | * noted below. | |
627 | * | |
628 | * @nptr may be null, and no conversion is performed then. | |
629 | * | |
630 | * If no conversion is performed, store @nptr in *@endptr and return | |
631 | * -EINVAL. | |
632 | * | |
633 | * If @endptr is null, and the string isn't fully converted, return | |
634 | * -EINVAL. This is the case when the pointer that would be stored in | |
635 | * a non-null @endptr points to a character other than '\0'. | |
636 | * | |
637 | * If the conversion overflows, store +/-HUGE_VAL in @result, depending | |
638 | * on the sign, and return -ERANGE. | |
639 | * | |
640 | * If the conversion underflows, store +/-0.0 in @result, depending on the | |
641 | * sign, and return -ERANGE. | |
642 | * | |
643 | * Else store the converted value in @result, and return zero. | |
644 | */ | |
645 | int qemu_strtod(const char *nptr, const char **endptr, double *result) | |
646 | { | |
647 | char *ep; | |
648 | ||
649 | if (!nptr) { | |
650 | if (endptr) { | |
651 | *endptr = nptr; | |
652 | } | |
653 | return -EINVAL; | |
654 | } | |
655 | ||
656 | errno = 0; | |
657 | *result = strtod(nptr, &ep); | |
6162f7da | 658 | return check_strtox_error(nptr, ep, endptr, false, errno); |
ca28f548 DH |
659 | } |
660 | ||
661 | /** | |
662 | * Convert string @nptr to a finite double. | |
663 | * | |
664 | * Works like qemu_strtod(), except that "NaN" and "inf" are rejected | |
665 | * with -EINVAL and no conversion is performed. | |
666 | */ | |
667 | int qemu_strtod_finite(const char *nptr, const char **endptr, double *result) | |
668 | { | |
669 | double tmp; | |
670 | int ret; | |
671 | ||
672 | ret = qemu_strtod(nptr, endptr, &tmp); | |
673 | if (!ret && !isfinite(tmp)) { | |
674 | if (endptr) { | |
675 | *endptr = nptr; | |
676 | } | |
677 | ret = -EINVAL; | |
678 | } | |
679 | ||
680 | if (ret != -EINVAL) { | |
681 | *result = tmp; | |
682 | } | |
683 | return ret; | |
684 | } | |
685 | ||
5c99fa37 KF |
686 | /** |
687 | * Searches for the first occurrence of 'c' in 's', and returns a pointer | |
688 | * to the trailing null byte if none was found. | |
689 | */ | |
690 | #ifndef HAVE_STRCHRNUL | |
691 | const char *qemu_strchrnul(const char *s, int c) | |
692 | { | |
693 | const char *e = strchr(s, c); | |
694 | if (!e) { | |
695 | e = s + strlen(s); | |
696 | } | |
697 | return e; | |
698 | } | |
699 | #endif | |
700 | ||
e3f9fe2d EH |
701 | /** |
702 | * parse_uint: | |
703 | * | |
704 | * @s: String to parse | |
705 | * @value: Destination for parsed integer value | |
706 | * @endptr: Destination for pointer to first character not consumed | |
707 | * @base: integer base, between 2 and 36 inclusive, or 0 | |
708 | * | |
709 | * Parse unsigned integer | |
710 | * | |
711 | * Parsed syntax is like strtoull()'s: arbitrary whitespace, a single optional | |
712 | * '+' or '-', an optional "0x" if @base is 0 or 16, one or more digits. | |
713 | * | |
714 | * If @s is null, or @base is invalid, or @s doesn't start with an | |
715 | * integer in the syntax above, set *@value to 0, *@endptr to @s, and | |
716 | * return -EINVAL. | |
717 | * | |
718 | * Set *@endptr to point right beyond the parsed integer (even if the integer | |
719 | * overflows or is negative, all digits will be parsed and *@endptr will | |
720 | * point right beyond them). | |
721 | * | |
722 | * If the integer is negative, set *@value to 0, and return -ERANGE. | |
723 | * | |
724 | * If the integer overflows unsigned long long, set *@value to | |
725 | * ULLONG_MAX, and return -ERANGE. | |
726 | * | |
727 | * Else, set *@value to the parsed integer, and return 0. | |
728 | */ | |
729 | int parse_uint(const char *s, unsigned long long *value, char **endptr, | |
730 | int base) | |
731 | { | |
732 | int r = 0; | |
733 | char *endp = (char *)s; | |
734 | unsigned long long val = 0; | |
735 | ||
53a90b97 | 736 | assert((unsigned) base <= 36 && base != 1); |
e3f9fe2d EH |
737 | if (!s) { |
738 | r = -EINVAL; | |
739 | goto out; | |
740 | } | |
741 | ||
742 | errno = 0; | |
743 | val = strtoull(s, &endp, base); | |
744 | if (errno) { | |
745 | r = -errno; | |
746 | goto out; | |
747 | } | |
748 | ||
749 | if (endp == s) { | |
750 | r = -EINVAL; | |
751 | goto out; | |
752 | } | |
753 | ||
754 | /* make sure we reject negative numbers: */ | |
db3d11ee | 755 | while (qemu_isspace(*s)) { |
e3f9fe2d EH |
756 | s++; |
757 | } | |
758 | if (*s == '-') { | |
759 | val = 0; | |
760 | r = -ERANGE; | |
761 | goto out; | |
762 | } | |
763 | ||
764 | out: | |
765 | *value = val; | |
766 | *endptr = endp; | |
767 | return r; | |
768 | } | |
769 | ||
770 | /** | |
771 | * parse_uint_full: | |
772 | * | |
773 | * @s: String to parse | |
774 | * @value: Destination for parsed integer value | |
775 | * @base: integer base, between 2 and 36 inclusive, or 0 | |
776 | * | |
777 | * Parse unsigned integer from entire string | |
778 | * | |
779 | * Have the same behavior of parse_uint(), but with an additional check | |
780 | * for additional data after the parsed number. If extra characters are present | |
781 | * after the parsed number, the function will return -EINVAL, and *@v will | |
782 | * be set to 0. | |
783 | */ | |
784 | int parse_uint_full(const char *s, unsigned long long *value, int base) | |
785 | { | |
786 | char *endp; | |
787 | int r; | |
788 | ||
789 | r = parse_uint(s, value, &endp, base); | |
790 | if (r < 0) { | |
791 | return r; | |
792 | } | |
793 | if (*endp) { | |
794 | *value = 0; | |
795 | return -EINVAL; | |
796 | } | |
797 | ||
798 | return 0; | |
799 | } | |
800 | ||
443916d1 SB |
801 | int qemu_parse_fd(const char *param) |
802 | { | |
e9c5c1f4 LE |
803 | long fd; |
804 | char *endptr; | |
443916d1 | 805 | |
e9c5c1f4 | 806 | errno = 0; |
443916d1 | 807 | fd = strtol(param, &endptr, 10); |
e9c5c1f4 LE |
808 | if (param == endptr /* no conversion performed */ || |
809 | errno != 0 /* not representable as long; possibly others */ || | |
810 | *endptr != '\0' /* final string not empty */ || | |
811 | fd < 0 /* invalid as file descriptor */ || | |
812 | fd > INT_MAX /* not representable as int */) { | |
443916d1 SB |
813 | return -1; |
814 | } | |
815 | return fd; | |
816 | } | |
9fb26641 | 817 | |
e6546bb9 OW |
818 | /* |
819 | * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128) | |
820 | * Input is limited to 14-bit numbers | |
821 | */ | |
822 | int uleb128_encode_small(uint8_t *out, uint32_t n) | |
823 | { | |
824 | g_assert(n <= 0x3fff); | |
825 | if (n < 0x80) { | |
7c960d61 | 826 | *out = n; |
e6546bb9 OW |
827 | return 1; |
828 | } else { | |
829 | *out++ = (n & 0x7f) | 0x80; | |
7c960d61 | 830 | *out = n >> 7; |
e6546bb9 OW |
831 | return 2; |
832 | } | |
833 | } | |
834 | ||
835 | int uleb128_decode_small(const uint8_t *in, uint32_t *n) | |
836 | { | |
837 | if (!(*in & 0x80)) { | |
7c960d61 | 838 | *n = *in; |
e6546bb9 OW |
839 | return 1; |
840 | } else { | |
841 | *n = *in++ & 0x7f; | |
842 | /* we exceed 14 bit number */ | |
843 | if (*in & 0x80) { | |
844 | return -1; | |
845 | } | |
7c960d61 | 846 | *n |= *in << 7; |
e6546bb9 OW |
847 | return 2; |
848 | } | |
849 | } | |
b16352ac AL |
850 | |
851 | /* | |
852 | * helper to parse debug environment variables | |
853 | */ | |
854 | int parse_debug_env(const char *name, int max, int initial) | |
855 | { | |
856 | char *debug_env = getenv(name); | |
857 | char *inv = NULL; | |
cc5d0e04 | 858 | long debug; |
b16352ac AL |
859 | |
860 | if (!debug_env) { | |
861 | return initial; | |
862 | } | |
cc5d0e04 | 863 | errno = 0; |
b16352ac AL |
864 | debug = strtol(debug_env, &inv, 10); |
865 | if (inv == debug_env) { | |
866 | return initial; | |
867 | } | |
cc5d0e04 | 868 | if (debug < 0 || debug > max || errno != 0) { |
05cb8ed5 | 869 | warn_report("%s not in [0, %d]", name, max); |
b16352ac AL |
870 | return initial; |
871 | } | |
872 | return debug; | |
873 | } | |
4297c8ee | 874 | |
22951aaa PX |
875 | /* |
876 | * Return human readable string for size @val. | |
877 | * @val can be anything that uint64_t allows (no more than "16 EiB"). | |
878 | * Use IEC binary units like KiB, MiB, and so forth. | |
879 | * Caller is responsible for passing it to g_free(). | |
880 | */ | |
881 | char *size_to_str(uint64_t val) | |
882 | { | |
883 | static const char *suffixes[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" }; | |
754da867 | 884 | uint64_t div; |
22951aaa PX |
885 | int i; |
886 | ||
887 | /* | |
888 | * The exponent (returned in i) minus one gives us | |
889 | * floor(log2(val * 1024 / 1000). The correction makes us | |
890 | * switch to the higher power when the integer part is >= 1000. | |
891 | * (see e41b509d68afb1f for more info) | |
892 | */ | |
893 | frexp(val / (1000.0 / 1024.0), &i); | |
894 | i = (i - 1) / 10; | |
895 | div = 1ULL << (i * 10); | |
896 | ||
897 | return g_strdup_printf("%0.3g %sB", (double)val / div, suffixes[i]); | |
898 | } | |
85e33a28 | 899 | |
709616c7 PMD |
900 | char *freq_to_str(uint64_t freq_hz) |
901 | { | |
902 | static const char *const suffixes[] = { "", "K", "M", "G", "T", "P", "E" }; | |
903 | double freq = freq_hz; | |
904 | size_t idx = 0; | |
905 | ||
6d7ccc57 | 906 | while (freq >= 1000.0) { |
709616c7 PMD |
907 | freq /= 1000.0; |
908 | idx++; | |
909 | } | |
6d7ccc57 | 910 | assert(idx < ARRAY_SIZE(suffixes)); |
709616c7 PMD |
911 | |
912 | return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]); | |
913 | } | |
914 | ||
85e33a28 MAL |
915 | int qemu_pstrcmp0(const char **str1, const char **str2) |
916 | { | |
917 | return g_strcmp0(*str1, *str2); | |
918 | } | |
f4f5ed2c PB |
919 | |
920 | static inline bool starts_with_prefix(const char *dir) | |
921 | { | |
922 | size_t prefix_len = strlen(CONFIG_PREFIX); | |
923 | return !memcmp(dir, CONFIG_PREFIX, prefix_len) && | |
924 | (!dir[prefix_len] || G_IS_DIR_SEPARATOR(dir[prefix_len])); | |
925 | } | |
926 | ||
927 | /* Return the next path component in dir, and store its length in *p_len. */ | |
928 | static inline const char *next_component(const char *dir, int *p_len) | |
929 | { | |
930 | int len; | |
342e3a4f SW |
931 | while ((*dir && G_IS_DIR_SEPARATOR(*dir)) || |
932 | (*dir == '.' && (G_IS_DIR_SEPARATOR(dir[1]) || dir[1] == '\0'))) { | |
f4f5ed2c PB |
933 | dir++; |
934 | } | |
935 | len = 0; | |
936 | while (dir[len] && !G_IS_DIR_SEPARATOR(dir[len])) { | |
937 | len++; | |
938 | } | |
939 | *p_len = len; | |
940 | return dir; | |
941 | } | |
942 | ||
06680b15 MAL |
943 | static const char *exec_dir; |
944 | ||
945 | void qemu_init_exec_dir(const char *argv0) | |
946 | { | |
947 | #ifdef G_OS_WIN32 | |
948 | char *p; | |
949 | char buf[MAX_PATH]; | |
950 | DWORD len; | |
951 | ||
952 | if (exec_dir) { | |
953 | return; | |
954 | } | |
955 | ||
956 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); | |
957 | if (len == 0) { | |
958 | return; | |
959 | } | |
960 | ||
961 | buf[len] = 0; | |
962 | p = buf + len - 1; | |
963 | while (p != buf && *p != '\\') { | |
964 | p--; | |
965 | } | |
966 | *p = 0; | |
967 | if (access(buf, R_OK) == 0) { | |
968 | exec_dir = g_strdup(buf); | |
969 | } else { | |
970 | exec_dir = CONFIG_BINDIR; | |
971 | } | |
972 | #else | |
973 | char *p = NULL; | |
974 | char buf[PATH_MAX]; | |
975 | ||
976 | if (exec_dir) { | |
977 | return; | |
978 | } | |
979 | ||
980 | #if defined(__linux__) | |
981 | { | |
982 | int len; | |
983 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); | |
984 | if (len > 0) { | |
985 | buf[len] = 0; | |
986 | p = buf; | |
987 | } | |
988 | } | |
989 | #elif defined(__FreeBSD__) \ | |
990 | || (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME)) | |
991 | { | |
992 | #if defined(__FreeBSD__) | |
993 | static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; | |
994 | #else | |
995 | static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME}; | |
996 | #endif | |
997 | size_t len = sizeof(buf) - 1; | |
998 | ||
999 | *buf = '\0'; | |
1000 | if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) && | |
1001 | *buf) { | |
1002 | buf[sizeof(buf) - 1] = '\0'; | |
1003 | p = buf; | |
1004 | } | |
1005 | } | |
1006 | #elif defined(__APPLE__) | |
1007 | { | |
1008 | char fpath[PATH_MAX]; | |
1009 | uint32_t len = sizeof(fpath); | |
1010 | if (_NSGetExecutablePath(fpath, &len) == 0) { | |
1011 | p = realpath(fpath, buf); | |
1012 | if (!p) { | |
1013 | return; | |
1014 | } | |
1015 | } | |
1016 | } | |
1017 | #elif defined(__HAIKU__) | |
1018 | { | |
1019 | image_info ii; | |
1020 | int32_t c = 0; | |
1021 | ||
1022 | *buf = '\0'; | |
1023 | while (get_next_image_info(0, &c, &ii) == B_OK) { | |
1024 | if (ii.type == B_APP_IMAGE) { | |
1025 | strncpy(buf, ii.name, sizeof(buf)); | |
1026 | buf[sizeof(buf) - 1] = 0; | |
1027 | p = buf; | |
1028 | break; | |
1029 | } | |
1030 | } | |
1031 | } | |
1032 | #endif | |
1033 | /* If we don't have any way of figuring out the actual executable | |
1034 | location then try argv[0]. */ | |
1035 | if (!p && argv0) { | |
1036 | p = realpath(argv0, buf); | |
1037 | } | |
1038 | if (p) { | |
1039 | exec_dir = g_path_get_dirname(p); | |
1040 | } else { | |
1041 | exec_dir = CONFIG_BINDIR; | |
1042 | } | |
1043 | #endif | |
1044 | } | |
1045 | ||
1046 | const char *qemu_get_exec_dir(void) | |
1047 | { | |
1048 | return exec_dir; | |
1049 | } | |
1050 | ||
f4f5ed2c PB |
1051 | char *get_relocated_path(const char *dir) |
1052 | { | |
1053 | size_t prefix_len = strlen(CONFIG_PREFIX); | |
1054 | const char *bindir = CONFIG_BINDIR; | |
1055 | const char *exec_dir = qemu_get_exec_dir(); | |
1056 | GString *result; | |
1057 | int len_dir, len_bindir; | |
1058 | ||
1059 | /* Fail if qemu_init_exec_dir was not called. */ | |
1060 | assert(exec_dir[0]); | |
1061 | if (!starts_with_prefix(dir) || !starts_with_prefix(bindir)) { | |
090afdc5 | 1062 | return g_strdup(dir); |
f4f5ed2c PB |
1063 | } |
1064 | ||
1065 | result = g_string_new(exec_dir); | |
1066 | ||
1067 | /* Advance over common components. */ | |
1068 | len_dir = len_bindir = prefix_len; | |
1069 | do { | |
1070 | dir += len_dir; | |
1071 | bindir += len_bindir; | |
1072 | dir = next_component(dir, &len_dir); | |
1073 | bindir = next_component(bindir, &len_bindir); | |
7a3b7f6b | 1074 | } while (len_dir && len_dir == len_bindir && !memcmp(dir, bindir, len_dir)); |
f4f5ed2c PB |
1075 | |
1076 | /* Ascend from bindir to the common prefix with dir. */ | |
1077 | while (len_bindir) { | |
1078 | bindir += len_bindir; | |
1079 | g_string_append(result, "/.."); | |
1080 | bindir = next_component(bindir, &len_bindir); | |
1081 | } | |
1082 | ||
1083 | if (*dir) { | |
1084 | assert(G_IS_DIR_SEPARATOR(dir[-1])); | |
1085 | g_string_append(result, dir - 1); | |
1086 | } | |
b6d003db | 1087 | return g_string_free(result, false); |
f4f5ed2c | 1088 | } |