Path: blob/main/crypto/krb5/src/util/support/strerror_r.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/strerror_r.c - strerror_r compatibility shim */2/*3* Copyright (C) 2014 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132#include <k5-platform.h>33#undef strerror_r3435#if defined(_WIN32)3637/* Implement strerror_r in terms of strerror_s. */38int39k5_strerror_r(int errnum, char *buf, size_t buflen)40{41int st;4243st = strerror_s(buf, buflen, errnum);44if (st != 0) {45errno = st;46return -1;47}48return 0;49}5051#elif !defined(HAVE_STRERROR_R)5253/* Implement strerror_r in terms of strerror (not thread-safe). */54int55k5_strerror_r(int errnum, char *buf, size_t buflen)56{57if (strlcpy(buf, strerror(errnum), buflen) >= buflen) {58errno = ERANGE;59return -1;60}61return 0;62}6364#elif defined(STRERROR_R_CHAR_P)6566/*67* Implement the POSIX strerror_r API in terms of the GNU strerror_r, which68* returns a pointer to either the caller buffer or a constant string. This is69* the default version on glibc systems when _GNU_SOURCE is defined.70*/71int72k5_strerror_r(int errnum, char *buf, size_t buflen)73{74const char *str;7576str = strerror_r(errnum, buf, buflen);77if (str != buf) {78if (strlcpy(buf, str, buflen) >= buflen) {79errno = ERANGE;80return -1;81}82}83return 0;84}8586#else8788/* Define a stub in terms of the real strerror_r, just to simplify the library89* export list. This shouldn't get used. */90int91k5_strerror_r(int errnum, char *buf, size_t buflen)92{93return strerror_r(errnum, buf, buflen);94}9596#endif979899