zephyr/lib/libc/minimal/source/string/strncasecmp.c
Abramo Bagnara 8521b43546 coding guidelines: comply with MISRA C:2012 Rule 21.13
MISRA C:2012 Rule 21.13 (Any value passed to a function in <ctype.h>
shall be representable as an unsigned char or be the value EOF).

Functions in <ctype.h> have undefined behavior if they are called with
any other value. Callers affected by this change are not prepared to
handle EOF anyway. The addition of these casts avoids the issue
and does not result in any performance penalty.

Signed-off-by: Abramo Bagnara <abramo.bagnara@bugseng.com>
Signed-off-by: Simon Hein <SHein@baumer.com>
2022-06-30 17:34:28 -04:00

28 lines
450 B
C

/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <ctype.h>
int
strncasecmp(const char *s1, const char *s2, size_t n)
{
unsigned char c = 1U;
for (; c && n != 0; n--) {
unsigned char lower1, lower2;
c = *s1++;
lower1 = tolower(c);
lower2 = tolower((unsigned char)*s2++);
if (lower1 != lower2) {
return (lower1 > lower2) - (lower1 < lower2);
}
}
return 0;
}