Make strerror(3) and strsignal(3) thread-safe, and add psignal(3) and psiginfo(3).
Change-Id: I426109db25e907980d6cb3a7a695796e45783b78
This commit is contained in:
committed by
Elliott Hughes
parent
4a9b7c694a
commit
b5f053b5a7
@@ -1,41 +0,0 @@
|
||||
/* $OpenBSD: strerror.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */
|
||||
/*
|
||||
* Copyright (c) 1988 Regents of the University of California.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
char *
|
||||
strerror(int num)
|
||||
{
|
||||
static char buf[256];
|
||||
|
||||
(void)strerror_r(num, buf, sizeof(buf));
|
||||
return (buf);
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
/* $OpenBSD: strerror_r.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */
|
||||
/* Public Domain <marc@snafu.org> */
|
||||
|
||||
#define sys_siglist _sys_siglist
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int code;
|
||||
const char* msg;
|
||||
} CodeString;
|
||||
|
||||
static const char*
|
||||
__code_string_lookup( const CodeString* strings,
|
||||
int code )
|
||||
{
|
||||
int nn = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (strings[nn].msg == NULL)
|
||||
break;
|
||||
|
||||
if (strings[nn].code == code)
|
||||
return strings[nn].msg;
|
||||
|
||||
nn++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const CodeString _sys_error_strings[] =
|
||||
{
|
||||
#define __BIONIC_ERRDEF(x,y,z) { x, z },
|
||||
#include <sys/_errdefs.h>
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
static size_t
|
||||
__digits10(unsigned int num)
|
||||
{
|
||||
size_t i = 0;
|
||||
|
||||
do {
|
||||
num /= 10;
|
||||
i++;
|
||||
} while (num != 0);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
static int
|
||||
__itoa(int num, int sign, char *buffer, size_t start, size_t end)
|
||||
{
|
||||
size_t pos;
|
||||
unsigned int a;
|
||||
int neg;
|
||||
|
||||
if (sign && num < 0) {
|
||||
a = -num;
|
||||
neg = 1;
|
||||
}
|
||||
else {
|
||||
a = num;
|
||||
neg = 0;
|
||||
}
|
||||
|
||||
pos = start + __digits10(a);
|
||||
if (neg)
|
||||
pos++;
|
||||
|
||||
if (pos < end)
|
||||
buffer[pos] = '\0';
|
||||
else
|
||||
return ERANGE;
|
||||
pos--;
|
||||
do {
|
||||
buffer[pos] = (a % 10) + '0';
|
||||
pos--;
|
||||
a /= 10;
|
||||
} while (a != 0);
|
||||
if (neg)
|
||||
buffer[pos] = '-';
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
strerror_r(int errnum, char *strerrbuf, size_t buflen)
|
||||
{
|
||||
int save_errno;
|
||||
int len, ret = 0;
|
||||
const char* msg;
|
||||
|
||||
save_errno = errno;
|
||||
msg = __code_string_lookup( _sys_error_strings, errnum );
|
||||
if (msg != NULL) {
|
||||
len = strlcpy(strerrbuf, msg, buflen);
|
||||
if ((size_t)len >= buflen)
|
||||
ret = ERANGE;
|
||||
} else {
|
||||
len = strlcpy(strerrbuf, "Unknown error: ", buflen);
|
||||
if ((size_t)len >= buflen)
|
||||
ret = ERANGE;
|
||||
else {
|
||||
int ret = __itoa(errnum, 1, strerrbuf, len, buflen);
|
||||
|
||||
if (ret == 0)
|
||||
ret = EINVAL;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static const CodeString _sys_signal_strings[] =
|
||||
{
|
||||
#define SIGDEF(x,y,z) { y, z },
|
||||
#include <sys/_sigdefs.h>
|
||||
};
|
||||
|
||||
|
||||
static int
|
||||
__num2string(int num, int sign, int setid, char *buf, size_t buflen,
|
||||
char * list[], size_t max, const char *def)
|
||||
{
|
||||
int ret = 0;
|
||||
size_t len;
|
||||
|
||||
if (0 <= num && num < max) {
|
||||
len = strlcpy(buf, def, buflen);
|
||||
if (len >= buflen)
|
||||
ret = ERANGE;
|
||||
} else {
|
||||
len = strlcpy(buf, def, buflen);
|
||||
if (len >= buflen)
|
||||
ret = ERANGE;
|
||||
else {
|
||||
ret = __itoa(num, sign, buf, len, buflen);
|
||||
if (ret == 0)
|
||||
ret = EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define USIGPREFIX "Unknown signal: "
|
||||
|
||||
char *
|
||||
__strsignal(int num, char *buf)
|
||||
{
|
||||
__num2string(num, 0, 2, buf, NL_TEXTMAX, (char **)sys_siglist, NSIG,
|
||||
USIGPREFIX);
|
||||
return buf;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user