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 char *strchr(const char *s
, int c
)
46 char *strrchr(const char *s
, int c
)
58 char *strncpy(char *to
, const char *from
, size_t count
)
78 char *strcpy(char *dst
, const char *src
)
89 int strcmp(const char *s1
, const char *s2
)
93 while ((r
= (*s1
- *s2
)) == 0 && *s1
) {
100 int strncmp(const char *s1
, const char *s2
, size_t maxlen
)
104 for (i
= 0; i
< maxlen
; i
++) {
105 if ((s1
[i
] != s2
[i
]) || (s1
[i
] == '\0'))
106 return s1
[i
] - s2
[i
];
112 size_t strspn(const char *str
, const char *spn
)
118 for (p
= spn
; *str
!= *p
; p
++)
127 size_t strcspn(const char *str
, const char *spn
)
133 for (p
= spn
; *p
!= '\0'; p
++)
142 char *strstr(const char *haystack
, const char *needle
)
144 size_t needle_len
= strlen(needle
);
145 for (; *haystack
; haystack
++) {
146 if (!strncmp(haystack
, needle
, needle_len
))
147 return (char *)haystack
;
152 char *strtok_r(char *str
, const char *delim
, char **ptr
)
159 start
= str
+ strspn(str
, delim
);
160 if (start
[0] == '\0')
163 end
= start
+ strcspn(start
, delim
);
170 char *strtok(char *str
, const char *delim
)
172 static char *strtok_ptr
;
174 return strtok_r(str
, delim
, &strtok_ptr
);
177 long atol(const char *str
)
182 str
+= strspn(str
, " \t\n\r\f\v");
187 } else if (*str
== '-') {
192 while (isdigit(*str
)) {