1 /*
2 FUNCTION
3 	<<strncmp>>---character string compare
4 
5 INDEX
6 	strncmp
7 
8 ANSI_SYNOPSIS
9 	#include <string.h>
10 	int strncmp(const char *<[a]>, const char * <[b]>, size_t <[length]>);
11 
12 TRAD_SYNOPSIS
13 	#include <string.h>
14 	int strncmp(<[a]>, <[b]>, <[length]>)
15 	char *<[a]>;
16 	char *<[b]>;
17 	size_t <[length]>
18 
19 DESCRIPTION
20 	<<strncmp>> compares up to <[length]> characters
21 	from the string at <[a]> to the string at <[b]>.
22 
23 RETURNS
24 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
25 	<<strncmp>> returns a number greater than zero.  If the two
26 	strings are equivalent, <<strncmp>> returns zero.  If <<*<[a]>>>
27 	sorts lexicographically before <<*<[b]>>>, <<strncmp>> returns a
28 	number less than zero.
29 
30 PORTABILITY
31 <<strncmp>> is ANSI C.
32 
33 <<strncmp>> requires no supporting OS subroutines.
34 
35 QUICKREF
36 	strncmp ansi pure
37 */
38 
39 #include <section_config.h>
40 #include <basic_types.h>
41 
42 #include <string.h>
43 #include <limits.h>
44 
45 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
46 #define UNALIGNED(X, Y) \
47   (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
48 
49 /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */
50 #if LONG_MAX == 2147483647L
51 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
52 #else
53 #if LONG_MAX == 9223372036854775807L
54 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
55 #else
56 #error long int is not a 32bit or 64bit type.
57 #endif
58 #endif
59 
60 #ifndef DETECTNULL
61 #error long int is not a 32bit or 64bit byte
62 #endif
63 
64 LIBC_ROM_TEXT_SECTION
65 _LONG_CALL_
_strncmp(const char * s1,const char * s2,size_t n)66 int _strncmp(const char *s1 , const char *s2 , size_t n)
67 {
68 #if defined(PREFER_SIZE_OVER_SPEED)
69   if (n == 0)
70     return 0;
71 
72   while (n-- != 0 && *s1 == *s2)
73     {
74       if (n == 0 || *s1 == '\0')
75 	break;
76       s1++;
77       s2++;
78     }
79 
80   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
81 #else
82   unsigned long *a1;
83   unsigned long *a2;
84 
85   if (n == 0)
86     return 0;
87 
88   /* If s1 or s2 are unaligned, then compare bytes. */
89   if (!UNALIGNED (s1, s2))
90     {
91       /* If s1 and s2 are word-aligned, compare them a word at a time. */
92       a1 = (unsigned long*)s1;
93       a2 = (unsigned long*)s2;
94       while (n >= sizeof (long) && *a1 == *a2)
95         {
96           n -= sizeof (long);
97 
98           /* If we've run out of bytes or hit a null, return zero
99 	     since we already know *a1 == *a2.  */
100           if (n == 0 || DETECTNULL (*a1))
101 	    return 0;
102 
103           a1++;
104           a2++;
105         }
106 
107       /* A difference was detected in last few bytes of s1, so search bytewise */
108       s1 = (char*)a1;
109       s2 = (char*)a2;
110     }
111 
112   while (n-- > 0 && *s1 == *s2)
113     {
114       /* If we've run out of bytes or hit a null, return zero
115 	 since we already know *s1 == *s2.  */
116       if (n == 0 || *s1 == '\0')
117 	return 0;
118       s1++;
119       s2++;
120     }
121   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
122 #endif /* not PREFER_SIZE_OVER_SPEED */
123 }
124