mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-08 22:36:48 +00:00
🆕 cat命令
This commit is contained in:
parent
9ee6d33318
commit
85707bd8cc
@ -336,8 +336,8 @@ find_lookup_success:; // 找到目标dentry
|
|||||||
finode->first_clus = ((tmp_dEntry->DIR_FstClusHI << 16) | tmp_dEntry->DIR_FstClusLO) & 0x0fffffff;
|
finode->first_clus = ((tmp_dEntry->DIR_FstClusHI << 16) | tmp_dEntry->DIR_FstClusLO) & 0x0fffffff;
|
||||||
finode->dEntry_location_clus = cluster;
|
finode->dEntry_location_clus = cluster;
|
||||||
finode->dEntry_location_clus_offset = tmp_dEntry - (struct fat32_Directory_t *)buf; //计算dentry的偏移量
|
finode->dEntry_location_clus_offset = tmp_dEntry - (struct fat32_Directory_t *)buf; //计算dentry的偏移量
|
||||||
kdebug("finode->dEntry_location_clus=%#018lx", finode->dEntry_location_clus);
|
// kdebug("finode->dEntry_location_clus=%#018lx", finode->dEntry_location_clus);
|
||||||
kdebug("finode->dEntry_location_clus_offset=%#018lx", finode->dEntry_location_clus_offset);
|
// kdebug("finode->dEntry_location_clus_offset=%#018lx", finode->dEntry_location_clus_offset);
|
||||||
finode->create_date = tmp_dEntry->DIR_CrtDate;
|
finode->create_date = tmp_dEntry->DIR_CrtDate;
|
||||||
finode->create_time = tmp_dEntry->DIR_CrtTime;
|
finode->create_time = tmp_dEntry->DIR_CrtTime;
|
||||||
finode->write_date = tmp_dEntry->DIR_WrtDate;
|
finode->write_date = tmp_dEntry->DIR_WrtDate;
|
||||||
@ -1079,11 +1079,11 @@ find_dir_success:;
|
|||||||
// 将文件夹位置坐标加32(即指向下一个目录项)
|
// 将文件夹位置坐标加32(即指向下一个目录项)
|
||||||
file_ptr->position += 32;
|
file_ptr->position += 32;
|
||||||
// todo: 计算ino_t
|
// todo: 计算ino_t
|
||||||
if(dentry_type & ATTR_DIRECTORY)
|
if (dentry_type & ATTR_DIRECTORY)
|
||||||
dentry_type = VFS_ATTR_DIR;
|
dentry_type = VFS_ATTR_DIR;
|
||||||
else
|
else
|
||||||
dentry_type = VFS_ATTR_FILE;
|
dentry_type = VFS_ATTR_FILE;
|
||||||
|
|
||||||
return filler(dirent, 0, dir_name, name_len, dentry_type, 0);
|
return filler(dirent, 0, dir_name, name_len, dentry_type, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,7 +340,7 @@ uint64_t sys_lseek(struct pt_regs *regs)
|
|||||||
long offset = (long)regs->r9;
|
long offset = (long)regs->r9;
|
||||||
int whence = (int)regs->r10;
|
int whence = (int)regs->r10;
|
||||||
|
|
||||||
kdebug("sys_lseek: fd=%d", fd_num);
|
// kdebug("sys_lseek: fd=%d", fd_num);
|
||||||
uint64_t retval = 0;
|
uint64_t retval = 0;
|
||||||
|
|
||||||
// 校验文件描述符范围
|
// 校验文件描述符范围
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
#include <libc/errno.h>
|
#include <libc/errno.h>
|
||||||
#include <libc/unistd.h>
|
#include <libc/unistd.h>
|
||||||
#include <libc/stdlib.h>
|
#include <libc/stdlib.h>
|
||||||
|
#include <libc/fcntl.h>
|
||||||
#include <libc/dirent.h>
|
#include <libc/dirent.h>
|
||||||
|
|
||||||
#include "cmd_help.h"
|
#include "cmd_help.h"
|
||||||
@ -200,11 +200,10 @@ done:;
|
|||||||
* @param argv
|
* @param argv
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
// todo:
|
|
||||||
int shell_cmd_ls(int argc, char **argv)
|
int shell_cmd_ls(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct DIR *dir = opendir(shell_current_path);
|
struct DIR *dir = opendir(shell_current_path);
|
||||||
|
|
||||||
if (dir == NULL)
|
if (dir == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -214,23 +213,26 @@ int shell_cmd_ls(int argc, char **argv)
|
|||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
buf = readdir(dir);
|
buf = readdir(dir);
|
||||||
if(buf == NULL)
|
if (buf == NULL)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
int color = COLOR_WHITE;
|
int color = COLOR_WHITE;
|
||||||
if(buf->d_type & VFS_ATTR_DIR)
|
if (buf->d_type & VFS_ATTR_DIR)
|
||||||
color = COLOR_YELLOW;
|
color = COLOR_YELLOW;
|
||||||
else if(buf->d_type & VFS_ATTR_FILE)
|
else if (buf->d_type & VFS_ATTR_FILE)
|
||||||
color = COLOR_INDIGO;
|
color = COLOR_INDIGO;
|
||||||
|
|
||||||
char output_buf[256] = {0};
|
char output_buf[256] = {0};
|
||||||
|
|
||||||
sprintf(output_buf, "%s ", buf->d_name);
|
sprintf(output_buf, "%s ", buf->d_name);
|
||||||
put_string(output_buf, color, COLOR_BLACK);
|
put_string(output_buf, color, COLOR_BLACK);
|
||||||
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
|
if (argc > 1)
|
||||||
|
free(argv);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,8 +247,8 @@ int shell_cmd_pwd(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
if (shell_current_path)
|
if (shell_current_path)
|
||||||
printf("%s\n", shell_current_path);
|
printf("%s\n", shell_current_path);
|
||||||
|
if (argc > 1)
|
||||||
free(argv);
|
free(argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -256,8 +258,47 @@ int shell_cmd_pwd(int argc, char **argv)
|
|||||||
* @param argv
|
* @param argv
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
// todo:
|
int shell_cmd_cat(int argc, char **argv)
|
||||||
int shell_cmd_cat(int argc, char **argv) {}
|
{
|
||||||
|
int cwd_len = strlen(shell_current_path);
|
||||||
|
|
||||||
|
// 计算文件完整路径的长度
|
||||||
|
int file_path_len = cwd_len + strlen(argv[1]);
|
||||||
|
|
||||||
|
char *file_path = (char *)malloc(file_path_len + 2);
|
||||||
|
|
||||||
|
memset(file_path, 0, file_path_len + 2);
|
||||||
|
|
||||||
|
strcpy(file_path, shell_current_path);
|
||||||
|
|
||||||
|
// 在文件路径中加入斜杠
|
||||||
|
if (cwd_len > 1)
|
||||||
|
file_path[cwd_len] = '/';
|
||||||
|
|
||||||
|
// 拼接完整路径
|
||||||
|
strcat(file_path, argv[1]);
|
||||||
|
|
||||||
|
// 打开文件
|
||||||
|
int fd = open(file_path, 0);
|
||||||
|
// 获取文件总大小
|
||||||
|
int file_size = lseek(fd, 0, SEEK_END);
|
||||||
|
// 将文件指针切换回文件起始位置
|
||||||
|
lseek(fd, 0, SEEK_SET);
|
||||||
|
|
||||||
|
char *buf = (char *)malloc(512);
|
||||||
|
memset(buf, 0, 512);
|
||||||
|
while (file_size > 0)
|
||||||
|
{
|
||||||
|
int l = read(fd, buf, 511);
|
||||||
|
buf[l] = '\0';
|
||||||
|
|
||||||
|
file_size -= l;
|
||||||
|
printf("%s", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 创建空文件的命令
|
* @brief 创建空文件的命令
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "cmd_help.h"
|
#include "cmd_help.h"
|
||||||
#include <libc/stdio.h>
|
#include <libc/stdio.h>
|
||||||
|
#include <libc/stdlib.h>
|
||||||
struct help_table_item_t
|
struct help_table_item_t
|
||||||
{
|
{
|
||||||
void (*func)();
|
void (*func)();
|
||||||
@ -10,11 +11,14 @@ struct help_table_item_t help_table[] = {
|
|||||||
|
|
||||||
static const int help_table_num = sizeof(help_table) / sizeof(struct help_table_item_t);
|
static const int help_table_num = sizeof(help_table) / sizeof(struct help_table_item_t);
|
||||||
|
|
||||||
void shell_help()
|
int shell_help(int argc, char **argv)
|
||||||
{
|
{
|
||||||
printf("Help:\n");
|
printf("Help:\n");
|
||||||
for (int i = 0; i < help_table_num; ++i)
|
for (int i = 0; i < help_table_num; ++i)
|
||||||
help_table[i].func();
|
help_table[i].func();
|
||||||
|
|
||||||
|
if(argc > 1)
|
||||||
|
free(argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shell_help_cd()
|
void shell_help_cd()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "cmd.h"
|
#include "cmd.h"
|
||||||
void shell_help();
|
int shell_help(int argc, char **argv);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief cd命令的帮助信息
|
* @brief cd命令的帮助信息
|
||||||
|
@ -59,13 +59,13 @@ void *memset(void *dst, unsigned char C, uint64_t size)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 拷贝指定字节数的字符串
|
* @brief 拷贝指定字节数的字符串
|
||||||
*
|
*
|
||||||
* @param dst 目标地址
|
* @param dst 目标地址
|
||||||
* @param src 源字符串
|
* @param src 源字符串
|
||||||
* @param Count 字节数
|
* @param Count 字节数
|
||||||
* @return char*
|
* @return char*
|
||||||
*/
|
*/
|
||||||
char *strncpy(char *dst, char *src, long Count)
|
char *strncpy(char *dst, const char *src, size_t Count)
|
||||||
{
|
{
|
||||||
__asm__ __volatile__("cld \n\t"
|
__asm__ __volatile__("cld \n\t"
|
||||||
"1: \n\t"
|
"1: \n\t"
|
||||||
@ -86,24 +86,31 @@ char *strncpy(char *dst, char *src, long Count)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 拼接两个字符串(将src接到dest末尾)
|
* @brief 拼接两个字符串(将src接到dest末尾)
|
||||||
*
|
*
|
||||||
* @param dest 目标串
|
* @param dest 目标串
|
||||||
* @param src 源串
|
* @param src 源串
|
||||||
* @return char*
|
* @return char*
|
||||||
*/
|
*/
|
||||||
char *strcat(char *dest, const char *src)
|
char *strcat(char *dest, const char *src)
|
||||||
{
|
{
|
||||||
unsigned int dest_size = strlen(dest);
|
strcpy(dest + strlen(dest), src);
|
||||||
unsigned int src_size = strlen(src);
|
|
||||||
|
|
||||||
char *d = dest;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < src_size; i++)
|
|
||||||
{
|
|
||||||
d[dest_size + i] = src[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
d[dest_size + src_size] = '\0';
|
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 拷贝整个字符串
|
||||||
|
*
|
||||||
|
* @param dst 目标地址
|
||||||
|
* @param src 源地址
|
||||||
|
* @return char* 目标字符串
|
||||||
|
*/
|
||||||
|
char *strcpy(char *dst, const char *src)
|
||||||
|
{
|
||||||
|
while (*src)
|
||||||
|
{
|
||||||
|
*(dst++) = *(src++);
|
||||||
|
}
|
||||||
|
*dst = 0;
|
||||||
|
|
||||||
|
return dst;
|
||||||
}
|
}
|
@ -28,7 +28,16 @@ int strcmp(const char *FirstPart, const char *SecondPart);
|
|||||||
* @param Count 字节数
|
* @param Count 字节数
|
||||||
* @return char*
|
* @return char*
|
||||||
*/
|
*/
|
||||||
char *strncpy(char *dst, char *src, long Count);
|
char *strncpy(char *dst, const char *src, size_t Count);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 拷贝整个字符串
|
||||||
|
*
|
||||||
|
* @param dst 目标地址
|
||||||
|
* @param src 源地址
|
||||||
|
* @return char* 目标字符串
|
||||||
|
*/
|
||||||
|
char* strcpy(char* dst, const char* src);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 拼接两个字符串(将src接到dest末尾)
|
* @brief 拼接两个字符串(将src接到dest末尾)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user