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