1*84d9c625SLionel Sambuc /* $NetBSD: strerror_r.c,v 1.3 2013/08/19 13:03:12 joerg Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1988 Regents of the University of California.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
33*84d9c625SLionel Sambuc __RCSID("$NetBSD: strerror_r.c,v 1.3 2013/08/19 13:03:12 joerg Exp $");
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include "namespace.h"
362fe8fb19SBen Gras #include <assert.h>
372fe8fb19SBen Gras #include <errno.h>
382fe8fb19SBen Gras #include <stdio.h>
392fe8fb19SBen Gras #include <string.h>
40*84d9c625SLionel Sambuc #ifdef NLS
41*84d9c625SLionel Sambuc #include <limits.h>
42*84d9c625SLionel Sambuc #include <nl_types.h>
43*84d9c625SLionel Sambuc #define __SETLOCALE_SOURCE__
44*84d9c625SLionel Sambuc #include <locale.h>
45*84d9c625SLionel Sambuc #include "setlocale_local.h"
46*84d9c625SLionel Sambuc #endif
47*84d9c625SLionel Sambuc
482fe8fb19SBen Gras #include "extern.h"
492fe8fb19SBen Gras
__weak_alias(strerror_r,_strerror_r)502fe8fb19SBen Gras __weak_alias(strerror_r, _strerror_r)
512fe8fb19SBen Gras
522fe8fb19SBen Gras int
53*84d9c625SLionel Sambuc _strerror_lr(int num, char *buf, size_t buflen, locale_t loc)
542fe8fb19SBen Gras {
552fe8fb19SBen Gras #define UPREFIX "Unknown error: %u"
562fe8fb19SBen Gras unsigned int errnum = num;
572fe8fb19SBen Gras int retval = 0;
582fe8fb19SBen Gras size_t slen;
592fe8fb19SBen Gras #ifdef NLS
602fe8fb19SBen Gras int saved_errno = errno;
612fe8fb19SBen Gras nl_catd catd;
62*84d9c625SLionel Sambuc catd = catopen_l("libc", NL_CAT_LOCALE, loc);
632fe8fb19SBen Gras #endif
642fe8fb19SBen Gras _DIAGASSERT(buf != NULL);
652fe8fb19SBen Gras
662fe8fb19SBen Gras if (errnum < (unsigned int) sys_nerr) {
672fe8fb19SBen Gras #ifdef NLS
682fe8fb19SBen Gras slen = strlcpy(buf, catgets(catd, 1, (int)errnum,
692fe8fb19SBen Gras sys_errlist[errnum]), buflen);
702fe8fb19SBen Gras #else
712fe8fb19SBen Gras slen = strlcpy(buf, sys_errlist[errnum], buflen);
722fe8fb19SBen Gras #endif
732fe8fb19SBen Gras } else {
742fe8fb19SBen Gras #ifdef NLS
75*84d9c625SLionel Sambuc slen = snprintf_l(buf, buflen, loc,
762fe8fb19SBen Gras catgets(catd, 1, 0xffff, UPREFIX), errnum);
772fe8fb19SBen Gras #else
782fe8fb19SBen Gras slen = snprintf(buf, buflen, UPREFIX, errnum);
792fe8fb19SBen Gras #endif
802fe8fb19SBen Gras retval = EINVAL;
812fe8fb19SBen Gras }
822fe8fb19SBen Gras
832fe8fb19SBen Gras if (slen >= buflen)
842fe8fb19SBen Gras retval = ERANGE;
852fe8fb19SBen Gras
862fe8fb19SBen Gras #ifdef NLS
872fe8fb19SBen Gras catclose(catd);
882fe8fb19SBen Gras errno = saved_errno;
892fe8fb19SBen Gras #endif
902fe8fb19SBen Gras
912fe8fb19SBen Gras return retval;
922fe8fb19SBen Gras }
93*84d9c625SLionel Sambuc
94*84d9c625SLionel Sambuc int
strerror_r(int num,char * buf,size_t buflen)95*84d9c625SLionel Sambuc strerror_r(int num, char *buf, size_t buflen)
96*84d9c625SLionel Sambuc {
97*84d9c625SLionel Sambuc #ifdef NLS
98*84d9c625SLionel Sambuc return _strerror_lr(num, buf, buflen, _current_locale());
99*84d9c625SLionel Sambuc #else
100*84d9c625SLionel Sambuc return _strerror_lr(num, buf, buflen, NULL);
101*84d9c625SLionel Sambuc #endif
102*84d9c625SLionel Sambuc }
103