1d64292d2SWarner Losh /* $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ */ 2a41df9e3SWarner Losh 3a41df9e3SWarner Losh /* 4a41df9e3SWarner Losh * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 5a41df9e3SWarner Losh * All rights reserved. 6a41df9e3SWarner Losh * 7a41df9e3SWarner Losh * Redistribution and use in source and binary forms, with or without 8a41df9e3SWarner Losh * modification, are permitted provided that the following conditions 9a41df9e3SWarner Losh * are met: 10a41df9e3SWarner Losh * 1. Redistributions of source code must retain the above copyright 11a41df9e3SWarner Losh * notice, this list of conditions and the following disclaimer. 12a41df9e3SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 13a41df9e3SWarner Losh * notice, this list of conditions and the following disclaimer in the 14a41df9e3SWarner Losh * documentation and/or other materials provided with the distribution. 15a41df9e3SWarner Losh * 3. The name of the author may not be used to endorse or promote products 16a41df9e3SWarner Losh * derived from this software without specific prior written permission. 17a41df9e3SWarner Losh * 18a41df9e3SWarner Losh * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19a41df9e3SWarner Losh * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20a41df9e3SWarner Losh * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21a41df9e3SWarner Losh * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22a41df9e3SWarner Losh * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23a41df9e3SWarner Losh * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24a41df9e3SWarner Losh * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25a41df9e3SWarner Losh * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26a41df9e3SWarner Losh * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27a41df9e3SWarner Losh * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28a41df9e3SWarner Losh */ 29a41df9e3SWarner Losh 30a41df9e3SWarner Losh #if defined(LIBC_SCCS) && !defined(lint) 31d64292d2SWarner Losh static char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $"; 32a41df9e3SWarner Losh #endif /* LIBC_SCCS and not lint */ 33a41df9e3SWarner Losh 34a41df9e3SWarner Losh #include <sys/types.h> 35a41df9e3SWarner Losh #include <string.h> 36a41df9e3SWarner Losh 37a41df9e3SWarner Losh /* 38a41df9e3SWarner Losh * Appends src to string dst of size siz (unlike strncat, siz is the 39a41df9e3SWarner Losh * full size of dst, not space left). At most siz-1 characters 40a41df9e3SWarner Losh * will be copied. Always NUL terminates (unless siz == 0). 41a41df9e3SWarner Losh * Returns strlen(src); if retval >= siz, truncation occurred. 42a41df9e3SWarner Losh */ 43a41df9e3SWarner Losh size_t strlcat(dst, src, siz) 44a41df9e3SWarner Losh char *dst; 45a41df9e3SWarner Losh const char *src; 46a41df9e3SWarner Losh size_t siz; 47a41df9e3SWarner Losh { 48a41df9e3SWarner Losh register char *d = dst; 49a41df9e3SWarner Losh register const char *s = src; 50a41df9e3SWarner Losh register size_t n = siz; 51a41df9e3SWarner Losh size_t dlen; 52a41df9e3SWarner Losh 53d64292d2SWarner Losh /* Find the end of dst and adjust bytes left but don't go past end */ 54d64292d2SWarner Losh while (*d != '\0' && n-- != 0) 55a41df9e3SWarner Losh d++; 56a41df9e3SWarner Losh dlen = d - dst; 57d64292d2SWarner Losh n = siz - dlen; 58a41df9e3SWarner Losh 59a41df9e3SWarner Losh if (n == 0) 60a41df9e3SWarner Losh return(dlen + strlen(s)); 61a41df9e3SWarner Losh while (*s != '\0') { 62a41df9e3SWarner Losh if (n != 1) { 63a41df9e3SWarner Losh *d++ = *s; 64a41df9e3SWarner Losh n--; 65a41df9e3SWarner Losh } 66a41df9e3SWarner Losh s++; 67a41df9e3SWarner Losh } 68a41df9e3SWarner Losh *d = '\0'; 69a41df9e3SWarner Losh 70a41df9e3SWarner Losh return(dlen + (s - src)); /* count does not include NUL */ 71a41df9e3SWarner Losh } 72