summaryrefslogtreecommitdiffstats
path: root/src/util/support/strerror_r.c
blob: e1ca5651078d602e765efc6353f4e3e85d0cb7f3 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* util/support/strerror_r.c - strerror_r compatibility shim */
/*
 * Copyright (C) 2014 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <k5-platform.h>
#undef strerror_r

#if defined(_WIN32)

/* Implement strerror_r in terms of strerror_s. */
int
k5_strerror_r(int errnum, char *buf, size_t buflen)
{
    int st;

    st = strerror_s(buf, buflen, errnum);
    if (st != 0) {
        errno = st;
        return -1;
    }
}

#elif !defined(HAVE_STRERROR_R)

/* Implement strerror_r in terms of strerror (not thread-safe). */
int
k5_strerror_r(int errnum, char *buf, size_t buflen)
{
    if (strlcpy(buf, strerror(errnum), buflen) >= buflen) {
        errno = ERANGE;
        return -1;
    }
    return 0;
}

#elif defined(STRERROR_R_CHAR_P)

/*
 * Implement the POSIX strerror_r API in terms of the GNU strerror_r, which
 * returns a pointer to either the caller buffer or a constant string.  This is
 * the default version on glibc systems when _GNU_SOURCE is defined.
 */
int
k5_strerror_r(int errnum, char *buf, size_t buflen)
{
    const char *str;

    str = strerror_r(errnum, buf, buflen);
    if (str != buf) {
        if (strlcpy(buf, str, buflen) >= buflen) {
            errno = ERANGE;
            return -1;
        }
    }
    return 0;
}

#else

/* Define a stub in terms of the real strerror_r, just to simplify the library
 * export list.  This shouldn't get used. */
int
k5_strerror_r(int errnum, char *buf, size_t buflen)
{
    return strerror_r(errnum, buf, buflen);
}

#endif