1 /* SPDX-License-Identifier: GPL-2.0-only */
9 char *strdup(const char *s
)
12 dead_code(); /* This can't be used without malloc(). */
14 size_t sz
= strlen(s
) + 1;
21 char *strconcat(const char *s1
, const char *s2
)
24 dead_code(); /* This can't be used without malloc(). */
26 size_t sz_1
= strlen(s1
);
27 size_t sz_2
= strlen(s2
);
28 char *d
= malloc(sz_1
+ sz_2
+ 1);
31 memcpy(d
+ sz_1
, s2
, sz_2
+ 1);
36 size_t strnlen(const char *src
, size_t max
)
39 while ((*src
++) && (i
< max
))
44 size_t strlen(const char *src
)
52 char *strchr(const char *s
, int c
)
62 char *strrchr(const char *s
, int c
)
74 char *strncpy(char *to
, const char *from
, size_t count
)
94 char *strcpy(char *dst
, const char *src
)
105 int strcmp(const char *s1
, const char *s2
)
109 while ((r
= (*s1
- *s2
)) == 0 && *s1
) {
116 int strncmp(const char *s1
, const char *s2
, size_t maxlen
)
120 for (i
= 0; i
< maxlen
; i
++) {
121 if ((s1
[i
] != s2
[i
]) || (s1
[i
] == '\0'))
122 return s1
[i
] - s2
[i
];
128 size_t strspn(const char *str
, const char *spn
)
134 for (p
= spn
; *str
!= *p
; p
++)
143 size_t strcspn(const char *str
, const char *spn
)
149 for (p
= spn
; *p
!= '\0'; p
++)
158 char *strstr(const char *haystack
, const char *needle
)
160 size_t needle_len
= strlen(needle
);
161 for (; *haystack
; haystack
++) {
162 if (!strncmp(haystack
, needle
, needle_len
))
163 return (char *)haystack
;
168 char *strtok_r(char *str
, const char *delim
, char **ptr
)
175 start
= str
+ strspn(str
, delim
);
176 if (start
[0] == '\0')
179 end
= start
+ strcspn(start
, delim
);
186 char *strtok(char *str
, const char *delim
)
188 static char *strtok_ptr
;
190 return strtok_r(str
, delim
, &strtok_ptr
);
193 long atol(const char *str
)
198 str
+= strspn(str
, " \t\n\r\f\v");
203 } else if (*str
== '-') {
208 while (isdigit(*str
)) {