summaryrefslogtreecommitdiffstats
path: root/commands/test.c
diff options
context:
space:
mode:
authorSascha Hauer <sha@octopus.labnet.pengutronix.de>2007-09-21 15:16:24 +0200
committerSascha Hauer <sha@octopus.labnet.pengutronix.de>2007-09-21 15:16:24 +0200
commite3f34e28da77623e39ed16ff62f7268f33f00dce (patch)
tree22c9cb6e12b52e37e699649d0caa448cd1a1375d /commands/test.c
parent8b999f4a5352aef3363e521c29aa2d74050221e1 (diff)
downloadbarebox-e3f34e28da77623e39ed16ff62f7268f33f00dce.tar.gz
barebox-e3f34e28da77623e39ed16ff62f7268f33f00dce.tar.xz
implement test options -f, -e and -d
Diffstat (limited to 'commands/test.c')
-rw-r--r--commands/test.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/commands/test.c b/commands/test.c
index c918c43ac0..0153a21f7c 100644
--- a/commands/test.c
+++ b/commands/test.c
@@ -24,6 +24,8 @@
*/
#include <common.h>
#include <command.h>
+#include <fs.h>
+#include <linux/stat.h>
typedef enum {
OPT_EQUAL,
@@ -38,6 +40,9 @@ typedef enum {
OPT_AND,
OPT_ZERO,
OPT_NONZERO,
+ OPT_DIRECTORY,
+ OPT_FILE,
+ OPT_EXISTS,
OPT_MAX,
} test_opts;
@@ -54,6 +59,9 @@ static char *test_options[] = {
[OPT_AND] = "-a",
[OPT_ZERO] = "-z",
[OPT_NONZERO] = "-n",
+ [OPT_FILE] = "-f",
+ [OPT_DIRECTORY] = "-d",
+ [OPT_EXISTS] = "-e",
};
static int parse_opt(const char *opt)
@@ -74,6 +82,7 @@ do_test (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
char **ap;
int left, adv, expr, last_expr, neg, last_cmp, opt, zero;
ulong a, b;
+ struct stat statbuf;
if (*argv[0] == '[') {
if (*argv[argc - 1] != ']') {
@@ -129,6 +138,36 @@ do_test (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
expr = (opt == OPT_ZERO) ? zero : !zero;
break;
+ case OPT_FILE:
+ case OPT_DIRECTORY:
+ case OPT_EXISTS:
+ adv = 2;
+ if (ap[1] && *ap[1] != ']' && strlen(ap[1])) {
+ expr = stat(ap[1], &statbuf);
+ printf("expr: %d\n", expr);
+ if (expr < 0) {
+ expr = 0;
+ break;
+ }
+ expr = 0;
+ if (opt == OPT_EXISTS) {
+ printf("exists\n");
+ expr = 1;
+ break;
+ }
+ if (opt == OPT_FILE && S_ISREG(statbuf.st_mode)) {
+ printf("reg\n");
+ expr = 1;
+ break;
+ }
+ if (opt == OPT_DIRECTORY && S_ISDIR(statbuf.st_mode)) {
+ printf("dir\n");
+ expr = 1;
+ break;
+ }
+ }
+ break;
+
/* three argument options */
default:
adv = 3;
@@ -191,7 +230,7 @@ char *test_aliases[] = { "[", NULL};
static __maybe_unused char cmd_test_help[] =
"Usage: test [OPTIONS]\n"
-"options: !, =, !=, -eq, -ne, -ge, -gt, -le, -lt, -o, -a, -z, -n\n"
+"options: !, =, !=, -eq, -ne, -ge, -gt, -le, -lt, -o, -a, -z, -n, -d, -e, -f\n"
"see 'man test' on your PC for more information.\n";
U_BOOT_CMD_START(test)