summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/__strerror.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/__strerror.c')
-rw-r--r--src/lib/libc/string/__strerror.c105
1 files changed, 5 insertions, 100 deletions
diff --git a/src/lib/libc/string/__strerror.c b/src/lib/libc/string/__strerror.c
index 87b47b68e4..4512c0bea0 100644
--- a/src/lib/libc/string/__strerror.c
+++ b/src/lib/libc/string/__strerror.c
@@ -28,114 +28,19 @@
28 */ 28 */
29 29
30#if defined(LIBC_SCCS) && !defined(lint) 30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: __strerror.c,v 1.12 2004/05/01 10:52:59 espie Exp $"; 31static char *rcsid = "$OpenBSD: __strerror.c,v 1.13 2004/05/03 05:07:34 espie Exp $";
32#endif /* LIBC_SCCS and not lint */ 32#endif /* LIBC_SCCS and not lint */
33 33
34#ifdef NLS
35#define catclose _catclose
36#define catgets _catgets
37#define catopen _catopen
38#include <nl_types.h>
39#endif
40
41#define sys_errlist _sys_errlist
42#define sys_nerr _sys_nerr
43
44#include <errno.h>
45#include <limits.h> 34#include <limits.h>
46#include <stdio.h>
47#include <string.h> 35#include <string.h>
48 36
49static size_t
50__digits10(unsigned int num)
51{
52 size_t i = 0;
53
54 do {
55 num /= 10;
56 i++;
57 } while (num != 0);
58
59 return i;
60}
61
62void
63__itoa(int num, char *buffer, size_t start, size_t end)
64{
65 size_t pos;
66 unsigned int a;
67 int neg;
68
69 if (num < 0) {
70 a = -num;
71 neg = 1;
72 }
73 else {
74 a = num;
75 neg = 0;
76 }
77
78 pos = start + __digits10(a);
79 if (neg)
80 pos++;
81
82 if (pos < end)
83 buffer[pos] = '\0';
84 else
85 buffer[end-1] = '\0';
86 pos--;
87 do {
88
89 if (pos < end)
90 buffer[pos] = (a % 10) + '0';
91 pos--;
92 a /= 10;
93 } while (a != 0);
94 if (neg)
95 if (pos < end)
96 buffer[pos] = '-';
97}
98
99/* 37/*
100 * Since perror() is not allowed to change the contents of strerror()'s 38 * __strerror() has been deprecated in favor of strerror_r()
101 * static buffer, both functions supply their own buffers to the 39 * and is provided for source compatibility only.
102 * internal function __strerror().
103 */ 40 */
104
105char * 41char *
106__strerror(int num, char *buf) 42__strerror(int num, char *buf)
107{ 43{
108#define UPREFIX "Unknown error: " 44 (void)strerror_r(num, buf, NL_TEXTMAX);
109 int len; 45 return (buf);
110#ifdef NLS
111 int save_errno;
112 nl_catd catd;
113
114 catd = catopen("libc", 0);
115#endif
116
117 if (num >= 0 && num < sys_nerr) {
118#ifdef NLS
119 strlcpy(buf, catgets(catd, 1, num,
120 (char *)sys_errlist[num]), NL_TEXTMAX);
121#else
122 return(sys_errlist[num]);
123#endif
124 } else {
125#ifdef NLS
126 len = strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX);
127#else
128 len = strlcpy(buf, UPREFIX, NL_TEXTMAX);
129#endif
130 __itoa(num, buf, len, NL_TEXTMAX);
131 errno = EINVAL;
132 }
133
134#ifdef NLS
135 save_errno = errno;
136 catclose(catd);
137 errno = save_errno;
138#endif
139
140 return buf;
141} 46}