From e44f85132083f5f9b46af1643e701e29218b5185 Mon Sep 17 00:00:00 2001 From: marc <> Date: Thu, 21 Nov 2002 20:45:05 +0000 Subject: [PATCH] Add strerror_r and functions versions of getchar_unlocked and putchar_unlocked. Crank the minor on related libs. OK fgs@, deraadt@ --- src/lib/libc/string/Makefile.inc | 4 ++-- src/lib/libc/string/strerror.3 | 22 +++++++++++++++++++++- src/lib/libc/string/strerror_r.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/lib/libc/string/strerror_r.c diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc index e7b81d0c..3cd17228 100644 --- a/src/lib/libc/string/Makefile.inc +++ b/src/lib/libc/string/Makefile.inc @@ -1,10 +1,10 @@ -# $OpenBSD: Makefile.inc,v 1.10 2001/09/05 16:27:01 mickey Exp $ +# $OpenBSD: Makefile.inc,v 1.11 2002/11/21 20:45:05 marc Exp $ # string sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \ - strlcat.c strmode.c strsignal.c strtok.c strxfrm.c \ + strerror_r.c strlcat.c strmode.c strsignal.c strtok.c strxfrm.c \ __strerror.c __strsignal.c # machine-dependent net sources diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3 index 11bacd31..05cb7e9d 100644 --- a/src/lib/libc/string/strerror.3 +++ b/src/lib/libc/string/strerror.3 @@ -33,7 +33,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: strerror.3,v 1.4 2000/10/23 19:14:41 aaron Exp $ +.\" $OpenBSD: strerror.3,v 1.5 2002/11/21 20:45:05 marc Exp $ .\" .Dd June 29, 1991 .Dt STRERROR 3 @@ -45,6 +45,8 @@ .Fd #include .Ft char * .Fn strerror "int errnum" +.Ft int +.Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen" .Sh DESCRIPTION The .Fn strerror @@ -58,6 +60,20 @@ characters, including the trailing NUL. The array pointed to is not to be modified by the program, but may be overwritten by subsequent calls to .Fn strerror . +.Pp +.Fn strerror_r +is a thread safe version of +.Fn strerror +that places the error message in the given buffer +.Fa strerrbuf . +If the error message is larger then +.Fa buflen +the message will be truncated to fit within buflen and +.Er ERANGE +is returned. +.Fn strerror_r +returns zero upon successful completion. +An error number is returned, otherwise. .Sh SEE ALSO .Xr intro 2 , .Xr perror 3 , @@ -67,3 +83,7 @@ The .Fn strerror function conforms to .St -ansiC . +The +.Fn strerror_r +function conforms to +.St -p1003.1 . diff --git a/src/lib/libc/string/strerror_r.c b/src/lib/libc/string/strerror_r.c new file mode 100644 index 00000000..aab6db53 --- /dev/null +++ b/src/lib/libc/string/strerror_r.c @@ -0,0 +1,30 @@ +/* $OpenBSD: strerror_r.c,v 1.1 2002/11/21 20:45:05 marc Exp $ */ +/* Public Domain */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: strerror_r.c,v 1.1 2002/11/21 20:45:05 marc Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +extern char *__strerror(int, char *); + +int +strerror_r(int errnum, char *strerrbuf, size_t buflen) +{ + int save_errno; + int ret_errno; + char buf[NL_TEXTMAX]; + + save_errno = errno; + errno = 0; + __strerror(errnum, buf); + if (strlcpy(strerrbuf, buf, buflen) >= buflen) + errno = ERANGE; + ret_errno = errno; + errno = save_errno; + + return (ret_errno); +}