175115Sfenner/*	$NetBSD: strlcpy.c,v 1.5 1999/09/20 04:39:47 lukem Exp $	*/
275115Sfenner/*	from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp 	*/
375115Sfenner
475115Sfenner/*
575115Sfenner * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
675115Sfenner * All rights reserved.
775115Sfenner *
875115Sfenner * Redistribution and use in source and binary forms, with or without
975115Sfenner * modification, are permitted provided that the following conditions
1075115Sfenner * are met:
1175115Sfenner * 1. Redistributions of source code must retain the above copyright
1275115Sfenner *    notice, this list of conditions and the following disclaimer.
1375115Sfenner * 2. Redistributions in binary form must reproduce the above copyright
1475115Sfenner *    notice, this list of conditions and the following disclaimer in the
1575115Sfenner *    documentation and/or other materials provided with the distribution.
1675115Sfenner * 3. The name of the author may not be used to endorse or promote products
1775115Sfenner *    derived from this software without specific prior written permission.
1875115Sfenner *
1975115Sfenner * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2075115Sfenner * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2175115Sfenner * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
2275115Sfenner * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2375115Sfenner * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2475115Sfenner * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2575115Sfenner * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2675115Sfenner * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2775115Sfenner * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2875115Sfenner * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2975115Sfenner */
3075115Sfenner
3175115Sfenner#ifndef lint
32127668Sbmsstatic const char rcsid[] _U_ =
33190207Srpaulo    "@(#) $Header: /tcpdump/master/tcpdump/missing/strlcpy.c,v 1.5 2003-11-16 09:36:52 guy Exp $ (LBL)";
3475115Sfenner#endif
3575115Sfenner
36127668Sbms#ifdef HAVE_CONFIG_H
3775115Sfenner#include <config.h>
38127668Sbms#endif
3975115Sfenner
40127668Sbms#include <tcpdump-stdinc.h>
41127668Sbms
4275115Sfenner#include <string.h>
4375115Sfenner
4475115Sfenner/*
4575115Sfenner * Copy src to string dst of size siz.  At most siz-1 characters
4675115Sfenner * will be copied.  Always NUL terminates (unless siz == 0).
4775115Sfenner * Returns strlen(src); if retval >= siz, truncation occurred.
4875115Sfenner */
4975115Sfennersize_t
50127668Sbmsstrlcpy(char *dst, const char *src, size_t siz)
5175115Sfenner{
5275115Sfenner	register char *d = dst;
5375115Sfenner	register const char *s = src;
5475115Sfenner	register size_t n = siz;
5575115Sfenner
5675115Sfenner	/* Copy as many bytes as will fit */
5775115Sfenner	if (n != 0 && --n != 0) {
5875115Sfenner		do {
5975115Sfenner			if ((*d++ = *s++) == 0)
6075115Sfenner				break;
6175115Sfenner		} while (--n != 0);
6275115Sfenner	}
6375115Sfenner
6475115Sfenner	/* Not enough room in dst, add NUL and traverse rest of src */
6575115Sfenner	if (n == 0) {
6675115Sfenner		if (siz != 0)
6775115Sfenner			*d = '\0';		/* NUL-terminate dst */
6875115Sfenner		while (*s++)
6975115Sfenner			;
7075115Sfenner	}
7175115Sfenner
7275115Sfenner	return(s - src - 1);	/* count does not include NUL */
7375115Sfenner}
74