Statistics
| Branch: | Revision:

root / cutils.c @ 1dd9ffb9

History | View | Annotate | Download (2.6 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 18607dcb bellard
27 18607dcb bellard
void pstrcpy(char *buf, int buf_size, const char *str)
28 18607dcb bellard
{
29 18607dcb bellard
    int c;
30 18607dcb bellard
    char *q = buf;
31 18607dcb bellard
32 18607dcb bellard
    if (buf_size <= 0)
33 18607dcb bellard
        return;
34 18607dcb bellard
35 18607dcb bellard
    for(;;) {
36 18607dcb bellard
        c = *str++;
37 18607dcb bellard
        if (c == 0 || q >= buf + buf_size - 1)
38 18607dcb bellard
            break;
39 18607dcb bellard
        *q++ = c;
40 18607dcb bellard
    }
41 18607dcb bellard
    *q = '\0';
42 18607dcb bellard
}
43 18607dcb bellard
44 18607dcb bellard
/* strcat and truncate. */
45 18607dcb bellard
char *pstrcat(char *buf, int buf_size, const char *s)
46 18607dcb bellard
{
47 18607dcb bellard
    int len;
48 18607dcb bellard
    len = strlen(buf);
49 5fafdf24 ths
    if (len < buf_size)
50 18607dcb bellard
        pstrcpy(buf + len, buf_size - len, s);
51 18607dcb bellard
    return buf;
52 18607dcb bellard
}
53 18607dcb bellard
54 18607dcb bellard
int strstart(const char *str, const char *val, const char **ptr)
55 18607dcb bellard
{
56 18607dcb bellard
    const char *p, *q;
57 18607dcb bellard
    p = str;
58 18607dcb bellard
    q = val;
59 18607dcb bellard
    while (*q != '\0') {
60 18607dcb bellard
        if (*p != *q)
61 18607dcb bellard
            return 0;
62 18607dcb bellard
        p++;
63 18607dcb bellard
        q++;
64 18607dcb bellard
    }
65 18607dcb bellard
    if (ptr)
66 18607dcb bellard
        *ptr = p;
67 18607dcb bellard
    return 1;
68 18607dcb bellard
}
69 18607dcb bellard
70 18607dcb bellard
int stristart(const char *str, const char *val, const char **ptr)
71 18607dcb bellard
{
72 18607dcb bellard
    const char *p, *q;
73 18607dcb bellard
    p = str;
74 18607dcb bellard
    q = val;
75 18607dcb bellard
    while (*q != '\0') {
76 cd390083 blueswir1
        if (qemu_toupper(*p) != qemu_toupper(*q))
77 18607dcb bellard
            return 0;
78 18607dcb bellard
        p++;
79 18607dcb bellard
        q++;
80 18607dcb bellard
    }
81 18607dcb bellard
    if (ptr)
82 18607dcb bellard
        *ptr = p;
83 18607dcb bellard
    return 1;
84 18607dcb bellard
}
85 3c6b2088 bellard
86 3c6b2088 bellard
time_t mktimegm(struct tm *tm)
87 3c6b2088 bellard
{
88 3c6b2088 bellard
    time_t t;
89 3c6b2088 bellard
    int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
90 3c6b2088 bellard
    if (m < 3) {
91 3c6b2088 bellard
        m += 12;
92 3c6b2088 bellard
        y--;
93 3c6b2088 bellard
    }
94 3c6b2088 bellard
    t = 86400 * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + 
95 3c6b2088 bellard
                 y / 400 - 719469);
96 3c6b2088 bellard
    t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
97 3c6b2088 bellard
    return t;
98 3c6b2088 bellard
}
99 b39ade83 aliguori
100 ad46db9a blueswir1
int qemu_fls(int i)
101 b39ade83 aliguori
{
102 8d371d4b aliguori
    return 32 - clz32(i);
103 b39ade83 aliguori
}