aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-10-01 07:02:58 +0100
committerRon Yorston <rmy@pobox.com>2023-10-01 07:06:19 +0100
commit1942077cade6374a7c37fcdcf6cbf55dd52e13c4 (patch)
treeb3fd39398157f86af3cd22ebab562242c6ae48f3
parent1ff6eb52032bb52501ef3bd8b49010ac19efa268 (diff)
downloadbusybox-w32-1942077cade6374a7c37fcdcf6cbf55dd52e13c4.tar.gz
busybox-w32-1942077cade6374a7c37fcdcf6cbf55dd52e13c4.tar.bz2
busybox-w32-1942077cade6374a7c37fcdcf6cbf55dd52e13c4.zip
sort: add support for sorting version strings
Add an implementation of strverscmp from musl so that the 'sort -V' option works. Add '-V' to the trivial usage message. Costs 248-256 bytes. (GitHub issue #370)
-rw-r--r--coreutils/sort.c5
-rw-r--r--include/mingw.h1
-rw-r--r--include/platform.h1
-rw-r--r--win32/Kbuild1
-rw-r--r--win32/strverscmp.c62
5 files changed, 69 insertions, 1 deletions
diff --git a/coreutils/sort.c b/coreutils/sort.c
index 2e952f81c..949948203 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -43,7 +43,12 @@
43 43
44//usage:#define sort_trivial_usage 44//usage:#define sort_trivial_usage
45//usage: "[-nru" 45//usage: "[-nru"
46//usage: IF_NOT_PLATFORM_MINGW32(
46//usage: IF_FEATURE_SORT_BIG("ghMcszbdfiokt] [-o FILE] [-k START[.OFS][OPTS][,END[.OFS][OPTS]] [-t CHAR") 47//usage: IF_FEATURE_SORT_BIG("ghMcszbdfiokt] [-o FILE] [-k START[.OFS][OPTS][,END[.OFS][OPTS]] [-t CHAR")
48//usage: )
49//usage: IF_PLATFORM_MINGW32(
50//usage: IF_FEATURE_SORT_BIG("ghMVcszbdfiokt] [-o FILE] [-k START[.OFS][OPTS][,END[.OFS][OPTS]] [-t CHAR")
51//usage: )
47//usage: "] [FILE]..." 52//usage: "] [FILE]..."
48//usage:#define sort_full_usage "\n\n" 53//usage:#define sort_full_usage "\n\n"
49//usage: "Sort lines of text\n" 54//usage: "Sort lines of text\n"
diff --git a/include/mingw.h b/include/mingw.h
index 6ed9bed4a..f6f0bf262 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -228,6 +228,7 @@ int unsetenv(const char *env);
228char *strndup(char const *s, size_t n); 228char *strndup(char const *s, size_t n);
229char *mingw_strerror(int errnum); 229char *mingw_strerror(int errnum);
230char *strsignal(int sig); 230char *strsignal(int sig);
231int strverscmp(const char *s1, const char *s2);
231 232
232#define strerror mingw_strerror 233#define strerror mingw_strerror
233 234
diff --git a/include/platform.h b/include/platform.h
index 8ae5ed4bc..5795a0cf3 100644
--- a/include/platform.h
+++ b/include/platform.h
@@ -476,7 +476,6 @@ typedef unsigned smalluint;
476# undef HAVE_STRCASESTR 476# undef HAVE_STRCASESTR
477# undef HAVE_STRCHRNUL 477# undef HAVE_STRCHRNUL
478# undef HAVE_STRSEP 478# undef HAVE_STRSEP
479# undef HAVE_STRVERSCMP
480#if !defined(__MINGW64_VERSION_MAJOR) 479#if !defined(__MINGW64_VERSION_MAJOR)
481# undef HAVE_VASPRINTF 480# undef HAVE_VASPRINTF
482#endif 481#endif
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}