Mercurial > hg > CbC > CbC_gcc
annotate libiberty/filename_cmp.c @ 158:494b0b89df80 default tip
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2020 18:13:55 +0900 |
parents | 1830386684a0 |
children |
rev | line source |
---|---|
0 | 1 /* File name comparison routine. |
2 | |
145 | 3 Copyright (C) 2007-2020 Free Software Foundation, Inc. |
0 | 4 |
5 This program is free software; you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 2, or (at your option) | |
8 any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software Foundation, | |
17 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ | |
18 | |
19 #ifdef HAVE_CONFIG_H | |
20 #include "config.h" | |
21 #endif | |
22 | |
23 #ifdef HAVE_STRING_H | |
24 #include <string.h> | |
25 #endif | |
26 | |
111 | 27 #ifdef HAVE_STDLIB_H |
28 #include <stdlib.h> | |
29 #endif | |
30 | |
0 | 31 #include "filenames.h" |
32 #include "safe-ctype.h" | |
111 | 33 #include "libiberty.h" |
0 | 34 |
35 /* | |
36 | |
37 @deftypefn Extension int filename_cmp (const char *@var{s1}, const char *@var{s2}) | |
38 | |
39 Return zero if the two file names @var{s1} and @var{s2} are equivalent. | |
40 If not equivalent, the returned value is similar to what @code{strcmp} | |
41 would return. In other words, it returns a negative value if @var{s1} | |
42 is less than @var{s2}, or a positive value if @var{s2} is greater than | |
43 @var{s2}. | |
44 | |
45 This function does not normalize file names. As a result, this function | |
46 will treat filenames that are spelled differently as different even in | |
47 the case when the two filenames point to the same underlying file. | |
48 However, it does handle the fact that on DOS-like file systems, forward | |
49 and backward slashes are equal. | |
50 | |
51 @end deftypefn | |
52 | |
53 */ | |
54 | |
55 int | |
56 filename_cmp (const char *s1, const char *s2) | |
57 { | |
111 | 58 #if !defined(HAVE_DOS_BASED_FILE_SYSTEM) \ |
59 && !defined(HAVE_CASE_INSENSITIVE_FILE_SYSTEM) | |
0 | 60 return strcmp(s1, s2); |
61 #else | |
62 for (;;) | |
63 { | |
111 | 64 int c1 = *s1; |
65 int c2 = *s2; | |
0 | 66 |
111 | 67 #if defined (HAVE_CASE_INSENSITIVE_FILE_SYSTEM) |
68 c1 = TOLOWER (c1); | |
69 c2 = TOLOWER (c2); | |
70 #endif | |
71 | |
72 #if defined (HAVE_DOS_BASED_FILE_SYSTEM) | |
0 | 73 /* On DOS-based file systems, the '/' and the '\' are equivalent. */ |
74 if (c1 == '/') | |
75 c1 = '\\'; | |
76 if (c2 == '/') | |
77 c2 = '\\'; | |
111 | 78 #endif |
0 | 79 |
80 if (c1 != c2) | |
81 return (c1 - c2); | |
82 | |
83 if (c1 == '\0') | |
84 return 0; | |
85 | |
86 s1++; | |
87 s2++; | |
88 } | |
89 #endif | |
90 } | |
91 | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
92 /* |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
93 |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
94 @deftypefn Extension int filename_ncmp (const char *@var{s1}, const char *@var{s2}, size_t @var{n}) |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
95 |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
96 Return zero if the two file names @var{s1} and @var{s2} are equivalent |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
97 in range @var{n}. |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
98 If not equivalent, the returned value is similar to what @code{strncmp} |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
99 would return. In other words, it returns a negative value if @var{s1} |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
100 is less than @var{s2}, or a positive value if @var{s2} is greater than |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
101 @var{s2}. |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
102 |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
103 This function does not normalize file names. As a result, this function |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
104 will treat filenames that are spelled differently as different even in |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
105 the case when the two filenames point to the same underlying file. |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
106 However, it does handle the fact that on DOS-like file systems, forward |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
107 and backward slashes are equal. |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
108 |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
109 @end deftypefn |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
110 |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
111 */ |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
112 |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
113 int |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
114 filename_ncmp (const char *s1, const char *s2, size_t n) |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
115 { |
111 | 116 #if !defined(HAVE_DOS_BASED_FILE_SYSTEM) \ |
117 && !defined(HAVE_CASE_INSENSITIVE_FILE_SYSTEM) | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
118 return strncmp(s1, s2, n); |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
119 #else |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
120 if (!n) |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
121 return 0; |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
122 for (; n > 0; --n) |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
123 { |
111 | 124 int c1 = *s1; |
125 int c2 = *s2; | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
126 |
111 | 127 #if defined (HAVE_CASE_INSENSITIVE_FILE_SYSTEM) |
128 c1 = TOLOWER (c1); | |
129 c2 = TOLOWER (c2); | |
130 #endif | |
131 | |
132 #if defined (HAVE_DOS_BASED_FILE_SYSTEM) | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
133 /* On DOS-based file systems, the '/' and the '\' are equivalent. */ |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
134 if (c1 == '/') |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
135 c1 = '\\'; |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
136 if (c2 == '/') |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
137 c2 = '\\'; |
111 | 138 #endif |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
139 |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
140 if (c1 == '\0' || c1 != c2) |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
141 return (c1 - c2); |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
142 |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
143 s1++; |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
144 s2++; |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
145 } |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
146 return 0; |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
147 #endif |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
148 } |
111 | 149 |
150 /* | |
151 | |
152 @deftypefn Extension hashval_t filename_hash (const void *@var{s}) | |
153 | |
154 Return the hash value for file name @var{s} that will be compared | |
155 using filename_cmp. | |
156 This function is for use with hashtab.c hash tables. | |
157 | |
158 @end deftypefn | |
159 | |
160 */ | |
161 | |
162 hashval_t | |
163 filename_hash (const void *s) | |
164 { | |
165 /* The cast is for -Wc++-compat. */ | |
166 const unsigned char *str = (const unsigned char *) s; | |
167 hashval_t r = 0; | |
168 unsigned char c; | |
169 | |
170 while ((c = *str++) != 0) | |
171 { | |
172 if (c == '\\') | |
173 c = '/'; | |
174 c = TOLOWER (c); | |
175 r = r * 67 + c - 113; | |
176 } | |
177 | |
178 return r; | |
179 } | |
180 | |
181 /* | |
182 | |
183 @deftypefn Extension int filename_eq (const void *@var{s1}, const void *@var{s2}) | |
184 | |
185 Return non-zero if file names @var{s1} and @var{s2} are equivalent. | |
186 This function is for use with hashtab.c hash tables. | |
187 | |
188 @end deftypefn | |
189 | |
190 */ | |
191 | |
192 int | |
193 filename_eq (const void *s1, const void *s2) | |
194 { | |
195 /* The casts are for -Wc++-compat. */ | |
196 return filename_cmp ((const char *) s1, (const char *) s2) == 0; | |
197 } | |
198 | |
199 /* | |
200 | |
201 @deftypefn Extension int canonical_filename_eq (const char *@var{a}, const char *@var{b}) | |
202 | |
203 Return non-zero if file names @var{a} and @var{b} are equivalent. | |
204 This function compares the canonical versions of the filenames as returned by | |
205 @code{lrealpath()}, so that so that different file names pointing to the same | |
206 underlying file are treated as being identical. | |
207 | |
208 @end deftypefn | |
209 | |
210 */ | |
211 | |
212 int | |
213 canonical_filename_eq (const char * a, const char * b) | |
214 { | |
215 char * ca = lrealpath(a); | |
216 char * cb = lrealpath(b); | |
217 int res = filename_eq (ca, cb); | |
218 free (ca); | |
219 free (cb); | |
220 return res; | |
221 } |