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

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