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