michael@0: /* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */ michael@0: michael@0: /* michael@0: * Copyright (c) 1998 Todd C. Miller michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, michael@0: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY michael@0: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL michael@0: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; michael@0: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, michael@0: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR michael@0: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF michael@0: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #if defined(LIBC_SCCS) && !defined(lint) michael@0: static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $"; michael@0: #endif /* LIBC_SCCS and not lint */ michael@0: michael@0: #include michael@0: michael@0: #include "event2/event-config.h" michael@0: michael@0: #ifndef _EVENT_HAVE_STRLCPY michael@0: #include "strlcpy-internal.h" michael@0: michael@0: /* michael@0: * Copy src to string dst of size siz. At most siz-1 characters michael@0: * will be copied. Always NUL terminates (unless siz == 0). michael@0: * Returns strlen(src); if retval >= siz, truncation occurred. michael@0: */ michael@0: size_t michael@0: _event_strlcpy(dst, src, siz) michael@0: char *dst; michael@0: const char *src; michael@0: size_t siz; michael@0: { michael@0: register char *d = dst; michael@0: register const char *s = src; michael@0: register size_t n = siz; michael@0: michael@0: /* Copy as many bytes as will fit */ michael@0: if (n != 0 && --n != 0) { michael@0: do { michael@0: if ((*d++ = *s++) == 0) michael@0: break; michael@0: } while (--n != 0); michael@0: } michael@0: michael@0: /* Not enough room in dst, add NUL and traverse rest of src */ michael@0: if (n == 0) { michael@0: if (siz != 0) michael@0: *d = '\0'; /* NUL-terminate dst */ michael@0: while (*s++) michael@0: ; michael@0: } michael@0: michael@0: return (s - src - 1); /* count does not include NUL */ michael@0: } michael@0: #endif