strlcat.c revision 1.1.1.3
1251881Speter/* NetBSD: strlcat.c,v 1.5 1999/09/20 04:39:47 lukem Exp */ 2251881Speter/* from OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp */ 3251881Speter 4251881Speter/* 5251881Speter * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 6251881Speter * All rights reserved. 7251881Speter * 8251881Speter * Redistribution and use in source and binary forms, with or without 9251881Speter * modification, are permitted provided that the following conditions 10251881Speter * are met: 11251881Speter * 1. Redistributions of source code must retain the above copyright 12251881Speter * notice, this list of conditions and the following disclaimer. 13251881Speter * 2. Redistributions in binary form must reproduce the above copyright 14251881Speter * notice, this list of conditions and the following disclaimer in the 15251881Speter * documentation and/or other materials provided with the distribution. 16251881Speter * 3. The name of the author may not be used to endorse or promote products 17251881Speter * derived from this software without specific prior written permission. 18251881Speter * 19251881Speter * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 20251881Speter * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 21251881Speter * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22251881Speter * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23251881Speter * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24251881Speter * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25251881Speter * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26251881Speter * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27251881Speter * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28251881Speter * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29251881Speter */ 30251881Speter 31251881Speter#ifdef HAVE_CONFIG_H 32251881Speter#include <config.h> 33251881Speter#endif 34251881Speter 35251881Speter#include <tcpdump-stdinc.h> 36251881Speter 37251881Speter#include <string.h> 38251881Speter 39251881Speter#include "interface.h" 40251881Speter 41251881Speter/* 42251881Speter * Appends src to string dst of size siz (unlike strncat, siz is the 43251881Speter * full size of dst, not space left). At most siz-1 characters 44251881Speter * will be copied. Always NUL terminates (unless siz == 0). 45251881Speter * Returns strlen(src); if retval >= siz, truncation occurred. 46251881Speter */ 47251881Spetersize_t 48251881Speterstrlcat(char *dst, const char *src, size_t siz) 49251881Speter{ 50251881Speter register char *d = dst; 51251881Speter register const char *s = src; 52251881Speter register size_t n = siz; 53251881Speter size_t dlen; 54262253Speter 55262253Speter /* Find the end of dst and adjust bytes left but don't go past end */ 56251881Speter while (*d != '\0' && n-- != 0) 57251881Speter d++; 58251881Speter dlen = d - dst; 59251881Speter n = siz - dlen; 60251881Speter 61251881Speter if (n == 0) 62251881Speter return(dlen + strlen(s)); 63251881Speter while (*s != '\0') { 64251881Speter if (n != 1) { 65251881Speter *d++ = *s; 66251881Speter n--; 67251881Speter } 68251881Speter s++; 69251881Speter } 70251881Speter *d = '\0'; 71251881Speter 72251881Speter return(dlen + (s - src)); /* count does not include NUL */ 73251881Speter} 74251881Speter