summaryrefslogtreecommitdiff
path: root/src/string/strncmp.c
blob: 0cd6d872dc230a7515db0bea13e629bc2b2cbb77 (plain)
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
#include <string.h>
#include "_safety.h"

/** compare bound strings **/

int strncmp(const char *s1, const char *s2, size_t n)
{
	SIGNAL_SAFE(0);
	ASSERT_NONNULL(s1);
	ASSERT_NONNULL(s2);
	/* no modifcation, overlap is OK */

	DANGEROUS_READ(s1, -1);
	if (strlen(s1) < n) {
		n = strlen(s1);
	}
	DANGER_OVER();

	DANGEROUS_READ(s2, -1);
	if (strlen(s2) < n) {
		n = strlen(s2);
	}
	DANGER_OVER();

	/*
	RETURN(NEGATIVE, ARGUMENT(s1) is less than ARGUMENT(s2));
	RETURN(ZERO, ARGUMENT(s1) is equal to ARGUMENT(s2));
	RETURN(POSITIVE, ARGUMENT(s1) is greater than ARGUMENT(s2));
	*/
	return memcmp(s1, s2, n);
}

CHECK_3(int, 0, strncmp, const char *, const char *, size_t)

/***
compares up to the first ARGUMENT(n) bytes of the strings at ARGUMENT(s1) and
ARGUMENT(s2), or until the first CHAR(\0), whichever comes first.
***/

/*
STDC(1)
*/