commit: 52d3e4497613357878150d9b4526ff5e59dd0dc4
parent f217c2fd65afc3dc6a3bd7cb58e5860026a306f7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 5 May 2025 13:01:08 +0200
cmd/test: Use strcoll(3) instead of strcmp(3) for '>' and '<' operators
As specified in POSIX.1-2024.
Diffstat:
3 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/cmd/test.1 b/cmd/test.1
@@ -209,15 +209,15 @@ are not identical.
.It Ar s1 Cm < Ar s2
True if string
.Ar s1
-comes before
+collates before
.Ar s2
-based on the binary value of their characters.
+based on the current locale.
.It Ar s1 Cm > Ar s2
True if string
.Ar s1
-comes after
+colllates after
.Ar s2
-based on the binary value of their characters.
+based on the current locale.
.It Ar n1 Fl eq Ar n2
True if the integers
.Ar n1
diff --git a/cmd/test.c b/cmd/test.c
@@ -211,11 +211,11 @@ main(int argc, char **argv)
/* no expression => false */
if(--argc <= 0) return 1;
- char *lc_ctype = setlocale(LC_CTYPE, "");
- if(lc_ctype == NULL)
+ errno = 0;
+ if(setlocale(LC_ALL, "") == NULL)
{
fprintf(stderr,
- "%s: warning: Failed loading locales. setlocale(LC_CTYPE, \"\"): %s\n",
+ "%s: warning: Failed loading locales. setlocale(LC_ALL, \"\"): %s\n",
argv0,
strerror(errno));
}
@@ -346,9 +346,9 @@ binop(enum token n)
case STRNE:
return strcmp(opnd1, opnd2) != 0;
case STRLT:
- return strcmp(opnd1, opnd2) < 0;
+ return strcoll(opnd1, opnd2) < 0;
case STRGT:
- return strcmp(opnd1, opnd2) > 0;
+ return strcoll(opnd1, opnd2) > 0;
case INTEQ:
return intcmp(opnd1, opnd2) == 0;
case INTNE:
diff --git a/test-cmd/test.sh b/test-cmd/test.sh
@@ -49,7 +49,8 @@ t ()
}
count=0
-echo "1..130"
+plan=132
+printf '1..%d\n' $plan
t 0 'b = b'
t 0 'b == b'
@@ -191,3 +192,11 @@ t 1 '\( ! -a \)'
t 0 '\( -n -o \)'
t 1 '\( -z -o \)'
t 1 '\( ! -o \)'
+
+t 1 'aaron \> baron'
+t 0 'aaron \< baron'
+
+if [ $plan -ne $count ]; then
+ printf '# error: Ran %d tests instead of the %d planned tests\n' "$count" "$plan"
+ exit 1
+fi