summaryrefslogtreecommitdiff
path: root/Build/source/libs/zziplib/zziplib-src/zzip/__string.h
blob: e62fb06564cde9155a6fcb36d191230ebaab960f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef __ZZIP_INTERNAL_STRING_H
#define __ZZIP_INTERNAL_STRING_H

#ifdef __linux__
#define _GNU_SOURCE _glibc_developers_are_idiots_to_call_strndup_gnu_specific_
#endif

#include <zzip/conf.h>

#if   defined ZZIP_HAVE_STRING_H
#include <string.h>
#elif defined ZZIP_HAVE_STRINGS_H
#include <strings.h>
#endif

#if defined ZZIP_HAVE_STRNLEN || defined strnlen
#define _zzip_strnlen strnlen
#else
#include <stdlib.h>

/* if your system does not have strnlen: */
static size_t
_zzip_strnlen(const char *p, size_t maxlen)
{
    const char * stop = (char *)memchr(p, '\0', maxlen);
    return stop ? (size_t)(stop - p) : maxlen;
}
#endif


#if defined ZZIP_HAVE_STRNDUP || defined strndup
#define _zzip_strndup strndup
#else
#include <stdlib.h>

/* if your system does not have strndup: */
zzip__new__ static char *
_zzip_strndup(char const *p, size_t maxlen)
{
    if (p == NULL)
    {
       return p;
    } else 
    {
        size_t len = _zzip_strnlen(p, maxlen);
        char* r = (char *)malloc(len + 1);
        if (r == NULL)
            return NULL; /* errno = ENOMEM */
        r[len] = '\0';
        return memcpy(r, p, len);
    }
}
#endif

#if defined ZZIP_HAVE_STRCASECMP || defined strcasecmp
#define _zzip_strcasecmp strcasecmp
#else

/* if your system does not have strcasecmp: */
static int
_zzip_strcasecmp(char *__zzip_restrict a, char *_zzip_restrict b)
{
    if (! a)
        return (b) ? 1 : 0;
    if (! b)
        return -1;
    while (1)
    {
        int v = tolower(*a) - tolower(*b);
        if (v)
            return v;
        if (! *a)
            return 1;
        if (! *b)
            return -1;
        a++;
        b++;
    }
}
#endif

#endif