Blame src/util/support/strerror_r.c

Packit fd8b60
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
Packit fd8b60
/* util/support/strerror_r.c - strerror_r compatibility shim */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (C) 2014 by the Massachusetts Institute of Technology.
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Redistribution and use in source and binary forms, with or without
Packit fd8b60
 * modification, are permitted provided that the following conditions
Packit fd8b60
 * are met:
Packit fd8b60
 *
Packit fd8b60
 * * Redistributions of source code must retain the above copyright
Packit fd8b60
 *   notice, this list of conditions and the following disclaimer.
Packit fd8b60
 *
Packit fd8b60
 * * Redistributions in binary form must reproduce the above copyright
Packit fd8b60
 *   notice, this list of conditions and the following disclaimer in
Packit fd8b60
 *   the documentation and/or other materials provided with the
Packit fd8b60
 *   distribution.
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit fd8b60
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit fd8b60
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit fd8b60
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit fd8b60
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit fd8b60
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Packit fd8b60
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit fd8b60
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit fd8b60
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit fd8b60
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit fd8b60
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit fd8b60
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include <k5-platform.h>
Packit fd8b60
#undef strerror_r
Packit fd8b60
Packit fd8b60
#if defined(_WIN32)
Packit fd8b60
Packit fd8b60
/* Implement strerror_r in terms of strerror_s. */
Packit fd8b60
int
Packit fd8b60
k5_strerror_r(int errnum, char *buf, size_t buflen)
Packit fd8b60
{
Packit fd8b60
    int st;
Packit fd8b60
Packit fd8b60
    st = strerror_s(buf, buflen, errnum);
Packit fd8b60
    if (st != 0) {
Packit fd8b60
        errno = st;
Packit fd8b60
        return -1;
Packit fd8b60
    }
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
#elif !defined(HAVE_STRERROR_R)
Packit fd8b60
Packit fd8b60
/* Implement strerror_r in terms of strerror (not thread-safe). */
Packit fd8b60
int
Packit fd8b60
k5_strerror_r(int errnum, char *buf, size_t buflen)
Packit fd8b60
{
Packit fd8b60
    if (strlcpy(buf, strerror(errnum), buflen) >= buflen) {
Packit fd8b60
        errno = ERANGE;
Packit fd8b60
        return -1;
Packit fd8b60
    }
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
#elif defined(STRERROR_R_CHAR_P)
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * Implement the POSIX strerror_r API in terms of the GNU strerror_r, which
Packit fd8b60
 * returns a pointer to either the caller buffer or a constant string.  This is
Packit fd8b60
 * the default version on glibc systems when _GNU_SOURCE is defined.
Packit fd8b60
 */
Packit fd8b60
int
Packit fd8b60
k5_strerror_r(int errnum, char *buf, size_t buflen)
Packit fd8b60
{
Packit fd8b60
    const char *str;
Packit fd8b60
Packit fd8b60
    str = strerror_r(errnum, buf, buflen);
Packit fd8b60
    if (str != buf) {
Packit fd8b60
        if (strlcpy(buf, str, buflen) >= buflen) {
Packit fd8b60
            errno = ERANGE;
Packit fd8b60
            return -1;
Packit fd8b60
        }
Packit fd8b60
    }
Packit fd8b60
    return 0;
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
#else
Packit fd8b60
Packit fd8b60
/* Define a stub in terms of the real strerror_r, just to simplify the library
Packit fd8b60
 * export list.  This shouldn't get used. */
Packit fd8b60
int
Packit fd8b60
k5_strerror_r(int errnum, char *buf, size_t buflen)
Packit fd8b60
{
Packit fd8b60
    return strerror_r(errnum, buf, buflen);
Packit fd8b60
}
Packit fd8b60
Packit fd8b60
#endif