aboutsummaryrefslogtreecommitdiffstats
path: root/src/string/strn
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-11-17 23:44:53 +0100
committerMattias Andrée <maandree@operamail.com>2015-11-17 23:44:53 +0100
commitdbb778cb5e6f585c760e0327c1b63aecb5568d84 (patch)
tree5f263fbe157bb6470db710d9122a62e625097cc6 /src/string/strn
parentgrammaro (diff)
downloadslibc-dbb778cb5e6f585c760e0327c1b63aecb5568d84.tar.gz
slibc-dbb778cb5e6f585c760e0327c1b63aecb5568d84.tar.bz2
slibc-dbb778cb5e6f585c760e0327c1b63aecb5568d84.tar.xz
split out string/strn/ from string/str/
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/string/strn')
-rw-r--r--src/string/strn/stpncpy.c46
-rw-r--r--src/string/strn/stpnmove.c47
-rw-r--r--src/string/strn/strcncpy.c54
-rw-r--r--src/string/strn/strcnmove.c54
-rw-r--r--src/string/strn/strncasecmp.c50
-rw-r--r--src/string/strn/strncasestr.c38
-rw-r--r--src/string/strn/strncat.c43
-rw-r--r--src/string/strn/strncmp.c39
-rw-r--r--src/string/strn/strncpy.c43
-rw-r--r--src/string/strn/strndup.c43
-rw-r--r--src/string/strn/strnlen.c36
-rw-r--r--src/string/strn/strnmove.c45
-rw-r--r--src/string/strn/strnstr.c39
-rw-r--r--src/string/strn/strstrncpy.c55
-rw-r--r--src/string/strn/strstrnmove.c54
15 files changed, 686 insertions, 0 deletions
diff --git a/src/string/strn/stpncpy.c b/src/string/strn/stpncpy.c
new file mode 100644
index 0000000..876eaf0
--- /dev/null
+++ b/src/string/strn/stpncpy.c
@@ -0,0 +1,46 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Copy a memory segment to another, non-overlapping, segment,
+ * stop when a NUL byte is encountered.
+ *
+ * This is a GNU extension.
+ *
+ * @param whither The destination memory segment.
+ * @param whence The source memory segment.
+ * @param maxlen The maximum number of bytes to copy.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @return `whither` plus the number of written bytes,
+ * excluding NUL bytes, is returned.
+ */
+char* stpncpy(char* restrict whither, const char* restrict whence, size_t maxlen)
+{
+ size_t n = strnlen(whence, maxlen);
+ memcpy(whither, whence, n);
+ memset(whither, 0, maxlen - n);
+ return whither + n;
+}
+
diff --git a/src/string/strn/stpnmove.c b/src/string/strn/stpnmove.c
new file mode 100644
index 0000000..a0a0de9
--- /dev/null
+++ b/src/string/strn/stpnmove.c
@@ -0,0 +1,47 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Copy a memory segment to another, possibly overlapping, segment,
+ * stop when a NUL byte is encountered.
+ *
+ * This is a slibc extension added for completeness.
+ * It is only available if GNU extensions are available.
+ *
+ * @param whither The destination memory segment.
+ * @param whence The source memory segment.
+ * @param maxlen The maximum number of bytes to copy.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @return `whither` plus the number of written bytes,
+ * excluding NUL bytes, is returned.
+ */
+char* stpnmove(char* whither, const char* whence, size_t maxlen)
+{
+ size_t n = strnlen(whence, maxlen);
+ memmove(whither, whence, n);
+ memset(whither, 0, maxlen - n);
+ return whither + n;
+}
+
diff --git a/src/string/strn/strcncpy.c b/src/string/strn/strcncpy.c
new file mode 100644
index 0000000..5036c7d
--- /dev/null
+++ b/src/string/strn/strcncpy.c
@@ -0,0 +1,54 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Copy a memory segment to another, non-overlapping, segment,
+ * stop when a NUL byte or a specified byte is encountered.
+ *
+ * This is a slibc extension added for completeness.
+ * It is only available if GNU extensions are available.
+ *
+ * @param whither The destination memory segment.
+ * @param whence The source memory segment.
+ * @param c The stop byte.
+ * @param maxlen The maximum number of bytes to copy.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @return `NULL` if `c` was not encountered, otherwise
+ * the position of `c` translated to `whither`,
+ * that is, the address of `whither` plus the
+ * number of copied characters; the address of
+ * one character passed the last written non-NUL
+ * character.
+ */
+char* strcncpy(char* restrict whither, const char* restrict whence, int c, size_t maxlen)
+{
+ const char* stop = memchr(whence, c, maxlen);
+ size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence);
+ char* r = stop == NULL ? NULL : (whither + n);
+ memcpy(whither, whence, n);
+ memset(whither, 0, maxlen - n);
+ return r;
+}
+
diff --git a/src/string/strn/strcnmove.c b/src/string/strn/strcnmove.c
new file mode 100644
index 0000000..f418e92
--- /dev/null
+++ b/src/string/strn/strcnmove.c
@@ -0,0 +1,54 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Copy a memory segment to another, possibly overlapping, segment,
+ * stop when a NUL byte or a specified byte is encountered.
+ *
+ * This is a slibc extension added for completeness.
+ * It is only available if GNU extensions are available.
+ *
+ * @param whither The destination memory segment.
+ * @param whence The source memory segment.
+ * @param c The stop byte.
+ * @param maxlen The maximum number of bytes to copy.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @return `NULL` if `c` was not encountered, otherwise
+ * the position of `c` translated to `whither`,
+ * that is, the address of `whither` plus the
+ * number of copied characters; the address of
+ * one character passed the last written non-NUL
+ * character.
+ */
+char* strcnmove(char* whither, const char* whence, int c, size_t maxlen)
+{
+ const char* stop = memchr(whence, c, maxlen);
+ size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence);
+ char* r = stop == NULL ? NULL : (whither + n);
+ memmove(whither, whence, n);
+ memset(whither, 0, maxlen - n);
+ return r;
+}
+
diff --git a/src/string/strn/strncasecmp.c b/src/string/strn/strncasecmp.c
new file mode 100644
index 0000000..919ff36
--- /dev/null
+++ b/src/string/strn/strncasecmp.c
@@ -0,0 +1,50 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+#include <ctype.h>
+
+
+
+/**
+ * Compare two strings alphabetically in a case insensitive manner.
+ * Be aware, only ASCII characters are case insensitive, non-ASCII
+ * characters are case sensitive.
+ *
+ * @param a A negative value is returned if this is the lesser.
+ * @param b A positive value is returned if this is the lesser.
+ * @param length The maximum number of characters to compare.
+ * @return Zero is returned if `a` and `b` are equal, otherwise,
+ * see the specifications for `a` and `b`.
+ */
+int strncasecmp(const char* a, const char* b, size_t length)
+{
+ int c1, c2;
+ for (; length--; a++, b++)
+ if (*a != *b)
+ {
+ c1 = isalpha(*a) ? tolower(*a) : (int)*a;
+ c2 = isalpha(*b) ? tolower(*b) : (int)*b;
+ if ((c1 -= c2))
+ return c1;
+ }
+ else if (!*a && !*b) return 0;
+ else if (!*a) return -1;
+ else if (!*b) return +1;
+ return 0;
+}
+
diff --git a/src/string/strn/strncasestr.c b/src/string/strn/strncasestr.c
new file mode 100644
index 0000000..e95dc4b
--- /dev/null
+++ b/src/string/strn/strncasestr.c
@@ -0,0 +1,38 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Finds the first occurrence of a substring.
+ * This search is case insensitive.
+ *
+ * This is a slibc extension added for completeness.
+ *
+ * @param haystack The string to search.
+ * @param needle The sought after substring.
+ * @param maxlen The maximum number of character to search.
+ * @return Pointer to the first occurrence of the
+ * substring, `NULL` if not found.
+ */
+char* (strncasestr)(const char* haystack, const char* needle, size_t maxlen)
+{
+ return (memcasemem)(haystack, strnlen(haystack, maxlen), needle, strlen(needle));
+}
+
diff --git a/src/string/strn/strncat.c b/src/string/strn/strncat.c
new file mode 100644
index 0000000..e7db0f2
--- /dev/null
+++ b/src/string/strn/strncat.c
@@ -0,0 +1,43 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Concatenate a string to the end of another string.
+ * The resulting strings must not overlap with the appended string.
+ *
+ * The use of this function is often a really bad idea.
+ *
+ * @param whither The string to extend.
+ * @param whence The string to append.
+ * @param maxlen The maximum number of bytes to copy.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @return `whither` is returned.
+ */
+char* strncat(char* restrict whither, const char* restrict whence, size_t maxlen)
+{
+ strncpy(whither + strlen(whither), whence, maxlen);
+ return whither;
+}
+
diff --git a/src/string/strn/strncmp.c b/src/string/strn/strncmp.c
new file mode 100644
index 0000000..daf5be5
--- /dev/null
+++ b/src/string/strn/strncmp.c
@@ -0,0 +1,39 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Compare two strings alphabetically in a case sensitive manner.
+ *
+ * @param a A negative value is returned if this is the lesser.
+ * @param b A positive value is returned if this is the lesser.
+ * @param length The maximum number of characters to compare.
+ * @return Zero is returned if `a` and `b` are equal, otherwise,
+ * see the specifications for `a` and `b`.
+ */
+int strncmp(const char* a, const char* b, size_t length)
+{
+ size_t n = strnlen(a, length);
+ size_t m = strnlen(b, length);
+ int r = memcmp(a, b, (n < m ? n : m));
+ return r ? r : n == m ? 0 : n < m ? -1 : +1;
+}
+
+
diff --git a/src/string/strn/strncpy.c b/src/string/strn/strncpy.c
new file mode 100644
index 0000000..f3eba84
--- /dev/null
+++ b/src/string/strn/strncpy.c
@@ -0,0 +1,43 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Copy a memory segment to another, non-overlapping, segment,
+ * stop when a NUL byte is encountered.
+ *
+ * @param whither The destination memory segment.
+ * @param whence The source memory segment.
+ * @param maxlen The maximum number of bytes to copy.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @return `whither` is returned.
+ */
+char* strncpy(char* restrict whither, const char* restrict whence, size_t maxlen)
+{
+ size_t n = strnlen(whence, maxlen);
+ memcpy(whither, whence, n);
+ memset(whither, 0, maxlen - n);
+ return whither;
+}
+
diff --git a/src/string/strn/strndup.c b/src/string/strn/strndup.c
new file mode 100644
index 0000000..ca3b3f0
--- /dev/null
+++ b/src/string/strn/strndup.c
@@ -0,0 +1,43 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+#include <stdlib.h>
+
+
+
+/**
+ * Duplicate a string.
+ *
+ * This is a GNU extension.
+ *
+ * @param string The string to duplicate.
+ * @param maxlen Truncate the string to this length, if it is longer.
+ * A NUL byte is guaranteed to always be written
+ * upon successful completion.
+ * @return The new string. `NULL` is returned on error
+ * and `errno` is set to indicate the error.
+ *
+ * @throws ENOMEM The process could not allocate sufficient amount of memory.
+ */
+char* strndup(const char* string, size_t maxlen)
+{
+ size_t n = strnlen(string, maxlen) + 1;
+ char* r = malloc(n * sizeof(char));
+ return r == NULL ? NULL : memcpy(r, string, n * sizeof(char));
+}
+
diff --git a/src/string/strn/strnlen.c b/src/string/strn/strnlen.c
new file mode 100644
index 0000000..5473682
--- /dev/null
+++ b/src/string/strn/strnlen.c
@@ -0,0 +1,36 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Variant of `strlen` that only inspects the
+ * beginning of a string.
+ *
+ * @param str The string.
+ * @param maxlen The number of bytes to inspect, at most.
+ * @return The number of bytes before, the first NUL byte.
+ * `maxlen` if no NUL byte was found.
+ */
+size_t strnlen(const char* str, size_t maxlen)
+{
+ const char* end = memchr(str, 0, maxlen);
+ return end == NULL ? maxlen : (size_t)(end - str);
+}
+
diff --git a/src/string/strn/strnmove.c b/src/string/strn/strnmove.c
new file mode 100644
index 0000000..a8b3224
--- /dev/null
+++ b/src/string/strn/strnmove.c
@@ -0,0 +1,45 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Copy a memory segment to another, possibly overlapping, segment,
+ * stop when a NUL byte is encountered.
+ *
+ * This is a slibc extension added for completeness.
+ *
+ * @param whither The destination memory segment.
+ * @param whence The source memory segment.
+ * @param maxlen The maximum number of bytes to copy.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @return `whither` is returned.
+ */
+char* strnmove(char* whither, const char* whence, size_t maxlen)
+{
+ size_t n = strnlen(whence, maxlen);
+ memmove(whither, whence, n);
+ memset(whither, 0, maxlen - n);
+ return whither;
+}
+
diff --git a/src/string/strn/strnstr.c b/src/string/strn/strnstr.c
new file mode 100644
index 0000000..8db61ac
--- /dev/null
+++ b/src/string/strn/strnstr.c
@@ -0,0 +1,39 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Finds the first occurrence of a substring.
+ * This search is case sensitive.
+ *
+ * This is a slibc extension added for because it was useful
+ * in implementing slibc itself.
+ *
+ * @param haystack The string to search.
+ * @param needle The sought after substring.
+ * @param maxlen The maximum number of character to search.
+ * @return Pointer to the first occurrence of the
+ * substring, `NULL` if not found.
+ */
+char* (strnstr)(const char* haystack, const char* needle, size_t maxlen)
+{
+ return (memmem)(haystack, strnlen(haystack, maxlen), needle, strlen(needle));
+}
+
diff --git a/src/string/strn/strstrncpy.c b/src/string/strn/strstrncpy.c
new file mode 100644
index 0000000..0beb8ec
--- /dev/null
+++ b/src/string/strn/strstrncpy.c
@@ -0,0 +1,55 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Copy a memory segment to another, non-overlapping, segment,
+ * stop when a NUL byte or a specified substring is encountered.
+ *
+ * This is a slibc extension added for completeness.
+ * It is only available if GNU extensions are available.
+ *
+ * @param whither The destination memory segment.
+ * @param whence The source memory segment.
+ * @param str The substring, ignored if `NULL`.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @param maxlen The maximum number of bytes to copy.
+ * @return `NULL` if `str` was not encountered, otherwise
+ * the position of `str` translated to `whither`,
+ * that is, the address of `whither` plus the
+ * number of copied characters; the address of
+ * one character passed the last written non-NUL
+ * character.
+ */
+char* strstrncpy(char* restrict whither, const char* restrict whence,
+ const char* restrict str, size_t maxlen)
+{
+ const char* stop = strnstr(whence, str, maxlen);
+ size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence);
+ char* r = stop == NULL ? NULL : (whither + n);
+ memcpy(whither, whence, n);
+ memset(whither, 0, maxlen - n);
+ return r;
+}
+
diff --git a/src/string/strn/strstrnmove.c b/src/string/strn/strstrnmove.c
new file mode 100644
index 0000000..9bec614
--- /dev/null
+++ b/src/string/strn/strstrnmove.c
@@ -0,0 +1,54 @@
+/**
+ * slibc — Yet another C library
+ * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+
+
+
+/**
+ * Copy a memory segment to another, possibly overlapping, segment,
+ * stop when a NUL byte or a specified substring is encountered.
+ *
+ * This is a slibc extension added for completeness.
+ * It is only available if GNU extensions are available.
+ *
+ * @param whither The destination memory segment.
+ * @param whence The source memory segment.
+ * @param str The substring, ignored if `NULL`.
+ * NOTE that if the resulting string at least this
+ * long, no NUL byte will be written to `whither'.
+ * On the otherhand, if the resultnig string is
+ * shorter, `whither` will be filled with NUL bytes
+ * until this amount of bytes have been written.
+ * @param maxlen The maximum number of bytes to copy.
+ * @return `NULL` if `str` was not encountered, otherwise
+ * the position of `str` translated to `whither`,
+ * that is, the address of `whither` plus the
+ * number of copied characters; the address of
+ * one character passed the last written non-NUL
+ * character.
+ */
+char* strstrnmove(char* whither, const char* whence, const char* restrict str, size_t maxlen)
+{
+ const char* stop = strnstr(whence, str, maxlen);
+ size_t n = stop == NULL ? strnlen(whence, maxlen) : (size_t)(stop - whence);
+ char* r = stop == NULL ? NULL : (whither + n);
+ memmove(whither, whence, n);
+ memset(whither, 0, maxlen - n);
+ return r;
+}
+