add ls in mshell
This commit is contained in:
parent
a834155cce
commit
6a10fb1a0b
2 changed files with 30 additions and 2 deletions
|
@ -123,9 +123,10 @@ static ssize_t initrd_read(fildes_t* file, uint8_t* buffer, size_t size)
|
|||
if (count > index) {
|
||||
k=0;
|
||||
do {
|
||||
buffer[k] = dirent->name[0];
|
||||
buffer[k] = dirent->name[k];
|
||||
k++;
|
||||
} while(dirent->name[k] != '\0');
|
||||
file->offset++;
|
||||
return k;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -41,6 +42,7 @@ void help() {
|
|||
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("cat <path>:\t list directories\n");
|
||||
printf("help :\t this short help\n");
|
||||
printf("exit :\t exit the shell \n");
|
||||
}
|
||||
|
@ -129,7 +131,6 @@ void ms_setcwd(char* path) {
|
|||
setenv("PWD", newpath, 1);
|
||||
}
|
||||
|
||||
|
||||
int ms_cat(char* filename)
|
||||
{
|
||||
int fd, r = 0;
|
||||
|
@ -153,6 +154,30 @@ int ms_cat(char* filename)
|
|||
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)
|
||||
{
|
||||
char commandline[MAXCMDLINE];
|
||||
|
@ -185,6 +210,8 @@ int main(int argc, char** argv)
|
|||
ms_setcwd(newargv[1]);
|
||||
} else if (!strcmp(command, "cat")) {
|
||||
ms_cat(newargv[1]);
|
||||
} else if (!strcmp(command, "ls")) {
|
||||
ms_ls(newargv[1]);
|
||||
} else {
|
||||
char path[MAXPATH];
|
||||
sprintf(path, "/bin/%s", command);
|
||||
|
|
Loading…
Add table
Reference in a new issue