lib/libc: Add implementation of strncpy.
This commit is contained in:
parent
9aa214077e
commit
98e583430f
|
@ -169,6 +169,23 @@ char *strcpy(char *dest, const char *src) {
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Public Domain implementation of strncpy from:
|
||||||
|
// http://en.wikibooks.org/wiki/C_Programming/Strings#The_strncpy_function
|
||||||
|
char *strncpy(char *s1, const char *s2, size_t n) {
|
||||||
|
char *dst = s1;
|
||||||
|
const char *src = s2;
|
||||||
|
/* Copy bytes, one at a time. */
|
||||||
|
while (n > 0) {
|
||||||
|
n--;
|
||||||
|
if ((*dst++ = *src++) == '\0') {
|
||||||
|
/* If we get here, we found a null character at the end of s2 */
|
||||||
|
*dst = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s1;
|
||||||
|
}
|
||||||
|
|
||||||
// needed because gcc optimises strcpy + strcat to this
|
// needed because gcc optimises strcpy + strcat to this
|
||||||
char *stpcpy(char *dest, const char *src) {
|
char *stpcpy(char *dest, const char *src) {
|
||||||
while (*src) {
|
while (*src) {
|
||||||
|
|
Loading…
Reference in New Issue