strlcpy.c revision 139815
149594Simp/*	$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $	*/
249593Simp
3139815Simp/*-
449593Simp * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
549593Simp * All rights reserved.
649593Simp *
749593Simp * Redistribution and use in source and binary forms, with or without
849593Simp * modification, are permitted provided that the following conditions
949593Simp * are met:
1049593Simp * 1. Redistributions of source code must retain the above copyright
1149593Simp *    notice, this list of conditions and the following disclaimer.
1249593Simp * 2. Redistributions in binary form must reproduce the above copyright
1349593Simp *    notice, this list of conditions and the following disclaimer in the
1449593Simp *    documentation and/or other materials provided with the distribution.
1549593Simp * 3. The name of the author may not be used to endorse or promote products
1649593Simp *    derived from this software without specific prior written permission.
1749593Simp *
1849593Simp * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1949593Simp * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2049593Simp * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
2149593Simp * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2249593Simp * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2349593Simp * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2449593Simp * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2549593Simp * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2649593Simp * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2749593Simp * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2849593Simp */
2949593Simp
3049593Simp#if defined(LIBC_SCCS) && !defined(lint)
3186170Sobrienstatic char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $");
3249593Simp#endif /* LIBC_SCCS and not lint */
3386170Sobrien#include <sys/cdefs.h>
3486170Sobrien__FBSDID("$FreeBSD: head/sys/libkern/strlcpy.c 139815 2005-01-07 00:24:33Z imp $");
3549593Simp
3649593Simp#include <sys/types.h>
37103000Speter#include <sys/libkern.h>
3849593Simp
3949593Simp/*
4049593Simp * Copy src to string dst of size siz.  At most siz-1 characters
4149593Simp * will be copied.  Always NUL terminates (unless siz == 0).
4249593Simp * Returns strlen(src); if retval >= siz, truncation occurred.
4349593Simp */
4449593Simpsize_t strlcpy(dst, src, siz)
4549593Simp	char *dst;
4649593Simp	const char *src;
4749593Simp	size_t siz;
4849593Simp{
4992889Sobrien	char *d = dst;
5092889Sobrien	const char *s = src;
5192889Sobrien	size_t n = siz;
5249593Simp
5349594Simp	/* Copy as many bytes as will fit */
5449594Simp	if (n != 0 && --n != 0) {
5549594Simp		do {
5649594Simp			if ((*d++ = *s++) == 0)
5749594Simp				break;
5849594Simp		} while (--n != 0);
5949593Simp	}
6049593Simp
6149594Simp	/* Not enough room in dst, add NUL and traverse rest of src */
6249594Simp	if (n == 0) {
6349594Simp		if (siz != 0)
6449594Simp			*d = '\0';		/* NUL-terminate dst */
6549594Simp		while (*s++)
6649594Simp			;
6749594Simp	}
6849594Simp
6949594Simp	return(s - src - 1);	/* count does not include NUL */
7049593Simp}
71