跟我一起写操作系统

    返回首页    发表留言
本文作者:李德强
          第六节 cat命令
 
 

           cat命令为concatenate files and print on the standard output的简写,功能为显示文件的内容,实现如下:

char *current_path = malloc(SHELL_CMD_LEN);
int params[2];
params[0] = 6;
params[1] = (int) current_path;
__asm__ volatile("int $0x80" :: "a"(params));
char *path = malloc(SHELL_CMD_LEN);
str_copy(args[1], path);
char *full_path = malloc(SHELL_CMD_LEN);
full_path[0] = '\0';
if (path[0] == '/')
{
        repath(path, NULL, full_path);
}
//不是以'/'开头: 相对路径
else
{
        repath(path, current_path, full_path);
}
FILE *fp = fopen(full_path, FS_MODE_READ);
if (fp == NULL)
{
        printf("-bash: \"%s\": no such file or permission denied.\n", path);
}
else
{
        char *content = malloc(fp->fs.size);
        fread(fp, fp->fs.size, content);
        printf("%s\n", content);
        free(content);
}
fclose(fp);
free(full_path);
free(current_path);

          运行结果:


 

        源代码的下载地址为:

https            https://github.com/magicworldos/lidqos.git 
git              git@github.com:magicworldos/lidqos.git 
subverion        https://github.com/magicworldos/lidqos 
branch           v0.30

 

    返回首页    返回顶部
  看不清?点击刷新

 

  Copyright © 2015-2023 问渠网 辽ICP备15013245号