summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2020-02-18 08:36:44 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2020-02-18 08:36:44 +0100
commitd6787a36a64aeaff019bfb61b40f247f04c5ade8 (patch)
tree8f4fd0981b811d925420b5ffb7c8bf12fd730939 /commands
parent0a54a21f182751a0f256119d8b048d8d169ed395 (diff)
parentf246b4079c746f80e29b07252c546d37a359105f (diff)
downloadbarebox-d6787a36a64aeaff019bfb61b40f247f04c5ade8.tar.gz
Merge branch 'for-next/misc'
Diffstat (limited to 'commands')
-rw-r--r--commands/boot.c60
-rw-r--r--commands/i2c.c30
-rw-r--r--commands/test.c32
3 files changed, 91 insertions, 31 deletions
diff --git a/commands/boot.c b/commands/boot.c
index 0257b3d..aeaba39 100644
--- a/commands/boot.c
+++ b/commands/boot.c
@@ -21,13 +21,29 @@
#include <linux/stat.h>
+static char *next_argv(void *context)
+{
+ char ***argv = context;
+ char *next = **argv;
+ (*argv)++;
+ return next;
+}
+
+static char *next_word(void *context)
+{
+ return strsep(context, " ");
+}
+
static int do_boot(int argc, char *argv[])
{
char *freep = NULL;
int opt, ret = 0, do_list = 0, do_menu = 0;
- int i, dryrun = 0, verbose = 0, timeout = -1;
+ int dryrun = 0, verbose = 0, timeout = -1;
struct bootentries *entries;
struct bootentry *entry;
+ void *handle;
+ const char *name;
+ char *(*next)(void *);
while ((opt = getopt(argc, argv, "vldmt:w:")) > 0) {
switch (opt) {
@@ -54,31 +70,38 @@ static int do_boot(int argc, char *argv[])
}
}
- entries = bootentries_alloc();
-
if (optind < argc) {
- for (i = optind; i < argc; i++) {
- ret = bootentry_create_from_name(entries, argv[i]);
- if (ret <= 0)
- printf("Nothing bootable found on '%s'\n", argv[i]);
- }
+ handle = &argv[optind];
+ next = next_argv;
} else {
const char *def;
- char *sep, *name;
def = getenv("global.boot.default");
if (!def)
return 0;
- sep = freep = xstrdup(def);
+ handle = freep = xstrdup(def);
+ next = next_word;
+ }
+
+ entries = bootentries_alloc();
+
+ while ((name = next(&handle)) != NULL) {
+ ret = bootentry_create_from_name(entries, name);
+ if (ret <= 0)
+ printf("Nothing bootable found on '%s'\n", name);
- while ((name = strsep(&sep, " ")) != NULL) {
- ret = bootentry_create_from_name(entries, name);
- if (ret <= 0)
- printf("Nothing bootable found on '%s'\n", name);
+ if (do_list || do_menu)
+ continue;
+
+ bootentries_for_each_entry(entries, entry) {
+ ret = boot_entry(entry, verbose, dryrun);
+ if (!ret)
+ break;
}
- free(freep);
+ bootentries_free(entries);
+ entries = bootentries_alloc();
}
if (list_empty(&entries->entries)) {
@@ -96,14 +119,9 @@ static int do_boot(int argc, char *argv[])
goto out;
}
- bootentries_for_each_entry(entries, entry) {
- ret = boot_entry(entry, verbose, dryrun);
- if (!ret)
- break;
- }
-
out:
bootentries_free(entries);
+ free(freep);
return ret;
}
diff --git a/commands/i2c.c b/commands/i2c.c
index 77d65e3..77a8f7f 100644
--- a/commands/i2c.c
+++ b/commands/i2c.c
@@ -45,17 +45,37 @@ static int do_i2c_probe(int argc, char *argv[])
{
struct i2c_adapter *adapter = NULL;
int startaddr = 4, stopaddr = 0x77;
+ int ret;
if (argc > 1) {
- adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
+ int busnum;
+
+ ret = kstrtoint(argv[1], 0, &busnum);
+ if (ret) {
+ printf("Cannot parse \"%s\" as a number\n", argv[1]);
+ return ret;
+ }
+
+ adapter = i2c_get_adapter(busnum);
if (!adapter)
return -ENODEV;
}
- if (argc > 2)
- startaddr = simple_strtol(argv[2], NULL, 0);
- if (argc > 3)
- stopaddr = simple_strtol(argv[3], NULL, 0);
+ if (argc > 2) {
+ ret = kstrtoint(argv[2], 0, &startaddr);
+ if (ret) {
+ printf("Cannot parse \"%s\" as a number\n", argv[1]);
+ return ret;
+ }
+ }
+
+ if (argc > 3) {
+ ret = kstrtoint(argv[3], 0, &stopaddr);
+ if (ret) {
+ printf("Cannot parse \"%s\" as a number\n", argv[1]);
+ return ret;
+ }
+ }
if (stopaddr > 0x7f)
stopaddr = 0x7f;
diff --git a/commands/test.c b/commands/test.c
index c4f4938..86636de 100644
--- a/commands/test.c
+++ b/commands/test.c
@@ -40,6 +40,8 @@ typedef enum {
OPT_DIRECTORY,
OPT_FILE,
OPT_EXISTS,
+ OPT_BLOCK,
+ OPT_CHAR,
OPT_SYMBOLIC_LINK,
OPT_MAX,
} test_opts;
@@ -60,6 +62,8 @@ static char *test_options[] = {
[OPT_FILE] = "-f",
[OPT_DIRECTORY] = "-d",
[OPT_EXISTS] = "-e",
+ [OPT_BLOCK] = "-b",
+ [OPT_CHAR] = "-c",
[OPT_SYMBOLIC_LINK] = "-L",
};
@@ -129,8 +133,11 @@ static int do_test(int argc, char *argv[])
case OPT_ZERO:
case OPT_NONZERO:
adv = 2;
+ if (left < 2)
+ break;
zero = 1;
- if (ap[1] && *ap[1] != ']' && strlen(ap[1]))
+
+ if (strlen(ap[1]))
zero = 0;
expr = (opt == OPT_ZERO) ? zero : !zero;
@@ -139,9 +146,13 @@ static int do_test(int argc, char *argv[])
case OPT_FILE:
case OPT_DIRECTORY:
case OPT_EXISTS:
+ case OPT_BLOCK:
+ case OPT_CHAR:
case OPT_SYMBOLIC_LINK:
adv = 2;
- if (ap[1] && *ap[1] != ']' && strlen(ap[1])) {
+ if (left < 2)
+ break;
+ if (strlen(ap[1])) {
expr = (opt == OPT_SYMBOLIC_LINK ? lstat : stat)(ap[1], &statbuf);
if (expr < 0) {
expr = 0;
@@ -164,16 +175,22 @@ static int do_test(int argc, char *argv[])
expr = 1;
break;
}
+ if (opt == OPT_BLOCK && S_ISBLK(statbuf.st_mode)) {
+ expr = 1;
+ break;
+ }
+ if (opt == OPT_CHAR && S_ISCHR(statbuf.st_mode)) {
+ expr = 1;
+ break;
+ }
}
break;
/* three argument options */
default:
adv = 3;
- if (left < 3) {
- expr = 1;
+ if (left < 3)
break;
- }
a = simple_strtol(ap[0], NULL, 0);
b = simple_strtol(ap[2], NULL, 0);
@@ -208,6 +225,11 @@ static int do_test(int argc, char *argv[])
}
}
+ if (left < adv) {
+ printf("test: failed to parse arguments\n");
+ return 1;
+ }
+
if (last_cmp == 0)
expr = last_expr || expr;
else if (last_cmp == 1)