1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /*
18 FUNCTION
19 <<strncmp>>---character string compare
20
21 INDEX
22 strncmp
23
24 SYNOPSIS
25 #include <string.h>
26 int strncmp(const char *<[a]>, const char * <[b]>, size_t <[length]>);
27
28 DESCRIPTION
29 <<strncmp>> compares up to <[length]> characters
30 from the string at <[a]> to the string at <[b]>.
31
32 RETURNS
33 If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
34 <<strncmp>> returns a number greater than zero. If the two
35 strings are equivalent, <<strncmp>> returns zero. If <<*<[a]>>>
36 sorts lexicographically before <<*<[b]>>>, <<strncmp>> returns a
37 number less than zero.
38
39 PORTABILITY
40 <<strncmp>> is ANSI C.
41
42 <<strncmp>> requires no supporting OS subroutines.
43
44 QUICKREF
45 strncmp ansi pure
46 */
47
48 #include <string.h>
49 #include <limits.h>
50 #include <stdint.h>
51
52 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
53 #define UNALIGNED(X, Y) \
54 (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
55
56 /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */
57 #if LONG_MAX == 2147483647L
58 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
59 #else
60 #if LONG_MAX == 9223372036854775807L
61 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
62 #else
63 #error long int is not a 32bit or 64bit type.
64 #endif
65 #endif
66
67 #ifndef DETECTNULL
68 #error long int is not a 32bit or 64bit byte
69 #endif
70
71 int
strncmp(const char * s1,const char * s2,size_t n)72 strncmp (const char *s1,
73 const char *s2,
74 size_t n)
75 {
76 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) || \
77 defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
78 if (n == 0)
79 return 0;
80
81 while (n-- != 0 && *s1 == *s2)
82 {
83 if (n == 0 || *s1 == '\0')
84 break;
85 s1++;
86 s2++;
87 }
88
89 return (*(unsigned char *) s1) - (*(unsigned char *) s2);
90 #else
91 unsigned long *a1;
92 unsigned long *a2;
93
94 if (n == 0)
95 return 0;
96
97 /* If s1 or s2 are unaligned, then compare bytes. */
98 if (!UNALIGNED (s1, s2))
99 {
100 /* If s1 and s2 are word-aligned, compare them a word at a time. */
101 a1 = (unsigned long*)s1;
102 a2 = (unsigned long*)s2;
103 while (n >= sizeof (long) && *a1 == *a2)
104 {
105 n -= sizeof (long);
106
107 /* If we've run out of bytes or hit a null, return zero
108 since we already know *a1 == *a2. */
109 if (n == 0 || DETECTNULL (*a1))
110 return 0;
111
112 a1++;
113 a2++;
114 }
115
116 /* A difference was detected in last few bytes of s1, so search bytewise */
117 s1 = (char*)a1;
118 s2 = (char*)a2;
119 }
120
121 while (n-- > 0 && *s1 == *s2)
122 {
123 /* If we've run out of bytes or hit a null, return zero
124 since we already know *s1 == *s2. */
125 if (n == 0 || *s1 == '\0')
126 return 0;
127 s1++;
128 s2++;
129 }
130 return (*(unsigned char *) s1) - (*(unsigned char *) s2);
131 #endif /* not PREFER_SIZE_OVER_SPEED */
132 }
133