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