-
Notifications
You must be signed in to change notification settings - Fork 2
/
compat.cc
52 lines (43 loc) · 1.2 KB
/
compat.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "compat.h"
#ifndef HAVE_MEMMEM
#ifndef _LIBC
# define __builtin_expect(expr, val) (expr)
#endif
#include <string.h>
/* Return the first occurrence of NEEDLE in HAYSTACK. */
void *
memmem (const void *haystack, size_t haystack_len, const void *needle,
size_t needle_len)
{
const char *begin;
const char *const last_possible
= (const char *) haystack + haystack_len - needle_len;
if (needle_len == 0)
/* The first occurrence of the empty string is deemed to occur at
the beginning of the string. */
return (void *) haystack;
/* Sanity check, otherwise the loop might search through the whole
memory. */
if (__builtin_expect (haystack_len < needle_len, 0))
return NULL;
for (begin = (const char *) haystack; begin <= last_possible; ++begin)
if (begin[0] == ((const char *) needle)[0] &&
!memcmp ((const void *) &begin[1],
(const void *) ((const char *) needle + 1),
needle_len - 1))
return (void *) begin;
return NULL;
}
#endif
#ifndef HAVE_STPNCPY
#include <string.h>
#include <stddef.h>
char *
stpncpy (char *dst, const char *src, size_t len)
{
size_t n = strlen (src);
if (n > len)
n = len;
return strncpy (dst, src, len) + n;
}
#endif