Statistics
| Branch: | Revision:

root / cutils.c @ 21de37a7

History | View | Annotate | Download (9.8 kB)

1 18607dcb bellard
/*
2 18607dcb bellard
 * Simple C functions to supplement the C library
3 5fafdf24 ths
 *
4 18607dcb bellard
 * Copyright (c) 2006 Fabrice Bellard
5 18607dcb bellard
 *
6 18607dcb bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 18607dcb bellard
 * of this software and associated documentation files (the "Software"), to deal
8 18607dcb bellard
 * in the Software without restriction, including without limitation the rights
9 18607dcb bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 18607dcb bellard
 * copies of the Software, and to permit persons to whom the Software is
11 18607dcb bellard
 * furnished to do so, subject to the following conditions:
12 18607dcb bellard
 *
13 18607dcb bellard
 * The above copyright notice and this permission notice shall be included in
14 18607dcb bellard
 * all copies or substantial portions of the Software.
15 18607dcb bellard
 *
16 18607dcb bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 18607dcb bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 18607dcb bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 18607dcb bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 18607dcb bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 18607dcb bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 18607dcb bellard
 * THE SOFTWARE.
23 18607dcb bellard
 */
24 faf07963 pbrook
#include "qemu-common.h"
25 8d371d4b aliguori
#include "host-utils.h"
26 9f9b17a4 Jes Sorensen
#include <math.h>
27 18607dcb bellard
28 18607dcb bellard
void pstrcpy(char *buf, int buf_size, const char *str)
29 18607dcb bellard
{
30 18607dcb bellard
    int c;
31 18607dcb bellard
    char *q = buf;
32 18607dcb bellard
33 18607dcb bellard
    if (buf_size <= 0)
34 18607dcb bellard
        return;
35 18607dcb bellard
36 18607dcb bellard
    for(;;) {
37 18607dcb bellard
        c = *str++;
38 18607dcb bellard
        if (c == 0 || q >= buf + buf_size - 1)
39 18607dcb bellard
            break;
40 18607dcb bellard
        *q++ = c;
41 18607dcb bellard
    }
42 18607dcb bellard
    *q = '\0';
43 18607dcb bellard
}
44 18607dcb bellard
45 18607dcb bellard
/* strcat and truncate. */
46 18607dcb bellard
char *pstrcat(char *buf, int buf_size, const char *s)
47 18607dcb bellard
{
48 18607dcb bellard
    int len;
49 18607dcb bellard
    len = strlen(buf);
50 5fafdf24 ths
    if (len < buf_size)
51 18607dcb bellard
        pstrcpy(buf + len, buf_size - len, s);
52 18607dcb bellard
    return buf;
53 18607dcb bellard
}
54 18607dcb bellard
55 18607dcb bellard
int strstart(const char *str, const char *val, const char **ptr)
56 18607dcb bellard
{
57 18607dcb bellard
    const char *p, *q;
58 18607dcb bellard
    p = str;
59 18607dcb bellard
    q = val;
60 18607dcb bellard
    while (*q != '\0') {
61 18607dcb bellard
        if (*p != *q)
62 18607dcb bellard
            return 0;
63 18607dcb bellard
        p++;
64 18607dcb bellard
        q++;
65 18607dcb bellard
    }
66 18607dcb bellard
    if (ptr)
67 18607dcb bellard
        *ptr = p;
68 18607dcb bellard
    return 1;
69 18607dcb bellard
}
70 18607dcb bellard
71 18607dcb bellard
int stristart(const char *str, const char *val, const char **ptr)
72 18607dcb bellard
{
73 18607dcb bellard
    const char *p, *q;
74 18607dcb bellard
    p = str;
75 18607dcb bellard
    q = val;
76 18607dcb bellard
    while (*q != '\0') {
77 cd390083 blueswir1
        if (qemu_toupper(*p) != qemu_toupper(*q))
78 18607dcb bellard
            return 0;
79 18607dcb bellard
        p++;
80 18607dcb bellard
        q++;
81 18607dcb bellard
    }
82 18607dcb bellard
    if (ptr)
83 18607dcb bellard
        *ptr = p;
84 18607dcb bellard
    return 1;
85 18607dcb bellard
}
86 3c6b2088 bellard
87 d43277c5 Blue Swirl
/* XXX: use host strnlen if available ? */
88 d43277c5 Blue Swirl
int qemu_strnlen(const char *s, int max_len)
89 d43277c5 Blue Swirl
{
90 d43277c5 Blue Swirl
    int i;
91 d43277c5 Blue Swirl
92 d43277c5 Blue Swirl
    for(i = 0; i < max_len; i++) {
93 d43277c5 Blue Swirl
        if (s[i] == '\0') {
94 d43277c5 Blue Swirl
            break;
95 d43277c5 Blue Swirl
        }
96 d43277c5 Blue Swirl
    }
97 d43277c5 Blue Swirl
    return i;
98 d43277c5 Blue Swirl
}
99 d43277c5 Blue Swirl
100 3c6b2088 bellard
time_t mktimegm(struct tm *tm)
101 3c6b2088 bellard
{
102 3c6b2088 bellard
    time_t t;
103 3c6b2088 bellard
    int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
104 3c6b2088 bellard
    if (m < 3) {
105 3c6b2088 bellard
        m += 12;
106 3c6b2088 bellard
        y--;
107 3c6b2088 bellard
    }
108 3c6b2088 bellard
    t = 86400 * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + 
109 3c6b2088 bellard
                 y / 400 - 719469);
