PatchworkOS
Loading...
Searching...
No Matches
strtok_s.c
Go to the documentation of this file.
2#include <errno.h>
3#include <stdint.h>
4#include <string.h>
5
7
8char* strtok_s(char* _RESTRICT s1, rsize_t* _RESTRICT s1max, const char* _RESTRICT s2, char** _RESTRICT ptr)
9{
10 const char* p = s2;
11
12 if (s1max == NULL || s2 == NULL || ptr == NULL || (s1 == NULL && *ptr == NULL) || *s1max > RSIZE_MAX)
13 {
15 return NULL;
16 }
17
18 if (s1 != NULL)
19 {
20 /* new string */
21 *ptr = s1;
22 }
23 else
24 {
25 /* old string continued */
26 if (*ptr == NULL)
27 {
28 /* No old string, no new string, nothing to do */
29 return NULL;
30 }
31
32 s1 = *ptr;
33 }
34
35 /* skip leading s2 characters */
36 while (*p && *s1)
37 {
38 if (*s1 == *p)
39 {
40 /* found separator; skip and start over */
41 if (*s1max == 0)
42 {
44 return NULL;
45 }
46
47 ++s1;
48 --(*s1max);
49 p = s2;
50 continue;
51 }
52
53 ++p;
54 }
55
56 if (!*s1)
57 {
58 /* no more to parse */
59 *ptr = s1;
60 return NULL;
61 }
62
63 /* skipping non-s2 characters */
64 *ptr = s1;
65
66 while (**ptr)
67 {
68 p = s2;
69
70 while (*p)
71 {
72 if (**ptr == *p++)
73 {
74 /* found separator; overwrite with '\0', position *ptr, return */
75 if (*s1max == 0)
76 {
78 return NULL;
79 }
80
81 --(*s1max);
82 *((*ptr)++) = '\0';
83 return s1;
84 }
85 }
86
87 if (*s1max == 0)
88 {
90 return NULL;
91 }
92
93 --(*s1max);
94 ++(*ptr);
95 }
96
97 /* parsed to end of string */
98 return s1;
99}
constraint_handler_t _constraintHandler
#define _CONSTRAINT_VIOLATION(e)
#define EINVAL
Invalid argument.
Definition errno.h:142
#define NULL
Pointer error value.
Definition NULL.h:23
#define _RESTRICT
Definition config.h:17
__SIZE_TYPE__ rsize_t
Definition rsize_t.h:4
char * strtok_s(char *_RESTRICT s1, rsize_t *_RESTRICT s1max, const char *_RESTRICT s2, char **_RESTRICT ptr)
Definition strtok_s.c:8