1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-30 06:25:37 +03:00

util: introduce strnpcpy() and strnscpy()

This commit is contained in:
Yu Watanabe 2019-04-30 18:56:33 +02:00
parent 961189af2a
commit 7e4831d296
2 changed files with 19 additions and 6 deletions

View File

@ -17,16 +17,13 @@
#include "strxcpyx.h"
size_t strpcpy(char **dest, size_t size, const char *src) {
size_t len;
size_t strnpcpy(char **dest, size_t size, const char *src, size_t len) {
assert(dest);
assert(src);
if (size == 0)
return 0;
len = strlen(src);
if (len >= size) {
if (size > 1)
*dest = mempcpy(*dest, src, size-1);
@ -40,6 +37,13 @@ size_t strpcpy(char **dest, size_t size, const char *src) {
return size;
}
size_t strpcpy(char **dest, size_t size, const char *src) {
assert(dest);
assert(src);
return strnpcpy(dest, size, src, strlen(src));
}
size_t strpcpyf(char **dest, size_t size, const char *src, ...) {
va_list va;
int i;
@ -76,14 +80,21 @@ size_t strpcpyl(char **dest, size_t size, const char *src, ...) {
return size;
}
size_t strscpy(char *dest, size_t size, const char *src) {
size_t strnscpy(char *dest, size_t size, const char *src, size_t len) {
char *s;
assert(dest);
assert(src);
s = dest;
return strpcpy(&s, size, src);
return strnpcpy(&s, size, src, len);
}
size_t strscpy(char *dest, size_t size, const char *src) {
assert(dest);
assert(src);
return strnscpy(dest, size, src, strlen(src));
}
size_t strscpyl(char *dest, size_t size, const char *src, ...) {

View File

@ -5,8 +5,10 @@
#include "macro.h"
size_t strnpcpy(char **dest, size_t size, const char *src, size_t len);
size_t strpcpy(char **dest, size_t size, const char *src);
size_t strpcpyf(char **dest, size_t size, const char *src, ...) _printf_(3, 4);
size_t strpcpyl(char **dest, size_t size, const char *src, ...) _sentinel_;
size_t strnscpy(char *dest, size_t size, const char *src, size_t len);
size_t strscpy(char *dest, size_t size, const char *src);
size_t strscpyl(char *dest, size_t size, const char *src, ...) _sentinel_;