diff options
Diffstat (limited to 'src/lib/libc/string/__strerror.c')
-rw-r--r-- | src/lib/libc/string/__strerror.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/lib/libc/string/__strerror.c b/src/lib/libc/string/__strerror.c new file mode 100644 index 0000000000..15436eaab2 --- /dev/null +++ b/src/lib/libc/string/__strerror.c | |||
@@ -0,0 +1,110 @@ | |||
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) | ||
31 | static char *rcsid = "$OpenBSD: __strerror.c,v 1.10 2003/06/02 20:18:38 millert 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_errlist _sys_errlist | ||
42 | #define sys_nerr _sys_nerr | ||
43 | |||
44 | #include <errno.h> | ||
45 | #include <limits.h> | ||
46 | #include <stdio.h> | ||
47 | #include <string.h> | ||
48 | |||
49 | static char * | ||
50 | itoa(num) | ||
51 | int num; | ||
52 | { | ||
53 | static char buffer[11]; | ||
54 | char *p; | ||
55 | |||
56 | p = buffer + 4; | ||
57 | while (num >= 10) { | ||
58 | *--p = (num % 10) + '0'; | ||
59 | num /= 10; | ||
60 | } | ||
61 | *p = (num % 10) + '0'; | ||
62 | return p; | ||
63 | } | ||
64 | |||
65 | /* | ||
66 | * Since perror() is not allowed to change the contents of strerror()'s | ||
67 | * static buffer, both functions supply their own buffers to the | ||
68 | * internal function __strerror(). | ||
69 | */ | ||
70 | |||
71 | char * | ||
72 | __strerror(num, buf) | ||
73 | int num; | ||
74 | char *buf; | ||
75 | { | ||
76 | #define UPREFIX "Unknown error: " | ||
77 | register unsigned int errnum; | ||
78 | #ifdef NLS | ||
79 | int save_errno; | ||
80 | nl_catd catd; | ||
81 | |||
82 | catd = catopen("libc", 0); | ||
83 | #endif | ||
84 | |||
85 | errnum = num; /* convert to unsigned */ | ||
86 | if (errnum < sys_nerr) { | ||
87 | #ifdef NLS | ||
88 | strlcpy(buf, catgets(catd, 1, errnum, | ||
89 | (char *)sys_errlist[errnum]), NL_TEXTMAX); | ||
90 | #else | ||
91 | return(sys_errlist[errnum]); | ||
92 | #endif | ||
93 | } else { | ||
94 | #ifdef NLS | ||
95 | strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX); | ||
96 | #else | ||
97 | strlcpy(buf, UPREFIX, NL_TEXTMAX); | ||
98 | #endif | ||
99 | strlcat(buf, itoa(errnum), NL_TEXTMAX); | ||
100 | errno = EINVAL; | ||
101 | } | ||
102 | |||
103 | #ifdef NLS | ||
104 | save_errno = errno; | ||
105 | catclose(catd); | ||
106 | errno = save_errno; | ||
107 | #endif | ||
108 | |||
109 | return buf; | ||
110 | } | ||