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