PatchworkOS
Loading...
Searching...
No Matches
strtok_s.c
Go to the documentation of this file.
1
#include "
common/use_annex_k.h
"
2
#include <
errno.h
>
3
#include <
stdint.h
>
4
#include <
string.h
>
5
6
#include "
common/constraint_handler.h
"
7
8
char
*
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
{
14
_constraintHandler
(
_CONSTRAINT_VIOLATION
(
EINVAL
));
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
{
43
_constraintHandler
(
_CONSTRAINT_VIOLATION
(
EINVAL
));
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
{
77
_constraintHandler
(
_CONSTRAINT_VIOLATION
(
EINVAL
));
78
return
NULL
;
79
}
80
81
--(*s1max);
82
*((*ptr)++) =
'\0'
;
83
return
s1;
84
}
85
}
86
87
if
(*s1max == 0)
88
{
89
_constraintHandler
(
_CONSTRAINT_VIOLATION
(
EINVAL
));
90
return
NULL
;
91
}
92
93
--(*s1max);
94
++(*ptr);
95
}
96
97
/* parsed to end of string */
98
return
s1;
99
}
_constraintHandler
constraint_handler_t _constraintHandler
Definition
constraint_handler.c:3
constraint_handler.h
_CONSTRAINT_VIOLATION
#define _CONSTRAINT_VIOLATION(e)
Definition
constraint_handler.h:10
errno.h
EINVAL
#define EINVAL
Invalid argument.
Definition
errno.h:142
NULL
#define NULL
Pointer error value.
Definition
NULL.h:23
_RESTRICT
#define _RESTRICT
Definition
config.h:17
rsize_t
__SIZE_TYPE__ rsize_t
Definition
rsize_t.h:4
stdint.h
string.h
strtok_s
char * strtok_s(char *_RESTRICT s1, rsize_t *_RESTRICT s1max, const char *_RESTRICT s2, char **_RESTRICT ptr)
Definition
strtok_s.c:8
use_annex_k.h
src
libstd
functions
string
strtok_s.c
Generated by
1.9.8