Skip to content

Commit 98e5834

Browse files
tvedpgeorge
authored andcommitted
lib/libc: Add implementation of strncpy.
1 parent 9aa2140 commit 98e5834

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/libc/string0.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,23 @@ char *strcpy(char *dest, const char *src) {
169169
return dest;
170170
}
171171

172+
// Public Domain implementation of strncpy from:
173+
// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strncpy_function
174+
char *strncpy(char *s1, const char *s2, size_t n) {
175+
char *dst = s1;
176+
const char *src = s2;
177+
/* Copy bytes, one at a time. */
178+
while (n > 0) {
179+
n--;
180+
if ((*dst++ = *src++) == '\0') {
181+
/* If we get here, we found a null character at the end of s2 */
182+
*dst = '\0';
183+
break;
184+
}
185+
}
186+
return s1;
187+
}
188+
172189
// needed because gcc optimises strcpy + strcat to this
173190
char *stpcpy(char *dest, const char *src) {
174191
while (*src) {

0 commit comments

Comments
 (0)