summaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 5403816ec8e0c125feb0e9a42c7e02ecac5c3c3e (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
43
44
45
46
47
48
49
50
51
52
53
#include <ctype.h>

#include "internal.h"

/**
 * Case insensitive string comparison
 *
 * \param s1 Pointer to string
 * \param s2 Pointer to string
 * \return 0 if strings match, <> 0 if no match
 */
int strcasecmp(const char *s1, const char *s2)
{
	int i;

	if (!s1 || !s2)
		return 1; /* this is arbitrary */

	if (s1 == s2)
		return 0;

	while ((i = tolower(*s1)) && i == tolower(*s2))
		s1++, s2++;

	return ((unsigned char) tolower(*s1) - (unsigned char) tolower(*s2));
}

/**
 * Length-limited case insensitive string comparison
 *
 * \param s1 Pointer to string
 * \param s2 Pointer to string
 * \param len Length to compare
 * \return 0 if strings match, <> 0 if no match
 */
int strncasecmp(const char *s1, const char *s2, size_t len)
{
	int i;

	if (!s1 || !s2)
		return 1; /* this is arbitrary */

	if (len == 0)
		return 0;

	if (s1 == s2)
		return 0;

	while (len-- && (i = tolower(*s1)) && i == tolower(*s2))
		s1++, s2++;

	return ((unsigned char) tolower(*s1) - (unsigned char) tolower(*s2));
}