| 1 | #ifndef _BABELTRACE_COMPAT_STRING_H |
| 2 | #define _BABELTRACE_COMPAT_STRING_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | |
| 29 | #if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)) |
| 30 | |
| 31 | /* XSI-compliant strerror_r */ |
| 32 | static inline |
| 33 | int compat_strerror_r(int errnum, char *buf, size_t buflen) |
| 34 | { |
| 35 | return strerror_r(errnum, buf, buflen); |
| 36 | } |
| 37 | |
| 38 | #else |
| 39 | |
| 40 | /* GNU-compliant strerror_r */ |
| 41 | static inline |
| 42 | int compat_strerror_r(int errnum, char *buf, size_t buflen) |
| 43 | { |
| 44 | char *retbuf; |
| 45 | |
| 46 | retbuf = strerror_r(errnum, buf, buflen); |
| 47 | if (retbuf != buf) |
| 48 | strncpy(buf, retbuf, buflen); |
| 49 | buf[buflen - 1] = '\0'; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | #endif |
| 54 | |
| 55 | #ifdef HAVE_STRNLEN |
| 56 | static inline |
| 57 | size_t bt_strnlen(const char *str, size_t max) |
| 58 | { |
| 59 | return strnlen(str, max); |
| 60 | } |
| 61 | #else |
| 62 | static inline |
| 63 | size_t bt_strnlen(const char *str, size_t max) |
| 64 | { |
| 65 | size_t ret; |
| 66 | const char *end; |
| 67 | |
| 68 | end = memchr(str, 0, max); |
| 69 | |
| 70 | if (end) { |
| 71 | ret = (size_t) (end - str); |
| 72 | } else { |
| 73 | ret = max; |
| 74 | } |
| 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | #endif /* HAVE_STRNLEN */ |
| 79 | |
| 80 | #ifdef HAVE_STRNDUP |
| 81 | static inline |
| 82 | char *bt_strndup(const char *s, size_t n) |
| 83 | { |
| 84 | return strndup(s, n); |
| 85 | } |
| 86 | #else |
| 87 | static inline |
| 88 | char *bt_strndup(const char *s, size_t n) |
| 89 | { |
| 90 | char *ret; |
| 91 | size_t navail; |
| 92 | |
| 93 | if (!s) { |
| 94 | ret = NULL; |
| 95 | goto end; |
| 96 | } |
| 97 | |
| 98 | /* min() */ |
| 99 | navail = strlen(s) + 1; |
| 100 | if ((n + 1) < navail) { |
| 101 | navail = n + 1; |
| 102 | } |
| 103 | |
| 104 | ret = malloc(navail); |
| 105 | if (!ret) { |
| 106 | goto end; |
| 107 | } |
| 108 | |
| 109 | memcpy(ret, s, navail); |
| 110 | ret[navail - 1] = '\0'; |
| 111 | end: |
| 112 | return ret; |
| 113 | } |
| 114 | #endif /* HAVE_STRNDUP */ |
| 115 | |
| 116 | #endif /* _BABELTRACE_COMPAT_STRING_H */ |