root / cutils.c @ 73f5e313
History | View | Annotate | Download (9.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 | 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 | 7267c094 | Anthony Liguori | qiov->iov = g_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 | 7267c094 | Anthony Liguori | qiov->iov = g_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 | 7267c094 | Anthony Liguori | g_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 | eba90e4e | Markus Armbruster | static int64_t suffix_mul(char suffix, int64_t unit) |
319 | eba90e4e | Markus Armbruster | { |
320 | eba90e4e | Markus Armbruster | switch (qemu_toupper(suffix)) {
|
321 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_B:
|
322 | eba90e4e | Markus Armbruster | return 1; |
323 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_KB:
|
324 | eba90e4e | Markus Armbruster | return unit;
|
325 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_MB:
|
326 | eba90e4e | Markus Armbruster | return unit * unit;
|
327 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_GB:
|
328 | eba90e4e | Markus Armbruster | return unit * unit * unit;
|
329 | eba90e4e | Markus Armbruster | case STRTOSZ_DEFSUFFIX_TB:
|
330 | eba90e4e | Markus Armbruster | return unit * unit * unit * unit;
|
331 | eba90e4e | Markus Armbruster | } |
332 | eba90e4e | Markus Armbruster | return -1; |
333 | eba90e4e | Markus Armbruster | } |
334 | eba90e4e | Markus Armbruster | |
335 | 9f9b17a4 | Jes Sorensen | /*
|
336 | 9f9b17a4 | Jes Sorensen | * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
|
337 | 8dddfb55 | Markus Armbruster | * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
|
338 | eba90e4e | Markus Armbruster | * in *end, if not NULL. Return -1 on error.
|
339 | 9f9b17a4 | Jes Sorensen | */
|
340 | a732e1ba | Joerg Roedel | int64_t strtosz_suffix_unit(const char *nptr, char **end, |
341 | a732e1ba | Joerg Roedel | const char default_suffix, int64_t unit) |
342 | 9f9b17a4 | Jes Sorensen | { |
343 | 70b4f4bb | Jes Sorensen | int64_t retval = -1;
|
344 | f3bd362a | Jes Sorensen | char *endptr;
|
345 | eba90e4e | Markus Armbruster | unsigned char c; |
346 | 9f9b17a4 | Jes Sorensen | int mul_required = 0; |
347 | 9f9b17a4 | Jes Sorensen | double val, mul, integral, fraction;
|
348 | 9f9b17a4 | Jes Sorensen | |
349 | 9f9b17a4 | Jes Sorensen | errno = 0;
|
350 | 9f9b17a4 | Jes Sorensen | val = strtod(nptr, &endptr); |
351 | 9f9b17a4 | Jes Sorensen | if (isnan(val) || endptr == nptr || errno != 0) { |
352 | 9f9b17a4 | Jes Sorensen | goto fail;
|
353 | 9f9b17a4 | Jes Sorensen | } |
354 | 7eb05349 | Jes Sorensen | fraction = modf(val, &integral); |
355 | 7eb05349 | Jes Sorensen | if (fraction != 0) { |
356 | 9f9b17a4 | Jes Sorensen | mul_required = 1;
|
357 | 9f9b17a4 | Jes Sorensen | } |
358 | 9f9b17a4 | Jes Sorensen | c = *endptr; |
359 | eba90e4e | Markus Armbruster | mul = suffix_mul(c, unit); |
360 | eba90e4e | Markus Armbruster | if (mul >= 0) { |
361 | eba90e4e | Markus Armbruster | endptr++; |
362 | eba90e4e | Markus Armbruster | } else {
|
363 | eba90e4e | Markus Armbruster | mul = suffix_mul(default_suffix, unit); |
364 | eba90e4e | Markus Armbruster | assert(mul >= 0);
|
365 | 9f9b17a4 | Jes Sorensen | } |
366 | eba90e4e | Markus Armbruster | if (mul == 1 && mul_required) { |
367 | 9f9b17a4 | Jes Sorensen | goto fail;
|
368 | 9f9b17a4 | Jes Sorensen | } |
369 | 70b4f4bb | Jes Sorensen | if ((val * mul >= INT64_MAX) || val < 0) { |
370 | 9f9b17a4 | Jes Sorensen | goto fail;
|
371 | 9f9b17a4 | Jes Sorensen | } |
372 | 9f9b17a4 | Jes Sorensen | retval = val * mul; |
373 | 9f9b17a4 | Jes Sorensen | |
374 | 9f9b17a4 | Jes Sorensen | fail:
|
375 | 9f9b17a4 | Jes Sorensen | if (end) {
|
376 | 9f9b17a4 | Jes Sorensen | *end = endptr; |
377 | 9f9b17a4 | Jes Sorensen | } |
378 | 9f9b17a4 | Jes Sorensen | |
379 | 9f9b17a4 | Jes Sorensen | return retval;
|
380 | 9f9b17a4 | Jes Sorensen | } |
381 | d8427002 | Jes Sorensen | |
382 | a732e1ba | Joerg Roedel | int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix) |
383 | a732e1ba | Joerg Roedel | { |
384 | fdc9c41a | Jan Kiszka | return strtosz_suffix_unit(nptr, end, default_suffix, 1024); |
385 | a732e1ba | Joerg Roedel | } |
386 | a732e1ba | Joerg Roedel | |
387 | 70b4f4bb | Jes Sorensen | int64_t strtosz(const char *nptr, char **end) |
388 | d8427002 | Jes Sorensen | { |
389 | d8427002 | Jes Sorensen | return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
|
390 | d8427002 | Jes Sorensen | } |
391 | 443916d1 | Stefan Berger | |
392 | 443916d1 | Stefan Berger | int qemu_parse_fd(const char *param) |
393 | 443916d1 | Stefan Berger | { |
394 | 443916d1 | Stefan Berger | int fd;
|
395 | 443916d1 | Stefan Berger | char *endptr = NULL; |
396 | 443916d1 | Stefan Berger | |
397 | 443916d1 | Stefan Berger | fd = strtol(param, &endptr, 10);
|
398 | 443916d1 | Stefan Berger | if (*endptr || (fd == 0 && param == endptr)) { |
399 | 443916d1 | Stefan Berger | return -1; |
400 | 443916d1 | Stefan Berger | } |
401 | 443916d1 | Stefan Berger | return fd;
|
402 | 443916d1 | Stefan Berger | } |