]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/String/strlcat.c
StdLib: Fix printf issues with floating point and wide character strings. Also resol...
[mirror_edk2.git] / StdLib / LibC / String / strlcat.c
CommitLineData
d7ce7006 1/* $NetBSD: strlcat.c,v 1.3 2007/06/04 18:19:27 christos Exp $ */\r
2/* $OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $ */\r
3\r
4/*\r
5 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>\r
6 *\r
7 * Permission to use, copy, modify, and distribute this software for any\r
8 * purpose with or without fee is hereby granted, provided that the above\r
9 * copyright notice and this permission notice appear in all copies.\r
10 *\r
11 * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL\r
12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE\r
14 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\r
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION\r
16 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN\r
17 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\r
18 */\r
19#include <LibConfig.h>\r
20\r
21#if !defined(_KERNEL) && !defined(_STANDALONE)\r
22#if HAVE_NBTOOL_CONFIG_H\r
23#include "nbtool_config.h"\r
24#endif\r
25\r
26#include <sys/cdefs.h>\r
27#if defined(LIBC_SCCS) && !defined(lint)\r
28__RCSID("$NetBSD: strlcat.c,v 1.3 2007/06/04 18:19:27 christos Exp $");\r
29#endif /* LIBC_SCCS and not lint */\r
30\r
31#ifdef _LIBC\r
32#include "namespace.h"\r
33#endif\r
34#include <sys/types.h>\r
35#include <assert.h>\r
36#include <string.h>\r
37\r
38#ifdef _LIBC\r
39# ifdef __weak_alias\r
40__weak_alias(strlcat, _strlcat)\r
41# endif\r
42#endif\r
43\r
44#else\r
45#include <lib/libkern/libkern.h>\r
46#endif /* !_KERNEL && !_STANDALONE */\r
47\r
48#ifndef HAVE_STRLCAT\r
49/*\r
50 * Appends src to string dst of size siz (unlike strncat, siz is the\r
51 * full size of dst, not space left). At most siz-1 characters\r
52 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).\r
53 * Returns strlen(src) + MIN(siz, strlen(initial dst)).\r
54 * If retval >= siz, truncation occurred.\r
55 */\r
56size_t\r
57strlcat(char *dst, const char *src, size_t siz)\r
58{\r
59 char *d = dst;\r
60 const char *s = src;\r
61 size_t n = siz;\r
62 size_t dlen;\r
63\r
64 _DIAGASSERT(dst != NULL);\r
65 _DIAGASSERT(src != NULL);\r
66\r
67 /* Find the end of dst and adjust bytes left but don't go past end */\r
68 while (n-- != 0 && *d != '\0')\r
69 d++;\r
70 dlen = d - dst;\r
71 n = siz - dlen;\r
72\r
73 if (n == 0)\r
74 return(dlen + strlen(s));\r
75 while (*s != '\0') {\r
76 if (n != 1) {\r
77 *d++ = *s;\r
78 n--;\r
79 }\r
80 s++;\r
81 }\r
82 *d = '\0';\r
83\r
84 return(dlen + (s - src)); /* count does not include NUL */\r
85}\r
86#endif\r