155714Skris#ifdef HAVE_CONFIG_H 255714Skris#include "config.h" 355714Skris#endif 455714Skris 555714Skris#ifdef HAVE_STRLCPY 655714Skris 755714Skrisint dummy; 8205128Ssimon 9205128Ssimon#else 1055714Skris 11205128Ssimon/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ 1255714Skris 1355714Skris/* 1455714Skris * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 1555714Skris * 1655714Skris * Permission to use, copy, modify, and distribute this software for any 1755714Skris * purpose with or without fee is hereby granted, provided that the above 1855714Skris * copyright notice and this permission notice appear in all copies. 19205128Ssimon * 20205128Ssimon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 2155714Skris * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 2255714Skris * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 2355714Skris * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 2455714Skris * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 2555714Skris * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 2655714Skris * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 2755714Skris */ 2855714Skris 2955714Skris#include <sys/types.h> 3055714Skris#include <string.h> 3155714Skris 32205128Ssimon/* 33205128Ssimon * Copy src to string dst of size siz. At most siz-1 characters 34205128Ssimon * will be copied. Always NUL terminates (unless siz == 0). 35205128Ssimon * Returns strlen(src); if retval >= siz, truncation occurred. 36205128Ssimon */ 37205128Ssimonsize_t 38205128Ssimonstrlcpy(char *dst, const char *src, size_t siz) 39205128Ssimon{ 40205128Ssimon char *d = dst; 41205128Ssimon const char *s = src; 42205128Ssimon size_t n = siz; 43205128Ssimon 44205128Ssimon /* Copy as many bytes as will fit */ 45205128Ssimon if (n != 0) { 46205128Ssimon while (--n != 0) { 47205128Ssimon if ((*d++ = *s++) == '\0') 4855714Skris break; 49205128Ssimon } 50205128Ssimon } 51205128Ssimon 52205128Ssimon /* Not enough room in dst, add NUL and traverse rest of src */ 53205128Ssimon if (n == 0) { 54205128Ssimon if (siz != 0) 55205128Ssimon *d = '\0'; /* NUL-terminate dst */ 56205128Ssimon while (*s++) 57205128Ssimon ; 58205128Ssimon } 59205128Ssimon 60205128Ssimon return(s - src - 1); /* count does not include NUL */ 61160814Ssimon} 6255714Skris 63205128Ssimon#endif 64160814Ssimon