aboutsummaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
Diffstat (limited to 'win32')
-rw-r--r--win32/Kbuild1
-rw-r--r--win32/strverscmp.c62
2 files changed, 63 insertions, 0 deletions
diff --git a/win32/Kbuild b/win32/Kbuild
index 3e1f845df..e705dae66 100644
--- a/win32/Kbuild
+++ b/win32/Kbuild
@@ -25,6 +25,7 @@ lib-$(CONFIG_FEATURE_PRNG_SHELL) += sh_random.o
25lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o 25lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o
26lib-$(CONFIG_PLATFORM_MINGW32) += strndup.o 26lib-$(CONFIG_PLATFORM_MINGW32) += strndup.o
27lib-$(CONFIG_PLATFORM_MINGW32) += strptime.o 27lib-$(CONFIG_PLATFORM_MINGW32) += strptime.o
28lib-$(CONFIG_PLATFORM_MINGW32) += strverscmp.o
28lib-$(CONFIG_PLATFORM_MINGW32) += system.o 29lib-$(CONFIG_PLATFORM_MINGW32) += system.o
29lib-$(CONFIG_PLATFORM_MINGW32) += termios.o 30lib-$(CONFIG_PLATFORM_MINGW32) += termios.o
30lib-$(CONFIG_PLATFORM_MINGW32) += timegm.o 31lib-$(CONFIG_PLATFORM_MINGW32) += timegm.o
diff --git a/win32/strverscmp.c b/win32/strverscmp.c
new file mode 100644
index 000000000..05dc60c39
--- /dev/null
+++ b/win32/strverscmp.c
@@ -0,0 +1,62 @@
1/*
2 strverscmp from musl (https://www.musl-libc.org/).
3
4 MIT licensed:
5
6----------------------------------------------------------------------
7Copyright © 2005-2020 Rich Felker, et al.
8
9Permission is hereby granted, free of charge, to any person obtaining
10a copy of this software and associated documentation files (the
11"Software"), to deal in the Software without restriction, including
12without limitation the rights to use, copy, modify, merge, publish,
13distribute, sublicense, and/or sell copies of the Software, and to
14permit persons to whom the Software is furnished to do so, subject to
15the following conditions:
16
17The above copyright notice and this permission notice shall be
18included in all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27----------------------------------------------------------------------
28*/
29#include "libbb.h"
30#include <ctype.h>
31#include <string.h>
32
33int strverscmp(const char *l0, const char *r0)
34{
35 const unsigned char *l = (const void *)l0;
36 const unsigned char *r = (const void *)r0;
37 size_t i, dp, j;
38 int z = 1;
39
40 /* Find maximal matching prefix and track its maximal digit
41 * suffix and whether those digits are all zeros. */
42 for (dp=i=0; l[i]==r[i]; i++) {
43 int c = l[i];
44 if (!c) return 0;
45 if (!isdigit(c)) dp=i+1, z=1;
46 else if (c!='0') z=0;
47 }
48
49 if (l[dp]-'1'<9U && r[dp]-'1'<9U) {
50 /* If we're looking at non-degenerate digit sequences starting
51 * with nonzero digits, longest digit string is greater. */
52 for (j=i; isdigit(l[j]); j++)
53 if (!isdigit(r[j])) return 1;
54 if (isdigit(r[j])) return -1;
55 } else if (z && dp<i && (isdigit(l[i]) || isdigit(r[i]))) {
56 /* Otherwise, if common prefix of digit sequence is
57 * all zeros, digits order less than non-digits. */
58 return (unsigned char)(l[i]-'0') - (unsigned char)(r[i]-'0');
59 }
60
61 return l[i] - r[i];
62}