Darren Tucker | 80c0d7e | 2005-11-10 16:05:37 +1100 | [diff] [blame] | 1 | /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ |
Damien Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
Damien Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 5 | * |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 6 | * 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 Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 9 | * |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 10 | * 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 Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 17 | */ |
| 18 | |
Darren Tucker | 7f24a0e | 2005-11-10 16:18:56 +1100 | [diff] [blame] | 19 | /* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */ |
| 20 | |
Ben Lindstrom | dd21fe9 | 2002-06-27 18:23:20 +0000 | [diff] [blame] | 21 | #include "includes.h" |
Damien Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 22 | #ifndef HAVE_STRLCAT |
| 23 | |
Damien Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 24 | #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 Miller | 6e77a53 | 2001-04-14 00:22:33 +1000 | [diff] [blame] | 30 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)). |
Damien Miller | 180207f | 2001-06-28 14:48:28 +1000 | [diff] [blame] | 31 | * Returns strlen(src) + MIN(siz, strlen(initial dst)). |
| 32 | * If retval >= siz, truncation occurred. |
Damien Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 33 | */ |
Damien Miller | 180207f | 2001-06-28 14:48:28 +1000 | [diff] [blame] | 34 | size_t |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 35 | strlcat(char *dst, const char *src, size_t siz) |
Damien Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 36 | { |
Darren Tucker | 80c0d7e | 2005-11-10 16:05:37 +1100 | [diff] [blame] | 37 | char *d = dst; |
| 38 | const char *s = src; |
| 39 | size_t n = siz; |
Damien Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 40 | size_t dlen; |
| 41 | |
| 42 | /* Find the end of dst and adjust bytes left but don't go past end */ |
Damien Miller | 6e77a53 | 2001-04-14 00:22:33 +1000 | [diff] [blame] | 43 | while (n-- != 0 && *d != '\0') |
Damien Miller | b3ca3aa | 1999-11-22 13:57:07 +1100 | [diff] [blame] | 44 | 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 */ |