xref: /freebsd/lib/libc/string/strerror.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
168cd9bedSMike Barcroft /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro 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"
48e73151ebSJilles Tjoelker 
49fba5c5faSAlexey Zelkin #define	UPREFIX		"Unknown error"
5068cd9bedSMike Barcroft 
515a98f074SMike Barcroft /*
525a98f074SMike Barcroft  * Define a buffer size big enough to describe a 64-bit signed integer
535a98f074SMike Barcroft  * converted to ASCII decimal (19 bytes), with an optional leading sign
54fba5c5faSAlexey Zelkin  * (1 byte); finally, we get the prefix, delimiter (": ") and a trailing
55fba5c5faSAlexey Zelkin  * NUL from UPREFIX.
565a98f074SMike Barcroft  */
57fba5c5faSAlexey Zelkin #define	EBUFSIZE	(20 + 2 + sizeof(UPREFIX))
585a98f074SMike Barcroft 
5968cd9bedSMike Barcroft /*
6068cd9bedSMike Barcroft  * Doing this by hand instead of linking with stdio(3) avoids bloat for
6168cd9bedSMike Barcroft  * statically linked binaries.
6268cd9bedSMike Barcroft  */
635a98f074SMike Barcroft static void
64fba5c5faSAlexey Zelkin errstr(int num, char *uprefix, char *buf, size_t len)
659c324dc0SWes Peters {
66e37f8b53SMike Barcroft 	char *t;
6768cd9bedSMike Barcroft 	unsigned int uerr;
685a98f074SMike Barcroft 	char tmp[EBUFSIZE];
6958f0484fSRodney W. Grimes 
70e37f8b53SMike Barcroft 	t = tmp + sizeof(tmp);
71e37f8b53SMike Barcroft 	*--t = '\0';
725a98f074SMike Barcroft 	uerr = (num >= 0) ? num : -num;
73f61a2edeSWes Peters 	do {
74e37f8b53SMike Barcroft 		*--t = "0123456789"[uerr % 10];
75f61a2edeSWes Peters 	} while (uerr /= 10);
76f61a2edeSWes Peters 	if (num < 0)
77e37f8b53SMike Barcroft 		*--t = '-';
78fba5c5faSAlexey Zelkin 	*--t = ' ';
79fba5c5faSAlexey Zelkin 	*--t = ':';
80fba5c5faSAlexey Zelkin 	strlcpy(buf, uprefix, len);
81d0509082SJacques Vidrine 	strlcat(buf, t, len);
825a98f074SMike Barcroft }
835a98f074SMike Barcroft 
845a98f074SMike Barcroft int
855a98f074SMike Barcroft strerror_r(int errnum, char *strerrbuf, size_t buflen)
865a98f074SMike Barcroft {
87fba5c5faSAlexey Zelkin 	int retval = 0;
88fba5c5faSAlexey Zelkin #if defined(NLS)
89fba5c5faSAlexey Zelkin 	int saved_errno = errno;
90fba5c5faSAlexey Zelkin 	nl_catd catd;
91fba5c5faSAlexey Zelkin 	catd = catopen("libc", NL_CAT_LOCALE);
92fba5c5faSAlexey Zelkin #endif
935a98f074SMike Barcroft 
94e73151ebSJilles Tjoelker 	if (errnum < 0 || errnum >= __hidden_sys_nerr) {
95fba5c5faSAlexey Zelkin 		errstr(errnum,
96fba5c5faSAlexey Zelkin #if defined(NLS)
97fba5c5faSAlexey Zelkin 			catgets(catd, 1, 0xffff, UPREFIX),
98fba5c5faSAlexey Zelkin #else
99fba5c5faSAlexey Zelkin 			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 
1225a98f074SMike Barcroft char *
1235a98f074SMike Barcroft strerror(int num)
1245a98f074SMike Barcroft {
125fba5c5faSAlexey Zelkin 	static char ebuf[NL_TEXTMAX];
1265a98f074SMike Barcroft 
127fba5c5faSAlexey Zelkin 	if (strerror_r(num, ebuf, sizeof(ebuf)) != 0)
1285a98f074SMike Barcroft 		errno = EINVAL;
129f61a2edeSWes Peters 	return (ebuf);
13058f0484fSRodney W. Grimes }
131