110 3c6b2088 bellard
    t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
111 3c6b2088 bellard
    return t;
112 3c6b2088 bellard
}
113 b39ade83 aliguori
114 ad46db9a blueswir1
int qemu_fls(int i)
115 b39ade83 aliguori
{
116 8d371d4b aliguori
    return 32 - clz32(i);
117 b39ade83 aliguori
}
118 44e3ee8a aliguori
119 6f1953c4 Christoph Hellwig
/*
120 6f1953c4 Christoph Hellwig
 * Make sure data goes on disk, but if possible do not bother to
121 6f1953c4 Christoph Hellwig
 * write out the inode just for timestamp updates.
122 6f1953c4 Christoph Hellwig
 *
123 6f1953c4 Christoph Hellwig
 * Unfortunately even in 2009 many operating systems do not support
124 6f1953c4 Christoph Hellwig
 * fdatasync and have to fall back to fsync.
125 6f1953c4 Christoph Hellwig
 */
126 6f1953c4 Christoph Hellwig
int qemu_fdatasync(int fd)
127 6f1953c4 Christoph Hellwig
{
128 5f6b9e8f Blue Swirl
#ifdef CONFIG_FDATASYNC
129 6f1953c4 Christoph Hellwig
    return fdatasync(fd);
130 6f1953c4 Christoph Hellwig
#else
131 6f1953c4 Christoph Hellwig
    return fsync(fd);
132 6f1953c4 Christoph Hellwig
#endif
133 6f1953c4 Christoph Hellwig
}
134 6f1953c4 Christoph Hellwig
135 44e3ee8a aliguori
/* io vectors */
136 44e3ee8a aliguori
137 44e3ee8a aliguori
void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
138 44e3ee8a aliguori
{
139 44e3ee8a aliguori
    qiov->iov = qemu_malloc(alloc_hint * sizeof(struct iovec));
140 44e3ee8a aliguori
    qiov->niov = 0;
141 44e3ee8a aliguori
    qiov->nalloc = alloc_hint;
142 249aa745 aliguori
    qiov->size = 0;
143 44e3ee8a aliguori
}
144 44e3ee8a aliguori
145 522584a5 aliguori
void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
146 522584a5 aliguori
{
147 522584a5 aliguori
    int i;
148 522584a5 aliguori
149 522584a5 aliguori
    qiov->iov = iov;
150 522584a5 aliguori
    qiov->niov = niov;
151 522584a5 aliguori
    qiov->nalloc = -1;
152 522584a5 aliguori
    qiov->size = 0;
153 522584a5 aliguori
    for (i = 0; i < niov; i++)
154 522584a5 aliguori
        qiov->size += iov[i].iov_len;
155 522584a5 aliguori
}
156 522584a5 aliguori
157 44e3ee8a aliguori
void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
158 44e3ee8a aliguori
{
159 522584a5 aliguori
    assert(qiov->nalloc != -1);
160 522584a5 aliguori
161 44e3ee8a aliguori
    if (qiov->niov == qiov->nalloc) {
162 44e3ee8a aliguori
        qiov->nalloc = 2 * qiov->nalloc + 1;
163 44e3ee8a aliguori
        qiov->iov = qemu_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
164 44e3ee8a aliguori
    }
165 44e3ee8a aliguori
    qiov->iov[qiov->niov].iov_base = base;
166 44e3ee8a aliguori
    qiov->iov[qiov->niov].iov_len = len;
167 249aa745 aliguori
    qiov->size += len;
168 44e3ee8a aliguori
    ++qiov->niov;
169 44e3ee8a aliguori
}
170 44e3ee8a aliguori
171 40b4f539 Kevin Wolf
/*
172 b8a83a4f Kevin Wolf
 * Copies iovecs from src to the end of dst. It starts copying after skipping
173 b8a83a4f Kevin Wolf
 * the given number of bytes in src and copies until src is completely copied
174 b8a83a4f Kevin Wolf
 * or the total size of the copied iovec reaches size.The size of the last
175 b8a83a4f Kevin Wolf
 * copied iovec is changed in order to fit the specified total size if it isn't
176 b8a83a4f Kevin Wolf
 * a perfect fit already.
177 40b4f539 Kevin Wolf
 */
