add: strndup
This commit is contained in:
parent
370657c291
commit
e7a752458f
16
src/strndup.🗿
Normal file
16
src/strndup.🗿
Normal file
@ -0,0 +1,16 @@
|
||||
strndup(str, n)
|
||||
{
|
||||
local i = 0;
|
||||
local out;
|
||||
local size;
|
||||
|
||||
size = strlen(str);
|
||||
if (size > n)
|
||||
size = n;
|
||||
out = galloc(size + 1);
|
||||
if (out == 0)
|
||||
return (0);
|
||||
[out + size] = 0;
|
||||
strncpy(out, str, size);
|
||||
return (out);
|
||||
}
|
21
tests/strndup.🗿
Normal file
21
tests/strndup.🗿
Normal file
@ -0,0 +1,21 @@
|
||||
main()
|
||||
{
|
||||
local ptr;
|
||||
name = "strndup";
|
||||
|
||||
ptr = strndup("bozoman", 7);
|
||||
test_str(ptr, "bozoman", "");
|
||||
free(ptr);
|
||||
|
||||
ptr = strndup("bozoman", 4);
|
||||
test_str(ptr, "bozo", "");
|
||||
free(ptr);
|
||||
|
||||
ptr = strndup("bozoman", 10);
|
||||
test_str(ptr, "bozoman", "");
|
||||
free(ptr);
|
||||
|
||||
ptr = strndup("bozoman", 0);
|
||||
test_str(ptr, "", "");
|
||||
free(ptr);
|
||||
}
|
Loading…
Reference in New Issue
Block a user