1192348Sdelphij/* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ 2192348Sdelphij 3192348Sdelphij/* 4192348Sdelphij * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 5192348Sdelphij * 6192348Sdelphij * Permission to use, copy, modify, and distribute this software for any 7192348Sdelphij * purpose with or without fee is hereby granted, provided that the above 8192348Sdelphij * copyright notice and this permission notice appear in all copies. 9192348Sdelphij * 10192348Sdelphij * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11192348Sdelphij * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12192348Sdelphij * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13192348Sdelphij * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14192348Sdelphij * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15192348Sdelphij * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16192348Sdelphij * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17192348Sdelphij */ 18192348Sdelphij 19192348Sdelphij/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */ 20192348Sdelphij#include "file.h" 21192348Sdelphij 22192348Sdelphij#include <sys/types.h> 23192348Sdelphij#include <string.h> 24192348Sdelphij 25192348Sdelphij/* 26192348Sdelphij * Copy src to string dst of size siz. At most siz-1 characters 27192348Sdelphij * will be copied. Always NUL terminates (unless siz == 0). 28192348Sdelphij * Returns strlen(src); if retval >= siz, truncation occurred. 29192348Sdelphij */ 30192348Sdelphijsize_t 31192348Sdelphijstrlcpy(char *dst, const char *src, size_t siz) 32192348Sdelphij{ 33192348Sdelphij char *d = dst; 34192348Sdelphij const char *s = src; 35192348Sdelphij size_t n = siz; 36192348Sdelphij 37192348Sdelphij /* Copy as many bytes as will fit */ 38192348Sdelphij if (n != 0 && --n != 0) { 39192348Sdelphij do { 40192348Sdelphij if ((*d++ = *s++) == 0) 41192348Sdelphij break; 42192348Sdelphij } while (--n != 0); 43192348Sdelphij } 44192348Sdelphij 45192348Sdelphij /* Not enough room in dst, add NUL and traverse rest of src */ 46192348Sdelphij if (n == 0) { 47192348Sdelphij if (siz != 0) 48192348Sdelphij *d = '\0'; /* NUL-terminate dst */ 49192348Sdelphij while (*s++) 50192348Sdelphij ; 51192348Sdelphij } 52192348Sdelphij 53192348Sdelphij return(s - src - 1); /* count does not include NUL */ 54192348Sdelphij} 55