summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorTom Rini <[email protected]>2026-03-25 14:38:02 -0600
committerTom Rini <[email protected]>2026-03-25 14:38:02 -0600
commit6701997e9ca2cab0c0e1cde96437ac1dabefdaa1 (patch)
tree66be5429de16b766e7eef0132b498e71bbfcfd27 /cmd
parent1e2052f76e98ae70ab29113d271bb376ed23e8bb (diff)
parent8b0619579b2282050e7fb0d92fbc645b79d18bae (diff)
Merge patch series "add [ as alias for test, fix 0/1 argument handling"
Rasmus Villemoes <[email protected]> says: Make 'test' behave a little more like its cousins in other shells, by allowing the [ ... ] spelling, and while here, fix up the handling of a single, non-empty argument to comply with POSIX. Link: https://lore.kernel.org/r/[email protected]
Diffstat (limited to 'cmd')
-rw-r--r--cmd/test.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/cmd/test.c b/cmd/test.c
index a9ac07e6143..0d0f090386c 100644
--- a/cmd/test.c
+++ b/cmd/test.c
@@ -63,10 +63,25 @@ static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
char * const *ap;
int i, op, left, adv, expr, last_expr, last_unop, last_binop;
- /* args? */
- if (argc < 3)
+ if (!strcmp(argv[0], "[")) {
+ if (strcmp(argv[argc - 1], "]")) {
+ printf("[: missing terminating ]\n");
+ return 1;
+ }
+ argc--;
+ }
+
+ /*
+ * Per POSIX, 'test' with 0 arguments should return 1, while
+ * 'test <arg>' should be equivalent to 'test -n <arg>',
+ * i.e. true if and only if <arg> is not empty.
+ */
+ if (argc < 2)
return 1;
+ if (argc == 2)
+ return !strcmp(argv[1], "");
+
#ifdef DEBUG
{
debug("test(%d):", argc);
@@ -212,6 +227,17 @@ U_BOOT_CMD(
"[args..]"
);
+/*
+ * This does not use the U_BOOT_CMD macro as [ can't be used in symbol names
+ */
+ll_entry_declare(struct cmd_tbl, lbracket, cmd) = {
+ "[", CONFIG_SYS_MAXARGS, cmd_always_repeatable, do_test,
+ "alias for 'test'",
+#ifdef CONFIG_SYS_LONGHELP
+ " <test expression> ]"
+#endif /* CONFIG_SYS_LONGHELP */
+};
+
static int do_false(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{