Implement strmem() and use it in join(1)

We want our delimiters to also contain 0 characters and have them
handled gracefully.
To accomplish this, I wrote a function strmem(), which looks for a
certain, arbitrarily long memory subset in a given string.
memmem() is a GNU extension and forces you to call strlen every time.
This commit is contained in:
FRIGN
2016-02-25 21:44:46 +01:00
committed by sin
parent e8eeb19fcd
commit 3396088666
4 changed files with 27 additions and 1 deletions

23
libutil/strmem.c Normal file
View File

@@ -0,0 +1,23 @@
/* See LICENSE file for copyright and license details. */
#include <stddef.h>
#include <string.h>
char *
strmem(char *haystack, char *needle, size_t needlelen)
{
size_t i;
for (i = 0; i < needlelen; i++) {
if (haystack[i] == '\0') {
return NULL;
}
}
for (; haystack[i]; i++) {
if (!(memcmp(haystack + i - needlelen, needle, needlelen))) {
return (haystack + i - needlelen);
}
}
return NULL;
}