1238106Sdes/* from openssh 4.3p2 compat/strlcpy.c */
2238106Sdes/*
3238106Sdes * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
4238106Sdes *
5238106Sdes * Permission to use, copy, modify, and distribute this software for any
6238106Sdes * purpose with or without fee is hereby granted, provided that the above
7238106Sdes * copyright notice and this permission notice appear in all copies.
8238106Sdes *
9238106Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10238106Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11238106Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12238106Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13238106Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14238106Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15238106Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16238106Sdes */
17238106Sdes
18238106Sdes/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */
19238106Sdes
20238106Sdes#include <config.h>
21238106Sdes#ifndef HAVE_STRLCPY
22238106Sdes
23238106Sdes#include <sys/types.h>
24238106Sdes#include <string.h>
25238106Sdes
26238106Sdes/*
27238106Sdes * Copy src to string dst of size siz.  At most siz-1 characters
28238106Sdes * will be copied.  Always NUL terminates (unless siz == 0).
29238106Sdes * Returns strlen(src); if retval >= siz, truncation occurred.
30238106Sdes */
31238106Sdessize_t
32238106Sdesstrlcpy(char *dst, const char *src, size_t siz)
33238106Sdes{
34238106Sdes	char *d = dst;
35238106Sdes	const char *s = src;
36238106Sdes	size_t n = siz;
37238106Sdes
38238106Sdes	/* Copy as many bytes as will fit */
39238106Sdes	if (n != 0 && --n != 0) {
40238106Sdes		do {
41238106Sdes			if ((*d++ = *s++) == 0)
42238106Sdes				break;
43238106Sdes		} while (--n != 0);
44238106Sdes	}
45238106Sdes
46238106Sdes	/* Not enough room in dst, add NUL and traverse rest of src */
47238106Sdes	if (n == 0) {
48238106Sdes		if (siz != 0)
49238106Sdes			*d = '\0';		/* NUL-terminate dst */
50238106Sdes		while (*s++)
51238106Sdes			;
52238106Sdes	}
53238106Sdes
54238106Sdes	return(s - src - 1);	/* count does not include NUL */
55238106Sdes}
56238106Sdes
57238106Sdes#endif /* !HAVE_STRLCPY */
58