Statistics
| Branch: | Revision:

root / cutils.c @ 9f9b17a4

History | View | Annotate | Download (8.7 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 db1a4972 Paolo Bonzini
#ifndef _WIN32
271 db1a4972 Paolo Bonzini
/* Sets a specific flag */
272 db1a4972 Paolo Bonzini
int fcntl_setfl(int fd, int flag)
273 db1a4972 Paolo Bonzini
{
274 db1a4972 Paolo Bonzini
    int flags;
275 db1a4972 Paolo Bonzini
276 db1a4972 Paolo Bonzini
    flags = fcntl(fd, F_GETFL);
277 db1a4972 Paolo Bonzini
    if (flags == -1)
278 db1a4972 Paolo Bonzini
        return -errno;
279 db1a4972 Paolo Bonzini
280 db1a4972 Paolo Bonzini
    if (fcntl(fd, F_SETFL, flags | flag) == -1)
281 db1a4972 Paolo Bonzini
        return -errno;
282 db1a4972 Paolo Bonzini
283 db1a4972 Paolo Bonzini
    return 0;
284 db1a4972 Paolo Bonzini
}
285 db1a4972 Paolo Bonzini
#endif
286 db1a4972 Paolo Bonzini
287 9f9b17a4 Jes Sorensen
/*
288 9f9b17a4 Jes Sorensen
 * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
289 9f9b17a4 Jes Sorensen
 * M/m for MB, G/g for GB or T/t for TB. Default without any postfix
290 9f9b17a4 Jes Sorensen
 * is MB. End pointer will be returned in *end, if not NULL. A valid
291 9f9b17a4 Jes Sorensen
 * value must be terminated by whitespace, ',' or '\0'. Return -1 on
292 9f9b17a4 Jes Sorensen
 * error.
293 9f9b17a4 Jes Sorensen
 */
294 9f9b17a4 Jes Sorensen
ssize_t strtosz(const char *nptr, char **end)
295 9f9b17a4 Jes Sorensen
{
296 9f9b17a4 Jes Sorensen
    ssize_t retval = -1;
297 9f9b17a4 Jes Sorensen
    char *endptr, c;
298 9f9b17a4 Jes Sorensen
    int mul_required = 0;
299 9f9b17a4 Jes Sorensen
    double val, mul, integral, fraction;
300 9f9b17a4 Jes Sorensen
301 9f9b17a4 Jes Sorensen
    errno = 0;
302 9f9b17a4 Jes Sorensen
    val = strtod(nptr, &endptr);
303 9f9b17a4 Jes Sorensen
    if (isnan(val) || endptr == nptr || errno != 0) {
304 9f9b17a4 Jes Sorensen
        goto fail;
305 9f9b17a4 Jes Sorensen
    }
306 9f9b17a4 Jes Sorensen
    integral = modf(val, &fraction);
307 9f9b17a4 Jes Sorensen
    if (integral != 0) {
308 9f9b17a4 Jes Sorensen
        mul_required = 1;
309 9f9b17a4 Jes Sorensen
    }
310 9f9b17a4 Jes Sorensen
    /*
311 9f9b17a4 Jes Sorensen
     * Any whitespace character is fine for terminating the number,
312 9f9b17a4 Jes Sorensen
     * in addition we accept ',' to handle strings where the size is
313 9f9b17a4 Jes Sorensen
     * part of a multi token argument.
314 9f9b17a4 Jes Sorensen
     */
315 9f9b17a4 Jes Sorensen
    c = *endptr;
316 9f9b17a4 Jes Sorensen
    if (isspace(c) || c == '\0' || c == ',') {
317 9f9b17a4 Jes Sorensen
        c = 0;
318 9f9b17a4 Jes Sorensen
    }
319 9f9b17a4 Jes Sorensen
    switch (c) {
320 9f9b17a4 Jes Sorensen
    case 'B':
321 9f9b17a4 Jes Sorensen
    case 'b':
322 9f9b17a4 Jes Sorensen
        mul = 1;
323 9f9b17a4 Jes Sorensen
        if (mul_required) {
324 9f9b17a4 Jes Sorensen
            goto fail;
325 9f9b17a4 Jes Sorensen
        }
326 9f9b17a4 Jes Sorensen
        break;
327 9f9b17a4 Jes Sorensen
    case 'K':
328 9f9b17a4 Jes Sorensen
    case 'k':
329 9f9b17a4 Jes Sorensen
        mul = 1 << 10;
330 9f9b17a4 Jes Sorensen
        break;
331 9f9b17a4 Jes Sorensen
    case 0:
332 9f9b17a4 Jes Sorensen
        if (mul_required) {
333 9f9b17a4 Jes Sorensen
            goto fail;
334 9f9b17a4 Jes Sorensen
        }
335 9f9b17a4 Jes Sorensen
    case 'M':
336 9f9b17a4 Jes Sorensen
    case 'm':
337 9f9b17a4 Jes Sorensen
        mul = 1ULL << 20;
338 9f9b17a4 Jes Sorensen
        break;
339 9f9b17a4 Jes Sorensen
    case 'G':
340 9f9b17a4 Jes Sorensen
    case 'g':
341 9f9b17a4 Jes Sorensen
        mul = 1ULL << 30;
342 9f9b17a4 Jes Sorensen
        break;
343 9f9b17a4 Jes Sorensen
    case 'T':
344 9f9b17a4 Jes Sorensen
    case 't':
345 9f9b17a4 Jes Sorensen
        mul = 1ULL << 40;
346 9f9b17a4 Jes Sorensen
        break;
347 9f9b17a4 Jes Sorensen
    default:
348 9f9b17a4 Jes Sorensen
        goto fail;
349 9f9b17a4 Jes Sorensen
    }
350 9f9b17a4 Jes Sorensen
    /*
351 9f9b17a4 Jes Sorensen
     * If not terminated by whitespace, ',', or \0, increment endptr
352 9f9b17a4 Jes Sorensen
     * to point to next character, then check that we are terminated
353 9f9b17a4 Jes Sorensen
     * by an appropriate separating character, ie. whitespace, ',', or
354 9f9b17a4 Jes Sorensen
     * \0. If not, we are seeing trailing garbage, thus fail.
355 9f9b17a4 Jes Sorensen
     */
356 9f9b17a4 Jes Sorensen
    if (c != 0) {
357 9f9b17a4 Jes Sorensen
        endptr++;
358 9f9b17a4 Jes Sorensen
        if (!isspace(*endptr) && *endptr != ',' && *endptr != 0) {
359 9f9b17a4 Jes Sorensen
            goto fail;
360 9f9b17a4 Jes Sorensen
        }
361 9f9b17a4 Jes Sorensen
    }
362 9f9b17a4 Jes Sorensen
    if ((val * mul >= ~(size_t)0) || val < 0) {
363 9f9b17a4 Jes Sorensen
        goto fail;
364 9f9b17a4 Jes Sorensen
    }
365 9f9b17a4 Jes Sorensen
    retval = val * mul;
366 9f9b17a4 Jes Sorensen
367 9f9b17a4 Jes Sorensen
fail:
368 9f9b17a4 Jes Sorensen
    if (end) {
369 9f9b17a4 Jes Sorensen
        *end = endptr;
370 9f9b17a4 Jes Sorensen
    }
371 9f9b17a4 Jes Sorensen
372 9f9b17a4 Jes Sorensen
    return retval;
373 9f9b17a4 Jes Sorensen
}