Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */ |
michael@0 | 2 | |
michael@0 | 3 | /* |
michael@0 | 4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
michael@0 | 5 | * All rights reserved. |
michael@0 | 6 | * |
michael@0 | 7 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 8 | * modification, are permitted provided that the following conditions |
michael@0 | 9 | * are met: |
michael@0 | 10 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | * documentation and/or other materials provided with the distribution. |
michael@0 | 15 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 16 | * derived from this software without specific prior written permission. |
michael@0 | 17 | * |
michael@0 | 18 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
michael@0 | 19 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
michael@0 | 20 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
michael@0 | 21 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
michael@0 | 24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
michael@0 | 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
michael@0 | 26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
michael@0 | 27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 28 | */ |
michael@0 | 29 | |
michael@0 | 30 | #if defined(LIBC_SCCS) && !defined(lint) |
michael@0 | 31 | static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $"; |
michael@0 | 32 | #endif /* LIBC_SCCS and not lint */ |
michael@0 | 33 | |
michael@0 | 34 | #include <sys/types.h> |
michael@0 | 35 | |
michael@0 | 36 | #include "event2/event-config.h" |
michael@0 | 37 | |
michael@0 | 38 | #ifndef _EVENT_HAVE_STRLCPY |
michael@0 | 39 | #include "strlcpy-internal.h" |
michael@0 | 40 | |
michael@0 | 41 | /* |
michael@0 | 42 | * Copy src to string dst of size siz. At most siz-1 characters |
michael@0 | 43 | * will be copied. Always NUL terminates (unless siz == 0). |
michael@0 | 44 | * Returns strlen(src); if retval >= siz, truncation occurred. |
michael@0 | 45 | */ |
michael@0 | 46 | size_t |
michael@0 | 47 | _event_strlcpy(dst, src, siz) |
michael@0 | 48 | char *dst; |
michael@0 | 49 | const char *src; |
michael@0 | 50 | size_t siz; |
michael@0 | 51 | { |
michael@0 | 52 | register char *d = dst; |
michael@0 | 53 | register const char *s = src; |
michael@0 | 54 | register size_t n = siz; |
michael@0 | 55 | |
michael@0 | 56 | /* Copy as many bytes as will fit */ |
michael@0 | 57 | if (n != 0 && --n != 0) { |
michael@0 | 58 | do { |
michael@0 | 59 | if ((*d++ = *s++) == 0) |
michael@0 | 60 | break; |
michael@0 | 61 | } while (--n != 0); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /* Not enough room in dst, add NUL and traverse rest of src */ |
michael@0 | 65 | if (n == 0) { |
michael@0 | 66 | if (siz != 0) |
michael@0 | 67 | *d = '\0'; /* NUL-terminate dst */ |
michael@0 | 68 | while (*s++) |
michael@0 | 69 | ; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | return (s - src - 1); /* count does not include NUL */ |
michael@0 | 73 | } |
michael@0 | 74 | #endif |