1184902Srwatson/*
2184902Srwatson * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
3184902Srwatson * All rights reserved.
4184902Srwatson *
5184902Srwatson * Redistribution and use in source and binary forms, with or without
6184902Srwatson * modification, are permitted provided that the following conditions
7184902Srwatson * are met:
8184902Srwatson * 1. Redistributions of source code must retain the above copyright
9184902Srwatson *    notice, this list of conditions and the following disclaimer.
10184902Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11184902Srwatson *    notice, this list of conditions and the following disclaimer in the
12184902Srwatson *    documentation and/or other materials provided with the distribution.
13184902Srwatson * 3. The name of the author may not be used to endorse or promote products
14184902Srwatson *    derived from this software without specific prior written permission.
15184902Srwatson *
16184902Srwatson * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17184902Srwatson * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18184902Srwatson * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
19184902Srwatson * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20184902Srwatson * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21184902Srwatson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22184902Srwatson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23184902Srwatson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24184902Srwatson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25184902Srwatson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26184902Srwatson *
27184902Srwatson * dollar OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp dollar
28184902Srwatson */
29184902Srwatson
30184902Srwatson/*
31184902Srwatson * Copy src to string dst of size siz.  At most siz-1 characters
32184902Srwatson * will be copied.  Always NUL terminates (unless siz == 0).
33184902Srwatson * Returns strlen(src); if retval >= siz, truncation occurred.
34184902Srwatson */
35184902Srwatsonstatic size_t
36184902Srwatsonstrlcpy(dst, src, siz)
37184902Srwatson	char *dst;
38184902Srwatson	const char *src;
39184902Srwatson	size_t siz;
40184902Srwatson{
41184902Srwatson	char *d = dst;
42184902Srwatson	const char *s = src;
43184902Srwatson	size_t n = siz;
44184902Srwatson
45184902Srwatson	/* Copy as many bytes as will fit */
46184902Srwatson	if (n != 0 && --n != 0) {
47184902Srwatson		do {
48184902Srwatson			if ((*d++ = *s++) == 0)
49184902Srwatson				break;
50184902Srwatson		} while (--n != 0);
51184902Srwatson	}
52184902Srwatson
53184902Srwatson	/* Not enough room in dst, add NUL and traverse rest of src */
54184902Srwatson	if (n == 0) {
55184902Srwatson		if (siz != 0)
56184902Srwatson			*d = '\0';		/* NUL-terminate dst */
57184902Srwatson		while (*s++)
58184902Srwatson			;
59184902Srwatson	}
60184902Srwatson
61184902Srwatson	return(s - src - 1);	/* count does not include NUL */
62184902Srwatson}
63