diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libc/string/__strerror.c | 74 |
1 files changed, 52 insertions, 22 deletions
diff --git a/src/lib/libc/string/__strerror.c b/src/lib/libc/string/__strerror.c index 15436eaab2..ccb430247e 100644 --- a/src/lib/libc/string/__strerror.c +++ b/src/lib/libc/string/__strerror.c | |||
| @@ -28,7 +28,7 @@ | |||
| 28 | */ | 28 | */ |
| 29 | 29 | ||
| 30 | #if defined(LIBC_SCCS) && !defined(lint) | 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 $"; | 31 | static char *rcsid = "$OpenBSD: __strerror.c,v 1.11 2004/04/30 17:13:02 espie Exp $"; |
| 32 | #endif /* LIBC_SCCS and not lint */ | 32 | #endif /* LIBC_SCCS and not lint */ |
| 33 | 33 | ||
| 34 | #ifdef NLS | 34 | #ifdef NLS |
| @@ -46,20 +46,52 @@ static char *rcsid = "$OpenBSD: __strerror.c,v 1.10 2003/06/02 20:18:38 millert | |||
| 46 | #include <stdio.h> | 46 | #include <stdio.h> |
| 47 | #include <string.h> | 47 | #include <string.h> |
| 48 | 48 | ||
| 49 | static 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 | |||
| 49 | static char * | 62 | static char * |
| 50 | itoa(num) | 63 | __itoa(int num, char *buffer, size_t maxlen) |
| 51 | int num; | ||
| 52 | { | 64 | { |
| 53 | static char buffer[11]; | ||
| 54 | char *p; | 65 | char *p; |
| 66 | size_t len; | ||
| 67 | unsigned int a; | ||
| 68 | int neg; | ||
| 55 | 69 | ||
| 56 | p = buffer + 4; | 70 | if (num < 0) { |
| 57 | while (num >= 10) { | 71 | a = -num; |
| 58 | *--p = (num % 10) + '0'; | 72 | neg = 1; |
| 59 | num /= 10; | 73 | } |
| 74 | else { | ||
| 75 | a = num; | ||
| 76 | neg = 0; | ||
| 60 | } | 77 | } |
| 61 | *p = (num % 10) + '0'; | 78 | |
| 62 | return p; | 79 | len = __digits10(a); |
| 80 | if (neg) | ||
| 81 | len++; | ||
| 82 | |||
| 83 | if (len >= maxlen) | ||
| 84 | return NULL; | ||
| 85 | |||
| 86 | buffer[len--] = '\0'; | ||
| 87 | do { | ||
| 88 | buffer[len--] = (a % 10) + '0'; | ||
| 89 | a /= 10; | ||
| 90 | } while (a != 0); | ||
| 91 | if (neg) | ||
| 92 | *buffer = '-'; | ||
| 93 | |||
| 94 | return buffer; | ||
| 63 | } | 95 | } |
| 64 | 96 | ||
| 65 | /* | 97 | /* |
| @@ -69,12 +101,10 @@ itoa(num) | |||
| 69 | */ | 101 | */ |
| 70 | 102 | ||
| 71 | char * | 103 | char * |
| 72 | __strerror(num, buf) | 104 | __strerror(int num, char *buf) |
| 73 | int num; | ||
| 74 | char *buf; | ||
| 75 | { | 105 | { |
| 76 | #define UPREFIX "Unknown error: " | 106 | #define UPREFIX "Unknown error: " |
| 77 | register unsigned int errnum; | 107 | int len; |
| 78 | #ifdef NLS | 108 | #ifdef NLS |
| 79 | int save_errno; | 109 | int save_errno; |
| 80 | nl_catd catd; | 110 | nl_catd catd; |
| @@ -82,21 +112,21 @@ __strerror(num, buf) | |||
| 82 | catd = catopen("libc", 0); | 112 | catd = catopen("libc", 0); |
| 83 | #endif | 113 | #endif |
| 84 | 114 | ||
| 85 | errnum = num; /* convert to unsigned */ | 115 | if (num >= 0 && num < sys_nerr) { |
| 86 | if (errnum < sys_nerr) { | ||
| 87 | #ifdef NLS | 116 | #ifdef NLS |
| 88 | strlcpy(buf, catgets(catd, 1, errnum, | 117 | strlcpy(buf, catgets(catd, 1, num, |
| 89 | (char *)sys_errlist[errnum]), NL_TEXTMAX); | 118 | (char *)sys_errlist[num]), NL_TEXTMAX); |
| 90 | #else | 119 | #else |
| 91 | return(sys_errlist[errnum]); | 120 | return(sys_errlist[num]); |
| 92 | #endif | 121 | #endif |
| 93 | } else { | 122 | } else { |
| 94 | #ifdef NLS | 123 | #ifdef NLS |
| 95 | strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX); | 124 | len = strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX); |
| 96 | #else | 125 | #else |
| 97 | strlcpy(buf, UPREFIX, NL_TEXTMAX); | 126 | len = strlcpy(buf, UPREFIX, NL_TEXTMAX); |
| 98 | #endif | 127 | #endif |
| 99 | strlcat(buf, itoa(errnum), NL_TEXTMAX); | 128 | if (len < NL_TEXTMAX) |
| 129 | __itoa(num, buf + len, NL_TEXTMAX - len); | ||
| 100 | errno = EINVAL; | 130 | errno = EINVAL; |
| 101 | } | 131 | } |
| 102 | 132 | ||
