mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-20 05:56:32 +00:00
🆕 完成了ls的功能
This commit is contained in:
@ -8,6 +8,8 @@
|
||||
#include <libc/unistd.h>
|
||||
#include <libc/stdlib.h>
|
||||
|
||||
#include <libc/dirent.h>
|
||||
|
||||
#include "cmd_help.h"
|
||||
|
||||
// 当前工作目录(在main_loop中初始化)
|
||||
@ -152,7 +154,7 @@ int shell_cmd_cd(int argc, char **argv)
|
||||
if (argv[1][0] == '.' && argv[1][1] == '/') // 相对路径
|
||||
dest_offset = 2;
|
||||
}
|
||||
|
||||
|
||||
int new_len = current_dir_len + dest_len - dest_offset;
|
||||
// ======进入相对路径=====
|
||||
if (new_len >= SHELL_CWD_MAX_SIZE - 1)
|
||||
@ -201,6 +203,34 @@ done:;
|
||||
// todo:
|
||||
int shell_cmd_ls(int argc, char **argv)
|
||||
{
|
||||
struct DIR *dir = opendir(shell_current_path);
|
||||
|
||||
if (dir == NULL)
|
||||
return -1;
|
||||
|
||||
struct dirent *buf = NULL;
|
||||
// printf("dir=%#018lx\n", dir);
|
||||
|
||||
while (1)
|
||||
{
|
||||
buf = readdir(dir);
|
||||
if(buf == NULL)
|
||||
break;
|
||||
|
||||
int color = COLOR_WHITE;
|
||||
if(buf->d_type & VFS_ATTR_DIR)
|
||||
color = COLOR_YELLOW;
|
||||
else if(buf->d_type & VFS_ATTR_FILE)
|
||||
color = COLOR_INDIGO;
|
||||
|
||||
char output_buf[256] = {0};
|
||||
|
||||
sprintf(output_buf, "%s ", buf->d_name);
|
||||
put_string(output_buf, color, COLOR_BLACK);
|
||||
|
||||
}
|
||||
printf("\n");
|
||||
closedir(dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ CFLAGS += -I .
|
||||
libc_sub_dirs=math
|
||||
|
||||
|
||||
libc: unistd.o fcntl.o malloc.o errno.o printf.o stdlib.o ctype.o string.o
|
||||
libc: unistd.o fcntl.o malloc.o errno.o printf.o stdlib.o ctype.o string.o dirent.o
|
||||
@list='$(libc_sub_dirs)'; for subdir in $$list; do \
|
||||
echo "make all in $$subdir";\
|
||||
cd $$subdir;\
|
||||
|
@ -17,10 +17,15 @@ struct DIR *opendir(const char *path)
|
||||
{
|
||||
int fd = open(path, O_DIRECTORY);
|
||||
if (fd < 0) // 目录打开失败
|
||||
{
|
||||
printf("Failed to open dir\n");
|
||||
return NULL;
|
||||
}
|
||||
// printf("open dir: %s\n", path);
|
||||
|
||||
// 分配DIR结构体
|
||||
struct DIR *dirp = (struct DIR *)malloc(sizeof(struct DIR));
|
||||
// printf("dirp = %#018lx", dirp);
|
||||
memset(dirp, 0, sizeof(struct DIR));
|
||||
dirp->fd = fd;
|
||||
dirp->buf_len = DIR_BUF_SIZE;
|
||||
@ -59,11 +64,13 @@ int64_t getdents(int fd, struct dirent *dirent, long count)
|
||||
* @param dir
|
||||
* @return struct dirent*
|
||||
*/
|
||||
struct dirent *reaaddir(struct DIR *dir)
|
||||
struct dirent *readdir(struct DIR *dir)
|
||||
{
|
||||
memset(dir, 0, DIR_BUF_SIZE);
|
||||
// printf("dir->buf = %#018lx\n", (dir->buf));
|
||||
memset((dir->buf), 0, DIR_BUF_SIZE);
|
||||
// printf("memeset_ok\n");
|
||||
int len = getdents(dir->fd, (struct dirent *)dir->buf, DIR_BUF_SIZE);
|
||||
|
||||
// printf("len=%d\n", len);
|
||||
if (len > 0)
|
||||
return (struct dirent *)dir->buf;
|
||||
else
|
||||
|
@ -1,6 +1,15 @@
|
||||
#pragma once
|
||||
#include <libc/sys/types.h>
|
||||
|
||||
|
||||
/**
|
||||
* @brief 目录项的属性(copy from vfs.h)
|
||||
*
|
||||
*/
|
||||
#define VFS_ATTR_FILE (1UL << 0)
|
||||
#define VFS_ATTR_DIR (1UL << 1)
|
||||
#define VFS_ATTR_DEVICE (1UL << 2)
|
||||
|
||||
#define DIR_BUF_SIZE 256
|
||||
/**
|
||||
* @brief 文件夹结构体
|
||||
@ -22,7 +31,7 @@ struct dirent
|
||||
off_t d_off; // dir偏移量
|
||||
unsigned short d_reclen; // 目录下的记录数
|
||||
unsigned char d_type; // entry的类型
|
||||
char d_name[256]; // 文件entry的名字
|
||||
char d_name[]; // 文件entry的名字(是一个零长数组)
|
||||
};
|
||||
|
||||
/**
|
||||
@ -54,4 +63,4 @@ int closedir(struct DIR *dirp);
|
||||
* @param dir
|
||||
* @return struct dirent*
|
||||
*/
|
||||
struct dirent* reaaddir(struct DIR* dir);
|
||||
struct dirent* readdir(struct DIR* dir);
|
Reference in New Issue
Block a user