filename_cmp.c 5.3 KB

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