PR c++/79657
[official-gcc.git] / libiberty / filename_cmp.c
blobe196bdfadd8549cf405d6ea7fdcf23a263b9de0a
1 /* File name comparison routine.
3 Copyright (C) 2007-2017 Free Software Foundation, Inc.
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.
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.
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. */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
31 #include "filenames.h"
32 #include "safe-ctype.h"
33 #include "libiberty.h"
37 @deftypefn Extension int filename_cmp (const char *@var{s1}, const char *@var{s2})
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}.
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.
51 @end deftypefn
55 int
56 filename_cmp (const char *s1, const char *s2)
58 #if !defined(HAVE_DOS_BASED_FILE_SYSTEM) \
59 && !defined(HAVE_CASE_INSENSITIVE_FILE_SYSTEM)
60 return strcmp(s1, s2);
61 #else
62 for (;;)
64 int c1 = *s1;
65 int c2 = *s2;
67 #if defined (HAVE_CASE_INSENSITIVE_FILE_SYSTEM)
68 c1 = TOLOWER (c1);
69 c2 = TOLOWER (c2);
70 #endif
72 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
73 /* On DOS-based file systems, the '/' and the '\' are equivalent. */
74 if (c1 == '/')
75 c1 = '\\';
76 if (c2 == '/')
77 c2 = '\\';
78 #endif
80 if (c1 != c2)
81 return (c1 - c2);
83 if (c1 == '\0')
84 return 0;
86 s1++;
87 s2++;
89 #endif
94 @deftypefn Extension int filename_ncmp (const char *@var{s1}, const char *@var{s2}, size_t @var{n})
96 Return zero if the two file names @var{s1} and @var{s2} are equivalent
97 in range @var{n}.
98 If not equivalent, the returned value is similar to what @code{strncmp}
99 would return. In other words, it returns a negative value if @var{s1}
100 is less than @var{s2}, or a positive value if @var{s2} is greater than
101 @var{s2}.
103 This function does not normalize file names. As a result, this function
104 will treat filenames that are spelled differently as different even in
105 the case when the two filenames point to the same underlying file.
106 However, it does handle the fact that on DOS-like file systems, forward
107 and backward slashes are equal.
109 @end deftypefn
114 filename_ncmp (const char *s1, const char *s2, size_t n)
116 #if !defined(HAVE_DOS_BASED_FILE_SYSTEM) \
117 && !defined(HAVE_CASE_INSENSITIVE_FILE_SYSTEM)
118 return strncmp(s1, s2, n);
119 #else
120 if (!n)
121 return 0;
122 for (; n > 0; --n)
124 int c1 = *s1;
125 int c2 = *s2;
127 #if defined (HAVE_CASE_INSENSITIVE_FILE_SYSTEM)
128 c1 = TOLOWER (c1);
129 c2 = TOLOWER (c2);
130 #endif
132 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
133 /* On DOS-based file systems, the '/' and the '\' are equivalent. */
134 if (c1 == '/')
135 c1 = '\\';
136 if (c2 == '/')
137 c2 = '\\';
138 #endif
140 if (c1 == '\0' || c1 != c2)
141 return (c1 - c2);
143 s1++;
144 s2++;
146 return 0;
147 #endif
152 @deftypefn Extension hashval_t filename_hash (const void *@var{s})
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.
158 @end deftypefn
162 hashval_t
163 filename_hash (const void *s)
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;
170 while ((c = *str++) != 0)
172 if (c == '\\')
173 c = '/';
174 c = TOLOWER (c);
175 r = r * 67 + c - 113;
178 return r;
183 @deftypefn Extension int filename_eq (const void *@var{s1}, const void *@var{s2})
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.
188 @end deftypefn
193 filename_eq (const void *s1, const void *s2)
195 /* The casts are for -Wc++-compat. */
196 return filename_cmp ((const char *) s1, (const char *) s2) == 0;
201 @deftypefn Extension int canonical_filename_eq (const char *@var{a}, const char *@var{b})
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.
208 @end deftypefn
213 canonical_filename_eq (const char * a, const char * b)
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;