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
, int 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
, int maxlen
)
120 for (i
= 0; i
< maxlen
; i
++) {
121 if ((s1
[i
] != s2
[i
]) || (s1
[i
] == '\0'))
122 return s1
[i
] - s2
[i
];
128 unsigned int skip_atoi(char **s
)
133 i
= i
*10 + *((*s
)++) - '0';
137 int strspn(const char *str
, const char *spn
)
143 for (p
= spn
; *str
!= *p
; p
++)
152 int strcspn(const char *str
, const char *spn
)
158 for (p
= spn
; *p
!= '\0'; p
++)
167 char *strstr(const char *haystack
, const char *needle
)
169 size_t needle_len
= strlen(needle
);
170 for (; *haystack
; haystack
++) {
171 if (!strncmp(haystack
, needle
, needle_len
))
172 return (char *)haystack
;
177 char *strtok_r(char *str
, const char *delim
, char **ptr
)
184 start
= str
+ strspn(str
, delim
);
185 if (start
[0] == '\0')
188 end
= start
+ strcspn(start
, delim
);
195 char *strtok(char *str
, const char *delim
)
197 static char *strtok_ptr
;
199 return strtok_r(str
, delim
, &strtok_ptr
);
202 long atol(const char *str
)
207 str
+= strspn(str
, " \t\n\r\f\v");
212 } else if (*str
== '-') {
217 while (isdigit(*str
)) {