summaryrefslogtreecommitdiffstats
path: root/common/cmd_exec.c
blob: f7ae918fc97229d607ab9029b9a484c0b114ce31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <common.h>
#include <command.h>
#include <fs.h>
#include <fcntl.h>
#include <linux/stat.h>
#include <errno.h>
#include <malloc.h>
#include <xfuncs.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 = xzalloc(s.st_size + 1);

	if (read(fd, buf, s.st_size) < s.st_size) {
		perror("read");
		goto out;
	}

	close(fd);
	return buf;

out:
	free(buf);
	close(fd);
	return NULL;
}

static 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;

		if (run_command (script, flag) == -1)
			goto out;
		free(script);
	}
	return 0;

out:
	free(script);
	return 1;
}

U_BOOT_CMD_START(exec)
	.maxargs	= CONFIG_MAXARGS,
	.cmd		= do_exec,
	.usage		= "execute a script",
U_BOOT_CMD_END