xref: /freebsd/contrib/openbsm/compat/strlcpy.h (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
152267f74SRobert Watson /*
252267f74SRobert Watson  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
352267f74SRobert Watson  * All rights reserved.
452267f74SRobert Watson  *
552267f74SRobert Watson  * Redistribution and use in source and binary forms, with or without
652267f74SRobert Watson  * modification, are permitted provided that the following conditions
752267f74SRobert Watson  * are met:
852267f74SRobert Watson  * 1. Redistributions of source code must retain the above copyright
952267f74SRobert Watson  *    notice, this list of conditions and the following disclaimer.
1052267f74SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1152267f74SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
1252267f74SRobert Watson  *    documentation and/or other materials provided with the distribution.
1352267f74SRobert Watson  * 3. The name of the author may not be used to endorse or promote products
1452267f74SRobert Watson  *    derived from this software without specific prior written permission.
1552267f74SRobert Watson  *
1652267f74SRobert Watson  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1752267f74SRobert Watson  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
1852267f74SRobert Watson  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
1952267f74SRobert Watson  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2052267f74SRobert Watson  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2152267f74SRobert Watson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2252267f74SRobert Watson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2352267f74SRobert Watson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2452267f74SRobert Watson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2552267f74SRobert Watson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2652267f74SRobert Watson  *
2752267f74SRobert Watson  * dollar OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp dollar
2852267f74SRobert Watson  */
2952267f74SRobert Watson 
3052267f74SRobert Watson /*
3152267f74SRobert Watson  * Copy src to string dst of size siz.  At most siz-1 characters
3252267f74SRobert Watson  * will be copied.  Always NUL terminates (unless siz == 0).
3352267f74SRobert Watson  * Returns strlen(src); if retval >= siz, truncation occurred.
3452267f74SRobert Watson  */
3552267f74SRobert Watson static size_t
strlcpy(dst,src,siz)3652267f74SRobert Watson strlcpy(dst, src, siz)
3752267f74SRobert Watson 	char *dst;
3852267f74SRobert Watson 	const char *src;
3952267f74SRobert Watson 	size_t siz;
4052267f74SRobert Watson {
4152267f74SRobert Watson 	char *d = dst;
4252267f74SRobert Watson 	const char *s = src;
4352267f74SRobert Watson 	size_t n = siz;
4452267f74SRobert Watson 
4552267f74SRobert Watson 	/* Copy as many bytes as will fit */
4652267f74SRobert Watson 	if (n != 0 && --n != 0) {
4752267f74SRobert Watson 		do {
4852267f74SRobert Watson 			if ((*d++ = *s++) == 0)
4952267f74SRobert Watson 				break;
5052267f74SRobert Watson 		} while (--n != 0);
5152267f74SRobert Watson 	}
5252267f74SRobert Watson 
5352267f74SRobert Watson 	/* Not enough room in dst, add NUL and traverse rest of src */
5452267f74SRobert Watson 	if (n == 0) {
5552267f74SRobert Watson 		if (siz != 0)
5652267f74SRobert Watson 			*d = '\0';		/* NUL-terminate dst */
5752267f74SRobert Watson 		while (*s++)
5852267f74SRobert Watson 			;
5952267f74SRobert Watson 	}
6052267f74SRobert Watson 
6152267f74SRobert Watson 	return(s - src - 1);	/* count does not include NUL */
6252267f74SRobert Watson }
63