| 1 |
#ifndef __ZZIP_INTERNAL_STRING_H |
| 2 |
#define __ZZIP_INTERNAL_STRING_H |
| 3 |
|
| 4 |
#ifdef __linux__ |
| 5 |
#define _GNU_SOURCE _glibc_developers_are_idiots_to_call_strndup_gnu_specific_ |
| 6 |
#endif |
| 7 |
|
| 8 |
#include <zzip/conf.h> |
| 9 |
|
| 10 |
#if defined ZZIP_HAVE_STRING_H |
| 11 |
#include <string.h> |
| 12 |
#elif defined ZZIP_HAVE_STRINGS_H |
| 13 |
#include <strings.h> |
| 14 |
#endif |
| 15 |
|
| 16 |
#if defined ZZIP_HAVE_STRNLEN || defined strnlen |
| 17 |
#define _zzip_strnlen strnlen |
| 18 |
#else |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
/* if your system does not have strnlen: */ |
| 22 |
static size_t |
| 23 |
_zzip_strnlen(const char *p, size_t maxlen) |
| 24 |
{ |
| 25 |
const char * stop = (char *)memchr(p, '\0', maxlen); |
| 26 |
return stop ? (size_t)(stop - p) : maxlen; |
| 27 |
} |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
#if defined ZZIP_HAVE_STRNDUP || defined strndup |
| 32 |
#define _zzip_strndup strndup |
| 33 |
#else |
| 34 |
#include <stdlib.h> |
| 35 |
|
| 36 |
/* if your system does not have strndup: */ |
| 37 |
zzip__new__ static char * |
| 38 |
_zzip_strndup(char const *p, size_t maxlen) |
| 39 |
{ |
| 40 |
if (p == NULL) |
| 41 |
{ |
| 42 |
return p; |
| 43 |
} else |
| 44 |
{ |
| 45 |
size_t len = _zzip_strnlen(p, maxlen); |
| 46 |
char* r = (char *)malloc(len + 1); |
| 47 |
if (r == NULL) |
| 48 |
return NULL; /* errno = ENOMEM */ |
| 49 |
r[len] = '\0'; |
| 50 |
return memcpy(r, p, len); |
| 51 |
} |
| 52 |
} |
| 53 |
#endif |
| 54 |
|
| 55 |
#if defined ZZIP_HAVE_STRCASECMP || defined strcasecmp |
| 56 |
#define _zzip_strcasecmp strcasecmp |
| 57 |
#else |
| 58 |
|
| 59 |
/* if your system does not have strcasecmp: */ |
| 60 |
static int |
| 61 |
_zzip_strcasecmp(char *__zzip_restrict a, char *_zzip_restrict b) |
| 62 |
{ |
| 63 |
if (! a) |
| 64 |
return (b) ? 1 : 0; |
| 65 |
if (! b) |
| 66 |
return -1; |
| 67 |
while (1) |
| 68 |
{ |
| 69 |
int v = tolower(*a) - tolower(*b); |
| 70 |
if (v) |
| 71 |
return v; |
| 72 |
if (! *a) |
| 73 |
return 1; |
| 74 |
if (! *b) |
| 75 |
return -1; |
| 76 |
a++; |
| 77 |
b++; |
| 78 |
} |
| 79 |
} |
| 80 |
#endif |
| 81 |
|
| 82 |
#endif |