| 1 | /* |
| 2 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdint.h> |
| 30 | #include <assert.h> |
| 31 | |
| 32 | #include "common.h" |
| 33 | #include "debug_priv.h" |
| 34 | |
| 35 | void *xmemdup (const void *src, size_t len) |
| 36 | { |
| 37 | void *new; |
| 38 | |
| 39 | _A(src != NULL); |
| 40 | |
| 41 | new = malloc(len); |
| 42 | memcpy (new, src, len); |
| 43 | |
| 44 | return (new); |
| 45 | } |
| 46 | |
| 47 | uint32_t xnumdigits (size_t size) |
| 48 | { |
| 49 | uint32_t i = 0; |
| 50 | |
| 51 | do { |
| 52 | ++i; |
| 53 | size /= 10; |
| 54 | } while (size > 0); |
| 55 | |
| 56 | return (i); |
| 57 | } |
| 58 | |
| 59 | int xstrncoll (const char *a, size_t alen, |
| 60 | const char *b, size_t blen) |
| 61 | { |
| 62 | int cmp; |
| 63 | |
| 64 | _A(a != NULL); |
| 65 | _A(b != NULL); |
| 66 | |
| 67 | if (alen > blen) { |
| 68 | cmp = strncmp (a, b, blen); |
| 69 | return (cmp == 0 ? 1 : cmp); |
| 70 | } else if (blen > alen) { |
| 71 | cmp = strncmp (a, b, alen); |
| 72 | return (cmp == 0 ? -1 : cmp); |
| 73 | } else { |
| 74 | return strncmp (a, b, alen); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void xsrandom (unsigned long seed) |
| 79 | { |
| 80 | srand(seed); |
| 81 | } |
| 82 | |
| 83 | long xrandom (void) |
| 84 | { |
| 85 | return rand(); |
| 86 | } |