summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorotto <>2005-05-26 12:56:01 +0000
committerotto <>2005-05-26 12:56:01 +0000
commitce5070801d547add0fa94980900e08880052ce3f (patch)
tree99309aafcf8396b5a58d7c64f7e2a59237b7d057 /src/lib
parent14e016757417d2457255bba31893dc273c0f3fb3 (diff)
downloadopenbsd-ce5070801d547add0fa94980900e08880052ce3f.tar.gz
openbsd-ce5070801d547add0fa94980900e08880052ce3f.tar.bz2
openbsd-ce5070801d547add0fa94980900e08880052ce3f.zip
Merge common functionality of __strsignal and strerror_r.
ok jaredy@ miod@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/string/Makefile.inc4
-rw-r--r--src/lib/libc/string/__strsignal.c99
-rw-r--r--src/lib/libc/string/strerror_r.c76
3 files changed, 50 insertions, 129 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
index 8150294d1c..299d7e106c 100644
--- a/src/lib/libc/string/Makefile.inc
+++ b/src/lib/libc/string/Makefile.inc
@@ -1,11 +1,11 @@
1# $OpenBSD: Makefile.inc,v 1.16 2005/04/16 12:26:12 jmc Exp $ 1# $OpenBSD: Makefile.inc,v 1.17 2005/05/26 12:56:01 otto Exp $
2 2
3# string sources 3# string sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string
5 5
6SRCS+= bm.c memccpy.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ 6SRCS+= bm.c memccpy.c strcasecmp.c strcasestr.c strcoll.c strdup.c \
7 strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \ 7 strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \
8 strxfrm.c __strsignal.c \ 8 strxfrm.c \
9 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ 9 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
10 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \ 10 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \
11 wcsstr.c wcstok.c wcswcs.c wmemchr.c wmemcmp.c wmemcpy.c \ 11 wcsstr.c wcstok.c wcswcs.c wmemchr.c wmemcmp.c wmemcpy.c \
diff --git a/src/lib/libc/string/__strsignal.c b/src/lib/libc/string/__strsignal.c
deleted file mode 100644
index 09054a9517..0000000000
--- a/src/lib/libc/string/__strsignal.c
+++ /dev/null
@@ -1,99 +0,0 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * 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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: __strsignal.c,v 1.10 2005/05/01 19:39:02 tom Exp $";
32#endif /* LIBC_SCCS and not lint */
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_siglist _sys_siglist
42
43#include <stdio.h>
44#include <limits.h>
45#include <signal.h>
46#include <string.h>
47
48static char *
49itoa(char *buffer, size_t buffer_size, unsigned int num)
50{
51 char *p = buffer + buffer_size;
52
53 *--p = '\0';
54 while (num >= 10 && p > buffer + 1) {
55 *--p = (num % 10) + '0';
56 num /= 10;
57 }
58 /* num < 10 || p == buffer + 1 */
59 *--p = (num % 10) + '0';
60 return p;
61}
62
63char *
64__strsignal(int num, char *buf)
65{
66#define UPREFIX "Unknown signal: "
67 unsigned int signum;
68
69#ifdef NLS
70 nl_catd catd ;
71 catd = catopen("libc", 0);
72#endif
73
74 signum = num; /* convert to unsigned */
75 if (signum < NSIG) {
76#ifdef NLS
77 strlcpy(buf, catgets(catd, 2, signum,
78 (char *)sys_siglist[signum]), NL_TEXTMAX);
79#else
80 return((char *)sys_siglist[signum]);
81#endif
82 } else {
83#define MAXINTDIGS 11
84 char str[MAXINTDIGS];
85
86#ifdef NLS
87 strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX);
88#else
89 strlcpy(buf, UPREFIX, NL_TEXTMAX);
90#endif
91 strlcat(buf, itoa(str, sizeof(str), signum), NL_TEXTMAX);
92 }
93
94#ifdef NLS
95 catclose(catd);
96#endif
97
98 return buf;
99}
diff --git a/src/lib/libc/string/strerror_r.c b/src/lib/libc/string/strerror_r.c
index 28bfd00a47..958a88df3e 100644
--- a/src/lib/libc/string/strerror_r.c
+++ b/src/lib/libc/string/strerror_r.c
@@ -1,8 +1,8 @@
1/* $OpenBSD: strerror_r.c,v 1.4 2005/05/08 06:25:44 otto Exp $ */ 1/* $OpenBSD: strerror_r.c,v 1.5 2005/05/26 12:56:01 otto 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.4 2005/05/08 06:25:44 otto Exp $"; 5static char *rcsid = "$OpenBSD: strerror_r.c,v 1.5 2005/05/26 12:56:01 otto Exp $";
6#endif /* LIBC_SCCS and not lint */ 6#endif /* LIBC_SCCS and not lint */
7 7
8#ifdef NLS 8#ifdef NLS
@@ -14,9 +14,11 @@ static char *rcsid = "$OpenBSD: strerror_r.c,v 1.4 2005/05/08 06:25:44 otto Exp
14 14
15#define sys_errlist _sys_errlist 15#define sys_errlist _sys_errlist
16#define sys_nerr _sys_nerr 16#define sys_nerr _sys_nerr
17#define sys_siglist _sys_siglist
17 18
18#include <errno.h> 19#include <errno.h>
19#include <limits.h> 20#include <limits.h>
21#include <signal.h>
20#include <string.h> 22#include <string.h>
21 23
22static size_t 24static size_t
@@ -33,13 +35,13 @@ __digits10(unsigned int num)
33} 35}
34 36
35static int 37static int
36__itoa(int num, char *buffer, size_t start, size_t end) 38__itoa(int num, int sign, char *buffer, size_t start, size_t end)
37{ 39{
38 size_t pos; 40 size_t pos;
39 unsigned int a; 41 unsigned int a;
40 int neg; 42 int neg;
41 43
42 if (num < 0) { 44 if (sign && num < 0) {
43 a = -num; 45 a = -num;
44 neg = 1; 46 neg = 1;
45 } 47 }
@@ -68,47 +70,39 @@ __itoa(int num, char *buffer, size_t start, size_t end)
68} 70}
69 71
70 72
71#define UPREFIX "Unknown error: " 73static int
72 74__num2string(int num, int sign, int setid, char *buf, size_t buflen,
73int 75 char * list[], size_t max, const char *def)
74strerror_r(int errnum, char *strerrbuf, size_t buflen)
75{ 76{
76 int save_errno; 77 int ret = 0;
77 int ret_errno;
78 size_t len; 78 size_t len;
79#ifdef NLS
80 nl_catd catd;
81#endif
82
83 save_errno = errno;
84 ret_errno = 0;
85 79
86#ifdef NLS 80#ifdef NLS
81 nl_catd catd;
87 catd = catopen("libc", 0); 82 catd = catopen("libc", 0);
88#endif 83#endif
89 84
90 if (errnum >= 0 && errnum < sys_nerr) { 85 if (0 <= num && num < max) {
91#ifdef NLS 86#ifdef NLS
92 len = strlcpy(strerrbuf, catgets(catd, 1, errnum, 87 len = strlcpy(buf, catgets(catd, setid, num, list[num]),
93 (char *)sys_errlist[errnum]), buflen); 88 buflen);
94#else 89#else
95 len = strlcpy(strerrbuf, sys_errlist[errnum], buflen); 90 len = strlcpy(buf, def, buflen);
96#endif 91#endif
97 if (len >= buflen) 92 if (len >= buflen)
98 ret_errno = ERANGE; 93 ret = ERANGE;
99 } else { 94 } else {
100#ifdef NLS 95#ifdef NLS
101 len = strlcpy(strerrbuf, catgets(catd, 1, 0xffff, UPREFIX), 96 len = strlcpy(buf, catgets(catd, setid, 0xffff, def), buflen);
102 buflen);
103#else 97#else
104 len = strlcpy(strerrbuf, UPREFIX, buflen); 98 len = strlcpy(buf, def, buflen);
105#endif 99#endif
106 if (len >= buflen) 100 if (len >= buflen)
107 ret_errno = ERANGE; 101 ret = ERANGE;
108 else { 102 else {
109 ret_errno = __itoa(errnum, strerrbuf, len, buflen); 103 ret = __itoa(num, sign, buf, len, buflen);
110 if (ret_errno == 0) 104 if (ret == 0)
111 ret_errno = EINVAL; 105 ret = EINVAL;
112 } 106 }
113 } 107 }
114 108
@@ -116,6 +110,32 @@ strerror_r(int errnum, char *strerrbuf, size_t buflen)
116 catclose(catd); 110 catclose(catd);
117#endif 111#endif
118 112
113 return ret;
114}
115
116#define UPREFIX "Unknown error: "
117
118int
119strerror_r(int errnum, char *strerrbuf, size_t buflen)
120{
121 int save_errno;
122 int ret_errno;
123
124 save_errno = errno;
125
126 ret_errno = __num2string(errnum, 1, 1, strerrbuf, buflen,
127 sys_errlist, sys_nerr, UPREFIX);
128
119 errno = ret_errno ? ret_errno : save_errno; 129 errno = ret_errno ? ret_errno : save_errno;
120 return (ret_errno); 130 return (ret_errno);
121} 131}
132
133#define USIGPREFIX "Unknown signal: "
134
135char *
136__strsignal(int num, char *buf)
137{
138 __num2string(num, 0, 2, buf, NL_TEXTMAX, (char **)sys_siglist, NSIG,
139 USIGPREFIX);
140 return buf;
141}