blob: bcc1b61ad885c12baa76e7191fe017e52b9ea745 [file] [log] [blame]
Darren Tucker80c0d7e2005-11-10 16:05:37 +11001/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie 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 *
Ben Lindstromaf4a6c32003-08-25 01:10:51 +000010 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Millerb3ca3aa1999-11-22 13:57:07 +110017 */
18
Darren Tucker7f24a0e2005-11-10 16:18:56 +110019/* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */
20
Ben Lindstromdd21fe92002-06-27 18:23:20 +000021#include "includes.h"
Damien Millerb3ca3aa1999-11-22 13:57:07 +110022#ifndef HAVE_STRLCAT
23
Damien Millerb3ca3aa1999-11-22 13:57:07 +110024#include <sys/types.h>
25#include <string.h>
26
27/*
28 * Appends src to string dst of size siz (unlike strncat, siz is the
29 * full size of dst, not space left). At most siz-1 characters
Damien Miller6e77a532001-04-14 00:22:33 +100030 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
Damien Miller180207f2001-06-28 14:48:28 +100031 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
32 * If retval >= siz, truncation occurred.
Damien Millerb3ca3aa1999-11-22 13:57:07 +110033 */
Damien Miller180207f2001-06-28 14:48:28 +100034size_t
Damien Millere323df62003-05-18 22:24:09 +100035strlcat(char *dst, const char *src, size_t siz)
Damien Millerb3ca3aa1999-11-22 13:57:07 +110036{
Darren Tucker80c0d7e2005-11-10 16:05:37 +110037 char *d = dst;
38 const char *s = src;
39 size_t n = siz;
Damien Millerb3ca3aa1999-11-22 13:57:07 +110040 size_t dlen;
41
42 /* Find the end of dst and adjust bytes left but don't go past end */
Damien Miller6e77a532001-04-14 00:22:33 +100043 while (n-- != 0 && *d != '\0')
Damien Millerb3ca3aa1999-11-22 13:57:07 +110044 d++;
45 dlen = d - dst;
46 n = siz - dlen;
47
48 if (n == 0)
49 return(dlen + strlen(s));
50 while (*s != '\0') {
51 if (n != 1) {
52 *d++ = *s;
53 n--;
54 }
55 s++;
56 }
57 *d = '\0';
58
59 return(dlen + (s - src)); /* count does not include NUL */
60}
61
62#endif /* !HAVE_STRLCAT */