strerror.c - vx32 - Local 9vx git repository for patches.
(HTM) git clone git://r-36.net/vx32
(DIR) Log
(DIR) Files
(DIR) Refs
---
strerror.c (986B)
---
1 #include <errno.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #define UPREFIX "Unknown error: "
6
7 /*
8 * Define a buffer size big enough to describe a 64-bit signed integer
9 * converted to ASCII decimal (19 bytes), with an optional leading sign
10 * (1 byte); finally, we get the prefix and a trailing NUL from UPREFIX.
11 */
12 #define EBUFSIZE (20 + sizeof(UPREFIX))
13
14 /*
15 * Doing this by hand instead of linking with stdio(3) avoids bloat for
16 * statically linked binaries.
17 */
18 static void
19 errstr(int num, char *buf, size_t len)
20 {
21 char *t;
22 unsigned int uerr;
23 char tmp[EBUFSIZE];
24
25 t = tmp + sizeof(tmp);
26 *--t = '\0';
27 uerr = (num >= 0) ? num : -num;
28 do {
29 *--t = "0123456789"[uerr % 10];
30 } while (uerr /= 10);
31 if (num < 0)
32 *--t = '-';
33 strlcpy(buf, UPREFIX, len);
34 strlcat(buf, t, len);
35 }
36
37 char *
38 strerror(int num)
39 {
40 static char ebuf[EBUFSIZE];
41
42 if (num > 0 && num < sys_nerr)
43 return ((char *)sys_errlist[num]);
44 errno = EINVAL;
45 errstr(num, ebuf, sizeof(ebuf));
46 return (ebuf);
47 }