- added cat command

This commit is contained in:
Marian Ohligs 2011-09-24 20:13:16 +02:00
parent 13aaaa8bdc
commit df4eaf5163

View file

@ -38,10 +38,11 @@ void showlogo() {
void help() {
printf("shell builtin commands: \n");
printf("cd <path>:\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 <path> :\t change current working directory to path\n");
printf("pwd :\t show current working directory\n");
printf("cat <path>:\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);