diff options
Diffstat (limited to 'firmware/common/strlcpy.c')
-rw-r--r-- | firmware/common/strlcpy.c | 64 |
1 files changed, 26 insertions, 38 deletions
diff --git a/firmware/common/strlcpy.c b/firmware/common/strlcpy.c index e320649140..bfdb6482a4 100644 --- a/firmware/common/strlcpy.c +++ b/firmware/common/strlcpy.c | |||
@@ -1,51 +1,39 @@ | |||
1 | /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ | 1 | /*************************************************************************** |
2 | 2 | * __________ __ ___. | |
3 | /* | 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
5 | * | 9 | * |
6 | * Permission to use, copy, modify, and distribute this software for any | 10 | * Copyright (C) 2022 William Wilgus |
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | 11 | * |
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | 12 | * This program is free software; you can redistribute it and/or |
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | 13 | * modify it under the terms of the GNU General Public License |
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | 14 | * as published by the Free Software Foundation; either version 2 |
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | 15 | * of the License, or (at your option) any later version. |
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 16 | * |
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 18 | * KIND, either express or implied. |
17 | */ | 19 | * |
20 | ****************************************************************************/ | ||
18 | 21 | ||
19 | #include <string.h> | 22 | #include <string.h> |
23 | #include "strmemccpy.h" | ||
20 | 24 | ||
21 | /* | 25 | /* |
22 | * Copy src to string dst of size siz. At most siz-1 characters | 26 | * Copy src to string dst of size siz. At most siz-1 characters |
23 | * will be copied. Always NUL terminates (unless siz == 0). | 27 | * will be copied. Always NUL terminates (unless siz == 0). |
24 | * Returns strlen(src); if retval >= siz, truncation occurred. | 28 | * Returns strlen(src); if retval >= siz, truncation occurred. |
25 | */ | 29 | */ |
26 | size_t | 30 | size_t strlcpy(char *dst, const char *src, size_t siz) |
27 | strlcpy(char *dst, const char *src, size_t siz) | ||
28 | { | 31 | { |
29 | char *d = dst; | 32 | /* Copy as many bytes as will fit */ |
30 | const char *s = src; | 33 | char *d = strmemccpy(dst, src, siz); |
31 | size_t n = siz; | 34 | if (d) |
32 | 35 | return (d - dst - 1); /* count does not include NUL */ | |
33 | /* Copy as many bytes as will fit */ | ||
34 | if (n != 0) { | ||
35 | while (--n != 0) { | ||
36 | if ((*d++ = *s++) == '\0') | ||
37 | break; | ||
38 | } | ||
39 | } | ||
40 | 36 | ||
41 | /* Not enough room in dst, add NUL and traverse rest of src */ | 37 | /* Not enough room in dst, add NUL and traverse rest of src */ |
42 | if (n == 0) { | 38 | return(siz + strlen(src+siz)); /* count does not include NUL */ |
43 | if (siz != 0) | ||
44 | *d = '\0'; /* NUL-terminate dst */ | ||
45 | while (*s++) | ||
46 | ; | ||
47 | } | ||
48 | |||
49 | return(s - src - 1); /* count does not include NUL */ | ||
50 | } | 39 | } |
51 | |||