1*f73a5f05Schristos /* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */ 24a71e5f3Schristos 34a71e5f3Schristos /* 44a71e5f3Schristos * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> 54a71e5f3Schristos * 64a71e5f3Schristos * Permission to use, copy, modify, and distribute this software for any 74a71e5f3Schristos * purpose with or without fee is hereby granted, provided that the above 84a71e5f3Schristos * copyright notice and this permission notice appear in all copies. 94a71e5f3Schristos * 104a71e5f3Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 114a71e5f3Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 124a71e5f3Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 134a71e5f3Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 144a71e5f3Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 154a71e5f3Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 164a71e5f3Schristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 174a71e5f3Schristos */ 184a71e5f3Schristos 194a71e5f3Schristos #include <config.h> 204a71e5f3Schristos 214a71e5f3Schristos #include <stddef.h> 224a71e5f3Schristos #include <string.h> 234a71e5f3Schristos 244a71e5f3Schristos #include "portability.h" 254a71e5f3Schristos 264a71e5f3Schristos /* 274a71e5f3Schristos * Appends src to string dst of size dsize (unlike strncat, dsize is the 284a71e5f3Schristos * full size of dst, not space left). At most dsize-1 characters 294a71e5f3Schristos * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). 304a71e5f3Schristos * Returns strlen(src) + MIN(dsize, strlen(initial dst)). 314a71e5f3Schristos * If retval >= dsize, truncation occurred. 324a71e5f3Schristos */ 334a71e5f3Schristos size_t 34*f73a5f05Schristos pcapint_strlcat(char * restrict dst, const char * restrict src, size_t dsize) 354a71e5f3Schristos { 364a71e5f3Schristos const char *odst = dst; 374a71e5f3Schristos const char *osrc = src; 384a71e5f3Schristos size_t n = dsize; 394a71e5f3Schristos size_t dlen; 404a71e5f3Schristos 414a71e5f3Schristos /* Find the end of dst and adjust bytes left but don't go past end. */ 424a71e5f3Schristos while (n-- != 0 && *dst != '\0') 434a71e5f3Schristos dst++; 444a71e5f3Schristos dlen = dst - odst; 454a71e5f3Schristos n = dsize - dlen; 464a71e5f3Schristos 474a71e5f3Schristos if (n-- == 0) 484a71e5f3Schristos return(dlen + strlen(src)); 494a71e5f3Schristos while (*src != '\0') { 504a71e5f3Schristos if (n != 0) { 514a71e5f3Schristos *dst++ = *src; 524a71e5f3Schristos n--; 534a71e5f3Schristos } 544a71e5f3Schristos src++; 554a71e5f3Schristos } 564a71e5f3Schristos *dst = '\0'; 574a71e5f3Schristos 584a71e5f3Schristos return(dlen + (src - osrc)); /* count does not include NUL */ 594a71e5f3Schristos } 60