C library: Add some new functions defined in POSIX.1-2017: stpncpy(), strsignal(), psignal(), psiginfo().

This commit is contained in:
Gregory Nutt 2018-09-17 13:55:33 -06:00
parent 3d14b63e8f
commit 68d4c1d4ed
10 changed files with 482 additions and 37 deletions

View File

@ -370,33 +370,35 @@ extern "C"
#define EXTERN extern #define EXTERN extern
#endif #endif
int kill(pid_t pid, int signo); int kill(pid_t pid, int signo);
int raise(int signo); void psignal(int signum, FAR const char *message);
int sigaction(int signo, FAR const struct sigaction *act, void psiginfo(FAR const siginfo_t *pinfo, FAR const char *message);
FAR struct sigaction *oact); int raise(int signo);
int sigaddset(FAR sigset_t *set, int signo); int sigaction(int signo, FAR const struct sigaction *act,
int sigdelset(FAR sigset_t *set, int signo); FAR struct sigaction *oact);
int sigemptyset(FAR sigset_t *set); int sigaddset(FAR sigset_t *set, int signo);
int sigfillset(FAR sigset_t *set); int sigdelset(FAR sigset_t *set, int signo);
int sighold(int signo); int sigemptyset(FAR sigset_t *set);
int sigismember(FAR const sigset_t *set, int signo); int sigfillset(FAR sigset_t *set);
int sigignore(int signo); int sighold(int signo);
int sigismember(FAR const sigset_t *set, int signo);
int sigignore(int signo);
_sa_handler_t signal(int signo, _sa_handler_t func); _sa_handler_t signal(int signo, _sa_handler_t func);
int sigpause(int signo); int sigpause(int signo);
int sigpending(FAR sigset_t *set); int sigpending(FAR sigset_t *set);
int sigprocmask(int how, FAR const sigset_t *set, FAR sigset_t *oset); int sigprocmask(int how, FAR const sigset_t *set, FAR sigset_t *oset);
#ifdef CONFIG_CAN_PASS_STRUCTS #ifdef CONFIG_CAN_PASS_STRUCTS
int sigqueue(int pid, int signo, union sigval value); int sigqueue(int pid, int signo, union sigval value);
#else #else
int sigqueue(int pid, int signo, FAR void *sival_ptr); int sigqueue(int pid, int signo, FAR void *sival_ptr);
#endif #endif
int sigrelse(int signo); int sigrelse(int signo);
_sa_handler_t sigset(int signo, _sa_handler_t func); _sa_handler_t sigset(int signo, _sa_handler_t func);
int sigwait(FAR const sigset_t *set, FAR int *sig); int sigwait(FAR const sigset_t *set, FAR int *sig);
int sigtimedwait(FAR const sigset_t *set, FAR struct siginfo *value, int sigtimedwait(FAR const sigset_t *set, FAR struct siginfo *value,
FAR const struct timespec *timeout); FAR const struct timespec *timeout);
int sigsuspend(FAR const sigset_t *sigmask); int sigsuspend(FAR const sigset_t *sigmask);
int sigwaitinfo(FAR const sigset_t *set, FAR struct siginfo *value); int sigwaitinfo(FAR const sigset_t *set, FAR struct siginfo *value);
#undef EXTERN #undef EXTERN
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -75,6 +75,7 @@ int strcoll(FAR const char *, FAR const char *s2);
FAR char *strcpy(FAR char *dest, FAR const char *src); FAR char *strcpy(FAR char *dest, FAR const char *src);
FAR char *stpcpy(FAR char *dest, FAR const char *src); FAR char *stpcpy(FAR char *dest, FAR const char *src);
FAR char *strncpy(FAR char *, FAR const char *, size_t); FAR char *strncpy(FAR char *, FAR const char *, size_t);
FAR char *stpncpy(FAR char *, FAR const char *, size_t);
FAR char *strpbrk(FAR const char *, FAR const char *); FAR char *strpbrk(FAR const char *, FAR const char *);
FAR char *strchr(FAR const char *s, int c); FAR char *strchr(FAR const char *s, int c);
FAR char *strrchr(FAR const char *s, int c); FAR char *strrchr(FAR const char *s, int c);
@ -82,6 +83,7 @@ size_t strspn(FAR const char *, FAR const char *);
size_t strcspn(FAR const char *, FAR const char *); size_t strcspn(FAR const char *, FAR const char *);
FAR char *strstr(FAR const char *, FAR const char *); FAR char *strstr(FAR const char *, FAR const char *);
FAR char *strcasestr(FAR const char *, FAR const char *); FAR char *strcasestr(FAR const char *, FAR const char *);
FAR char *strsignal(int signum);
FAR char *strtok(FAR char *, FAR const char *); FAR char *strtok(FAR char *, FAR const char *);
FAR char *strtok_r(FAR char *, FAR const char *, FAR char **); FAR char *strtok_r(FAR char *, FAR const char *, FAR char **);
size_t strxfrm(FAR char *, FAR const char *, size_t n); size_t strxfrm(FAR char *, FAR const char *, size_t n);

