filevercmp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  3. Copyright (C) 2001 Anthony Towns <aj@azure.humbug.org.au>
  4. Copyright (C) 2008-2018 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 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include <config.h>
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include "lib/strutil.h"
  22. /*** global variables ****************************************************************************/
  23. /*** file scope macro definitions ****************************************************************/
  24. /*** file scope type declarations ****************************************************************/
  25. /*** file scope variables ************************************************************************/
  26. /* --------------------------------------------------------------------------------------------- */
  27. /*** file scope functions ************************************************************************/
  28. /* --------------------------------------------------------------------------------------------- */
  29. /* Match a file suffix defined by this regular expression: /(\.[A-Za-z~][A-Za-z0-9~]*)*$/
  30. *
  31. * @str pointer to string to scan.
  32. *
  33. * @return pointer to the matching suffix, or NULL if not found.
  34. * Upon return, @str points to terminating NUL.
  35. */
  36. static const char *
  37. match_suffix (const char **str)
  38. {
  39. const char *match = NULL;
  40. gboolean read_alpha = FALSE;
  41. while (**str != '\0')
  42. {
  43. if (read_alpha)
  44. {
  45. read_alpha = FALSE;
  46. if (!g_ascii_isalpha (**str) && **str != '~')
  47. match = NULL;
  48. }
  49. else if (**str == '.')
  50. {
  51. read_alpha = TRUE;
  52. if (match == NULL)
  53. match = *str;
  54. }
  55. else if (!g_ascii_isalnum (**str) && **str != '~')
  56. match = NULL;
  57. (*str)++;
  58. }
  59. return match;
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. /* verrevcmp helper function */
  63. static int
  64. order (unsigned char c)
  65. {
  66. if (g_ascii_isdigit (c))
  67. return 0;
  68. if (g_ascii_isalpha (c))
  69. return c;
  70. if (c == '~')
  71. return -1;
  72. return (int) c + UCHAR_MAX + 1;
  73. }
  74. /* --------------------------------------------------------------------------------------------- */
  75. /* Slightly modified verrevcmp function from dpkg
  76. *
  77. * This implements the algorithm for comparison of version strings
  78. * specified by Debian and now widely adopted. The detailed
  79. * specification can be found in the Debian Policy Manual in the
  80. * section on the 'Version' control field. This version of the code
  81. * implements that from s5.6.12 of Debian Policy v3.8.0.1
  82. * https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
  83. *
  84. * @s1 first string to compare
  85. * @s1_len length of @s1
  86. * @s2 second string to compare
  87. * @s2_len length of @s2
  88. *
  89. * @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
  90. */
  91. static int
  92. verrevcmp (const char *s1, size_t s1_len, const char *s2, size_t s2_len)
  93. {
  94. size_t s1_pos = 0;
  95. size_t s2_pos = 0;
  96. while (s1_pos < s1_len || s2_pos < s2_len)
  97. {
  98. int first_diff = 0;
  99. while ((s1_pos < s1_len && !g_ascii_isdigit (s1[s1_pos]))
  100. || (s2_pos < s2_len && !g_ascii_isdigit (s2[s2_pos])))
  101. {
  102. int s1_c = 0;
  103. int s2_c = 0;
  104. if (s1_pos != s1_len)
  105. s1_c = order (s1[s1_pos]);
  106. if (s2_pos != s2_len)
  107. s2_c = order (s2[s2_pos]);
  108. if (s1_c != s2_c)
  109. return (s1_c - s2_c);
  110. s1_pos++;
  111. s2_pos++;
  112. }
  113. while (s1[s1_pos] == '0')
  114. s1_pos++;
  115. while (s2[s2_pos] == '0')
  116. s2_pos++;
  117. while (g_ascii_isdigit (s1[s1_pos]) && g_ascii_isdigit (s2[s2_pos]))
  118. {
  119. if (first_diff == 0)
  120. first_diff = s1[s1_pos] - s2[s2_pos];
  121. s1_pos++;
  122. s2_pos++;
  123. }
  124. if (g_ascii_isdigit (s1[s1_pos]))
  125. return 1;
  126. if (g_ascii_isdigit (s2[s2_pos]))
  127. return -1;
  128. if (first_diff != 0)
  129. return first_diff;
  130. }
  131. return 0;
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. /*** public functions ****************************************************************************/
  135. /* --------------------------------------------------------------------------------------------- */
  136. /* Compare version strings.
  137. *
  138. * @s1 first string to compare
  139. * @s2 second string to compare
  140. *
  141. * @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
  142. */
  143. int
  144. filevercmp (const char *s1, const char *s2)
  145. {
  146. const char *s1_pos, *s2_pos;
  147. const char *s1_suffix, *s2_suffix;
  148. size_t s1_len, s2_len;
  149. int simple_cmp, result;
  150. /* easy comparison to see if strings are identical */
  151. simple_cmp = strcmp (s1, s2);
  152. if (simple_cmp == 0)
  153. return 0;
  154. /* special handle for "", "." and ".." */
  155. if (*s1 == '\0')
  156. return -1;
  157. if (*s2 == '\0')
  158. return 1;
  159. if (DIR_IS_DOT (s1))
  160. return -1;
  161. if (DIR_IS_DOT (s2))
  162. return 1;
  163. if (DIR_IS_DOTDOT (s1))
  164. return -1;
  165. if (DIR_IS_DOTDOT (s2))
  166. return 1;
  167. /* special handle for other hidden files */
  168. if (*s1 == '.' && *s2 != '.')
  169. return -1;
  170. if (*s1 != '.' && *s2 == '.')
  171. return 1;
  172. if (*s1 == '.' && *s2 == '.')
  173. {
  174. s1++;
  175. s2++;
  176. }
  177. /* "cut" file suffixes */
  178. s1_pos = s1;
  179. s2_pos = s2;
  180. s1_suffix = match_suffix (&s1_pos);
  181. s2_suffix = match_suffix (&s2_pos);
  182. s1_len = (s1_suffix != NULL ? s1_suffix : s1_pos) - s1;
  183. s2_len = (s2_suffix != NULL ? s2_suffix : s2_pos) - s2;
  184. /* restore file suffixes if strings are identical after "cut" */
  185. if ((s1_suffix != NULL || s2_suffix != NULL) && (s1_len == s2_len)
  186. && strncmp (s1, s2, s1_len) == 0)
  187. {
  188. s1_len = s1_pos - s1;
  189. s2_len = s2_pos - s2;
  190. }
  191. result = verrevcmp (s1, s1_len, s2, s2_len);
  192. return result == 0 ? simple_cmp : result;
  193. }
  194. /* --------------------------------------------------------------------------------------------- */