Skip to content

Commit 9993eb9

Browse files
committed
posix: c_lib_ext: add remaining string functions
Add the string handling functions stpcpy(), stpncpy(), strcasecmp(), strncasecmp(), strdup(), strndup(), and strnlen(). Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent ef39514 commit 9993eb9

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

lib/posix/c_lib_ext/stpcpy.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdbool.h>
8+
#include <string.h>
9+
10+
#include <zephyr/toolchain.h>
11+
12+
char *stpncpy(char *ZRESTRICT s1, const char *ZRESTRICT s2, size_t n)
13+
{
14+
char *ret = NULL;
15+
16+
for (; n != 0; ++s1, ++s2, --n) {
17+
*s1 = *s2;
18+
if (*s2 == '\0') {
19+
ret = s1;
20+
}
21+
}
22+
23+
for (; n != 0; --n) {
24+
*s1++ = '\0';
25+
}
26+
27+
if (ret == NULL) {
28+
ret = s1;
29+
}
30+
31+
return ret;
32+
}
33+
34+
char *stpcpy(char *ZRESTRICT s1, const char *ZRESTRICT s2)
35+
{
36+
return stpncpy(s1, s2, strlen(s2) + 1);
37+
}

lib/posix/c_lib_ext/strcasecmp.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ctype.h>
8+
#include <stddef.h>
9+
10+
int strncasecmp(const char *s1, const char *s2, size_t n)
11+
{
12+
int c1, c2;
13+
14+
for (; n != 0; --n) {
15+
c1 = (unsigned char)*s1++;
16+
c2 = (unsigned char)*s2++;
17+
if (c1 == '\0') {
18+
return c1 - c2;
19+
}
20+
if (c2 == '\0') {
21+
return c1 - c2;
22+
}
23+
24+
c1 = tolower(c1);
25+
c2 = tolower(c2);
26+
27+
if (c1 != c2) {
28+
return c1 - c2;
29+
}
30+
}
31+
32+
return 0;
33+
}
34+
35+
int strcasecmp(const char *s1, const char *s2)
36+
{
37+
return strncasecmp(s1, s2, MIN(strlen(s1), strlen(s2)));
38+
}

lib/posix/c_lib_ext/strdup.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stddef.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
char *strndup(const char *s, size_t n)
12+
{
13+
size_t len = strlen(s);
14+
char *ret = malloc(n);
15+
16+
if (ret != NULL) {
17+
strncpy(ret, s, n);
18+
ret[n - 1] = '\0';
19+
}
20+
21+
return ret;
22+
}
23+
24+
char *strdup(const char *s)
25+
{
26+
return strndup(s, strlen(s) + 1);
27+
}

lib/posix/c_lib_ext/strnlen.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2025 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stddef.h>
8+
#include <string.h>
9+
10+
size_t strnlen(const char *s, size_t maxlen)
11+
{
12+
size_t len;
13+
14+
for (len = 0; len < maxlen && s[len] != '\0'; ++len) {
15+
/* nothing to do */
16+
}
17+
18+
return len;
19+
}

0 commit comments

Comments
 (0)