summaryrefslogtreecommitdiffstats
path: root/libhb/compat.c
blob: 6d0bdddf05462f18da61bbbfb7e657ac9a827ab9 (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
/* compat.c

   Copyright (c) 2003-2019 HandBrake Team
   This file is part of the HandBrake source code
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License v2.
   For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
 */

#include "compat.h"

#ifdef HB_NEED_STRTOK_R
#include <string.h>

char *strtok_r(char *s, const char *delim, char **save_ptr) 
{
    char *token;

    if (s == NULL) s = *save_ptr;

    /* Scan leading delimiters.  */
    s += strspn(s, delim);
    if (*s == '\0') return NULL;

    /* Find the end of the token.  */
    token = s;
    s = strpbrk(token, delim);
    if (s == NULL)
    {
        /* This token finishes the string.  */
        *save_ptr = strchr(token, '\0');
    }
    else
    {
        /* Terminate the token and make *save_ptr point past it. */
        *s = '\0';
        *save_ptr = s + 1;
    }

    return token;
}
#endif // HB_NEED_STRTOK_R

#ifndef HAS_STRERROR_R
#ifndef _GNU_SOURCE
#include <sys/types.h>
#include <errno.h>
#include <string.h>

#define ERRSTR_LEN 20

int strerror_r(int errnum, char *strerrbuf, size_t buflen)
{
    int ret = 0;
    char errstr[ERRSTR_LEN];

    if (strerrbuf == NULL || buflen == 0)
    {
        ret = ERANGE;
        goto done;
    }

    if(snprintf(errstr, ERRSTR_LEN - 1, "unknown error %d", errnum) < 0)
    {
        ret = EINVAL;
        goto done;
    }

    if (snprintf(strerrbuf, buflen, errstr) < 0)
    {
        ret = EINVAL;
        goto done;
    }

done:
    return ret;
}
#endif // _GNU_SOURCE
#endif // HAS_STRERROR_R