feat(nsh_cat): allow cat to read from stdin
Now, if we run cat without arguments, it will just read from stdin. It can be used with redirect like `cat < infile > outfile`.
This commit is contained in:
parent
4104019e1c
commit
8fba726a7d
@ -158,8 +158,8 @@ static const struct cmdmap_s g_cmdmap[] =
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_NSH_DISABLE_CAT
|
||||
CMD_MAP("cat", cmd_cat, 2, CONFIG_NSH_MAXARGUMENTS,
|
||||
"<path> [<path> [<path> ...]]"),
|
||||
CMD_MAP("cat", cmd_cat, 1, CONFIG_NSH_MAXARGUMENTS,
|
||||
"[<path> [<path> [<path> ...]]]"),
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_DISABLE_ENVIRON
|
||||
|
@ -791,6 +791,25 @@ int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
char *buf = malloc(BUFSIZ);
|
||||
|
||||
/* Dump from input */
|
||||
|
||||
while (true)
|
||||
{
|
||||
ssize_t n = nsh_read(vtbl, buf, BUFSIZ);
|
||||
|
||||
if (n == 0)
|
||||
break;
|
||||
|
||||
nsh_write(vtbl, buf, n);
|
||||
}
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user