From 91cb63bd8223191ce6a215de17a443593f687017 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 18 May 1999 04:48:58 +0000 Subject: Add a strsignal(3) (like strerror(3)) for libc compatability with other systems. NetBSD, Linux, SVR4 etc all have it. --- include/signal.h | 1 + include/string.h | 1 + lib/libc/gen/psignal.3 | 23 +++++++++++--- lib/libc/gen/siglist.c | 1 + lib/libc/string/Makefile.inc | 5 +-- lib/libc/string/strsignal.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 lib/libc/string/strsignal.c diff --git a/include/signal.h b/include/signal.h index cba3a44..69832e0 100644 --- a/include/signal.h +++ b/include/signal.h @@ -44,6 +44,7 @@ #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE) extern __const char *__const sys_signame[NSIG]; extern __const char *__const sys_siglist[NSIG]; +extern __const int sys_nsig; #endif __BEGIN_DECLS diff --git a/include/string.h b/include/string.h index acfebfc..5699767 100644 --- a/include/string.h +++ b/include/string.h @@ -86,6 +86,7 @@ char *strdup __P((const char *)); void strmode __P((int, char *)); int strncasecmp __P((const char *, const char *, size_t)); char *strsep __P((char **, const char *)); +char *strsignal __P((int)); char *strtok_r __P((char *, const char *, char **)); void swab __P((const void *, void *, size_t)); #endif diff --git a/lib/libc/gen/psignal.3 b/lib/libc/gen/psignal.3 index aa78732..0018c8a 100644 --- a/lib/libc/gen/psignal.3 +++ b/lib/libc/gen/psignal.3 @@ -36,6 +36,7 @@ .Os BSD 4.2 .Sh NAME .Nm psignal , +.Nm strsignal , .Nm sys_siglist , .Nm sys_signame .Nd system signal messages @@ -43,16 +44,29 @@ .Fd #include .Ft void .Fn psignal "unsigned sig" "const char *s" +.Ft "char *" +.Fn strsignal "unsigned sig" .Vt extern const char * const sys_siglist[]; .Vt extern const char * const sys_signame[]; .Sh DESCRIPTION The .Fn psignal -function locates the descriptive message -string for the given signal number +and +.Fn strsignal +functions locate the descriptive message +string for a signal number. +.Pp +The +.Fn strsignal +function accepts a signal number argument .Fa sig -and writes it to the standard error. +and returns a pointer to the corresponding message string. .Pp +The +.Fn psignal +function accepts an signal number argument +.Fa sig +and writes it to the standard error. If the argument .Fa s is @@ -86,7 +100,8 @@ and .Va sys_signame . .Sh SEE ALSO .Xr sigaction 2 , -.Xr perror 3 +.Xr perror 3 , +.Xr strerror 3 .Sh HISTORY The .Fn psignal diff --git a/lib/libc/gen/siglist.c b/lib/libc/gen/siglist.c index be6334d..e6987d5 100644 --- a/lib/libc/gen/siglist.c +++ b/lib/libc/gen/siglist.c @@ -107,3 +107,4 @@ const char *const sys_siglist[NSIG] = { "User defined signal 1", /* SIGUSR1 */ "User defined signal 2" /* SIGUSR2 */ }; +const int sys_nsig = sizeof(sys_siglist) / sizeof(sys_siglist[0]); diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index 42c8c39..2defef1 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -1,5 +1,5 @@ # @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 -# $Id: Makefile.inc,v 1.14 1998/02/20 08:23:55 jb Exp $ +# $Id: Makefile.inc,v 1.15 1999/01/19 23:45:48 jdp Exp $ .PATH: ${.CURDIR}/../libc/${MACHINE_ARCH}/string ${.CURDIR}/../libc/string @@ -10,7 +10,7 @@ MISRCS+=bcmp.c bcopy.c bzero.c ffs.c index.c memccpy.c memchr.c memcmp.c \ memcpy.c memmove.c memset.c rindex.c strcasecmp.c strcat.c strchr.c \ strcmp.c strcoll.c strcpy.c strcspn.c strdup.c strerror.c strlen.c \ strmode.c strncat.c strncmp.c strncpy.c strpbrk.c strrchr.c strsep.c \ - strspn.c strstr.c strtok.c strxfrm.c swab.c + strsignal.c strspn.c strstr.c strtok.c strxfrm.c swab.c # machine-dependent string sources .include "${.CURDIR}/../libc/${MACHINE_ARCH}/string/Makefile.inc" @@ -27,5 +27,6 @@ MLINKS+=strcat.3 strncat.3 MLINKS+=strcmp.3 strncmp.3 MLINKS+=strcpy.3 strncpy.3 MLINKS+=strerror.3 perror.3 strerror.3 sys_errlist.3 strerror.3 sys_nerr.3 +MLINKS+=psignal.3 strsignal.3 psignal.3 sys_siglist.3 psignal.3 sys_signame.3 MLINKS+=strtok.3 strtok_r.3 .endif diff --git a/lib/libc/string/strsignal.c b/lib/libc/string/strsignal.c new file mode 100644 index 0000000..5ec6365 --- /dev/null +++ b/lib/libc/string/strsignal.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 1988, 1993 + * The 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. 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. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)strerror.c 8.1 (Berkeley) 6/4/93"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +char * +strsignal(num) + int num; +{ +#define UPREFIX "Unknown signal: " + static char ebuf[40] = UPREFIX; /* 64-bit number + slop */ + register unsigned int signum; + register char *p, *t; + char tmp[40]; + + signum = num; /* convert to unsigned */ + if (signum < sys_nsig) + return ((char *)sys_siglist[signum]); + + /* Do this by hand, so we don't link to stdio(3). */ + t = tmp; + if (num < 0) + signum = -signum; + do { + *t++ = "0123456789"[signum % 10]; + } while (signum /= 10); + if (num < 0) + *t++ = '-'; + for (p = ebuf + sizeof(UPREFIX) - 1;;) { + *p++ = *--t; + if (t <= tmp) + break; + } + *p = '\0'; + return (ebuf); +} -- cgit v1.1