1157016Sdes/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ 2126274Sdes 398937Sdes/* 498937Sdes * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 598937Sdes * 6124208Sdes * Permission to use, copy, modify, and distribute this software for any 7124208Sdes * purpose with or without fee is hereby granted, provided that the above 8124208Sdes * copyright notice and this permission notice appear in all copies. 998937Sdes * 10124208Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11124208Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12124208Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13124208Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14124208Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15124208Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16124208Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1798937Sdes */ 1898937Sdes 19157016Sdes/* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */ 20157016Sdes 21106121Sdes#include "includes.h" 2298937Sdes#ifndef HAVE_STRLCAT 2398937Sdes 2498937Sdes#include <sys/types.h> 2598937Sdes#include <string.h> 2698937Sdes 2798937Sdes/* 2898937Sdes * Appends src to string dst of size siz (unlike strncat, siz is the 2998937Sdes * full size of dst, not space left). At most siz-1 characters 3098937Sdes * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 3198937Sdes * Returns strlen(src) + MIN(siz, strlen(initial dst)). 3298937Sdes * If retval >= siz, truncation occurred. 3398937Sdes */ 3498937Sdessize_t 35124208Sdesstrlcat(char *dst, const char *src, size_t siz) 3698937Sdes{ 37157016Sdes char *d = dst; 38157016Sdes const char *s = src; 39157016Sdes size_t n = siz; 4098937Sdes size_t dlen; 4198937Sdes 4298937Sdes /* Find the end of dst and adjust bytes left but don't go past end */ 4398937Sdes while (n-- != 0 && *d != '\0') 4498937Sdes d++; 4598937Sdes dlen = d - dst; 4698937Sdes n = siz - dlen; 4798937Sdes 4898937Sdes if (n == 0) 4998937Sdes return(dlen + strlen(s)); 5098937Sdes while (*s != '\0') { 5198937Sdes if (n != 1) { 5298937Sdes *d++ = *s; 5398937Sdes n--; 5498937Sdes } 5598937Sdes s++; 5698937Sdes } 5798937Sdes *d = '\0'; 5898937Sdes 5998937Sdes return(dlen + (s - src)); /* count does not include NUL */ 6098937Sdes} 6198937Sdes 6298937Sdes#endif /* !HAVE_STRLCAT */ 63