xv-6 EX(学习路径 超全总结 持续更新中)

xv-6 EX2

基础知识

图片说明了每个文件在【从用户态到内核态】的【工作机制】中
所处的位置https://www.cnblogs.com/YuanZiming/p/14218997.html

文字描述了每个文件的具体内容/作用https://zhuanlan.zhihu.com/p/371396502

trace

具体实验过程https://blog.csdn.net/u013577996/article/details/109166463
配合食用https://zhuanlan.zhihu.com/p/274369780

sysinfo

https://zhuanlan.zhihu.com/p/274369780

https://blog.csdn.net/u013577996/article/details/109166463

xv-6 EX1

read

while(read(p[0], &i, 4) > 0) {// i从管道p读取内容
    if (i % prime != 0) {
        write(p2[1], &i, 4);// 将i写入管道p
    }
}

xargs

https://blog.csdn.net/laplacebh/article/details/117518581

字符串指针:
https://www.cnblogs.com/focusDing/p/12435239.html

find

参考一:
https://blog.csdn.net/u013577996/article/details/108680888
参考二:
https://zhuanlan.zhihu.com/p/272199762
参考三:
https://blog.csdn.net/laplacebh/article/details/117518581

详解xv-6之ls

参考:https://zhuanlan.zhihu.com/p/353963631

中文注释

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h"

char*
fmtname(char *path)
{
  static char buf[DIRSIZ+1];
  char *p;

  // Find first character after last slash.
  for(p=path+strlen(path); p >= path && *p != '/'; p--)
    ;
  p++;

  // Return blank-padded name.
  if(strlen(p) >= DIRSIZ)
    return p;
  memmove(buf, p, strlen(p));
  memset(buf+strlen(p), ' ', DIRSIZ-strlen(p));
  return buf;
}

void
ls(char *path)
{
  char buf[512], *p;
  int fd;
  struct dirent de;//指的是目录项这一结构体(在kernel/fs.h中定义)
  struct stat st;//指的是文件的统计信息(在kernel/stat.h中定义)
                //包含文件类型(目录或文件)/inode/文件引用nlink/文件大小/存放fs的disk dev

  if((fd = open(path, 0)) < 0)//open系统调用 打开文件
                              //第二个参数指示的是打开方式,0代表的是O_RDONLY只读的形式。
                              //返回值是file descriptor >=0,<0说明open失败
  {
    fprintf(2, "ls: cannot open %s\n", path);
    return;
  }

  if(fstat(fd, &st) < 0)//fstat与open类似,将fd指代的文件的相关信息存入结构体st中
  {
    fprintf(2, "ls: cannot stat %s\n", path);
    close(fd);
    return;
  }
  //以上两个if是对读入失败的情况进行处理

  switch(st.type){
  case T_FILE://如果是文件
              //直接打印st存储的文件的相关信息
              //st.type有三个值分别是({1:目录,2:文件,3:console}) fmtname返回值就是文件名称
    printf("%s %d %d %l\n", fmtname(path), st.type, st.ino, st.size);
    break;

  case T_DIR://如果是目录
    if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf)//if用来检查缓存有没有溢出
    {
      printf("ls: path too long\n");
      break;
    }
    
    //下面的几句话就是在拼接字符串
    //目的是为了以路径名访问这个目录里面的内容, read操作就是在访问目录内容
    strcpy(buf, path);
    p = buf+strlen(buf);
    *p++ = '/';
    //p指向的指针的内容是buf?
    while(read(fd, &de, sizeof(de)) == sizeof(de))//读取fd,存到de
    {
      if(de.inum == 0)//代表了此文件夹无文件,所以直接continue进行下一次read
        continue;
      memmove(p, de.name, DIRSIZ);//将de.name的内容移动到p指向的指针中
      //memmove实现在(user/ulib.c),主要意思就是内存之间的迁移
      p[DIRSIZ] = 0;
      if(stat(buf, &st) < 0){
        printf("ls: cannot stat %s\n", buf);
        continue;
      }
      printf("%s %d %d %d\n", fmtname(buf), st.type, st.ino, st.size);
    }
    break;
  }
  close(fd);
}

int
main(int argc, char *argv[])
{
  int i;

  if(argc < 2){//判断argc,输入只有“ls"
               //没有其他参数
    ls(".");//调用ls函数,参数为.,即当前文件夹
    exit();
  }
  for(i=1; i<argc; i++)
    ls(argv[i]);
  exit();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值