1238104Sdes/* from openssh 4.3p2 compat/strlcpy.c */
2238104Sdes/*
3238104Sdes * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
4238104Sdes *
5238104Sdes * Permission to use, copy, modify, and distribute this software for any
6238104Sdes * purpose with or without fee is hereby granted, provided that the above
7238104Sdes * copyright notice and this permission notice appear in all copies.
8238104Sdes *
9238104Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10238104Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11238104Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12238104Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13238104Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14238104Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15238104Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16238104Sdes */
17238104Sdes
18238104Sdes/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */
19238104Sdes
20238104Sdes#include <ldns/config.h>
21238104Sdes#ifndef HAVE_STRLCPY
22238104Sdes
23238104Sdes#include <sys/types.h>
24238104Sdes#include <string.h>
25238104Sdes
26238104Sdes/*
27238104Sdes * Copy src to string dst of size siz.  At most siz-1 characters
28238104Sdes * will be copied.  Always NUL terminates (unless siz == 0).
29238104Sdes * Returns strlen(src); if retval >= siz, truncation occurred.
30238104Sdes */
31238104Sdessize_t
32238104Sdesstrlcpy(char *dst, const char *src, size_t siz)
33238104Sdes{
34238104Sdes	char *d = dst;
35238104Sdes	const char *s = src;
36238104Sdes	size_t n = siz;
37238104Sdes
38238104Sdes	/* Copy as many bytes as will fit */
39238104Sdes	if (n != 0 && --n != 0) {
40238104Sdes		do {
41238104Sdes			if ((*d++ = *s++) == 0)
42238104Sdes				break;
43238104Sdes		} while (--n != 0);
44238104Sdes	}
45238104Sdes
46238104Sdes	/* Not enough room in dst, add NUL and traverse rest of src */
47238104Sdes	if (n == 0) {
48238104Sdes		if (siz != 0)
49238104Sdes			*d = '\0';		/* NUL-terminate dst */
50238104Sdes		while (*s++)
51238104Sdes			;
52238104Sdes	}
53238104Sdes
54238104Sdes	return(s - src - 1);	/* count does not include NUL */
55238104Sdes}
56238104Sdes
57238104Sdes#endif /* !HAVE_STRLCPY */
58