add ls in mshell

This commit is contained in:
Marian Ohligs 2011-11-09 12:14:31 +01:00
parent a834155cce
commit 6a10fb1a0b
2 changed files with 30 additions and 2 deletions

View file

@ -123,9 +123,10 @@ static ssize_t initrd_read(fildes_t* file, uint8_t* buffer, size_t size)
if (count > index) { if (count > index) {
k=0; k=0;
do { do {
buffer[k] = dirent->name[0]; buffer[k] = dirent->name[k];
k++; k++;
} while(dirent->name[k] != '\0'); } while(dirent->name[k] != '\0');
file->offset++;
return k; return k;
} }
} }

View file

@ -22,6 +22,7 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <dirent.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -41,6 +42,7 @@ void help() {
printf("cd <path> :\t change current working directory to path\n"); printf("cd <path> :\t change current working directory to path\n");
printf("pwd :\t show current working directory\n"); printf("pwd :\t show current working directory\n");
printf("cat <path>:\t print files\n"); printf("cat <path>:\t print files\n");
printf("cat <path>:\t list directories\n");
printf("help :\t this short help\n"); printf("help :\t this short help\n");
printf("exit :\t exit the shell \n"); printf("exit :\t exit the shell \n");
} }
@ -129,7 +131,6 @@ void ms_setcwd(char* path) {
setenv("PWD", newpath, 1); setenv("PWD", newpath, 1);
} }
int ms_cat(char* filename) int ms_cat(char* filename)
{ {
int fd, r = 0; int fd, r = 0;
@ -153,6 +154,30 @@ int ms_cat(char* filename)
return 0; return 0;
} }
int ms_ls(char* dirname)
{
DIR* dir;
dirent_t* dirent;
dir = opendir(dirname);
if (dir < 1) {
printf("ls: No such file or directory");
printf("\n");
return -1;
}
do {
dirent = readdir(dir);
if (dirent == NULL)
break;
printf("%s", dirent->d_name);
printf("\n");
} while (dirent != NULL);
closedir(dir);
return 0;
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
char commandline[MAXCMDLINE]; char commandline[MAXCMDLINE];
@ -185,6 +210,8 @@ int main(int argc, char** argv)
ms_setcwd(newargv[1]); ms_setcwd(newargv[1]);
} else if (!strcmp(command, "cat")) { } else if (!strcmp(command, "cat")) {
ms_cat(newargv[1]); ms_cat(newargv[1]);
} else if (!strcmp(command, "ls")) {
ms_ls(newargv[1]);
} else { } else {
char path[MAXPATH]; char path[MAXPATH];
sprintf(path, "/bin/%s", command); sprintf(path, "/bin/%s", command);