9
0
Fork 0

svn_rev_418

do not use getopt
This commit is contained in:
Sascha Hauer 2007-07-05 18:01:52 +02:00 committed by Sascha Hauer
parent a9416dd8a6
commit 007c37fb9a
1 changed files with 34 additions and 13 deletions

View File

@ -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