xref: /rk3399_ARM-atf/lib/libc/strncmp.c (revision 25002a0042382f641f228e7045f55539d7d1103b)
1c6fdaa73SAntonio Nino Diaz /*-
2c6fdaa73SAntonio Nino Diaz  * SPDX-License-Identifier: BSD-3-Clause
3c6fdaa73SAntonio Nino Diaz  *
4c6fdaa73SAntonio Nino Diaz  * Copyright (c) 1989, 1993
5c6fdaa73SAntonio Nino Diaz  *	The Regents of the University of California.  All rights reserved.
6c6fdaa73SAntonio Nino Diaz  *
7c6fdaa73SAntonio Nino Diaz  * Redistribution and use in source and binary forms, with or without
8c6fdaa73SAntonio Nino Diaz  * modification, are permitted provided that the following conditions
9c6fdaa73SAntonio Nino Diaz  * are met:
10c6fdaa73SAntonio Nino Diaz  * 1. Redistributions of source code must retain the above copyright
11c6fdaa73SAntonio Nino Diaz  *    notice, this list of conditions and the following disclaimer.
12c6fdaa73SAntonio Nino Diaz  * 2. Redistributions in binary form must reproduce the above copyright
13c6fdaa73SAntonio Nino Diaz  *    notice, this list of conditions and the following disclaimer in the
14c6fdaa73SAntonio Nino Diaz  *    documentation and/or other materials provided with the distribution.
15c6fdaa73SAntonio Nino Diaz  * 3. Neither the name of the University nor the names of its contributors
16c6fdaa73SAntonio Nino Diaz  *    may be used to endorse or promote products derived from this software
17c6fdaa73SAntonio Nino Diaz  *    without specific prior written permission.
18c6fdaa73SAntonio Nino Diaz  *
19c6fdaa73SAntonio Nino Diaz  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20c6fdaa73SAntonio Nino Diaz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21c6fdaa73SAntonio Nino Diaz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22c6fdaa73SAntonio Nino Diaz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23c6fdaa73SAntonio Nino Diaz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24c6fdaa73SAntonio Nino Diaz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25c6fdaa73SAntonio Nino Diaz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26c6fdaa73SAntonio Nino Diaz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27c6fdaa73SAntonio Nino Diaz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28c6fdaa73SAntonio Nino Diaz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29c6fdaa73SAntonio Nino Diaz  * SUCH DAMAGE.
30c6fdaa73SAntonio Nino Diaz  */
31c6fdaa73SAntonio Nino Diaz 
322c5aca6eSAntonio Nino Diaz /*
33*34d7f196SBoyan Karatotev  * Portions copyright (c) 2018-2025, Arm Limited and Contributors.
342c5aca6eSAntonio Nino Diaz  * All rights reserved.
352c5aca6eSAntonio Nino Diaz  */
36c6fdaa73SAntonio Nino Diaz 
37*34d7f196SBoyan Karatotev #include <string_private.h>
38c6fdaa73SAntonio Nino Diaz 
39c6fdaa73SAntonio Nino Diaz int
strncmp(const char * s1,const char * s2,size_t n)40c6fdaa73SAntonio Nino Diaz strncmp(const char *s1, const char *s2, size_t n)
41c6fdaa73SAntonio Nino Diaz {
42c6fdaa73SAntonio Nino Diaz 
43c6fdaa73SAntonio Nino Diaz 	if (n == 0)
44c6fdaa73SAntonio Nino Diaz 		return (0);
45c6fdaa73SAntonio Nino Diaz 	do {
46c6fdaa73SAntonio Nino Diaz 		if (*s1 != *s2++)
47c6fdaa73SAntonio Nino Diaz 			return (*(const unsigned char *)s1 -
48c6fdaa73SAntonio Nino Diaz 				*(const unsigned char *)(s2 - 1));
49c6fdaa73SAntonio Nino Diaz 		if (*s1++ == '\0')
50c6fdaa73SAntonio Nino Diaz 			break;
51c6fdaa73SAntonio Nino Diaz 	} while (--n != 0);
52c6fdaa73SAntonio Nino Diaz 	return (0);
53c6fdaa73SAntonio Nino Diaz }
54