summaryrefslogtreecommitdiffstats
path: root/common/cmd_exec.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2007-07-05 18:01:41 +0200
committerSascha Hauer <sha@octopus.labnet.pengutronix.de>2007-07-05 18:01:41 +0200
commit479d8ffb5efc63bb11388d5d6d3f59ffa198bd2e (patch)
tree601168a948378ae964254fa7e0e87ca75841a338 /common/cmd_exec.c
parent67422a4a69c3489151f8cf044ecee68def1caf81 (diff)
downloadbarebox-479d8ffb5efc63bb11388d5d6d3f59ffa198bd2e.tar.gz
barebox-479d8ffb5efc63bb11388d5d6d3f59ffa198bd2e.tar.xz
svn_rev_300
add exec command
Diffstat (limited to 'common/cmd_exec.c')
-rw-r--r--common/cmd_exec.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/common/cmd_exec.c b/common/cmd_exec.c
new file mode 100644
index 0000000000..89b953e529
--- /dev/null
+++ b/common/cmd_exec.c
@@ -0,0 +1,85 @@
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <linux/stat.h>
+#include <errno.h>
+#include <malloc.h>
+
+#ifdef CONFIG_HUSH_PARSER
+#include <hush.h>
+#endif
+
+static void *read_file(const char *file)
+{
+ struct stat s;
+ void *buf = NULL;
+ int fd = 0;
+
+ if (stat(file, &s)) {
+ perror("stat");
+ return NULL;
+ }
+
+ fd = open(file, O_RDONLY);
+ if (fd < 0) {
+ perror("open");
+ return NULL;
+ }
+
+ buf = malloc(s.st_size + 1);
+
+ if (read(fd, buf, s.st_size) < s.st_size) {
+ perror("read");
+ goto out;
+ }
+
+ *(char *)(buf + s.st_size) = 0;
+ close(fd);
+ return buf;
+
+out:
+ free(buf);
+ close(fd);
+ return NULL;
+}
+
+int do_exec(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
+{
+ int i;
+ char *script;
+
+ if (argc < 2) {
+ printf ("Usage:\n%s\n", cmdtp->usage);
+ return 1;
+ }
+
+ for (i=1; i<argc; ++i) {
+ script = read_file(argv[i]);
+ if (!script)
+ return 1;
+
+#ifndef CONFIG_HUSH_PARSER
+ if (run_command (script, flag) == -1)
+ goto out;
+#else
+ if (parse_string_outer(script,
+ FLAG_PARSE_SEMICOLON) != 0)
+ goto out;
+#endif
+ free(script);
+ }
+ return 0;
+
+out:
+ free(script);
+ return 1;
+}
+
+int do_exec (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
+U_BOOT_CMD(
+ exec, CONFIG_MAXARGS, 1, do_exec,
+ "exec - execute a script\n",
+ "<filename> - execute <filename>\n"
+);
+