1*0a6a1f1dSLionel Sambuc /* $NetBSD: strerror.c,v 1.17 2015/01/20 18:31:25 christos 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*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: strerror.c,v 1.17 2015/01/20 18:31:25 christos Exp $");
3484d9c625SLionel Sambuc
3584d9c625SLionel Sambuc #define __SETLOCALE_SOURCE__
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include "namespace.h"
382fe8fb19SBen Gras #include <errno.h>
3984d9c625SLionel Sambuc #include <limits.h>
4084d9c625SLionel Sambuc #include <locale.h>
4184d9c625SLionel Sambuc #include <stdlib.h>
4284d9c625SLionel Sambuc #include <string.h>
432fe8fb19SBen Gras
4484d9c625SLionel Sambuc #include "extern.h"
4584d9c625SLionel Sambuc #ifdef _REENTRANT
4684d9c625SLionel Sambuc #include "reentrant.h"
4784d9c625SLionel Sambuc #endif
4884d9c625SLionel Sambuc #include "setlocale_local.h"
4984d9c625SLionel Sambuc
__weak_alias(strerror_l,_strerror_l)5084d9c625SLionel Sambuc __weak_alias(strerror_l, _strerror_l)
512fe8fb19SBen Gras
522fe8fb19SBen Gras __aconst char *
532fe8fb19SBen Gras strerror(int num)
542fe8fb19SBen Gras {
5584d9c625SLionel Sambuc
5684d9c625SLionel Sambuc return strerror_l(num, _current_locale());
5784d9c625SLionel Sambuc }
5884d9c625SLionel Sambuc
5984d9c625SLionel Sambuc #ifdef _REENTRANT
6084d9c625SLionel Sambuc static thread_key_t strerror_key;
6184d9c625SLionel Sambuc static once_t strerror_once = ONCE_INITIALIZER;
6284d9c625SLionel Sambuc
6384d9c625SLionel Sambuc static void
strerror_setup(void)6484d9c625SLionel Sambuc strerror_setup(void)
6584d9c625SLionel Sambuc {
6684d9c625SLionel Sambuc
6784d9c625SLionel Sambuc thr_keycreate(&strerror_key, free);
6884d9c625SLionel Sambuc }
6984d9c625SLionel Sambuc #endif
7084d9c625SLionel Sambuc
7184d9c625SLionel Sambuc __aconst char *
strerror_l(int num,locale_t loc)7284d9c625SLionel Sambuc strerror_l(int num, locale_t loc)
7384d9c625SLionel Sambuc {
7484d9c625SLionel Sambuc int error;
7584d9c625SLionel Sambuc #ifdef _REENTRANT
7684d9c625SLionel Sambuc char *buf;
7784d9c625SLionel Sambuc
7884d9c625SLionel Sambuc thr_once(&strerror_once, strerror_setup);
7984d9c625SLionel Sambuc buf = thr_getspecific(strerror_key);
8084d9c625SLionel Sambuc if (buf == NULL) {
8184d9c625SLionel Sambuc buf = malloc(NL_TEXTMAX);
8284d9c625SLionel Sambuc if (buf == NULL) {
8384d9c625SLionel Sambuc static char fallback_buf[NL_TEXTMAX];
8484d9c625SLionel Sambuc buf = fallback_buf;
8584d9c625SLionel Sambuc }
8684d9c625SLionel Sambuc thr_setspecific(strerror_key, buf);
8784d9c625SLionel Sambuc }
8884d9c625SLionel Sambuc #else
892fe8fb19SBen Gras static char buf[NL_TEXTMAX];
9084d9c625SLionel Sambuc #endif
9184d9c625SLionel Sambuc
9284d9c625SLionel Sambuc error = _strerror_lr(num, buf, NL_TEXTMAX, loc);
932fe8fb19SBen Gras if (error)
942fe8fb19SBen Gras errno = error;
952fe8fb19SBen Gras return buf;
962fe8fb19SBen Gras }
97