File: strcasecmp.c

package info (click to toggle)
nmh 1.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,056 kB
  • ctags: 4,531
  • sloc: ansic: 50,788; sh: 3,141; makefile: 965; awk: 74
file content (57 lines) | stat: -rw-r--r-- 1,207 bytes parent folder | download
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
54
55
56
57

/*
 * strcasecmp.c -- compare strings, ignoring case
 *
 * $Id: strcasecmp.c,v 1.3 2006/03/08 12:14:16 bress Exp $
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>

/*
 * Our version of strcasecmp has to deal with NULL strings.
 * Once that is fixed in the rest of the code, we can use the
 * native version, instead of this one.
 */

int
mh_strcasecmp (const char *s1, const char *s2) 
{
    const unsigned char *us1, *us2;

    us1 = (const unsigned char *) s1,
    us2 = (const unsigned char *) s2;

    if (!us1)
	us1 = "";
    if (!us2)
	us2 = "";
 
    while (tolower(*us1) == tolower(*us2++)) 
	if (*us1++ == '\0')
	    return (0);
    return (tolower(*us1) - tolower(*--us2));
}
 

int
mh_strncasecmp (const char *s1, const char *s2, size_t n)
{
    const unsigned char *us1, *us2;

    if (n != 0) { 
	us1 = (const unsigned char *) s1,
	us2 = (const unsigned char *) s2;

	do {  
	    if (tolower(*us1) != tolower(*us2++))
		return (tolower(*us1) - tolower(*--us2));
	    if (*us1++ == '\0')
		break;  
	} while (--n != 0);
    } 
    return (0);
}