1 /* File name comparison routine.
3 Copyright (C) 2007-2023 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)
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. */
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
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.
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
);
67 #if defined (HAVE_CASE_INSENSITIVE_FILE_SYSTEM)
72 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
73 /* On DOS-based file systems, the '/' and the '\' are equivalent. */
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
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
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.
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
);
127 #if defined (HAVE_CASE_INSENSITIVE_FILE_SYSTEM)
132 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
133 /* On DOS-based file systems, the '/' and the '\' are equivalent. */
140 if (c1
== '\0' || c1
!= c2
)
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
156 This function is for use with hashtab.c hash tables.
163 filename_hash (const void *s
)
165 /* The cast is for -Wc++-compat. */
166 const unsigned char *str
= (const unsigned char *) s
;
170 while ((c
= *str
++) != 0)
175 r
= r
* 67 + c
- 113;
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.
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.
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
);