You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.0 KiB
73 lines
2.0 KiB
5 years ago
|
#include <string.h>
|
||
|
#include <stddef.h>
|
||
|
|
||
|
size_t strlcat(char *__restrict__ dst, const char *__restrict__ src, size_t capacity)
|
||
|
{
|
||
|
if (!dst) { return 0; }
|
||
|
if (!src) { return 0; }
|
||
|
size_t used = strnlen(dst, capacity);
|
||
|
if (used >= capacity) { used = capacity - 1; }
|
||
|
size_t to_print = strlen(src);
|
||
|
// to_print does not include the terminator
|
||
|
const size_t needed = used + to_print + 1;
|
||
|
if (needed > capacity) {
|
||
|
to_print = capacity - used - 1;
|
||
|
}
|
||
|
memcpy(dst + used, src, to_print);
|
||
|
*(dst + used + to_print) = '\0';
|
||
|
return needed;
|
||
|
}
|
||
|
|
||
|
size_t strlncat(char *__restrict__ dst, const char *__restrict__ src, size_t capacity, size_t num)
|
||
|
{
|
||
|
if (!dst) { return 0; }
|
||
|
if (!src) { return 0; }
|
||
|
size_t used = strnlen(dst, capacity);
|
||
|
if (used >= capacity) { used = capacity - 1; }
|
||
|
size_t to_print = strlen(src);
|
||
|
if (to_print > num) {
|
||
|
to_print = num;
|
||
|
}
|
||
|
// to_print does not include the terminator
|
||
|
const size_t needed = used + to_print + 1;
|
||
|
if (needed > capacity) {
|
||
|
to_print = capacity - used - 1;
|
||
|
}
|
||
|
memcpy(dst + used, src, to_print);
|
||
|
*(dst + used + to_print) = '\0';
|
||
|
return needed;
|
||
|
}
|
||
|
|
||
|
size_t strlcpy(char *__restrict__ dst, const char *__restrict__ src, size_t capacity)
|
||
|
{
|
||
|
if (!dst) { return 0; }
|
||
|
if (!src) { return 0; }
|
||
|
size_t to_print = strlen(src);
|
||
|
// to_print does not include the terminator
|
||
|
const size_t needed = to_print + 1;
|
||
|
if (needed > capacity) {
|
||
|
to_print = capacity - 1;
|
||
|
}
|
||
|
memcpy(dst, src, to_print);
|
||
|
*(dst + to_print) = '\0';
|
||
|
return needed;
|
||
|
}
|
||
|
|
||
|
size_t strlncpy(char *__restrict__ dst, const char *__restrict__ src, size_t capacity, size_t num)
|
||
|
{
|
||
|
if (!dst) { return 0; }
|
||
|
if (!src) { return 0; }
|
||
|
size_t to_print = strlen(src);
|
||
|
if (to_print > num) {
|
||
|
to_print = num;
|
||
|
}
|
||
|
// to_print does not include the terminator
|
||
|
const size_t needed = to_print + 1;
|
||
|
if (needed > capacity) {
|
||
|
to_print = capacity - 1;
|
||
|
}
|
||
|
memcpy(dst, src, to_print);
|
||
|
*(dst + to_print) = '\0';
|
||
|
return needed;
|
||
|
}
|