blob: d55761d4c842b01d4de8565e58827bdd75583a3c [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Copyright (C) 2002 Manuel Novoa III
3 * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
4 *
5 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6 */
7
8/* OpenBSD function:
9 * Append at most n-1-strlen(dst) chars from src to dst and nul-terminate dst.
10 * Returns strlen(src) + strlen({original} dst), so truncation occurred if the
11 * return val is >= n.
12 * Note: If dst doesn't contain a nul in the first n chars, strlen(dst) is
13 * taken as n. */
14
15#include "_string.h"
16
17size_t strlcat(register char *__restrict dst,
18 register const char *__restrict src,
19 size_t n)
20{
21 size_t len;
22 char dummy[1];
23
24 len = 0;
25
26 while (1) {
27 if (len >= n) {
28 dst = dummy;
29 break;
30 }
31 if (!*dst) {
32 break;
33 }
34 ++dst;
35 ++len;
36 }
37
38 while ((*dst = *src) != 0) {
39 if (++len < n) {
40 ++dst;
41 }
42 ++src;
43 }
44
45 return len;
46}
47libc_hidden_def(strlcat)