Commit bc002e85 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Pavel Emelyanov

Add strlcpy helper

Same as kernel provides, adopted from Linux sources.

strlcpy is similar to strncpy but _always_ adds \0
at the end of string even if destination is shorter.
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent aaeff46e
...@@ -12,6 +12,9 @@ ifeq ($(call try-cc,$(TCP_REPAIR_TEST),,),y) ...@@ -12,6 +12,9 @@ ifeq ($(call try-cc,$(TCP_REPAIR_TEST),,),y)
endif endif
ifeq ($(call try-cc,$(PRLIMIT_TEST),,),y) ifeq ($(call try-cc,$(PRLIMIT_TEST),,),y)
$(Q) @echo '#define CONFIG_HAS_PRLIMIT' >> $@ $(Q) @echo '#define CONFIG_HAS_PRLIMIT' >> $@
endif
ifeq ($(call try-cc,$(STRLCPY_TEST),,),y)
$(Q) @echo '#define CONFIG_HAS_STRLCPY' >> $@
endif endif
$(Q) @echo '#endif /* __CR_CONFIG_H__ */' >> $@ $(Q) @echo '#endif /* __CR_CONFIG_H__ */' >> $@
......
...@@ -50,6 +50,7 @@ obj-y += page-xfer.o ...@@ -50,6 +50,7 @@ obj-y += page-xfer.o
obj-y += page-read.o obj-y += page-read.o
obj-y += kerndat.o obj-y += kerndat.o
obj-y += stats.o obj-y += stats.o
obj-y += string.o
obj-y += sigframe.o obj-y += sigframe.o
obj-y += arch/$(ARCH)/vdso.o obj-y += arch/$(ARCH)/vdso.o
......
#ifndef __CR_STRING_H__
#define __CR_STRING_H__
#include <sys/types.h>
#include <string.h>
#include "config.h"
#ifndef CONFIG_HAS_STRLCPY
extern size_t strlcpy(char *dest, const char *src, size_t size);
#endif
#endif /* __CR_STRING_H__ */
...@@ -29,3 +29,16 @@ int main(void) ...@@ -29,3 +29,16 @@ int main(void)
return prlimit(getpid(), RLIMIT_CPU, &limit, NULL); return prlimit(getpid(), RLIMIT_CPU, &limit, NULL);
} }
endef endef
define STRLCPY_TEST
#include <string.h>
int main(void)
{
char src[32] = "strlcpy";
char dst[32];
return strlcpy(dst, src, sizeof(dst));
}
endef
/*
* Adopted from linux kernel
*/
#include <sys/types.h>
#include <string.h>
#include "string.h"
#ifndef CONFIG_HAS_STRLCPY
/**
* strlcpy - Copy a %NUL terminated string into a sized buffer
* @dest: Where to copy the string to
* @src: Where to copy the string from
* @size: size of destination buffer
*
* Compatible with *BSD: the result is always a valid
* NUL-terminated string that fits in the buffer (unless,
* of course, the buffer size is zero). It does not pad
* out the result like strncpy() does.
*/
size_t strlcpy(char *dest, const char *src, size_t size)
{
size_t ret = strlen(src);
if (size) {
size_t len = (ret >= size) ? size - 1 : ret;
memcpy(dest, src, len);
dest[len] = '\0';
}
return ret;
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment