summaryrefslogtreecommitdiff
path: root/src/string/strncpy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/string/strncpy.c')
-rw-r--r--src/string/strncpy.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/string/strncpy.c b/src/string/strncpy.c
new file mode 100644
index 00000000..ee632d69
--- /dev/null
+++ b/src/string/strncpy.c
@@ -0,0 +1,34 @@
+#include <string.h>
+#include "nonstd/assert.h"
+
+/** copy bounded string **/
+char * strncpy(char * restrict s1, const char * restrict s2, size_t n)
+{
+ size_t i;
+
+ ASSERT_NONNULL(s1);
+ ASSERT_NONNULL(s2);
+ ASSERT_NOOVERLAP(s1, s2, n);
+
+ for (i = 0; i < n; i++) {
+ s1[i] = s2[i];
+ if (s1[i] == '\0') {
+ memset(s1 + i, '\0', n - i);
+ break;
+ }
+ }
+
+ return s1;
+}
+
+/***
+copies up to ARGUMENT(n) bytes from the string at ARGUMENT(s2)
+to ARGUMENT(s1). If a CHAR(\0) is encountered, null characters are appended to
+ARGUMENT(s1) until ARGUMENT(n) bytes have been written. If no null characters are copied
+in the first ARGUMENT(n) bytes of ARGUMENT(s2), the resulting string will not be null
+terminated.
+***/
+/*
+ RETURN_ALWAYS(ARGUMENT(s1));
+STDC(1)
+*/