diff options
author | Ron Yorston <rmy@pobox.com> | 2023-10-01 07:02:58 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2023-10-01 07:06:19 +0100 |
commit | 1942077cade6374a7c37fcdcf6cbf55dd52e13c4 (patch) | |
tree | b3fd39398157f86af3cd22ebab562242c6ae48f3 | |
parent | 1ff6eb52032bb52501ef3bd8b49010ac19efa268 (diff) | |
download | busybox-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.c | 5 | ||||
-rw-r--r-- | include/mingw.h | 1 | ||||
-rw-r--r-- | include/platform.h | 1 | ||||
-rw-r--r-- | win32/Kbuild | 1 | ||||
-rw-r--r-- | win32/strverscmp.c | 62 |
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); | |||
228 | char *strndup(char const *s, size_t n); | 228 | char *strndup(char const *s, size_t n); |
229 | char *mingw_strerror(int errnum); | 229 | char *mingw_strerror(int errnum); |
230 | char *strsignal(int sig); | 230 | char *strsignal(int sig); |
231 | int 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 | |||
25 | lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o | 25 | lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o |
26 | lib-$(CONFIG_PLATFORM_MINGW32) += strndup.o | 26 | lib-$(CONFIG_PLATFORM_MINGW32) += strndup.o |
27 | lib-$(CONFIG_PLATFORM_MINGW32) += strptime.o | 27 | lib-$(CONFIG_PLATFORM_MINGW32) += strptime.o |
28 | lib-$(CONFIG_PLATFORM_MINGW32) += strverscmp.o | ||
28 | lib-$(CONFIG_PLATFORM_MINGW32) += system.o | 29 | lib-$(CONFIG_PLATFORM_MINGW32) += system.o |
29 | lib-$(CONFIG_PLATFORM_MINGW32) += termios.o | 30 | lib-$(CONFIG_PLATFORM_MINGW32) += termios.o |
30 | lib-$(CONFIG_PLATFORM_MINGW32) += timegm.o | 31 | 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 @@ | |||
1 | /* | ||
2 | strverscmp from musl (https://www.musl-libc.org/). | ||
3 | |||
4 | MIT licensed: | ||
5 | |||
6 | ---------------------------------------------------------------------- | ||
7 | Copyright © 2005-2020 Rich Felker, et al. | ||
8 | |||
9 | Permission is hereby granted, free of charge, to any person obtaining | ||
10 | a copy of this software and associated documentation files (the | ||
11 | "Software"), to deal in the Software without restriction, including | ||
12 | without limitation the rights to use, copy, modify, merge, publish, | ||
13 | distribute, sublicense, and/or sell copies of the Software, and to | ||
14 | permit persons to whom the Software is furnished to do so, subject to | ||
15 | the following conditions: | ||
16 | |||
17 | The above copyright notice and this permission notice shall be | ||
18 | included in all copies or substantial portions of the Software. | ||
19 | |||
20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
21 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
23 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
24 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
25 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
26 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
27 | ---------------------------------------------------------------------- | ||
28 | */ | ||
29 | #include "libbb.h" | ||
30 | #include <ctype.h> | ||
31 | #include <string.h> | ||
32 | |||
33 | int 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 | } | ||