Statistics
| Branch: | Revision:

root / cutils.c @ 8c5e95d8

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