From b51087922ab7012ee143f116fadfa49877a1e91a Mon Sep 17 00:00:00 2001 From: KAAAsS Date: Wed, 20 Apr 2022 22:39:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=BA=E5=88=86=20ls=E3=80=81ll=20=E6=8C=87?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab5/user/lab5/apps/init.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lab5/user/lab5/apps/init.c b/lab5/user/lab5/apps/init.c index 821d3f5..43ec581 100644 --- a/lab5/user/lab5/apps/init.c +++ b/lab5/user/lab5/apps/init.c @@ -218,7 +218,7 @@ int fs_read_all(char *path, void **ret_buf) return size; } -int do_ls(char *cmdline) +int do_ls(char *cmdline, bool detail) { char pathbuf[BUFLEN]; char fpathbuf[BUFLEN]; @@ -240,17 +240,21 @@ int do_ls(char *cmdline) for (int i = 0; i < ret; i++) { dirat = (struct dirent *) print_ptr; - // 文件类型 - char type = 'f'; - if (dirat->d_type == 2) { - type = 'd'; // 目录 + if (detail) { + // 文件类型 + char type = 'f'; + if (dirat->d_type == 2) { + type = 'd'; // 目录 + } + // 文件大小 + strcpy(fpathbuf, pathbuf); + path_append(fpathbuf, dirat->d_name); + int size = fs_get_size(fpathbuf); + // 输出 + printf("%c %10d %s\n", type, size, dirat->d_name); + } else { + printf("%s\n", dirat->d_name); } - // 文件大小 - strcpy(fpathbuf, pathbuf); - path_append(fpathbuf, dirat->d_name); - int size = fs_get_size(fpathbuf); - // 输出 - printf("%c %10d %s\n", type, size, dirat->d_name); print_ptr += dirat->d_reclen; } } else { @@ -318,9 +322,13 @@ int builtin_cmd(char *cmdline) return !ret ? 1 : -1; } if (!strcmp(cmd, "ls")) { - ret = do_ls(cmdline); + ret = do_ls(cmdline, false); return !ret ? 1 : -1; } + if (!strcmp(cmd, "ll")) { + ret = do_ls(cmdline, true); + return !ret ? 1 : -1; + } if (!strcmp(cmd, "echo")) { ret = do_echo(cmdline); return !ret ? 1 : -1;