Blame


1 24fb1a39 2025-06-02 kirill /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
2 24fb1a39 2025-06-02 kirill
3 24fb1a39 2025-06-02 kirill /*
4 24fb1a39 2025-06-02 kirill * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 24fb1a39 2025-06-02 kirill *
6 24fb1a39 2025-06-02 kirill * Permission to use, copy, modify, and distribute this software for any
7 24fb1a39 2025-06-02 kirill * purpose with or without fee is hereby granted, provided that the above
8 24fb1a39 2025-06-02 kirill * copyright notice and this permission notice appear in all copies.
9 24fb1a39 2025-06-02 kirill *
10 24fb1a39 2025-06-02 kirill * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 24fb1a39 2025-06-02 kirill * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 24fb1a39 2025-06-02 kirill * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 24fb1a39 2025-06-02 kirill * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 24fb1a39 2025-06-02 kirill * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 24fb1a39 2025-06-02 kirill * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 24fb1a39 2025-06-02 kirill * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 24fb1a39 2025-06-02 kirill */
18 24fb1a39 2025-06-02 kirill
19 24fb1a39 2025-06-02 kirill /* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */
20 24fb1a39 2025-06-02 kirill
21 24fb1a39 2025-06-02 kirill #include "openbsd-compat.h"
22 24fb1a39 2025-06-02 kirill
23 24fb1a39 2025-06-02 kirill #include <sys/types.h>
24 24fb1a39 2025-06-02 kirill #include <string.h>
25 24fb1a39 2025-06-02 kirill
26 24fb1a39 2025-06-02 kirill /*
27 24fb1a39 2025-06-02 kirill * Copy src to string dst of size siz. At most siz-1 characters
28 24fb1a39 2025-06-02 kirill * will be copied. Always NUL terminates (unless siz == 0).
29 24fb1a39 2025-06-02 kirill * Returns strlen(src); if retval >= siz, truncation occurred.
30 24fb1a39 2025-06-02 kirill */
31 24fb1a39 2025-06-02 kirill size_t
32 24fb1a39 2025-06-02 kirill strlcpy(char *dst, const char *src, size_t siz)
33 24fb1a39 2025-06-02 kirill {
34 24fb1a39 2025-06-02 kirill char *d = dst;
35 24fb1a39 2025-06-02 kirill const char *s = src;
36 24fb1a39 2025-06-02 kirill size_t n = siz;
37 24fb1a39 2025-06-02 kirill
38 24fb1a39 2025-06-02 kirill /* Copy as many bytes as will fit */
39 24fb1a39 2025-06-02 kirill if (n != 0) {
40 24fb1a39 2025-06-02 kirill while (--n != 0) {
41 24fb1a39 2025-06-02 kirill if ((*d++ = *s++) == '\0')
42 24fb1a39 2025-06-02 kirill break;
43 24fb1a39 2025-06-02 kirill }
44 24fb1a39 2025-06-02 kirill }
45 24fb1a39 2025-06-02 kirill
46 24fb1a39 2025-06-02 kirill /* Not enough room in dst, add NUL and traverse rest of src */
47 24fb1a39 2025-06-02 kirill if (n == 0) {
48 24fb1a39 2025-06-02 kirill if (siz != 0)
49 24fb1a39 2025-06-02 kirill *d = '\0'; /* NUL-terminate dst */
50 24fb1a39 2025-06-02 kirill while (*s++)
51 24fb1a39 2025-06-02 kirill ;
52 24fb1a39 2025-06-02 kirill }
53 24fb1a39 2025-06-02 kirill
54 24fb1a39 2025-06-02 kirill return(s - src - 1); /* count does not include NUL */
55 24fb1a39 2025-06-02 kirill }