summaryrefslogtreecommitdiff
path: root/firmware/common
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/common')
-rw-r--r--firmware/common/dircache.c8
-rw-r--r--firmware/common/strlcpy.c64
-rw-r--r--firmware/common/strmemccpy.c38
3 files changed, 68 insertions, 42 deletions
diff --git a/firmware/common/dircache.c b/firmware/common/dircache.c
index 1d6371a6b5..5902f8b3fd 100644
--- a/firmware/common/dircache.c
+++ b/firmware/common/dircache.c
@@ -1707,8 +1707,8 @@ static int sab_process_dir(struct dircache_entry *ce)
1707 /* save current paths size */ 1707 /* save current paths size */
1708 int pathpos = strlen(sab_path); 1708 int pathpos = strlen(sab_path);
1709 /* append entry */ 1709 /* append entry */
1710 strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos); 1710 strmemccpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos);
1711 strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1); 1711 strmemccpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1);
1712 1712
1713 int rc = sab_process_dir(ce->down); 1713 int rc = sab_process_dir(ce->down);
1714 /* restore path */ 1714 /* restore path */
@@ -1735,7 +1735,7 @@ static int sab_process_dir(struct dircache_entry *ce)
1735static int sab_process_volume(IF_MV(int volume,) struct dircache_entry *ce) 1735static int sab_process_volume(IF_MV(int volume,) struct dircache_entry *ce)
1736{ 1736{
1737 memset(ce, 0, sizeof(struct dircache_entry)); 1737 memset(ce, 0, sizeof(struct dircache_entry));
1738 strlcpy(sab_path, "/", sizeof sab_path); 1738 strmemccpy(sab_path, "/", sizeof sab_path);
1739 return sab_process_dir(ce); 1739 return sab_process_dir(ce);
1740} 1740}
1741 1741
@@ -1755,7 +1755,7 @@ int dircache_readdir_r(struct dircache_dirscan *dir, struct DIRENT *result)
1755 1755
1756 dir->scanidx = ce - dircache_root; 1756 dir->scanidx = ce - dircache_root;
1757 1757
1758 strlcpy(result->d_name, ce->d_name, sizeof (result->d_name)); 1758 strmemccpy(result->d_name, ce->d_name, sizeof (result->d_name));
1759 result->info = ce->dirinfo; 1759 result->info = ce->dirinfo;
1760 1760
1761 return 1; 1761 return 1;
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 */
26size_t 30size_t strlcpy(char *dst, const char *src, size_t siz)
27strlcpy(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
diff --git a/firmware/common/strmemccpy.c b/firmware/common/strmemccpy.c
new file mode 100644
index 0000000000..830907f55e
--- /dev/null
+++ b/firmware/common/strmemccpy.c
@@ -0,0 +1,38 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2022 William Wilgus
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21/* (firmware/common/strmemccpy.c) */
22#include "string-extra.h"
23
24/* copies src to a buffer of len bytes stopping after
25 * len or the first NULL (\0) in src
26 * NULL terminates except when len = 0
27 * If len was exceeded NULL is returned otherwise returns
28 * a pointer to the first byte following the NULL in dst.
29*/
30char * strmemccpy(char *dst, const char *src, size_t len)
31{
32 char * ret = (char *)memccpy(dst, src, '\0', len);
33 if (ret == NULL && len > 0)
34 {
35 dst[len - 1] = '\0';
36 }
37 return ret;
38}