strlcpy.c revision 1.3.78.2 1 1.3.78.1 martin /* $NetBSD: strlcpy.c,v 1.3.78.2 2020/04/21 19:37:51 martin Exp $ */
2 1.1 christos /* $OpenBSD: strlcpy.c,v 1.7 2003/04/12 21:56:39 millert Exp $ */
3 1.1 christos
4 1.1 christos /*
5 1.1 christos * Copyright (c) 1998 Todd C. Miller <Todd.Miller (at) courtesan.com>
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
12 1.1 christos * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 1.1 christos * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
14 1.1 christos * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16 1.1 christos * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17 1.1 christos * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #if !defined(_KERNEL) && !defined(_STANDALONE)
21 1.1 christos #if HAVE_NBTOOL_CONFIG_H
22 1.1 christos #include "nbtool_config.h"
23 1.1 christos #endif
24 1.1 christos
25 1.1 christos #include <sys/cdefs.h>
26 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
27 1.3.78.1 martin __RCSID("$NetBSD: strlcpy.c,v 1.3.78.2 2020/04/21 19:37:51 martin Exp $");
28 1.1 christos #endif /* LIBC_SCCS and not lint */
29 1.1 christos
30 1.1 christos #ifdef _LIBC
31 1.1 christos #include "namespace.h"
32 1.1 christos #endif
33 1.1 christos #include <sys/types.h>
34 1.1 christos #include <assert.h>
35 1.1 christos #include <string.h>
36 1.1 christos
37 1.1 christos #ifdef _LIBC
38 1.1 christos # ifdef __weak_alias
39 1.1 christos __weak_alias(strlcpy, _strlcpy)
40 1.1 christos # endif
41 1.1 christos #endif
42 1.1 christos #else
43 1.1 christos #include <lib/libkern/libkern.h>
44 1.1 christos #endif /* !_KERNEL && !_STANDALONE */
45 1.1 christos
46 1.1 christos
47 1.1 christos #if !HAVE_STRLCPY
48 1.1 christos /*
49 1.1 christos * Copy src to string dst of size siz. At most siz-1 characters
50 1.1 christos * will be copied. Always NUL terminates (unless siz == 0).
51 1.1 christos * Returns strlen(src); if retval >= siz, truncation occurred.
52 1.1 christos */
53 1.1 christos size_t
54 1.3 christos strlcpy(char *dst, const char *src, size_t siz)
55 1.1 christos {
56 1.1 christos char *d = dst;
57 1.1 christos const char *s = src;
58 1.1 christos size_t n = siz;
59 1.1 christos
60 1.1 christos _DIAGASSERT(dst != NULL);
61 1.1 christos _DIAGASSERT(src != NULL);
62 1.1 christos
63 1.1 christos /* Copy as many bytes as will fit */
64 1.1 christos if (n != 0 && --n != 0) {
65 1.1 christos do {
66 1.1 christos if ((*d++ = *s++) == 0)
67 1.1 christos break;
68 1.1 christos } while (--n != 0);
69 1.1 christos }
70 1.1 christos
71 1.1 christos /* Not enough room in dst, add NUL and traverse rest of src */
72 1.1 christos if (n == 0) {
73 1.1 christos if (siz != 0)
74 1.1 christos *d = '\0'; /* NUL-terminate dst */
75 1.1 christos while (*s++)
76 1.1 christos ;
77 1.1 christos }
78 1.1 christos
79 1.1 christos return(s - src - 1); /* count does not include NUL */
80 1.1 christos }
81 1.1 christos #endif
82