178 b8a83a4f Kevin Wolf
void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
179 b8a83a4f Kevin Wolf
    size_t size)
180 40b4f539 Kevin Wolf
{
181 40b4f539 Kevin Wolf
    int i;
182 40b4f539 Kevin Wolf
    size_t done;
183 b8a83a4f Kevin Wolf
    void *iov_base;
184 b8a83a4f Kevin Wolf
    uint64_t iov_len;
185 40b4f539 Kevin Wolf
186 40b4f539 Kevin Wolf
    assert(dst->nalloc != -1);
187 40b4f539 Kevin Wolf
188 40b4f539 Kevin Wolf
    done = 0;
189 40b4f539 Kevin Wolf
    for (i = 0; (i < src->niov) && (done != size); i++) {
190 b8a83a4f Kevin Wolf
        if (skip >= src->iov[i].iov_len) {
191 b8a83a4f Kevin Wolf
            /* Skip the whole iov */
192 b8a83a4f Kevin Wolf
            skip -= src->iov[i].iov_len;
193 b8a83a4f Kevin Wolf
            continue;
194 b8a83a4f Kevin Wolf
        } else {
195 b8a83a4f Kevin Wolf
            /* Skip only part (or nothing) of the iov */
196 b8a83a4f Kevin Wolf
            iov_base = (uint8_t*) src->iov[i].iov_base + skip;
197 b8a83a4f Kevin Wolf
            iov_len = src->iov[i].iov_len - skip;
198 b8a83a4f Kevin Wolf
            skip = 0;
199 b8a83a4f Kevin Wolf
        }
200 b8a83a4f Kevin Wolf
201 b8a83a4f Kevin Wolf
        if (done + iov_len > size) {
202 b8a83a4f Kevin Wolf
            qemu_iovec_add(dst, iov_base, size - done);
203 40b4f539 Kevin Wolf
            break;
204 40b4f539 Kevin Wolf
        } else {
205 b8a83a4f Kevin Wolf
            qemu_iovec_add(dst, iov_base, iov_len);
206 40b4f539 Kevin Wolf
        }
207 b8a83a4f Kevin Wolf
        done += iov_len;
208 40b4f539 Kevin Wolf
    }
209 40b4f539 Kevin Wolf
}
210 40b4f539 Kevin Wolf
211 b8a83a4f Kevin Wolf
void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size)
212 b8a83a4f Kevin Wolf
{
213 b8a83a4f Kevin Wolf
    qemu_iovec_copy(dst, src, 0, size);
214 b8a83a4f Kevin Wolf
}
215 b8a83a4f Kevin Wolf
216 44e3ee8a aliguori
void qemu_iovec_destroy(QEMUIOVector *qiov)
217 44e3ee8a aliguori
{
218 522584a5 aliguori
    assert(qiov->nalloc != -1);
219 522584a5 aliguori
220 44e3ee8a aliguori
    qemu_free(qiov->iov);
221 44e3ee8a aliguori
}
222 44e3ee8a aliguori
223 be959463 aliguori
void qemu_iovec_reset(QEMUIOVector *qiov)
224 be959463 aliguori
{
225 522584a5 aliguori
    assert(qiov->nalloc != -1);
226 522584a5 aliguori
227 be959463 aliguori
    qiov->niov = 0;
228 be959463 aliguori
    qiov->size = 0;
229 be959463 aliguori
}
230 be959463 aliguori
231 44e3ee8a aliguori
void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf)
232 44e3ee8a aliguori
{
233 44e3ee8a aliguori
    uint8_t *p = (uint8_t *)buf;
234 44e3ee8a aliguori
    int i;
235 44e3ee8a aliguori
236 44e3ee8a aliguori
    for (i = 0; i < qiov->niov; ++i) {
237 44e3ee8a aliguori
        memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
238 44e3ee8a aliguori
        p += qiov->iov[i].iov_len;
239 44e3ee8a aliguori
    }
240 44e3ee8a aliguori
}
241 44e3ee8a aliguori
242 249aa745 aliguori
void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count)
243 44e3ee8a aliguori
{
244 44e3ee8a aliguori
    const uint8_t *p = (const uint8_t *)buf;
245 249aa745 aliguori
    size_t copy;
246 44e3ee8a aliguori
    int i;
247 44e3ee8a aliguori
248 249aa745 aliguori
    for (i = 0; i < qiov->niov && count; ++i) {
249 249aa745 aliguori
        copy = count;
250 249aa745 aliguori
        if (copy > qiov->iov[i].iov_len)
251 249aa745 aliguori
            copy = qiov->iov[i].iov_len;
252 249aa745 aliguori
        memcpy(qiov->iov[i].iov_base, p, copy);
253 249aa745 aliguori
        p     += copy;
254 249aa745 aliguori
        count -= copy;
255 44e3ee8a aliguori
    }
256 44e3ee8a aliguori
}
257 db1a4972 Paolo Bonzini
258 b8a83a4f Kevin Wolf
void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count)
259 b8a83a4f Kevin Wolf
{
260 b8a83a4f Kevin Wolf
    size_t n;
261 b8a83a4f Kevin Wolf
    int i;
262 b8a83a4f Kevin Wolf
263 b8a83a4f Kevin Wolf
    for (i = 0; i < qiov->niov && count; ++i) {
264 b8a83a4f Kevin Wolf
        n = MIN(count, qiov->iov[i].iov_len);
265 b8a83a4f Kevin Wolf
        memset(qiov->iov[i].iov_base, c, n);
266 b8a83a4f Kevin Wolf
        count -= n;
267 b8a83a4f Kevin Wolf
    }
268 b8a83a4f Kevin Wolf
}
269 b8a83a4f Kevin Wolf
270 e0d9c6f9 Chunqiang Tang
void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
271 e0d9c6f9 Chunqiang Tang
                            size_t skip)
