strlcat.c revision 706f2543
1706f2543Smrg/* 2706f2543Smrg * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 3706f2543Smrg * 4706f2543Smrg * Permission to use, copy, modify, and distribute this software for any 5706f2543Smrg * purpose with or without fee is hereby granted, provided that the above 6706f2543Smrg * copyright notice and this permission notice appear in all copies. 7706f2543Smrg * 8706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL 9706f2543Smrg * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 10706f2543Smrg * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE 11706f2543Smrg * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12706f2543Smrg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 13706f2543Smrg * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 14706f2543Smrg * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15706f2543Smrg */ 16706f2543Smrg 17706f2543Smrg 18706f2543Smrg#ifdef HAVE_DIX_CONFIG_H 19706f2543Smrg#include <dix-config.h> 20706f2543Smrg#endif 21706f2543Smrg 22706f2543Smrg#include <sys/types.h> 23706f2543Smrg#include <string.h> 24706f2543Smrg#include "os.h" 25706f2543Smrg 26706f2543Smrg/* 27706f2543Smrg * Appends src to string dst of size siz (unlike strncat, siz is the 28706f2543Smrg * full size of dst, not space left). At most siz-1 characters 29706f2543Smrg * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 30706f2543Smrg * Returns strlen(src) + MIN(siz, strlen(initial dst)). 31706f2543Smrg * If retval >= siz, truncation occurred. 32706f2543Smrg */ 33706f2543Smrgsize_t 34706f2543Smrgstrlcat(char *dst, const char *src, size_t siz) 35706f2543Smrg{ 36706f2543Smrg register char *d = dst; 37706f2543Smrg register const char *s = src; 38706f2543Smrg register size_t n = siz; 39706f2543Smrg size_t dlen; 40706f2543Smrg 41706f2543Smrg /* Find the end of dst and adjust bytes left but don't go past end */ 42706f2543Smrg while (n-- != 0 && *d != '\0') 43706f2543Smrg d++; 44706f2543Smrg dlen = d - dst; 45706f2543Smrg n = siz - dlen; 46706f2543Smrg 47706f2543Smrg if (n == 0) 48706f2543Smrg return(dlen + strlen(s)); 49706f2543Smrg while (*s != '\0') { 50706f2543Smrg if (n != 1) { 51706f2543Smrg *d++ = *s; 52706f2543Smrg n--; 53706f2543Smrg } 54706f2543Smrg s++; 55706f2543Smrg } 56706f2543Smrg *d = '\0'; 57706f2543Smrg 58706f2543Smrg return(dlen + (s - src)); /* count does not include NUL */ 59706f2543Smrg} 60