2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2006 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
23 int (*pcollator
)(const char *, const char *);
27 set_collation(int collation
, int ctype
)
29 extern int collator(const char *, const char *); /* set to strcoll if available in system.h */
38 * This may not have the desired effect, if collator is not
39 * defined to be strcoll in os.h and strcmp and friends
40 * don't know about locales. If your system does have strcoll
41 * but we haven't defined collator to be strcoll in os.h, let us know.
43 status
= setlocale(LC_COLLATE
, "");
46 * If there is an error or if the locale is the "C" locale, then we
47 * don't want to use strcoll because in the default "C" locale strcoll
48 * uses strcmp ordering and we want strucmp ordering.
50 * The problem with this is that setlocale returns a string which is
51 * not equal to "C" on some systems even when the locale is "C", so we
52 * can't really tell on those systems. On some systems like that, we
53 * may end up with a strcmp-style collation instead of a strucmp-style.
54 * We recommend that the users of those systems explicitly set
55 * LC_COLLATE in their environment.
57 if(status
&& !(status
[0] == 'C' && status
[1] == '\0'))
63 (void)setlocale(LC_CTYPE
, "");
68 setlocale(LC_TIME
, "");
74 * sstrcasecmp - compare two pointers to strings case independently
77 sstrcasecmp(const qsort_t
*s1
, const qsort_t
*s2
)
79 return((*pcollator
)(*(char **)s1
, *(char **)s2
));
85 /*--------------------------------------------------
86 A case insensitive strcmp()
88 Args: o, r -- The two strings to compare
90 Result: integer indicating which is greater
93 strucmp(const char *o
, const char *r
)
105 && ((isupper((unsigned char)(*o
))
106 ? (unsigned char)tolower((unsigned char)(*o
))
107 : (unsigned char)(*o
))
108 == (isupper((unsigned char)(*r
))
109 ? (unsigned char)tolower((unsigned char)(*r
))
110 : (unsigned char)(*r
)))){
115 return((isupper((unsigned char)(*o
))
116 ? tolower((unsigned char)(*o
))
117 : (int)(unsigned char)(*o
))
118 - (isupper((unsigned char)(*r
))
119 ? tolower((unsigned char)(*r
))
120 : (int)(unsigned char)(*r
)));
123 /*----------------------------------------------------------------------
124 A case insensitive strncmp()
126 Args: o, r -- The two strings to compare
127 n -- length to stop comparing strings at
129 Result: integer indicating which is greater
133 struncmp(const char *o
, const char *r
, int n
)
149 && ((isupper((unsigned char)(*o
))
150 ? (unsigned char)tolower((unsigned char)(*o
))
151 : (unsigned char)(*o
))
152 == (isupper((unsigned char)(*r
))
153 ? (unsigned char)tolower((unsigned char)(*r
))
154 : (unsigned char)(*r
)))){
160 return((isupper((unsigned char)(*o
))
161 ? tolower((unsigned char)(*o
))
162 : (int)(unsigned char)(*o
))
163 - (isupper((unsigned char)(*r
))
164 ? tolower((unsigned char)(*r
))
165 : (int)(unsigned char)(*r
)));