filevercmp.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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-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 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 <http://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #include "filevercmp.h"
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <c-ctype.h>
  22. #include <limits.h>
  23. /* Match a file suffix defined by this regular expression:
  24. /(\.[A-Za-z~][A-Za-z0-9~]*)*$/
  25. Scan the string *STR and return a pointer to the matching suffix, or
  26. NULL if not found. Upon return, *STR points to terminating NUL. */
  27. static const char *
  28. match_suffix (const char **str)
  29. {
  30. const char *match = NULL;
  31. bool read_alpha = false;
  32. while (**str)
  33. {
  34. if (read_alpha)
  35. {
  36. read_alpha = false;
  37. if (!c_isalpha (**str) && '~' != **str)
  38. match = NULL;
  39. }
  40. else if ('.' == **str)
  41. {
  42. read_alpha = true;
  43. if (!match)
  44. match = *str;
  45. }
  46. else if (!c_isalnum (**str) && '~' != **str)
  47. match = NULL;
  48. (*str)++;
  49. }
  50. return match;
  51. }
  52. /* verrevcmp helper function */
  53. static int
  54. order (unsigned char c)
  55. {
  56. if (c_isdigit (c))
  57. return 0;
  58. else if (c_isalpha (c))
  59. return c;
  60. else if (c == '~')
  61. return -1;
  62. else
  63. return (int) c + UCHAR_MAX + 1;
  64. }
  65. /* slightly modified verrevcmp function from dpkg
  66. S1, S2 - compared string
  67. S1_LEN, S2_LEN - length of strings to be scanned
  68. This implements the algorithm for comparison of version strings
  69. specified by Debian and now widely adopted. The detailed
  70. specification can be found in the Debian Policy Manual in the
  71. section on the 'Version' control field. This version of the code
  72. implements that from s5.6.12 of Debian Policy v3.8.0.1
  73. http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version */
  74. static int _GL_ATTRIBUTE_PURE
  75. verrevcmp (const char *s1, size_t s1_len, const char *s2, size_t s2_len)
  76. {
  77. size_t s1_pos = 0;
  78. size_t s2_pos = 0;
  79. while (s1_pos < s1_len || s2_pos < s2_len)
  80. {
  81. int first_diff = 0;
  82. while ((s1_pos < s1_len && !c_isdigit (s1[s1_pos]))
  83. || (s2_pos < s2_len && !c_isdigit (s2[s2_pos])))
  84. {
  85. int s1_c = (s1_pos == s1_len) ? 0 : order (s1[s1_pos]);
  86. int s2_c = (s2_pos == s2_len) ? 0 : order (s2[s2_pos]);
  87. if (s1_c != s2_c)
  88. return s1_c - s2_c;
  89. s1_pos++;
  90. s2_pos++;
  91. }
  92. while (s1[s1_pos] == '0')
  93. s1_pos++;
  94. while (s2[s2_pos] == '0')
  95. s2_pos++;
  96. while (c_isdigit (s1[s1_pos]) && c_isdigit (s2[s2_pos]))
  97. {
  98. if (!first_diff)
  99. first_diff = s1[s1_pos] - s2[s2_pos];
  100. s1_pos++;
  101. s2_pos++;
  102. }
  103. if (c_isdigit (s1[s1_pos]))
  104. return 1;
  105. if (c_isdigit (s2[s2_pos]))
  106. return -1;
  107. if (first_diff)
  108. return first_diff;
  109. }
  110. return 0;
  111. }
  112. /* Compare version strings S1 and S2.
  113. See filevercmp.h for function description. */
  114. int
  115. filevercmp (const char *s1, const char *s2)
  116. {
  117. const char *s1_pos;
  118. const char *s2_pos;
  119. const char *s1_suffix, *s2_suffix;
  120. size_t s1_len, s2_len;
  121. int result;
  122. /* easy comparison to see if strings are identical */
  123. int simple_cmp = strcmp (s1, s2);
  124. if (simple_cmp == 0)
  125. return 0;
  126. /* special handle for "", "." and ".." */
  127. if (!*s1)
  128. return -1;
  129. if (!*s2)
  130. return 1;
  131. if (0 == strcmp (".", s1))
  132. return -1;
  133. if (0 == strcmp (".", s2))
  134. return 1;
  135. if (0 == strcmp ("..", s1))
  136. return -1;
  137. if (0 == strcmp ("..", s2))
  138. return 1;
  139. /* special handle for other hidden files */
  140. if (*s1 == '.' && *s2 != '.')
  141. return -1;
  142. if (*s1 != '.' && *s2 == '.')
  143. return 1;
  144. if (*s1 == '.' && *s2 == '.')
  145. {
  146. s1++;
  147. s2++;
  148. }
  149. /* "cut" file suffixes */
  150. s1_pos = s1;
  151. s2_pos = s2;
  152. s1_suffix = match_suffix (&s1_pos);
  153. s2_suffix = match_suffix (&s2_pos);
  154. s1_len = (s1_suffix ? s1_suffix : s1_pos) - s1;
  155. s2_len = (s2_suffix ? s2_suffix : s2_pos) - s2;
  156. /* restore file suffixes if strings are identical after "cut" */
  157. if ((s1_suffix || s2_suffix) && (s1_len == s2_len)
  158. && 0 == strncmp (s1, s2, s1_len))
  159. {
  160. s1_len = s1_pos - s1;
  161. s2_len = s2_pos - s2;
  162. }
  163. result = verrevcmp (s1, s1_len, s2, s2_len);
  164. return result == 0 ? simple_cmp : result;
  165. }