PatchworkOS
Loading...
Searching...
No Matches
strncpy.c
Go to the documentation of this file.
1#include <string.h>
2
3char* strncpy(char* _RESTRICT s1, const char* _RESTRICT s2, size_t n)
4{
5 char* rc = s1;
6
7 while (n && (*s1++ = *s2++))
8 {
9 /* Cannot do "n--" in the conditional as size_t is unsigned and we have
10 to check it again for >0 in the next loop below, so we must not risk
11 underflow.
12 */
13 --n;
14 }
15
16 /* Checking against 1 as we missed the last --n in the loop above. */
17 while (n-- > 1)
18 {
19 *s1++ = '\0';
20 }
21
22 return rc;
23}
#define _RESTRICT
Definition config.h:17
char * strncpy(char *_RESTRICT s1, const char *_RESTRICT s2, size_t n)
Definition strncpy.c:3