xref: /freebsd/crypto/krb5/src/util/support/strerror_r.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* util/support/strerror_r.c - strerror_r compatibility shim */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2014 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert #include <k5-platform.h>
34*7f2fe78bSCy Schubert #undef strerror_r
35*7f2fe78bSCy Schubert 
36*7f2fe78bSCy Schubert #if defined(_WIN32)
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert /* Implement strerror_r in terms of strerror_s. */
39*7f2fe78bSCy Schubert int
k5_strerror_r(int errnum,char * buf,size_t buflen)40*7f2fe78bSCy Schubert k5_strerror_r(int errnum, char *buf, size_t buflen)
41*7f2fe78bSCy Schubert {
42*7f2fe78bSCy Schubert     int st;
43*7f2fe78bSCy Schubert 
44*7f2fe78bSCy Schubert     st = strerror_s(buf, buflen, errnum);
45*7f2fe78bSCy Schubert     if (st != 0) {
46*7f2fe78bSCy Schubert         errno = st;
47*7f2fe78bSCy Schubert         return -1;
48*7f2fe78bSCy Schubert     }
49*7f2fe78bSCy Schubert     return 0;
50*7f2fe78bSCy Schubert }
51*7f2fe78bSCy Schubert 
52*7f2fe78bSCy Schubert #elif !defined(HAVE_STRERROR_R)
53*7f2fe78bSCy Schubert 
54*7f2fe78bSCy Schubert /* Implement strerror_r in terms of strerror (not thread-safe). */
55*7f2fe78bSCy Schubert int
k5_strerror_r(int errnum,char * buf,size_t buflen)56*7f2fe78bSCy Schubert k5_strerror_r(int errnum, char *buf, size_t buflen)
57*7f2fe78bSCy Schubert {
58*7f2fe78bSCy Schubert     if (strlcpy(buf, strerror(errnum), buflen) >= buflen) {
59*7f2fe78bSCy Schubert         errno = ERANGE;
60*7f2fe78bSCy Schubert         return -1;
61*7f2fe78bSCy Schubert     }
62*7f2fe78bSCy Schubert     return 0;
63*7f2fe78bSCy Schubert }
64*7f2fe78bSCy Schubert 
65*7f2fe78bSCy Schubert #elif defined(STRERROR_R_CHAR_P)
66*7f2fe78bSCy Schubert 
67*7f2fe78bSCy Schubert /*
68*7f2fe78bSCy Schubert  * Implement the POSIX strerror_r API in terms of the GNU strerror_r, which
69*7f2fe78bSCy Schubert  * returns a pointer to either the caller buffer or a constant string.  This is
70*7f2fe78bSCy Schubert  * the default version on glibc systems when _GNU_SOURCE is defined.
71*7f2fe78bSCy Schubert  */
72*7f2fe78bSCy Schubert int
k5_strerror_r(int errnum,char * buf,size_t buflen)73*7f2fe78bSCy Schubert k5_strerror_r(int errnum, char *buf, size_t buflen)
74*7f2fe78bSCy Schubert {
75*7f2fe78bSCy Schubert     const char *str;
76*7f2fe78bSCy Schubert 
77*7f2fe78bSCy Schubert     str = strerror_r(errnum, buf, buflen);
78*7f2fe78bSCy Schubert     if (str != buf) {
79*7f2fe78bSCy Schubert         if (strlcpy(buf, str, buflen) >= buflen) {
80*7f2fe78bSCy Schubert             errno = ERANGE;
81*7f2fe78bSCy Schubert             return -1;
82*7f2fe78bSCy Schubert         }
83*7f2fe78bSCy Schubert     }
84*7f2fe78bSCy Schubert     return 0;
85*7f2fe78bSCy Schubert }
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert #else
88*7f2fe78bSCy Schubert 
89*7f2fe78bSCy Schubert /* Define a stub in terms of the real strerror_r, just to simplify the library
90*7f2fe78bSCy Schubert  * export list.  This shouldn't get used. */
91*7f2fe78bSCy Schubert int
k5_strerror_r(int errnum,char * buf,size_t buflen)92*7f2fe78bSCy Schubert k5_strerror_r(int errnum, char *buf, size_t buflen)
93*7f2fe78bSCy Schubert {
94*7f2fe78bSCy Schubert     return strerror_r(errnum, buf, buflen);
95*7f2fe78bSCy Schubert }
96*7f2fe78bSCy Schubert 
97*7f2fe78bSCy Schubert #endif
98