strerror_r.c revision 256281
1290494Sbapt/*
2290494Sbapt * Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska H��gskolan
3290494Sbapt * (Royal Institute of Technology, Stockholm, Sweden).
4290494Sbapt * All rights reserved.
5127474Stjr *
6127474Stjr * Redistribution and use in source and binary forms, with or without
7127474Stjr * modification, are permitted provided that the following conditions
8290494Sbapt * are met:
9127474Stjr *
10127474Stjr * 1. Redistributions of source code must retain the above copyright
11290494Sbapt *    notice, this list of conditions and the following disclaimer.
12127474Stjr *
13127474Stjr * 2. Redistributions in binary form must reproduce the above copyright
14290494Sbapt *    notice, this list of conditions and the following disclaimer in the
15127474Stjr *    documentation and/or other materials provided with the distribution.
16127474Stjr *
17290494Sbapt * 3. Neither the name of the Institute nor the names of its contributors
18290494Sbapt *    may be used to endorse or promote products derived from this software
19290494Sbapt *    without specific prior written permission.
20290494Sbapt *
21127474Stjr * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22127474Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23290494Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24127474Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25127474Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26290494Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27127474Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28127474Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29290494Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30127474Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31127474Stjr * SUCH DAMAGE.
32290494Sbapt */
33127474Stjr
34127474Stjr#include <config.h>
35290494Sbapt
36127474Stjr#if (!defined(HAVE_STRERROR_R) && !defined(strerror_r)) || (!defined(STRERROR_R_PROTO_COMPATIBLE) && defined(HAVE_STRERROR_R))
37127474Stjr
38290494Sbapt#include <stdio.h>
39127474Stjr#include <string.h>
40127474Stjr#include <errno.h>
41290494Sbapt#include "roken.h"
42127474Stjr
43127474Stjr#ifdef _MSC_VER
44290494Sbapt
45127474Stjrint ROKEN_LIB_FUNCTION
46127474Stjrrk_strerror_r(int eno, char * strerrbuf, size_t buflen)
47290494Sbapt{
48127474Stjr    errno_t err;
49127474Stjr
50127474Stjr    err = strerror_s(strerrbuf, buflen, eno);
51    if (err != 0) {
52        int code;
53        code = sprintf_s(strerrbuf, buflen, "Error % occurred.", eno);
54        err = ((code != 0)? errno : 0);
55    }
56
57    return err;
58}
59
60#else  /* _MSC_VER */
61
62int ROKEN_LIB_FUNCTION
63rk_strerror_r(int eno, char *strerrbuf, size_t buflen)
64{
65    /* Assume is the linux broken strerror_r (returns the a buffer (char *) if the input buffer wasn't use */
66#ifdef HAVE_STRERROR_R
67    const char *str;
68    str = strerror_r(eno, strerrbuf, buflen);
69    if (str != strerrbuf)
70	if (strlcpy(strerrbuf, str, buflen) >= buflen)
71	    return ERANGE;
72    return 0;
73#else
74    int ret;
75    ret = strlcpy(strerrbuf, strerror(eno), buflen);
76    if (ret > buflen)
77	return ERANGE;
78    return 0;
79#endif
80}
81
82#endif  /* !_MSC_VER */
83
84#endif
85