Skip to content

Commit

Permalink
feat(nsh_cat): allow cat to read from stdin
Browse files Browse the repository at this point in the history
Now, if we run cat without arguments, it will just read from stdin.

It can be used with redirect like `cat < infile > outfile`.
  • Loading branch information
casaroli authored and acassis committed Aug 8, 2024
1 parent 4104019 commit 8fba726
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions nshlib/nsh_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions nshlib/nsh_fscmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8fba726

Please sign in to comment.