mirror of
https://git.rtems.org/rtems-libbsd/
synced 2025-05-14 00:59:17 +08:00
Add wrapper for strndup()
This commit is contained in:
parent
791ea27c8d
commit
d1c8a7fbf5
@ -107,7 +107,10 @@ void *
|
||||
rtems_bsd_program_reallocf(void *ptr, size_t size);
|
||||
|
||||
char *
|
||||
rtems_bsd_program_strdup(const char *s1);
|
||||
rtems_bsd_program_strdup(const char *s);
|
||||
|
||||
char *
|
||||
rtems_bsd_program_strndup(const char *s, size_t size);
|
||||
|
||||
int
|
||||
rtems_bsd_program_vasprintf(char **strp, const char *fmt, va_list ap);
|
||||
@ -177,7 +180,11 @@ rtems_bsd_program_free(void *ptr);
|
||||
#endif
|
||||
|
||||
#ifndef RTEMS_BSD_PROGRAM_NO_STRDUP_WRAP
|
||||
#define strdup(s1) rtems_bsd_program_strdup(s1)
|
||||
#define strdup(s) rtems_bsd_program_strdup(s)
|
||||
#endif
|
||||
|
||||
#ifndef RTEMS_BSD_PROGRAM_NO_STRNDUP_WRAP
|
||||
#define strndup(s, size) rtems_bsd_program_strndup(s, size)
|
||||
#endif
|
||||
|
||||
#ifndef RTEMS_BSD_PROGRAM_NO_VASPRINTF_WRAP
|
||||
|
@ -47,6 +47,7 @@
|
||||
|
||||
#undef printf
|
||||
#define RTEMS_BSD_PROGRAM_NO_STRDUP_WRAP
|
||||
#define RTEMS_BSD_PROGRAM_NO_STRNDUP_WRAP
|
||||
#include <machine/rtems-bsd-program.h>
|
||||
|
||||
struct rtems_bsd_program_control *
|
||||
|
@ -546,18 +546,32 @@ rtems_bsd_program_reallocf(void *ptr, size_t size)
|
||||
}
|
||||
|
||||
char *
|
||||
rtems_bsd_program_strdup(const char *s1)
|
||||
rtems_bsd_program_strdup(const char *s)
|
||||
{
|
||||
size_t size = strlen(s1) + 1; /* add one for null termination */
|
||||
char *item;
|
||||
size_t size;
|
||||
void *s2;
|
||||
|
||||
item = rtems_bsd_program_alloc(size, NULL);
|
||||
|
||||
if (item != NULL) {
|
||||
memcpy(item, s1, size);
|
||||
size = strlen(s) + 1;
|
||||
s2 = rtems_bsd_program_alloc(size, NULL);
|
||||
if (s2 == NULL) {
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return item;
|
||||
return (memcpy(s2, s, size));
|
||||
}
|
||||
|
||||
char *
|
||||
rtems_bsd_program_strndup(const char *s, size_t size)
|
||||
{
|
||||
void *s2;
|
||||
|
||||
size = strnlen(s, size) + 1;
|
||||
s2 = rtems_bsd_program_alloc(size, NULL);
|
||||
if (s2 == NULL) {
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
return (memcpy(s2, s, size));
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -52,12 +52,13 @@
|
||||
|
||||
struct alloc_ctx {
|
||||
enum alloc_type {
|
||||
ALLOC_MALLOC = 0,
|
||||
ALLOC_CALLOC = 1,
|
||||
ALLOC_REALLOC = 2,
|
||||
ALLOC_STRDUP = 3,
|
||||
ALLOC_ASPRINTF = 4,
|
||||
ALLOC_LAST = 5,
|
||||
ALLOC_MALLOC,
|
||||
ALLOC_CALLOC,
|
||||
ALLOC_REALLOC,
|
||||
ALLOC_STRDUP,
|
||||
ALLOC_STRNDUP,
|
||||
ALLOC_ASPRINTF,
|
||||
ALLOC_LAST
|
||||
} type;
|
||||
unsigned free;
|
||||
#define ALLOC_FREE_NONE 0x0
|
||||
@ -511,8 +512,25 @@ call_alloc(void *ctx)
|
||||
break;
|
||||
case ALLOC_STRDUP:
|
||||
first = strdup(teststring);
|
||||
assert(first != NULL);
|
||||
assert(strcmp(first, teststring) == 0);
|
||||
second = strdup(teststring);
|
||||
assert(second != NULL);
|
||||
assert(strcmp(second, teststring) == 0);
|
||||
third = strdup(teststring);
|
||||
assert(third != NULL);
|
||||
assert(strcmp(third, teststring) == 0);
|
||||
break;
|
||||
case ALLOC_STRNDUP:
|
||||
first = strndup(teststring, 1);
|
||||
assert(first != NULL);
|
||||
assert(strncmp(first, "t", 1) == 0);
|
||||
second = strndup(teststring, 2);
|
||||
assert(second != NULL);
|
||||
assert(strncmp(second, "te", 2) == 0);
|
||||
third = strndup(teststring, 4);
|
||||
assert(third != NULL);
|
||||
assert(strcmp(third, teststring) == 0);
|
||||
break;
|
||||
case ALLOC_ASPRINTF:
|
||||
asprintf(&first, "a number %d", 0x123456);
|
||||
|
Loading…
x
Reference in New Issue
Block a user