272 e0d9c6f9 Chunqiang Tang
{
273 e0d9c6f9 Chunqiang Tang
    int i;
274 e0d9c6f9 Chunqiang Tang
    size_t done;
275 e0d9c6f9 Chunqiang Tang
    void *iov_base;
276 e0d9c6f9 Chunqiang Tang
    uint64_t iov_len;
277 e0d9c6f9 Chunqiang Tang
278 e0d9c6f9 Chunqiang Tang
    done = 0;
279 e0d9c6f9 Chunqiang Tang
    for (i = 0; (i < qiov->niov) && (done != count); i++) {
280 e0d9c6f9 Chunqiang Tang
        if (skip >= qiov->iov[i].iov_len) {
281 e0d9c6f9 Chunqiang Tang
            /* Skip the whole iov */
282 e0d9c6f9 Chunqiang Tang
            skip -= qiov->iov[i].iov_len;
283 e0d9c6f9 Chunqiang Tang
            continue;
284 e0d9c6f9 Chunqiang Tang
        } else {
285 e0d9c6f9 Chunqiang Tang
            /* Skip only part (or nothing) of the iov */
286 e0d9c6f9 Chunqiang Tang
            iov_base = (uint8_t*) qiov->iov[i].iov_base + skip;
287 e0d9c6f9 Chunqiang Tang
            iov_len = qiov->iov[i].iov_len - skip;
288 e0d9c6f9 Chunqiang Tang
            skip = 0;
289 e0d9c6f9 Chunqiang Tang
        }
290 e0d9c6f9 Chunqiang Tang
291 e0d9c6f9 Chunqiang Tang
        if (done + iov_len > count) {
292 e0d9c6f9 Chunqiang Tang
            memset(iov_base, c, count - done);
293 e0d9c6f9 Chunqiang Tang
            break;
294 e0d9c6f9 Chunqiang Tang
        } else {
295 e0d9c6f9 Chunqiang Tang
            memset(iov_base, c, iov_len);
296 e0d9c6f9 Chunqiang Tang
        }
297 e0d9c6f9 Chunqiang Tang
        done += iov_len;
298 e0d9c6f9 Chunqiang Tang
    }
299 e0d9c6f9 Chunqiang Tang
}
300 e0d9c6f9 Chunqiang Tang
301 db1a4972 Paolo Bonzini
#ifndef _WIN32
302 db1a4972 Paolo Bonzini
/* Sets a specific flag */
303 db1a4972 Paolo Bonzini
int fcntl_setfl(int fd, int flag)
304 db1a4972 Paolo Bonzini
{
305 db1a4972 Paolo Bonzini
    int flags;
306 db1a4972 Paolo Bonzini
307 db1a4972 Paolo Bonzini
    flags = fcntl(fd, F_GETFL);
308 db1a4972 Paolo Bonzini
    if (flags == -1)
309 db1a4972 Paolo Bonzini
        return -errno;
310 db1a4972 Paolo Bonzini
311 db1a4972 Paolo Bonzini
    if (fcntl(fd, F_SETFL, flags | flag) == -1)
312 db1a4972 Paolo Bonzini
        return -errno;
313 db1a4972 Paolo Bonzini
314 db1a4972 Paolo Bonzini
    return 0;
315 db1a4972 Paolo Bonzini
}
316 db1a4972 Paolo Bonzini
#endif
317 db1a4972 Paolo Bonzini
318 9f9b17a4 Jes Sorensen
/*
319 9f9b17a4 Jes Sorensen
 * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
320 9f9b17a4 Jes Sorensen
 * M/m for MB, G/g for GB or T/t for TB. Default without any postfix
321 9f9b17a4 Jes Sorensen
 * is MB. End pointer will be returned in *end, if not NULL. A valid
322 9f9b17a4 Jes Sorensen
 * value must be terminated by whitespace, ',' or '\0'. Return -1 on
323 9f9b17a4 Jes Sorensen
 * error.
324 9f9b17a4 Jes Sorensen
 */
