summaryrefslogtreecommitdiff
path: root/snmplib/strlcpy.c
diff options
context:
space:
mode:
Diffstat (limited to 'snmplib/strlcpy.c')
-rw-r--r--snmplib/strlcpy.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/snmplib/strlcpy.c b/snmplib/strlcpy.c
new file mode 100644
index 0000000..0235a30
--- /dev/null
+++ b/snmplib/strlcpy.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms specified in the COPYING file
+ * distributed with the Net-SNMP package.
+ */
+#include <net-snmp/net-snmp-config.h>
+
+#if HAVE_STRING_H
+#include <string.h>
+#else
+#include <strings.h>
+#endif
+
+#include <sys/types.h>
+
+#include <net-snmp/library/system.h>
+
+/*
+ * Copies src to the dest buffer. The copy will never overflow the dest buffer
+ * and dest will always be null terminated, len is the size of the dest buffer.
+ *
+ * Returns the length of the src buffer.
+ */
+size_t
+strlcpy(char *dest, const char *src, size_t len)
+{
+ size_t src_len = strlen(src);
+ size_t new_len;
+
+ if (len == 0) {
+ return (src_len);
+ }
+
+ if (src_len >= len) {
+ new_len = len - 1;
+ } else {
+ new_len = src_len;
+ }
+
+ memcpy(dest, src, new_len);
+ dest[new_len] = '\0';
+ return (src_len);
+}