root / cutils.c @ 2ada0ed7
History | View | Annotate | Download (4 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 | } |
104 | 44e3ee8a | aliguori | |
105 | 44e3ee8a | aliguori | /* io vectors */
|
106 | 44e3ee8a | aliguori | |
107 | 44e3ee8a | aliguori | void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint) |
108 | 44e3ee8a | aliguori | { |
109 | 44e3ee8a | aliguori | qiov->iov = qemu_malloc(alloc_hint * sizeof(struct iovec)); |
110 | 44e3ee8a | aliguori | qiov->niov = 0;
|
111 | 44e3ee8a | aliguori | qiov->nalloc = alloc_hint; |
112 | 249aa745 | aliguori | qiov->size = 0;
|
113 | 44e3ee8a | aliguori | } |
114 | 44e3ee8a | aliguori | |
115 | 44e3ee8a | aliguori | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) |
116 | 44e3ee8a | aliguori | { |
117 | 44e3ee8a | aliguori | if (qiov->niov == qiov->nalloc) {
|
118 | 44e3ee8a | aliguori | qiov->nalloc = 2 * qiov->nalloc + 1; |
119 | 44e3ee8a | aliguori | qiov->iov = qemu_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec)); |
120 | 44e3ee8a | aliguori | } |
121 | 44e3ee8a | aliguori | qiov->iov[qiov->niov].iov_base = base; |
122 | 44e3ee8a | aliguori | qiov->iov[qiov->niov].iov_len = len; |
123 | 249aa745 | aliguori | qiov->size += len; |
124 | 44e3ee8a | aliguori | ++qiov->niov; |
125 | 44e3ee8a | aliguori | } |
126 | 44e3ee8a | aliguori | |
127 | 44e3ee8a | aliguori | void qemu_iovec_destroy(QEMUIOVector *qiov)
|
128 | 44e3ee8a | aliguori | { |
129 | 44e3ee8a | aliguori | qemu_free(qiov->iov); |
130 | 44e3ee8a | aliguori | } |
131 | 44e3ee8a | aliguori | |
132 | be959463 | aliguori | void qemu_iovec_reset(QEMUIOVector *qiov)
|
133 | be959463 | aliguori | { |
134 | be959463 | aliguori | qiov->niov = 0;
|
135 | be959463 | aliguori | qiov->size = 0;
|
136 | be959463 | aliguori | } |
137 | be959463 | aliguori | |
138 | 44e3ee8a | aliguori | void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf) |
139 | 44e3ee8a | aliguori | { |
140 | 44e3ee8a | aliguori | uint8_t *p = (uint8_t *)buf; |
141 | 44e3ee8a | aliguori | int i;
|
142 | 44e3ee8a | aliguori | |
143 | 44e3ee8a | aliguori | for (i = 0; i < qiov->niov; ++i) { |
144 | 44e3ee8a | aliguori | memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len); |
145 | 44e3ee8a | aliguori | p += qiov->iov[i].iov_len; |
146 | 44e3ee8a | aliguori | } |
147 | 44e3ee8a | aliguori | } |
148 | 44e3ee8a | aliguori | |
149 | 249aa745 | aliguori | void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count) |
150 | 44e3ee8a | aliguori | { |
151 | 44e3ee8a | aliguori | const uint8_t *p = (const uint8_t *)buf; |
152 | 249aa745 | aliguori | size_t copy; |
153 | 44e3ee8a | aliguori | int i;
|
154 | 44e3ee8a | aliguori | |
155 | 249aa745 | aliguori | for (i = 0; i < qiov->niov && count; ++i) { |
156 | 249aa745 | aliguori | copy = count; |
157 | 249aa745 | aliguori | if (copy > qiov->iov[i].iov_len)
|
158 | 249aa745 | aliguori | copy = qiov->iov[i].iov_len; |
159 | 249aa745 | aliguori | memcpy(qiov->iov[i].iov_base, p, copy); |
160 | 249aa745 | aliguori | p += copy; |
161 | 249aa745 | aliguori | count -= copy; |
162 | 44e3ee8a | aliguori | } |
163 | 44e3ee8a | aliguori | } |