* Clean up some function definitions to comply with strict
[alpine.git] / pith / osdep / collate.c
blob1481d28080874cf90d65b553dd8a3e7a59ddfc57
1 /*
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 * ========================================================================
15 #include <system.h>
17 #include "collate.h"
21 * global hook
23 int (*pcollator)(const char *, const char *);
26 void
27 set_collation(int collation, int ctype)
29 extern int collator(const char *, const char *); /* set to strcoll if available in system.h */
31 pcollator = strucmp;
33 #ifdef LC_COLLATE
34 if(collation){
35 char *status = NULL;
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'))
58 pcollator = collator;
60 #endif
61 #ifdef LC_CTYPE
62 if(ctype){
63 (void)setlocale(LC_CTYPE, "");
65 #endif
67 #ifdef LC_TIME
68 setlocale(LC_TIME, "");
69 #endif
74 * sstrcasecmp - compare two pointers to strings case independently
76 int
77 sstrcasecmp(const qsort_t *s1, const qsort_t *s2)
79 return((*pcollator)(*(char **)s1, *(char **)s2));
83 #ifndef _WINDOWS
85 /*--------------------------------------------------
86 A case insensitive strcmp()
88 Args: o, r -- The two strings to compare
90 Result: integer indicating which is greater
91 ---*/
92 int
93 strucmp(const char *o, const char *r)
95 if(o == NULL){
96 if(r == NULL)
97 return 0;
98 else
99 return -1;
101 else if(r == NULL)
102 return 1;
104 while(*o && *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)))){
111 o++;
112 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
131 ----*/
133 struncmp(const char *o, const char *r, int n)
135 if(n < 1)
136 return 0;
138 if(o == NULL){
139 if(r == NULL)
140 return 0;
141 else
142 return -1;
144 else if(r == NULL)
145 return 1;
147 n--;
148 while(n && *o && *r
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)))){
155 o++;
156 r++;
157 n--;
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)));
167 #endif