summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libc/string/__strerror.c105
-rw-r--r--src/lib/libc/string/strerror.c14
-rw-r--r--src/lib/libc/string/strerror_r.c110
3 files changed, 109 insertions, 120 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}
diff --git a/src/lib/libc/string/strerror.c b/src/lib/libc/string/strerror.c
index 6496f50afd..edb6af7386 100644
--- a/src/lib/libc/string/strerror.c
+++ b/src/lib/libc/string/strerror.c
@@ -28,23 +28,17 @@
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.5 2003/06/11 21:08:16 deraadt Exp $"; 31static char *rcsid = "$OpenBSD: strerror.c,v 1.6 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#include <string.h> 34#include <string.h>
35#include <limits.h> 35#include <limits.h>
36 36
37/*
38 * Since perror() is not allowed to change the contents of strerror()'s
39 * static buffer, both functions supply their own buffers to the
40 * internal function __strerror().
41 */
42
43extern char *__strerror(int, char *);
44
45char * 37char *
46strerror(int num) 38strerror(int num)
47{ 39{
48 static char buf[NL_TEXTMAX]; 40 static char buf[NL_TEXTMAX];
49 return __strerror(num, buf); 41
42 (void)strerror_r(num, buf, sizeof(buf));
43 return (buf);
50} 44}
diff --git a/src/lib/libc/string/strerror_r.c b/src/lib/libc/string/strerror_r.c
index aab6db5303..db264bcf50 100644
--- a/src/lib/libc/string/strerror_r.c
+++ b/src/lib/libc/string/strerror_r.c
@@ -1,30 +1,120 @@
1/* $OpenBSD: strerror_r.c,v 1.1 2002/11/21 20:45:05 marc Exp $ */ 1/* $OpenBSD: strerror_r.c,v 1.2 2004/05/03 05:07:34 espie Exp $ */
2/* Public Domain <marc@snafu.org> */ 2/* Public Domain <marc@snafu.org> */
3 3
4#if defined(LIBC_SCCS) && !defined(lint) 4#if defined(LIBC_SCCS) && !defined(lint)
5static char *rcsid = "$OpenBSD: strerror_r.c,v 1.1 2002/11/21 20:45:05 marc Exp $"; 5static char *rcsid = "$OpenBSD: strerror_r.c,v 1.2 2004/05/03 05:07:34 espie Exp $";
6#endif /* LIBC_SCCS and not lint */ 6#endif /* LIBC_SCCS and not lint */
7 7
8#ifdef NLS
9#define catclose _catclose
10#define catgets _catgets
11#define catopen _catopen
12#include <nl_types.h>
13#endif
14
15#define sys_errlist _sys_errlist
16#define sys_nerr _sys_nerr
17
8#include <errno.h> 18#include <errno.h>
9#include <limits.h> 19#include <limits.h>
10#include <string.h> 20#include <string.h>
11 21
12extern char *__strerror(int, char *); 22static size_t
23__digits10(unsigned int num)
24{
25 size_t i = 0;
26
27 do {
28 num /= 10;
29 i++;
30 } while (num != 0);
31
32 return i;
33}
34
35static void
36__itoa(int num, char *buffer, size_t start, size_t end)
37{
38 size_t pos;
39 unsigned int a;
40 int neg;
41
42 if (num < 0) {
43 a = -num;
44 neg = 1;
45 }
46 else {
47 a = num;
48 neg = 0;
49 }
50
51 pos = start + __digits10(a);
52 if (neg)
53 pos++;
54
55 if (pos < end)
56 buffer[pos] = '\0';
57 else {
58 if (end)
59 buffer[--end] = '\0'; /* XXX */
60 }
61 pos--;
62 do {
63
64 if (pos < end)
65 buffer[pos] = (a % 10) + '0';
66 pos--;
67 a /= 10;
68 } while (a != 0);
69 if (neg)
70 if (pos < end)
71 buffer[pos] = '-';
72}
73
74
75#define UPREFIX "Unknown error: "
13 76
14int 77int
15strerror_r(int errnum, char *strerrbuf, size_t buflen) 78strerror_r(int errnum, char *strerrbuf, size_t buflen)
16{ 79{
17 int save_errno; 80 int save_errno;
18 int ret_errno; 81 int ret_errno;
19 char buf[NL_TEXTMAX]; 82 size_t len;
83#ifdef NLS
84 nl_catd catd;
85#endif
20 86
21 save_errno = errno; 87 save_errno = errno;
22 errno = 0; 88 ret_errno = 0;
23 __strerror(errnum, buf); 89
24 if (strlcpy(strerrbuf, buf, buflen) >= buflen) 90#ifdef NLS
25 errno = ERANGE; 91 catd = catopen("libc", 0);
26 ret_errno = errno; 92#endif
27 errno = save_errno; 93
94 if (errnum >= 0 && errnum < sys_nerr) {
95#ifdef NLS
96 len = strlcpy(strerrbuf, catgets(catd, 1, errnum,
97 (char *)sys_errlist[errnum]), buflen);
98#else
99 len = strlcpy(strerrbuf, sys_errlist[errnum], buflen);
100#endif
101 if (len >= buflen)
102 ret_errno = ERANGE;
103 } else {
104#ifdef NLS
105 len = strlcpy(strerrbuf, catgets(catd, 1, 0xffff, UPREFIX),
106 buflen);
107#else
108 len = strlcpy(strerrbuf, UPREFIX, buflen);
109#endif
110 __itoa(errnum, strerrbuf, len, buflen);
111 ret_errno = EINVAL;
112 }
113
114#ifdef NLS
115 catclose(catd);
116#endif
28 117
118 errno = ret_errno ? ret_errno : save_errno;
29 return (ret_errno); 119 return (ret_errno);
30} 120}