xref: /freebsd/lib/libc/string/strerror.c (revision 8fa113e5fc65fe6abc757f0089f477a87ee4d185)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)strerror.c	8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <stdio.h>
41 #include <string.h>
42 #include <errno.h>
43 
44 
45 int
46 strerror_r(int errnum, char *strerrbuf, size_t buflen)
47 {
48 #define	UPREFIX	"Unknown error: "
49 	unsigned int uerr;
50 	char *p, *t;
51 	char tmp[40];				/* 64-bit number + slop */
52 	int len;
53 
54 	uerr = errnum;				/* convert to unsigned */
55 	if (uerr < sys_nerr) {
56 		len = strlcpy(strerrbuf, (char *)sys_errlist[uerr], buflen);
57 		return (len <= buflen) ? 0 : ERANGE;
58 	}
59 
60 	/* Print unknown errno by hand so we don't link to stdio(3). */
61 	t = tmp;
62 	if (errnum < 0)
63 		uerr = -uerr;
64 	do {
65 		*t++ = "0123456789"[uerr % 10];
66 	} while (uerr /= 10);
67 
68 	if (errnum < 0)
69 		*t++ = '-';
70 
71 	strlcpy(strerrbuf, UPREFIX, buflen);
72 	for (p = strerrbuf + sizeof(UPREFIX) - 1; p < strerrbuf + buflen; ) {
73 		*p++ = *--t;
74 		if (t <= tmp)
75 			break;
76 	}
77 
78 	if (p < strerrbuf + buflen) {
79 		*p = '\0';
80 		return 0;
81 	}
82 
83 	return ERANGE;
84 }
85 
86 
87 /*
88  * NOTE: the following length should be enough to hold the longest defined
89  * error message in sys_errlist, defined in ../gen/errlst.c.  This is a WAG
90  * that is better than the previous value.
91  */
92 #define ERR_LEN 64
93 
94 char *
95 strerror(num)
96 	int num;
97 {
98 	unsigned int uerr;
99 	static char ebuf[ERR_LEN];
100 
101 	uerr = num;				/* convert to unsigned */
102 	if (uerr < sys_nerr)
103 		return (char *)sys_errlist[uerr];
104 
105 	/* strerror can't fail so handle truncation semi-elegantly */
106 	if (strerror_r(num, ebuf, (size_t) ERR_LEN) != 0)
107 	    ebuf[ERR_LEN - 1] = '\0';
108 
109 	return ebuf;
110 }
111 
112 
113 #ifdef STANDALONE_TEST
114 main()
115 {
116 	char mybuf[64];
117 	int ret;
118 
119 	printf("strerror(47) yeilds: %s\n", strerror(47));
120 	strerror_r(11, mybuf, 64);
121 	printf("strerror_r(11) yeilds: %s\n", mybuf);
122 	strerror_r(1234, mybuf, 64);
123 	printf("strerror_r(1234) yeilds: %s\n", mybuf);
124 	memset(mybuf, '*', 63);
125 	ret = strerror_r(4321, mybuf, 16);
126 	printf("strerror_r on short buffer returns %d (%s)\n", ret, mybuf);
127 }
128 #endif
129