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.
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 <https://www.gnu.org/licenses/>. */
20 #include "filevercmp.h"
22 #include <sys/types.h>
29 /* Match a file suffix defined by this regular expression:
30 /(\.[A-Za-z~][A-Za-z0-9~]*)*$/
31 Scan the string *STR and return a pointer to the matching suffix, or
32 NULL if not found. Upon return, *STR points to terminating NUL. */
34 match_suffix (const char **str
)
36 const char *match
= NULL
;
37 bool read_alpha
= false;
43 if (!c_isalpha (**str
) && '~' != **str
)
46 else if ('.' == **str
)
52 else if (!c_isalnum (**str
) && '~' != **str
)
59 /* verrevcmp helper function */
61 order (unsigned char c
)
65 else if (c_isalpha (c
))
70 return (int) c
+ UCHAR_MAX
+ 1;
73 /* slightly modified verrevcmp function from dpkg
74 S1, S2 - compared string
75 S1_LEN, S2_LEN - length of strings to be scanned
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 static int _GL_ATTRIBUTE_PURE
84 verrevcmp (const char *s1
, size_t s1_len
, const char *s2
, size_t s2_len
)
88 while (s1_pos
< s1_len
|| s2_pos
< s2_len
)
91 while ((s1_pos
< s1_len
&& !c_isdigit (s1
[s1_pos
]))
92 || (s2_pos
< s2_len
&& !c_isdigit (s2
[s2_pos
])))
94 int s1_c
= (s1_pos
== s1_len
) ? 0 : order (s1
[s1_pos
]);
95 int s2_c
= (s2_pos
== s2_len
) ? 0 : order (s2
[s2_pos
]);
101 while (s1
[s1_pos
] == '0')
103 while (s2
[s2_pos
] == '0')
105 while (c_isdigit (s1
[s1_pos
]) && c_isdigit (s2
[s2_pos
]))
108 first_diff
= s1
[s1_pos
] - s2
[s2_pos
];
112 if (c_isdigit (s1
[s1_pos
]))
114 if (c_isdigit (s2
[s2_pos
]))
122 /* Compare version strings S1 and S2.
123 See filevercmp.h for function description. */
125 filevercmp (const char *s1
, const char *s2
)
129 const char *s1_suffix
, *s2_suffix
;
130 size_t s1_len
, s2_len
;
133 /* easy comparison to see if strings are identical */
134 int simple_cmp
= strcmp (s1
, s2
);
138 /* special handle for "", "." and ".." */
143 if (0 == strcmp (".", s1
))
145 if (0 == strcmp (".", s2
))
147 if (0 == strcmp ("..", s1
))
149 if (0 == strcmp ("..", s2
))
152 /* special handle for other hidden files */
153 if (*s1
== '.' && *s2
!= '.')
155 if (*s1
!= '.' && *s2
== '.')
157 if (*s1
== '.' && *s2
== '.')
163 /* "cut" file suffixes */
166 s1_suffix
= match_suffix (&s1_pos
);
167 s2_suffix
= match_suffix (&s2_pos
);
168 s1_len
= (s1_suffix
? s1_suffix
: s1_pos
) - s1
;
169 s2_len
= (s2_suffix
? s2_suffix
: s2_pos
) - s2
;
171 /* restore file suffixes if strings are identical after "cut" */
172 if ((s1_suffix
|| s2_suffix
) && (s1_len
== s2_len
)
173 && 0 == strncmp (s1
, s2
, s1_len
))
175 s1_len
= s1_pos
- s1
;
176 s2_len
= s2_pos
- s2
;
179 result
= verrevcmp (s1
, s1_len
, s2
, s2_len
);
180 return result
== 0 ? simple_cmp
: result
;