diff options
author | Thorsten von Eicken <tve@voneicken.com> | 2020-07-02 12:48:16 -0700 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2020-07-20 23:42:04 +1000 |
commit | 98e583430fb7b793119db27bad9f98119e81579f (patch) | |
tree | fcd445593cb440209e9f3c13827140f23ada6452 | |
parent | 9aa214077e6d1e0fba1a775431fedea4c8d76558 (diff) | |
download | micropython-98e583430fb7b793119db27bad9f98119e81579f.tar.gz micropython-98e583430fb7b793119db27bad9f98119e81579f.zip |
lib/libc: Add implementation of strncpy.
-rw-r--r-- | lib/libc/string0.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/libc/string0.c b/lib/libc/string0.c index 8c86bf65f7..5f40a71e3e 100644 --- a/lib/libc/string0.c +++ b/lib/libc/string0.c @@ -169,6 +169,23 @@ char *strcpy(char *dest, const char *src) { 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 char *stpcpy(char *dest, const char *src) { while (*src) { |