Damien Miller | b9cd049 | 2011-09-23 10:38:11 +1000 | [diff] [blame] | 1 | /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [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 | d4a8b7e | 1999-10-27 13:42:43 +1000 | [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 | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 17 | */ |
| 18 | |
Darren Tucker | 7f24a0e | 2005-11-10 16:18:56 +1100 | [diff] [blame] | 19 | /* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */ |
| 20 | |
Ben Lindstrom | dd21fe9 | 2002-06-27 18:23:20 +0000 | [diff] [blame] | 21 | #include "includes.h" |
Damien Miller | 04f8014 | 1999-11-19 15:32:34 +1100 | [diff] [blame] | 22 | #ifndef HAVE_STRLCPY |
| 23 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | /* |
| 28 | * Copy src to string dst of size siz. At most siz-1 characters |
| 29 | * will be copied. Always NUL terminates (unless siz == 0). |
| 30 | * Returns strlen(src); if retval >= siz, truncation occurred. |
| 31 | */ |
Damien Miller | 180207f | 2001-06-28 14:48:28 +1000 | [diff] [blame] | 32 | size_t |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 33 | strlcpy(char *dst, const char *src, size_t siz) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 34 | { |
Darren Tucker | 5224566 | 2005-11-10 16:26:17 +1100 | [diff] [blame] | 35 | char *d = dst; |
| 36 | const char *s = src; |
| 37 | size_t n = siz; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | |
| 39 | /* Copy as many bytes as will fit */ |
Damien Miller | b9cd049 | 2011-09-23 10:38:11 +1000 | [diff] [blame] | 40 | if (n != 0) { |
| 41 | while (--n != 0) { |
| 42 | if ((*d++ = *s++) == '\0') |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 43 | break; |
Damien Miller | b9cd049 | 2011-09-23 10:38:11 +1000 | [diff] [blame] | 44 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* Not enough room in dst, add NUL and traverse rest of src */ |
| 48 | if (n == 0) { |
| 49 | if (siz != 0) |
| 50 | *d = '\0'; /* NUL-terminate dst */ |
| 51 | while (*s++) |
| 52 | ; |
| 53 | } |
| 54 | |
| 55 | return(s - src - 1); /* count does not include NUL */ |
| 56 | } |
Damien Miller | e413cba | 1999-10-28 14:12:54 +1000 | [diff] [blame] | 57 | |
| 58 | #endif /* !HAVE_STRLCPY */ |