2
0

cutils.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Simple C functions to supplement the C library
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu-common.h"
  25. #include "host-utils.h"
  26. #include <math.h>
  27. void pstrcpy(char *buf, int buf_size, const char *str)
  28. {
  29. int c;
  30. char *q = buf;
  31. if (buf_size <= 0)
  32. return;
  33. for(;;) {
  34. c = *str++;
  35. if (c == 0 || q >= buf + buf_size - 1)
  36. break;
  37. *q++ = c;
  38. }
  39. *q = '\0';
  40. }
  41. /* strcat and truncate. */
  42. char *pstrcat(char *buf, int buf_size, const char *s)
  43. {
  44. int len;
  45. len = strlen(buf);
  46. if (len < buf_size)
  47. pstrcpy(buf + len, buf_size - len, s);
  48. return buf;
  49. }
  50. int strstart(const char *str, const char *val, const char **ptr)
  51. {
  52. const char *p, *q;
  53. p = str;
  54. q = val;
  55. while (*q != '\0') {
  56. if (*p != *q)
  57. return 0;
  58. p++;
  59. q++;
  60. }
  61. if (ptr)
  62. *ptr = p;
  63. return 1;
  64. }
  65. int stristart(const char *str, const char *val, const char **ptr)
  66. {
  67. const char *p, *q;
  68. p = str;
  69. q = val;
  70. while (*q != '\0') {
  71. if (qemu_toupper(*p) != qemu_toupper(*q))
  72. return 0;
  73. p++;
  74. q++;
  75. }
  76. if (ptr)
  77. *ptr = p;
  78. return 1;
  79. }
  80. /* XXX: use host strnlen if available ? */
  81. int qemu_strnlen(const char *s, int max_len)
  82. {
  83. int i;
  84. for(i = 0; i < max_len; i++) {
  85. if (s[i] == '\0') {
  86. break;
  87. }
  88. }
  89. return i;
  90. }
  91. time_t mktimegm(struct tm *tm)
  92. {
  93. time_t t;
  94. int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
  95. if (m < 3) {
  96. m += 12;
  97. y--;
  98. }
  99. t = 86400 * (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 +
  100. y / 400 - 719469);
  101. t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
  102. return t;
  103. }
  104. int qemu_fls(int i)
  105. {
  106. return 32 - clz32(i);
  107. }
  108. /*
  109. * Make sure data goes on disk, but if possible do not bother to
  110. * write out the inode just for timestamp updates.
  111. *
  112. * Unfortunately even in 2009 many operating systems do not support
  113. * fdatasync and have to fall back to fsync.
  114. */
  115. int qemu_fdatasync(int fd)
  116. {
  117. #ifdef CONFIG_FDATASYNC
  118. return fdatasync(fd);
  119. #else
  120. return fsync(fd);
  121. #endif
  122. }
  123. /* io vectors */
  124. void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
  125. {
  126. qiov->iov = g_malloc(alloc_hint * sizeof(struct iovec));
  127. qiov->niov = 0;
  128. qiov->nalloc = alloc_hint;
  129. qiov->size = 0;
  130. }
  131. void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov)
  132. {
  133. int i;
  134. qiov->iov = iov;
  135. qiov->niov = niov;
  136. qiov->nalloc = -1;
  137. qiov->size = 0;
  138. for (i = 0; i < niov; i++)
  139. qiov->size += iov[i].iov_len;
  140. }
  141. void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
  142. {
  143. assert(qiov->nalloc != -1);
  144. if (qiov->niov == qiov->nalloc) {
  145. qiov->nalloc = 2 * qiov->nalloc + 1;
  146. qiov->iov = g_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec));
  147. }
  148. qiov->iov[qiov->niov].iov_base = base;
  149. qiov->iov[qiov->niov].iov_len = len;
  150. qiov->size += len;
  151. ++qiov->niov;
  152. }
  153. /*
  154. * Copies iovecs from src to the end of dst. It starts copying after skipping
  155. * the given number of bytes in src and copies until src is completely copied
  156. * or the total size of the copied iovec reaches size.The size of the last
  157. * copied iovec is changed in order to fit the specified total size if it isn't
  158. * a perfect fit already.
  159. */
  160. void qemu_iovec_copy(QEMUIOVector *dst, QEMUIOVector *src, uint64_t skip,
  161. size_t size)
  162. {
  163. int i;
  164. size_t done;
  165. void *iov_base;
  166. uint64_t iov_len;
  167. assert(dst->nalloc != -1);
  168. done = 0;
  169. for (i = 0; (i < src->niov) && (done != size); i++) {
  170. if (skip >= src->iov[i].iov_len) {
  171. /* Skip the whole iov */
  172. skip -= src->iov[i].iov_len;
  173. continue;
  174. } else {
  175. /* Skip only part (or nothing) of the iov */
  176. iov_base = (uint8_t*) src->iov[i].iov_base + skip;
  177. iov_len = src->iov[i].iov_len - skip;
  178. skip = 0;
  179. }
  180. if (done + iov_len > size) {
  181. qemu_iovec_add(dst, iov_base, size - done);
  182. break;
  183. } else {
  184. qemu_iovec_add(dst, iov_base, iov_len);
  185. }
  186. done += iov_len;
  187. }
  188. }
  189. void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size)
  190. {
  191. qemu_iovec_copy(dst, src, 0, size);
  192. }
  193. void qemu_iovec_destroy(QEMUIOVector *qiov)
  194. {
  195. assert(qiov->nalloc != -1);
  196. qemu_iovec_reset(qiov);
  197. g_free(qiov->iov);
  198. qiov->nalloc = 0;
  199. qiov->iov = NULL;
  200. }
  201. void qemu_iovec_reset(QEMUIOVector *qiov)
  202. {
  203. assert(qiov->nalloc != -1);
  204. qiov->niov = 0;
  205. qiov->size = 0;
  206. }
  207. void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf)
  208. {
  209. uint8_t *p = (uint8_t *)buf;
  210. int i;
  211. for (i = 0; i < qiov->niov; ++i) {
  212. memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
  213. p += qiov->iov[i].iov_len;
  214. }
  215. }
  216. void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count)
  217. {
  218. const uint8_t *p = (const uint8_t *)buf;
  219. size_t copy;
  220. int i;
  221. for (i = 0; i < qiov->niov && count; ++i) {
  222. copy = count;
  223. if (copy > qiov->iov[i].iov_len)
  224. copy = qiov->iov[i].iov_len;
  225. memcpy(qiov->iov[i].iov_base, p, copy);
  226. p += copy;
  227. count -= copy;
  228. }
  229. }
  230. void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count)
  231. {
  232. size_t n;
  233. int i;
  234. for (i = 0; i < qiov->niov && count; ++i) {
  235. n = MIN(count, qiov->iov[i].iov_len);
  236. memset(qiov->iov[i].iov_base, c, n);
  237. count -= n;
  238. }
  239. }
  240. void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
  241. size_t skip)
  242. {
  243. int i;
  244. size_t done;
  245. void *iov_base;
  246. uint64_t iov_len;
  247. done = 0;
  248. for (i = 0; (i < qiov->niov) && (done != count); i++) {
  249. if (skip >= qiov->iov[i].iov_len) {
  250. /* Skip the whole iov */
  251. skip -= qiov->iov[i].iov_len;
  252. continue;
  253. } else {
  254. /* Skip only part (or nothing) of the iov */
  255. iov_base = (uint8_t*) qiov->iov[i].iov_base + skip;
  256. iov_len = qiov->iov[i].iov_len - skip;
  257. skip = 0;
  258. }
  259. if (done + iov_len > count) {
  260. memset(iov_base, c, count - done);
  261. break;
  262. } else {
  263. memset(iov_base, c, iov_len);
  264. }
  265. done += iov_len;
  266. }
  267. }
  268. #ifndef _WIN32
  269. /* Sets a specific flag */
  270. int fcntl_setfl(int fd, int flag)
  271. {
  272. int flags;
  273. flags = fcntl(fd, F_GETFL);
  274. if (flags == -1)
  275. return -errno;
  276. if (fcntl(fd, F_SETFL, flags | flag) == -1)
  277. return -errno;
  278. return 0;
  279. }
  280. #endif
  281. static int64_t suffix_mul(char suffix, int64_t unit)
  282. {
  283. switch (qemu_toupper(suffix)) {
  284. case STRTOSZ_DEFSUFFIX_B:
  285. return 1;
  286. case STRTOSZ_DEFSUFFIX_KB:
  287. return unit;
  288. case STRTOSZ_DEFSUFFIX_MB:
  289. return unit * unit;
  290. case STRTOSZ_DEFSUFFIX_GB:
  291. return unit * unit * unit;
  292. case STRTOSZ_DEFSUFFIX_TB:
  293. return unit * unit * unit * unit;
  294. }
  295. return -1;
  296. }
  297. /*
  298. * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
  299. * M/m for MB, G/g for GB or T/t for TB. End pointer will be returned
  300. * in *end, if not NULL. Return -1 on error.
  301. */
  302. int64_t strtosz_suffix_unit(const char *nptr, char **end,
  303. const char default_suffix, int64_t unit)
  304. {
  305. int64_t retval = -1;
  306. char *endptr;
  307. unsigned char c;
  308. int mul_required = 0;
  309. double val, mul, integral, fraction;
  310. errno = 0;
  311. val = strtod(nptr, &endptr);
  312. if (isnan(val) || endptr == nptr || errno != 0) {
  313. goto fail;
  314. }
  315. fraction = modf(val, &integral);
  316. if (fraction != 0) {
  317. mul_required = 1;
  318. }
  319. c = *endptr;
  320. mul = suffix_mul(c, unit);
  321. if (mul >= 0) {
  322. endptr++;
  323. } else {
  324. mul = suffix_mul(default_suffix, unit);
  325. assert(mul >= 0);
  326. }
  327. if (mul == 1 && mul_required) {
  328. goto fail;
  329. }
  330. if ((val * mul >= INT64_MAX) || val < 0) {
  331. goto fail;
  332. }
  333. retval = val * mul;
  334. fail:
  335. if (end) {
  336. *end = endptr;
  337. }
  338. return retval;
  339. }
  340. int64_t strtosz_suffix(const char *nptr, char **end, const char default_suffix)
  341. {
  342. return strtosz_suffix_unit(nptr, end, default_suffix, 1024);
  343. }
  344. int64_t strtosz(const char *nptr, char **end)
  345. {
  346. return strtosz_suffix(nptr, end, STRTOSZ_DEFSUFFIX_MB);
  347. }
  348. int qemu_parse_fd(const char *param)
  349. {
  350. int fd;
  351. char *endptr = NULL;
  352. fd = strtol(param, &endptr, 10);
  353. if (*endptr || (fd == 0 && param == endptr)) {
  354. return -1;
  355. }
  356. return fd;
  357. }