extensions.h
| 1 | #ifndef CUTILS_EXTENSIONS_H |
| 2 | #define CUTILS_EXTENSIONS_H |
| 3 | |
| 4 | #include <cutils/hedley.h> |
| 5 | #include <errno.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <cutils/backport.h> |
| 8 | #include <string.h> |
| 9 | #include <ctype.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <stdio.h> |
| 12 | |
| 13 | int cutil_strcasecmp(const char* s1, const char* s2); |
| 14 | int cutil_strncasecmp(const char* s1, const char* s2, size_t n); |
| 15 | |
| 16 | size_t cutil_strnlen(const char *s, size_t maxlen); |
| 17 | |
| 18 | char* cutil_strdup(const char *s); |
| 19 | char* cutil_strndup(const char *s, size_t n); |
| 20 | |
| 21 | #if __STDC_VERSION__ >= 199901L |
| 22 | int cutil_asprintf(char** strp, const char* format, ...); |
| 23 | #endif |
| 24 | |
| 25 | HEDLEY_INLINE |
| 26 | static void* cutil_reallocarray(void *ptr, size_t nmemb, size_t size) |
| 27 | { |
| 28 | size_t new_size = nmemb * size; |
| 29 | |
| 30 | if(nmemb != 0 && new_size / nmemb != size) |
| 31 | { |
| 32 | errno = ENOMEM; |
| 33 | return NULL; |
| 34 | } else { |
| 35 | return realloc(ptr, new_size); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | HEDLEY_INLINE |
| 40 | static void* cutil_reallocarray_inc(void *ptr, size_t nmemb, size_t size, size_t add) |
| 41 | { |
| 42 | if(SIZE_MAX - add < size) |
| 43 | { |
| 44 | errno = ENOMEM; |
| 45 | return NULL; |
| 46 | } else { |
| 47 | return cutil_reallocarray(ptr, nmemb, size+add); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | #endif /* CUTILS_EXTENSIONS_H */ |
| 54 |