From df4eaf51631af9fce523d6b5ae31a32de96eb1e4 Mon Sep 17 00:00:00 2001 From: Marian Ohligs Date: Sat, 24 Sep 2011 20:13:16 +0200 Subject: [PATCH] - added cat command --- newlib/examples/mshell.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/newlib/examples/mshell.c b/newlib/examples/mshell.c index 1f43a821..2f03fe4a 100644 --- a/newlib/examples/mshell.c +++ b/newlib/examples/mshell.c @@ -38,10 +38,11 @@ void showlogo() { void help() { printf("shell builtin commands: \n"); - printf("cd :\t change current working directory to path\n"); - printf("pwd :\t show current working directory\n"); - printf("help :\t this short help\n"); - printf("exit :\t exit the shell \n"); + printf("cd :\t change current working directory to path\n"); + printf("pwd :\t show current working directory\n"); + printf("cat :\t print files\n"); + printf("help :\t this short help\n"); + printf("exit :\t exit the shell \n"); } #define MAXARGS 16 @@ -128,6 +129,27 @@ void ms_setcwd(char* path) { setenv("PWD", newpath, 1); } + +int ms_cat(char* filename) +{ + int fd, r = 0; + char* buffer = malloc(1024*sizeof(char)); + + fd = open(filename, 0, "wr"); + + if ((!filename) || (fd < 0)) + printf("cat: No such file or directory"); + + do { + r = read(fd, buffer, 1024); + write(1, buffer, r); + } while (r > 0); + + printf("\n"); + close(fd); + return 0; +} + int main(int argc, char** argv) { char commandline[MAXCMDLINE]; @@ -158,6 +180,8 @@ int main(int argc, char** argv) printf("%s\n", ms_getcwd()); } else if (!strcmp(command, "cd")) { ms_setcwd(newargv[1]); + } else if (!strcmp(command, "cat")) { + ms_cat(newargv[1]); } else { char path[MAXPATH]; sprintf(path, "/bin/%s", command);