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. 28420923e9SChris D. Faulhaber * 29420923e9SChris D. Faulhaber * $FreeBSD$ 30a41df9e3SWarner Losh */ 31a41df9e3SWarner Losh 32a41df9e3SWarner Losh #if defined(LIBC_SCCS) && !defined(lint) 33d64292d2SWarner Losh static char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $"; 34a41df9e3SWarner Losh #endif /* LIBC_SCCS and not lint */ 35a41df9e3SWarner Losh 36a41df9e3SWarner Losh #include <sys/types.h> 37a41df9e3SWarner Losh #include <string.h> 38a41df9e3SWarner Losh 39a41df9e3SWarner Losh /* 40a41df9e3SWarner Losh * Appends src to string dst of size siz (unlike strncat, siz is the 41a41df9e3SWarner Losh * full size of dst, not space left). At most siz-1 characters 42420923e9SChris D. Faulhaber * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 43420923e9SChris D. Faulhaber * Returns strlen(initial dst) + strlen(src); if retval >= siz, 44420923e9SChris D. Faulhaber * truncation occurred. 45a41df9e3SWarner Losh */ 46a41df9e3SWarner Losh size_t strlcat(dst, src, siz) 47a41df9e3SWarner Losh char *dst; 48a41df9e3SWarner Losh const char *src; 49a41df9e3SWarner Losh size_t siz; 50a41df9e3SWarner Losh { 51a41df9e3SWarner Losh register char *d = dst; 52a41df9e3SWarner Losh register const char *s = src; 53a41df9e3SWarner Losh register size_t n = siz; 54a41df9e3SWarner Losh size_t dlen; 55a41df9e3SWarner Losh 56d64292d2SWarner Losh /* Find the end of dst and adjust bytes left but don't go past end */ 57420923e9SChris D. Faulhaber while (n-- != 0 && *d != '\0') 58a41df9e3SWarner Losh d++; 59a41df9e3SWarner Losh dlen = d - dst; 60d64292d2SWarner Losh n = siz - dlen; 61a41df9e3SWarner Losh 62a41df9e3SWarner Losh if (n == 0) 63a41df9e3SWarner Losh return(dlen + strlen(s)); 64a41df9e3SWarner Losh while (*s != '\0') { 65a41df9e3SWarner Losh if (n != 1) { 66a41df9e3SWarner Losh *d++ = *s; 67a41df9e3SWarner Losh n--; 68a41df9e3SWarner Losh } 69a41df9e3SWarner Losh s++; 70a41df9e3SWarner Losh } 71a41df9e3SWarner Losh *d = '\0'; 72a41df9e3SWarner Losh 73a41df9e3SWarner Losh return(dlen + (s - src)); /* count does not include NUL */ 74a41df9e3SWarner Losh } 75