View File

@ -38,8 +38,8 @@ ifneq ($(CONFIG_DISABLE_SIGNALS),y)
# Add the signal C files to the build # Add the signal C files to the build
CSRCS += sig_addset.c sig_delset.c sig_emptyset.c sig_fillset.c CSRCS += sig_addset.c sig_delset.c sig_emptyset.c sig_fillset.c
CSRCS += sig_hold.c sig_ignore.c sig_ismember.c sig_pause.c sig_raise.c CSRCS += sig_hold.c sig_ignore.c sig_ismember.c sig_pause.c sig_psignal.c
CSRCS += sig_relse.c sig_set.c signal.c sigwait.c CSRCS += sig_raise.c sig_relse.c sig_set.c sig_signal.c sig_wait.c
# Add the signal directory to the build # Add the signal directory to the build

View File

@ -0,0 +1,124 @@
/****************************************************************************
* libs/libc/signal/sig_psignal.c
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
/* Uses streams... not available to kernel code */
#ifndef __KERNEL__
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: psignal
*
* Description:
* The psignal() functions will write a language-dependent message
* associated with a signal number to the standard error stream as
* follows:
*
* First, if message is not a null pointer and is not the empty string,
* the string pointed to by the message argument will be written,
* followed by a colon and a space.
*
* Then the signal description string associated with signum or with the
* signal indicated by pinfo will be written, followed by a newline.
*
* Returned Value
* None. The errno value is never set in this implementation.
*
****************************************************************************/
void psignal(int signum, FAR const char *message)
{
/* For now, just a brainless write to stderr (fd == 2). C buffered I/O is
* used!
*/
if (message != NULL)
{
(void)fprintf(stderr, "%s: %s\n", strsignal(signum));
}
else
{
(void)fprintf(stderr, "%s\n", strsignal(signum));
}
}
/****************************************************************************
* Name: psiginfo
*
* Description:
* The psiginfo() functions will write a language-dependent message
* associated with a signal number to the standard error stream as
* follows:
*
* First, if message is not a null pointer and is not the empty string,
* the string pointed to by the message argument will be written,
* followed by a colon and a space.
*
* Then the signal description string associated with signum or with the
* signal indicated by pinfo will be written, followed by a newline.
*
* Returned Value
* None. Since no value is returned, an application wishing to check for
* error situations should set errno to 0, then call psiginfo() then check
* errno.
*
****************************************************************************/
void psiginfo(FAR const siginfo_t *pinfo, FAR const char *message)
{
if (pinfo == NULL)
{
set_errno(EINVAL);
}
else
{
psignal(pinfo->si_signo, message);
}
}
#endif /* __KERNEL__ */

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* libs/libc/signal/signal.c * libs/libc/signal/sig_signal.c
* *
* Copyright (C) 2015-2017 Gregory Nutt. All rights reserved. * Copyright (C) 2015-2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* libs/libc/signal/sigwait.c * libs/libc/signal/sig_wait.c
* *
* Copyright (C) 2017 Gregory Nutt. All rights reserved. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>

View File

