summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2022-11-13 00:43:43 -0500
committerWilliam Wilgus <me.theuser@yahoo.com>2022-11-13 01:14:49 -0500
commita634557a881b59b8be1dc156f9822c6d20bd8741 (patch)
treefd953a0ea1d9a02a8d73a15f1aead27b59c5ffaa /firmware
parentffe2df2e92cbdeb507a49279a85ac88cac2fbe4f (diff)
downloadrockbox-a634557a881b59b8be1dc156f9822c6d20bd8741.tar.gz
rockbox-a634557a881b59b8be1dc156f9822c6d20bd8741.zip
fix strptokspn, add strcspn, fix splash.c
fix off by 1 error in strptokspn, add strcspn, fix fallout in splash.c Change-Id: I61475d9633fc35db5a8ae30cbe588f69f2f7fabc
Diffstat (limited to 'firmware')
-rw-r--r--firmware/SOURCES2
-rw-r--r--firmware/common/strptokspn.c33
-rw-r--r--firmware/libc/strcspn.c45
3 files changed, 57 insertions, 23 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES
index 4aa7c38daf..76d6cee921 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -271,7 +271,7 @@ libc/strcat.c
271libc/strchr.c 271libc/strchr.c
272libc/strcmp.c 272libc/strcmp.c
273libc/strcpy.c 273libc/strcpy.c
274 274libc/strcspn.c
275libc/strncmp.c 275libc/strncmp.c
276libc/strrchr.c 276libc/strrchr.c
277libc/strstr.c 277libc/strstr.c
diff --git a/firmware/common/strptokspn.c b/firmware/common/strptokspn.c
index f4b92c0712..16aafc66ef 100644
--- a/firmware/common/strptokspn.c
+++ b/firmware/common/strptokspn.c
@@ -39,40 +39,29 @@
39 * Pointer **end 39 * Pointer **end
40 * 40 *
41 * Note the returned token is NOT NULL terminated by the function as in strtok_r 41 * Note the returned token is NOT NULL terminated by the function as in strtok_r
42 * However the caller can use ret[len+1] = '\0'; to emulate a call to strtok_r 42 * However the caller can use ret[len] = '\0'; to emulate a call to strtok_r
43*/ 43*/
44
45const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end) 44const char *strptokspn_r(const char *ptr, const char *sep, size_t *len, const char **end)
46{ 45{
47 *len = 0; 46 if (ptr == NULL) /* we got NULL input so then we get last position instead */
48 if (!ptr) 47 {
49 /* we got NULL input so then we get our last position instead */
50 ptr = *end; 48 ptr = *end;
49 }
51 50
52 /* pass all letters that are including in the separator string */ 51 /* pass all letters that are including in the separator string */
53 while (*ptr && strchr(sep, *ptr)) 52 while (*ptr && strchr(sep, *ptr))
54 ++ptr; 53 ++ptr;
55 54
56 if (*ptr) { 55 if (*ptr != '\0')
56 {
57 /* so this is where the next piece of string starts */ 57 /* so this is where the next piece of string starts */
58 const char *start = ptr; 58 const char *start = ptr;
59 59 *len = strcspn(ptr, sep); /* Get span until any sep character in string */
60 /* set the end pointer to the first byte after the start */ 60 *end = ptr + *len;
61 *end = start + 1; 61 if (**end) /* the end is not a null byte */
62
63 /* scan through the string to find where it ends, it ends on a
64 null byte or a character that exists in the separator string */
65 while (**end && !strchr(sep, **end))
66 ++*end; 62 ++*end;
67 *len = (*end - start) - 1; /* this would be the string len if there actually was a NULL */ 63 return start;
68 if (**end) { /* the end is not a null byte */
69 ++*end; /* advance last pointer to beyond the match */
70 }
71
72 return start; /* return the position where the string starts */
73 } 64 }
74
75 /* we ended up on a null byte, there are no more strings to find! */
76 return NULL; 65 return NULL;
77} 66}
78 67
@@ -82,7 +71,7 @@ char * strtok_r(char *ptr, const char *sep, char **end)
82 size_t len; 71 size_t len;
83 char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end); 72 char * ret = (char*) strptokspn_r((const char*)ptr, sep, &len, (const char**) end);
84 if (ret) 73 if (ret)
85 ret[len + 1] = '\0'; 74 ret[len] = '\0';
86 return ret; 75 return ret;
87} 76}
88#endif 77#endif
diff --git a/firmware/libc/strcspn.c b/firmware/libc/strcspn.c
new file mode 100644
index 0000000000..ee50066ed1
--- /dev/null
+++ b/firmware/libc/strcspn.c
@@ -0,0 +1,45 @@
1/*
2FUNCTION
3 <<strcspn>>---count characters not in string
4INDEX
5 strcspn
6ANSI_SYNOPSIS
7 size_t strcspn(const char *<[s1]>, const char *<[s2]>);
8TRAD_SYNOPSIS
9 size_t strcspn(<[s1]>, <[s2]>)
10 char *<[s1]>;
11 char *<[s2]>;
12DESCRIPTION
13 This function computes the length of the initial part of
14 the string pointed to by <[s1]> which consists entirely of
15 characters <[NOT]> from the string pointed to by <[s2]>
16 (excluding the terminating null character).
17RETURNS
18 <<strcspn>> returns the length of the substring found.
19PORTABILITY
20<<strcspn>> is ANSI C.
21<<strcspn>> requires no supporting OS subroutines.
22 */
23#include <string.h>
24#include "_ansi.h" /* for _DEFUN */
25
26size_t
27_DEFUN (strcspn, (s1, s2),
28 _CONST char *s1 _AND
29 _CONST char *s2)
30{
31 _CONST char *s = s1;
32 _CONST char *c;
33 while (*s1)
34 {
35 for (c = s2; *c; c++)
36 {
37 if (*s1 == *c)
38 break;
39 }
40 if (*c)
41 break;
42 s1++;
43 }
44 return s1 - s;
45}