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-2011 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "filevercmp.h"
22 #include <sys/types.h>
29 /* The attribute __pure__ was added in gcc 2.96. */
30 #undef _GL_ATTRIBUTE_PURE
31 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
32 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
34 # define _GL_ATTRIBUTE_PURE /* empty */
37 /* Match a file suffix defined by this regular expression:
38 /(\.[A-Za-z~][A-Za-z0-9~]*)*$/
39 Scan the string *STR and return a pointer to the matching suffix, or
40 NULL if not found. Upon return, *STR points to terminating NUL. */
42 match_suffix (const char **str)
44 const char *match = NULL;
45 bool read_alpha = false;
51 if (!c_isalpha (**str) && '~' != **str)
54 else if ('.' == **str)
60 else if (!c_isalnum (**str) && '~' != **str)
67 /* verrevcmp helper function */
69 order (unsigned char c)
73 else if (c_isalpha (c))
78 return (int) c + UCHAR_MAX + 1;
81 /* slightly modified verrevcmp function from dpkg
82 S1, S2 - compared string
83 S1_LEN, S2_LEN - length of strings to be scanned
85 This implements the algorithm for comparison of version strings
86 specified by Debian and now widely adopted. The detailed
87 specification can be found in the Debian Policy Manual in the
88 section on the `Version' control field. This version of the code
89 implements that from s5.6.12 of Debian Policy v3.8.0.1
90 http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version */
91 static int _GL_ATTRIBUTE_PURE
92 verrevcmp (const char *s1, size_t s1_len, const char *s2, size_t s2_len)
96 while (s1_pos < s1_len || s2_pos < s2_len)
99 while ((s1_pos < s1_len && !c_isdigit (s1[s1_pos]))
100 || (s2_pos < s2_len && !c_isdigit (s2[s2_pos])))
102 int s1_c = (s1_pos == s1_len) ? 0 : order (s1[s1_pos]);
103 int s2_c = (s2_pos == s2_len) ? 0 : order (s2[s2_pos]);
109 while (s1[s1_pos] == '0')
111 while (s2[s2_pos] == '0')
113 while (c_isdigit (s1[s1_pos]) && c_isdigit (s2[s2_pos]))
116 first_diff = s1[s1_pos] - s2[s2_pos];
120 if (c_isdigit (s1[s1_pos]))
122 if (c_isdigit (s2[s2_pos]))
130 /* Compare version strings S1 and S2.
131 See filevercmp.h for function description. */
133 filevercmp (const char *s1, const char *s2)
137 const char *s1_suffix, *s2_suffix;
138 size_t s1_len, s2_len;
141 /* easy comparison to see if strings are identical */
142 int simple_cmp = strcmp (s1, s2);
146 /* special handle for "", "." and ".." */
151 if (0 == strcmp (".", s1))
153 if (0 == strcmp (".", s2))
155 if (0 == strcmp ("..", s1))
157 if (0 == strcmp ("..", s2))
160 /* special handle for other hidden files */
161 if (*s1 == '.' && *s2 != '.')
163 if (*s1 != '.' && *s2 == '.')
165 if (*s1 == '.' && *s2 == '.')
171 /* "cut" file suffixes */
174 s1_suffix = match_suffix (&s1_pos);
175 s2_suffix = match_suffix (&s2_pos);
176 s1_len = (s1_suffix ? s1_suffix : s1_pos) - s1;
177 s2_len = (s2_suffix ? s2_suffix : s2_pos) - s2;
179 /* restore file suffixes if strings are identical after "cut" */
180 if ((s1_suffix || s2_suffix) && (s1_len == s2_len)
181 && 0 == strncmp (s1, s2, s1_len))
183 s1_len = s1_pos - s1;
184 s2_len = s2_pos - s2;
187 result = verrevcmp (s1, s1_len, s2, s2_len);
188 return result == 0 ? simple_cmp : result;