@ -38,13 +38,13 @@
CSRCS += lib_ffs.c lib_ffsl.c lib_ffsll.c lib_fls.c lib_flsl.c CSRCS += lib_ffs.c lib_ffsl.c lib_ffsll.c lib_fls.c lib_flsl.c
CSRCS += lib_flsll.c lib_isbasedigit.c lib_memset.c lib_memchr.c CSRCS += lib_flsll.c lib_isbasedigit.c lib_memset.c lib_memchr.c
CSRCS += lib_memccpy.c lib_memcmp.c lib_memmove.c lib_skipspace.c CSRCS += lib_memccpy.c lib_memcmp.c lib_memmove.c lib_skipspace.c
CSRCS += lib_stpcpy.c lib_strcasecmp.c lib_strcat.c lib_strchr.c CSRCS += lib_stpcpy.c lib_stpncpy.c lib_strcasecmp.c lib_strcat.c
CSRCS += lib_strcpy.c lib_strcmp.c lib_strcspn.c lib_strdup.c CSRCS += lib_strchr.c lib_strcpy.c lib_strcmp.c lib_strcspn.c
CSRCS += lib_strerror.c lib_strlen.c lib_strnlen.c lib_strncasecmp.c CSRCS += lib_strdup.c lib_strerror.c lib_strlen.c lib_strnlen.c
CSRCS += lib_strncat.c lib_strncmp.c lib_strncpy.c lib_strndup.c CSRCS += lib_strncasecmp.c lib_strncat.c lib_strncmp.c lib_strncpy.c
CSRCS += lib_strcasestr.c lib_strpbrk.c lib_strrchr.c lib_strspn.c CSRCS += lib_strndup.c lib_strcasestr.c lib_strpbrk.c lib_strrchr.c
CSRCS += lib_strstr.c lib_strtok.c lib_strtokr.c lib_strerrorr.c CSRCS += lib_strspn.c lib_strstr.c lib_strtok.c lib_strtokr.c
CSRCS += lib_explicit_bzero.c CSRCS += lib_strerrorr.c lib_explicit_bzero.c
ifneq ($(CONFIG_LIBC_ARCH_MEMCPY),y) ifneq ($(CONFIG_LIBC_ARCH_MEMCPY),y)
ifeq ($(CONFIG_MEMCPY_VIK),y) ifeq ($(CONFIG_MEMCPY_VIK),y)
@ -58,6 +58,10 @@ ifeq ($(CONFIG_LIBC_LOCALE),y)
CSRCS += lib_strcoll.c lib_strxfrm.c CSRCS += lib_strcoll.c lib_strxfrm.c
endif endif
ifneq ($(CONFIG_DISABLE_SIGNALS),y)
CSRCS += lib_strsignal.c
endif
# Add the string directory to the build # Add the string directory to the build
DEPPATH += --dep-path string DEPPATH += --dep-path string

View File

@ -0,0 +1,100 @@
/****************************************************************************
* libs/libc/string/lib_stpncpy.c
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <string.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: strncpy
*
* Description:
* Copies the string pointed to by 'src' (including the terminating NUL
* character) into the array pointed to by 'dest'. strncpy() will not
* copy more than 'n' bytes from 'src' to 'dest' array (including the
* NUL terminator).
*
* If the array pointed to by 'src' is a string that is shorter than 'n'
* bytes, NUL characters will be appended to the copy in the array
* pointed to by 'dest', until 'n' bytes in all are written.
*
* If copying takes place between objects that overlap, the behavior is
* undefined.
*
* Returned Value:
* If a NUL character is written to the destination, the stpncpy()
* function will return the address of the first such NUL character.
* Otherwise, it will return &dest[n]
*
****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_STRNCPY
FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n)
{
FAR char *end = dest + n; /* End of dest buffer + 1 byte */
FAR char *ret; /* Value to be returned */
/* Copy up n bytes, breaking out of the loop early if a NUL terminator is
* encountered.
*/
while ((dest != end) && (*dest++ = *src++) != '\0')
{
}
/* Return the pointer to the NUL terminator (or to the end of the buffer
* + 1.
*/
ret = dest;
/* Pad the remainder of the array pointer to 'dest' with NULs */
while (dest != end)
{
*dest++ = '\0';
}
return ret;
}
#endif

View File

