strlcpy.c revision 126274
1126274Sdes/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */ 2126274Sdes 3124208Sdes/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ 498937Sdes 598937Sdes/* 698937Sdes * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 798937Sdes * 8124208Sdes * Permission to use, copy, modify, and distribute this software for any 9124208Sdes * purpose with or without fee is hereby granted, provided that the above 10124208Sdes * copyright notice and this permission notice appear in all copies. 1198937Sdes * 12124208Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13124208Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14124208Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15124208Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16124208Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17124208Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18124208Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1998937Sdes */ 2098937Sdes 21106121Sdes#include "includes.h" 2298937Sdes#ifndef HAVE_STRLCPY 2398937Sdes 2498937Sdes#if defined(LIBC_SCCS) && !defined(lint) 25124208Sdesstatic char *rcsid = "$OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $"; 2698937Sdes#endif /* LIBC_SCCS and not lint */ 2798937Sdes 2898937Sdes#include <sys/types.h> 2998937Sdes#include <string.h> 3098937Sdes 3198937Sdes/* 3298937Sdes * Copy src to string dst of size siz. At most siz-1 characters 3398937Sdes * will be copied. Always NUL terminates (unless siz == 0). 3498937Sdes * Returns strlen(src); if retval >= siz, truncation occurred. 3598937Sdes */ 3698937Sdessize_t 37124208Sdesstrlcpy(char *dst, const char *src, size_t siz) 3898937Sdes{ 3998937Sdes register char *d = dst; 4098937Sdes register const char *s = src; 4198937Sdes register size_t n = siz; 4298937Sdes 4398937Sdes /* Copy as many bytes as will fit */ 4498937Sdes if (n != 0 && --n != 0) { 4598937Sdes do { 4698937Sdes if ((*d++ = *s++) == 0) 4798937Sdes break; 4898937Sdes } while (--n != 0); 4998937Sdes } 5098937Sdes 5198937Sdes /* Not enough room in dst, add NUL and traverse rest of src */ 5298937Sdes if (n == 0) { 5398937Sdes if (siz != 0) 5498937Sdes *d = '\0'; /* NUL-terminate dst */ 5598937Sdes while (*s++) 5698937Sdes ; 5798937Sdes } 5898937Sdes 5998937Sdes return(s - src - 1); /* count does not include NUL */ 6098937Sdes} 6198937Sdes 6298937Sdes#endif /* !HAVE_STRLCPY */ 63