From 1942077cade6374a7c37fcdcf6cbf55dd52e13c4 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 1 Oct 2023 07:02:58 +0100 Subject: 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) --- coreutils/sort.c | 5 +++++ include/mingw.h | 1 + include/platform.h | 1 - win32/Kbuild | 1 + win32/strverscmp.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 win32/strverscmp.c 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 @@ //usage:#define sort_trivial_usage //usage: "[-nru" +//usage: IF_NOT_PLATFORM_MINGW32( //usage: IF_FEATURE_SORT_BIG("ghMcszbdfiokt] [-o FILE] [-k START[.OFS][OPTS][,END[.OFS][OPTS]] [-t CHAR") +//usage: ) +//usage: IF_PLATFORM_MINGW32( +//usage: IF_FEATURE_SORT_BIG("ghMVcszbdfiokt] [-o FILE] [-k START[.OFS][OPTS][,END[.OFS][OPTS]] [-t CHAR") +//usage: ) //usage: "] [FILE]..." //usage:#define sort_full_usage "\n\n" //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); char *strndup(char const *s, size_t n); char *mingw_strerror(int errnum); char *strsignal(int sig); +int strverscmp(const char *s1, const char *s2); #define strerror mingw_strerror 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; # undef HAVE_STRCASESTR # undef HAVE_STRCHRNUL # undef HAVE_STRSEP -# undef HAVE_STRVERSCMP #if !defined(__MINGW64_VERSION_MAJOR) # undef HAVE_VASPRINTF #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 lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o lib-$(CONFIG_PLATFORM_MINGW32) += strndup.o lib-$(CONFIG_PLATFORM_MINGW32) += strptime.o +lib-$(CONFIG_PLATFORM_MINGW32) += strverscmp.o lib-$(CONFIG_PLATFORM_MINGW32) += system.o lib-$(CONFIG_PLATFORM_MINGW32) += termios.o lib-$(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 @@ +/* + strverscmp from musl (https://www.musl-libc.org/). + + MIT licensed: + +---------------------------------------------------------------------- +Copyright © 2005-2020 Rich Felker, et al. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +---------------------------------------------------------------------- +*/ +#include "libbb.h" +#include +#include + +int strverscmp(const char *l0, const char *r0) +{ + const unsigned char *l = (const void *)l0; + const unsigned char *r = (const void *)r0; + size_t i, dp, j; + int z = 1; + + /* Find maximal matching prefix and track its maximal digit + * suffix and whether those digits are all zeros. */ + for (dp=i=0; l[i]==r[i]; i++) { + int c = l[i]; + if (!c) return 0; + if (!isdigit(c)) dp=i+1, z=1; + else if (c!='0') z=0; + } + + if (l[dp]-'1'<9U && r[dp]-'1'<9U) { + /* If we're looking at non-degenerate digit sequences starting + * with nonzero digits, longest digit string is greater. */ + for (j=i; isdigit(l[j]); j++) + if (!isdigit(r[j])) return 1; + if (isdigit(r[j])) return -1; + } else if (z && dp