blob: 8df7577306c0ea0d1e000326f942400b7b150d21 [file] [log] [blame]
Damien Millere323df62003-05-18 22:24:09 +10001/* $OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $ */
Damien Millerb3ca3aa1999-11-22 13:57:07 +11002
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
Damien Millerb3ca3aa1999-11-22 13:57:07 +11005 *
Damien Millere323df62003-05-18 22:24:09 +10006 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
Damien Millerb3ca3aa1999-11-22 13:57:07 +11009 *
Damien Millere323df62003-05-18 22:24:09 +100010 * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
13 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Millerb3ca3aa1999-11-22 13:57:07 +110017 */
18
Ben Lindstromdd21fe92002-06-27 18:23:20 +000019#include "includes.h"
Damien Millerb3ca3aa1999-11-22 13:57:07 +110020#ifndef HAVE_STRLCAT
21
22#if defined(LIBC_SCCS) && !defined(lint)
Damien Millere323df62003-05-18 22:24:09 +100023static char *rcsid = "$OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $";
Damien Millerb3ca3aa1999-11-22 13:57:07 +110024#endif /* LIBC_SCCS and not lint */
25
26#include <sys/types.h>
27#include <string.h>
Damien Miller79b332d2001-06-27 23:36:08 +100028#include "strlcat.h"
Damien Millerb3ca3aa1999-11-22 13:57:07 +110029
30/*
31 * Appends src to string dst of size siz (unlike strncat, siz is the
32 * full size of dst, not space left). At most siz-1 characters
Damien Miller6e77a532001-04-14 00:22:33 +100033 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
Damien Miller180207f2001-06-28 14:48:28 +100034 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
35 * If retval >= siz, truncation occurred.
Damien Millerb3ca3aa1999-11-22 13:57:07 +110036 */
Damien Miller180207f2001-06-28 14:48:28 +100037size_t
Damien Millere323df62003-05-18 22:24:09 +100038strlcat(char *dst, const char *src, size_t siz)
Damien Millerb3ca3aa1999-11-22 13:57:07 +110039{
40 register char *d = dst;
41 register const char *s = src;
42 register size_t n = siz;
43 size_t dlen;
44
45 /* Find the end of dst and adjust bytes left but don't go past end */
Damien Miller6e77a532001-04-14 00:22:33 +100046 while (n-- != 0 && *d != '\0')
Damien Millerb3ca3aa1999-11-22 13:57:07 +110047 d++;
48 dlen = d - dst;
49 n = siz - dlen;
50
51 if (n == 0)
52 return(dlen + strlen(s));
53 while (*s != '\0') {
54 if (n != 1) {
55 *d++ = *s;
56 n--;
57 }
58 s++;
59 }
60 *d = '\0';
61
62 return(dlen + (s - src)); /* count does not include NUL */
63}
64
65#endif /* !HAVE_STRLCAT */