parent
a9416dd8a6
commit
007c37fb9a
|
@ -1,34 +1,52 @@
|
|||
#include <common.h>
|
||||
#include <command.h>
|
||||
#include <getopt.h>
|
||||
#include <fs.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
static int do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int i, optind = 1;
|
||||
int fd = stdout, opt, newline = 1;
|
||||
char *file = NULL;
|
||||
int oflags = O_WRONLY | O_CREAT;
|
||||
getopt_reset();
|
||||
|
||||
while((opt = getopt(argc, argv, "a:o:n")) > 0) {
|
||||
switch(opt) {
|
||||
/* We can't use getopt() here because we want to
|
||||
* echo all things we don't understand.
|
||||
*/
|
||||
while (optind < argc && *argv[optind] == '-') {
|
||||
if (!*(argv[optind] + 1) || *(argv[optind] + 2))
|
||||
break;
|
||||
|
||||
opt = *(argv[optind] + 1);
|
||||
switch (opt) {
|
||||
case 'n':
|
||||
newline = 0;
|
||||
break;
|
||||
case 'a':
|
||||
oflags |= O_APPEND;
|
||||
file = optarg;
|
||||
if (optind + 1 < argc)
|
||||
file = argv[optind + 1];
|
||||
else
|
||||
goto no_optarg_out;
|
||||
optind++;
|
||||
break;
|
||||
case 'o':
|
||||
oflags |= O_TRUNC;
|
||||
file = optarg;
|
||||
file = argv[optind + 1];
|
||||
if (optind + 1 < argc)
|
||||
file = argv[optind + 1];
|
||||
else
|
||||
goto no_optarg_out;
|
||||
optind++;
|
||||
break;
|
||||
default:
|
||||
goto exit_parse;
|
||||
}
|
||||
optind++;
|
||||
}
|
||||
|
||||
exit_parse:
|
||||
if (file) {
|
||||
fd = open(file, oflags);
|
||||
if (fd < 0) {
|
||||
|
@ -50,12 +68,15 @@ static int do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
|
|||
close(fd);
|
||||
|
||||
return 0;
|
||||
|
||||
no_optarg_out:
|
||||
printf("option requires an argument -- %c\n", opt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
U_BOOT_CMD(
|
||||
echo, CONFIG_MAXARGS, 0, do_echo,
|
||||
"echo - echo args to console\n",
|
||||
"[args..]\n"
|
||||
" - echo args to console; \\c suppresses newline\n"
|
||||
);
|
||||
U_BOOT_CMD_START(echo)
|
||||
.maxargs = CONFIG_MAXARGS,
|
||||
.cmd = do_echo,
|
||||
.usage = "echo - echo args to console\n",
|
||||
U_BOOT_CMD_END
|
||||
|
||||
|
|
Loading…
Reference in New Issue