summaryrefslogtreecommitdiff
path: root/userland/libc/string/strlcpy.c
blob: a2d3dd9bb8f77eb2c0919bf9850f34810451d706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string.h>

// Copy string s2 to buffer s1 of size n.  At most n-1
// chars will be copied.  Always NUL terminates (unless n == 0).
// Returns strlen(s2); if retval >= n, truncation occurred.
size_t *strlcpy(char *s1, const char *s2, size_t n) {
  size_t tmp_n = n;
  const char *os2 = s2;
  for (; tmp_n; tmp_n--) {
    if ((*s1++ = *s2++) == '\0')
      break;
  }
  if (tmp_n == 0) {
    if (n != 0)
      *s1 = '\0'; /* NUL-terminate s1 */
    while (*s2++)
      ;
  }
  return s2 - os2 - 1;
}