xref: /freebsd/lib/libc/string/strerror.c (revision 675079b1ea61b310f3a42cb0d352a49c1780f89a)
168cd9bedSMike Barcroft /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1988, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
153fb3b97cSEd Maste  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
3258f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3358f0484fSRodney W. Grimes static char sccsid[] = "@(#)strerror.c	8.1 (Berkeley) 6/4/93";
3458f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
35de5fe5d5SDavid E. O'Brien #include <sys/cdefs.h>
36de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$");
3758f0484fSRodney W. Grimes 
38fba5c5faSAlexey Zelkin #if defined(NLS)
39fba5c5faSAlexey Zelkin #include <nl_types.h>
40fba5c5faSAlexey Zelkin #endif
419c324dc0SWes Peters 
42fc7c3528SAlexey Zelkin #include <limits.h>
43fba5c5faSAlexey Zelkin #include <errno.h>
44fba5c5faSAlexey Zelkin #include <string.h>
45fba5c5faSAlexey Zelkin #include <stdio.h>
46fba5c5faSAlexey Zelkin 
47e73151ebSJilles Tjoelker #include "errlst.h"
48*675079b1SKonstantin Belousov #include "../locale/xlocale_private.h"
49*675079b1SKonstantin Belousov #include "libc_private.h"
50e73151ebSJilles Tjoelker 
515a98f074SMike Barcroft /*
52cd49e866SKonstantin Belousov  * Define buffer big enough to contain delimiter (": ", 2 bytes),
53cd49e866SKonstantin Belousov  * 64-bit signed integer converted to ASCII decimal (19 bytes) with
54cd49e866SKonstantin Belousov  * optional leading sign (1 byte), and a trailing NUL.
555a98f074SMike Barcroft  */
56cd49e866SKonstantin Belousov #define	EBUFSIZE	(2 + 19 + 1 + 1)
575a98f074SMike Barcroft 
5868cd9bedSMike Barcroft /*
5968cd9bedSMike Barcroft  * Doing this by hand instead of linking with stdio(3) avoids bloat for
6068cd9bedSMike Barcroft  * statically linked binaries.
6168cd9bedSMike Barcroft  */
625a98f074SMike Barcroft static void
63311a1725SKonstantin Belousov errstr(int num, const char *uprefix, char *buf, size_t len)
649c324dc0SWes Peters {
65e37f8b53SMike Barcroft 	char *t;
6668cd9bedSMike Barcroft 	unsigned int uerr;
675a98f074SMike Barcroft 	char tmp[EBUFSIZE];
6858f0484fSRodney W. Grimes 
69e37f8b53SMike Barcroft 	t = tmp + sizeof(tmp);
70e37f8b53SMike Barcroft 	*--t = '\0';
715a98f074SMike Barcroft 	uerr = (num >= 0) ? num : -num;
72f61a2edeSWes Peters 	do {
73e37f8b53SMike Barcroft 		*--t = "0123456789"[uerr % 10];
74f61a2edeSWes Peters 	} while (uerr /= 10);
75f61a2edeSWes Peters 	if (num < 0)
76e37f8b53SMike Barcroft 		*--t = '-';
77fba5c5faSAlexey Zelkin 	*--t = ' ';
78fba5c5faSAlexey Zelkin 	*--t = ':';
79fba5c5faSAlexey Zelkin 	strlcpy(buf, uprefix, len);
80d0509082SJacques Vidrine 	strlcat(buf, t, len);
815a98f074SMike Barcroft }
825a98f074SMike Barcroft 
83*675079b1SKonstantin Belousov static int
84*675079b1SKonstantin Belousov strerror_rl(int errnum, char *strerrbuf, size_t buflen, locale_t locale)
855a98f074SMike Barcroft {
86fba5c5faSAlexey Zelkin 	int retval = 0;
87fba5c5faSAlexey Zelkin #if defined(NLS)
88fba5c5faSAlexey Zelkin 	int saved_errno = errno;
89fba5c5faSAlexey Zelkin 	nl_catd catd;
90*675079b1SKonstantin Belousov 
91*675079b1SKonstantin Belousov 	catd = __catopen_l("libc", NL_CAT_LOCALE, locale);
92fba5c5faSAlexey Zelkin #endif
935a98f074SMike Barcroft 
94e73151ebSJilles Tjoelker 	if (errnum < 0 || errnum >= __hidden_sys_nerr) {
95fba5c5faSAlexey Zelkin 		errstr(errnum,
96fba5c5faSAlexey Zelkin #if defined(NLS)
97cd49e866SKonstantin Belousov 		    catgets(catd, 1, 0xffff, __uprefix),
98fba5c5faSAlexey Zelkin #else
99cd49e866SKonstantin Belousov 		    __uprefix,
100fba5c5faSAlexey Zelkin #endif
101fba5c5faSAlexey Zelkin 		   strerrbuf, buflen);
102fba5c5faSAlexey Zelkin 		retval = EINVAL;
103fba5c5faSAlexey Zelkin 	} else {
104fba5c5faSAlexey Zelkin 		if (strlcpy(strerrbuf,
105fba5c5faSAlexey Zelkin #if defined(NLS)
106e73151ebSJilles Tjoelker 		    catgets(catd, 1, errnum, __hidden_sys_errlist[errnum]),
107fba5c5faSAlexey Zelkin #else
108e73151ebSJilles Tjoelker 		    __hidden_sys_errlist[errnum],
109fba5c5faSAlexey Zelkin #endif
110fba5c5faSAlexey Zelkin 		    buflen) >= buflen)
111fba5c5faSAlexey Zelkin 			retval = ERANGE;
11268cd9bedSMike Barcroft 	}
113fba5c5faSAlexey Zelkin 
114fba5c5faSAlexey Zelkin #if defined(NLS)
115fba5c5faSAlexey Zelkin 	catclose(catd);
116fba5c5faSAlexey Zelkin 	errno = saved_errno;
117fba5c5faSAlexey Zelkin #endif
118fba5c5faSAlexey Zelkin 
119fba5c5faSAlexey Zelkin 	return (retval);
1205a98f074SMike Barcroft }
1215a98f074SMike Barcroft 
122*675079b1SKonstantin Belousov int
123*675079b1SKonstantin Belousov strerror_r(int errnum, char *strerrbuf, size_t buflen)
124*675079b1SKonstantin Belousov {
125*675079b1SKonstantin Belousov 	return (strerror_rl(errnum, strerrbuf, buflen, __get_locale()));
126*675079b1SKonstantin Belousov }
127*675079b1SKonstantin Belousov 
128*675079b1SKonstantin Belousov char *
129*675079b1SKonstantin Belousov strerror_l(int num, locale_t locale)
130*675079b1SKonstantin Belousov {
131*675079b1SKonstantin Belousov #ifndef __NO_TLS
132*675079b1SKonstantin Belousov 	static _Thread_local char ebuf[NL_TEXTMAX];
133*675079b1SKonstantin Belousov 
134*675079b1SKonstantin Belousov 	if (strerror_rl(num, ebuf, sizeof(ebuf), locale) != 0)
135*675079b1SKonstantin Belousov 		errno = EINVAL;
136*675079b1SKonstantin Belousov 	return (ebuf);
137*675079b1SKonstantin Belousov #else
138*675079b1SKonstantin Belousov 	errno = ENOTSUP;
139*675079b1SKonstantin Belousov 	return (NULL);
140*675079b1SKonstantin Belousov #endif
141*675079b1SKonstantin Belousov }
142*675079b1SKonstantin Belousov 
1435a98f074SMike Barcroft char *
1445a98f074SMike Barcroft strerror(int num)
1455a98f074SMike Barcroft {
146fba5c5faSAlexey Zelkin 	static char ebuf[NL_TEXTMAX];
1475a98f074SMike Barcroft 
148*675079b1SKonstantin Belousov 	if (strerror_rl(num, ebuf, sizeof(ebuf), __get_locale()) != 0)
1495a98f074SMike Barcroft 		errno = EINVAL;
150f61a2edeSWes Peters 	return (ebuf);
15158f0484fSRodney W. Grimes }
152