[PATCH] Add strlcpy helper

Cyrill Gorcunov gorcunov at openvz.org
Fri Aug 23 12:19:32 EDT 2013


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: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Makefile.config           |  3 +++
 Makefile.crtools          |  1 +
 include/string.h          | 13 +++++++++++++
 scripts/feature-tests.mak | 13 +++++++++++++
 string.c                  | 32 ++++++++++++++++++++++++++++++++
 5 files changed, 62 insertions(+)
 create mode 100644 include/string.h
 create mode 100644 string.c

diff --git a/Makefile.config b/Makefile.config
index 8f81bbc..f376da1 100644
--- a/Makefile.config
+++ b/Makefile.config
@@ -13,6 +13,9 @@ endif
 ifeq ($(call try-cc,$(PRLIMIT_TEST),,),y)
 	$(Q) @echo '#define CONFIG_HAS_PRLIMIT' >> $@
 endif
+ifeq ($(call try-cc,$(STRLCPY_TEST),,),y)
+	$(Q) @echo '#define CONFIG_HAS_STRLCPY' >> $@
+endif
 	$(Q) @echo '#endif /* __CR_CONFIG_H__ */' >> $@
 
 config: $(CONFIG)
diff --git a/Makefile.crtools b/Makefile.crtools
index 7163438..eabea55 100644
--- a/Makefile.crtools
+++ b/Makefile.crtools
@@ -50,6 +50,7 @@ obj-y	+= page-xfer.o
 obj-y	+= page-read.o
 obj-y	+= kerndat.o
 obj-y	+= stats.o
+obj-y	+= string.o
 obj-y	+= sigframe.o
 obj-y	+= arch/$(ARCH)/vdso.o
 
diff --git a/include/string.h b/include/string.h
new file mode 100644
index 0000000..f455fbc
--- /dev/null
+++ b/include/string.h
@@ -0,0 +1,13 @@
+#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__ */
diff --git a/scripts/feature-tests.mak b/scripts/feature-tests.mak
index 80ba32f..25a5e67 100644
--- a/scripts/feature-tests.mak
+++ b/scripts/feature-tests.mak
@@ -29,3 +29,16 @@ int main(void)
 	return prlimit(getpid(), RLIMIT_CPU, &limit, NULL);
 }
 endef
+
+define STRLCPY_TEST
+
+#include <string.h>
+
+int main(void)
+{
+	char src[32] = "strlcpy";
+	char dst[32];
+
+	return strlcpy(dst, src, sizeof(dst));
+}
+endef
diff --git a/string.c b/string.c
new file mode 100644
index 0000000..1748239
--- /dev/null
+++ b/string.c
@@ -0,0 +1,32 @@
+/*
+ * 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
-- 
1.8.1.4


--vkogqOf2sHV7VnPd--


More information about the CRIU mailing list