@ -47,15 +47,43 @@
/**************************************************************************** /****************************************************************************
* Name: strncpy * Name: strncpy
*
* Description:
* Copies the string pointed to by 'src' (including the terminating NUL
* character) into the array pointed to by 'dest'. strncpy() will not
* copy more than 'n' bytes from 'src' to 'dest' array (including the
* NUL terminator).
*
* If the array pointed to by 'src' is a string that is shorter than 'n'
* bytes, NUL characters will be appended to the copy in the array
* pointed to by 'dest', until 'n' bytes in all are written.
*
* If copying takes place between objects that overlap, the behavior is
* undefined.
*
* Returned Value:
* The strncpy() function returns the pointer to 'dest'
*
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_LIBC_ARCH_STRNCPY #ifndef CONFIG_LIBC_ARCH_STRNCPY
char *strncpy(FAR char *dest, FAR const char *src, size_t n) FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n)
{ {
char *ret = dest; /* Value to be returned */ FAR char *ret = dest; /* Value to be returned */
char *end = dest + n; /* End of dest buffer + 1 byte */ FAR char *end = dest + n; /* End of dest buffer + 1 byte */
/* Copy up n bytes, breaking out of the loop early if a NUL terminator is
* encountered.
*/
while ((dest != end) && (*dest++ = *src++) != '\0')
{
}
/* Note that there may be no NUL terminator in 'dest' */
/* Pad the remainder of the array pointer to 'dest' with NULs */
while ((dest != end) && (*dest++ = *src++) != '\0');
while (dest != end) while (dest != end)
{ {
*dest++ = '\0'; *dest++ = '\0';

View File

@ -0,0 +1,185 @@
/****************************************************************************
* libs/libc/string/lib_strsignal.c
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <signal.h>
#include <string.h>
/****************************************************************************
* Private Data
****************************************************************************/
/* We don't know what signals names will be assigned to which signals in
* advance and we do not want to return a volatile value. One solution is
* this silly array of useless names:
*/
static FAR const char *g_default_sigstr[32] =
{
"Signal 0",
"Signal 1",
"Signal 2",
"Signal 3",
"Signal 4",
"Signal 5",
"Signal 6",
"Signal 7",
"Signal 8",
"Signal 9",
"Signal 10",
"Signal 11",
"Signal 12",
"Signal 13",
"Signal 14",
"Signal 15",
"Signal 16",
"Signal 17",
"Signal 18",
"Signal 19",
"Signal 20",
"Signal 21",
"Signal 22",
"Signal 23",
"Signal 24",
"Signal 25",
"Signal 26",
"Signal 27",
"Signal 28",
"Signal 29",
"Signal 30",
"Signal 31",
};
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: psignal
*
* Description:
* The strsignal() function will map the signal number in signum to an
* implementation-defined string and will return a pointer to it.
*
****************************************************************************/
FAR char *strsignal(int signum)
{
/* Handle invalid signals */
if (!GOOD_SIGNO(signum))
{
return (FAR char *)"Invalid Signal";
}
/* Handle named signals */
switch (signum)
{
/* Standard signals */
#ifdef SIGUSR1
case SIGUSR1:
return (FAR char *)"SIGUSR1";
#endif
#ifdef SIGUSR2
case SIGUSR2:
return (FAR char *)"SIGUSR2";
#endif
#ifdef SIGALRM
case SIGALRM:
return (FAR char *)"SIGALRM";
#endif
#ifdef SIGCHLD
case SIGCHLD:
return (FAR char *)"SIGCHLD";
#endif
#ifdef SIGPOLL
case SIGPOLL:
return (FAR char *)"SIGPOLL";
#endif
#ifdef SIGSTOP
case SIGSTOP:
return (FAR char *)"SIGSTOP";
#endif
#ifdef SIGSTP
case SIGSTP:
return (FAR char *)"SIGSTP";
#endif
#ifdef SIGCONT
case SIGCONT:
return (FAR char *)"SIGCONT";
#endif
#ifdef SIGKILL
case SIGKILL:
return (FAR char *)"SIGKILL";
#endif
#ifdef SIGINT
case SIGINT:
return (FAR char *)"SIGINT";
#endif
/* Non-standard signals */
#ifdef SIGCONDTIMEDOUT
case SIGCONDTIMEDOUT:
return (FAR char *)"SIGCONDTIMEDOUT";
#endif
#ifdef SIGWORK
case SIGWORK:
return (FAR char *)"SIGWORK";
#endif
default:
break;
}
/* Return a string devoid is meaning */
return (FAR char *)g_default_sigstr[signum];
}