325 70b4f4bb Jes Sorensen
int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
326 9f9b17a4 Jes Sorensen
{
327 70b4f4bb Jes Sorensen
    int64_t retval = -1;
328 f3bd362a Jes Sorensen
    char *endptr;
329 f3bd362a Jes Sorensen
    unsigned char c, d;
330 9f9b17a4 Jes Sorensen
    int mul_required = 0;
331 9f9b17a4 Jes Sorensen
    double val, mul, integral, fraction;
332 9f9b17a4 Jes Sorensen
333 9f9b17a4 Jes Sorensen
    errno = 0;
334 9f9b17a4 Jes Sorensen
    val = strtod(nptr, &endptr);
335 9f9b17a4 Jes Sorensen
    if (isnan(val) || endptr == nptr || errno != 0) {
336 9f9b17a4 Jes Sorensen
        goto fail;
337 9f9b17a4 Jes Sorensen
    }
338 7eb05349 Jes Sorensen
    fraction = modf(val, &integral);
339 7eb05349 Jes Sorensen
    if (fraction != 0) {
340 9f9b17a4 Jes Sorensen
        mul_required = 1;
341 9f9b17a4 Jes Sorensen
    }
342 9f9b17a4 Jes Sorensen
    /*
343 9f9b17a4 Jes Sorensen
     * Any whitespace character is fine for terminating the number,
344 9f9b17a4 Jes Sorensen
     * in addition we accept ',' to handle strings where the size is
345 9f9b17a4 Jes Sorensen
     * part of a multi token argument.
346 9f9b17a4 Jes Sorensen
     */
347 9f9b17a4 Jes Sorensen
    c = *endptr;
348 d8427002 Jes Sorensen
    d = c;
349 f3bd362a Jes Sorensen
    if (qemu_isspace(c) || c == '\0' || c == ',') {
350 9f9b17a4 Jes Sorensen
        c = 0;
351 d8427002 Jes Sorensen
        if (default_suffix) {
352 d8427002 Jes Sorensen
            d = default_suffix;
353 d8427002 Jes Sorensen
        } else {
354 d8427002 Jes Sorensen
            d = c;
355 d8427002 Jes Sorensen
        }
356 9f9b17a4 Jes Sorensen
    }
357 a2afc2c1 Jes Sorensen
    switch (qemu_toupper(d)) {
358 2be22ca5 Jes Sorensen
    case STRTOSZ_DEFSUFFIX_B:
359 9f9b17a4 Jes Sorensen
        mul = 1;
360 9f9b17a4 Jes Sorensen
        if (mul_required) {
361 9f9b17a4 Jes Sorensen
            goto fail;
362 9f9b17a4 Jes Sorensen
        }
363 9f9b17a4 Jes Sorensen
        break;
364 2be22ca5 Jes Sorensen
    case STRTOSZ_DEFSUFFIX_KB:
365 9f9b17a4 Jes Sorensen
        mul = 1 << 10;
366 9f9b17a4 Jes Sorensen
        break;
367 9f9b17a4 Jes Sorensen
    case 0:
368 9f9b17a4 Jes Sorensen
        if (mul_required) {
369 9f9b17a4 Jes Sorensen
            goto fail;
370 9f9b17a4 Jes Sorensen
        }
371 2be22ca5 Jes Sorensen
    case STRTOSZ_DEFSUFFIX_MB:
372 9f9b17a4 Jes Sorensen
        mul = 1ULL << 20;
373 9f9b17a4 Jes Sorensen
        break;
374 2be22ca5 Jes Sorensen
    case STRTOSZ_DEFSUFFIX_GB:
375 9f9b17a4 Jes Sorensen
        mul = 1ULL << 30;
376 9f9b17a4 Jes Sorensen
        break;
377 2be22ca5 Jes Sorensen
    case STRTOSZ_DEFSUFFIX_TB:
378 9f9b17a4 Jes Sorensen
        mul = 1ULL << 40;
379 9f9b17a4 Jes Sorensen
        break;
380 9f9b17a4 Jes Sorensen
    default:
381 9f9b17a4 Jes Sorensen
        goto fail;
382 9f9b17a4 Jes Sorensen
    }
383 9f9b17a4 Jes Sorensen
    /*
384 9f9b17a4 Jes Sorensen
     * If not terminated by whitespace, ',', or \0, increment endptr
385 9f9b17a4 Jes Sorensen
     * to point to next character, then check that we are terminated
386 9f9b17a4 Jes Sorensen
     * by an appropriate separating character, ie. whitespace, ',', or
387 9f9b17a4 Jes Sorensen
     * \0. If not, we are seeing trailing garbage, thus fail.
388 9f9b17a4 Jes Sorensen
     */
389 9f9b17a4 Jes Sorensen
    if (c != 0) {
390 9f9b17a4 Jes Sorensen
        endptr++;
391 f3bd362a Jes Sorensen
        if (!qemu_isspace(*endptr) && *endptr != ',' && *endptr != 0) {
392 9f9b17a4 Jes Sorensen
            goto fail;
393 9f9b17a4 Jes Sorensen
        }
394 9f9b17a4 Jes Sorensen
    }
395 70b4f4bb Jes Sorensen
    if ((val * mul >= INT64_MAX) || val < 0) {
396 9f9b17a4 Jes Sorensen
        goto fail;
397 9f9b17a4 Jes Sorensen
    }
398 9f9b17a4 Jes Sorensen
    retval = val * mul;
399 9f9b17a4 Jes Sorensen
400 9f9b17a4 Jes Sorensen
fail:
401 9f9b17a4 Jes Sorensen
    if (end) {
402 9f9b17a4 Jes Sorensen
        *end = endptr;
403 9f9b17a4 Jes Sorensen
    }
404 9f9b17a4 Jes Sorensen
405 9f9b17a4 Jes Sorensen
    return retval;
406 9f9b17a4 Jes Sorensen
}
407 d8427002 Jes Sorensen
408 70b4f4bb Jes Sorensen
int64_t strtosz(const char *nptr, char **end)
409 d8427002 Jes Sorensen
{
410 d8427002 Jes Sorensen
    return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
411 d8427002 Jes Sorensen
}