X-Git-Url: https://pd.if.org/git/?p=zos;a=blobdiff_plain;f=klib%2Fstrncpy.c;fp=klib%2Fstrncpy.c;h=e67442000b18406475b1b05639db423bdca342c6;hp=0000000000000000000000000000000000000000;hb=5c0e560e481e2e97b793f7574e849a5882781df9;hpb=e57b692bd3a86ee40fd89416a06fd696f1ff3bf9 diff --git a/klib/strncpy.c b/klib/strncpy.c new file mode 100644 index 0000000..e674420 --- /dev/null +++ b/klib/strncpy.c @@ -0,0 +1,17 @@ +#include + +char *strncpy(char *s1, const char *s2, size_t n) { + char * rc = s1; + while ( (n > 0) && (*s1++ = *s2++) ) { + /* Cannot do "n--" in the conditional as size_t is unsigned and we have + * to check it again for >0 in the next loop below, so we must not risk + * underflow. + */ + --n; + } + /* Checking against 1 as we missed the last --n in the loop above. */ + while (n-- > 1) { + *s1++ = '\0'; + } + return rc; +}