From c147ba95d210a2a6b2f4e37230e7a00e0a54c606 Mon Sep 17 00:00:00 2001 From: pi97tejy3 Date: Fri, 24 Jun 2022 20:50:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0getcwd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stat.c1 | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 stat.c1 diff --git a/stat.c1 b/stat.c1 new file mode 100644 index 0000000..16ca848 --- /dev/null +++ b/stat.c1 @@ -0,0 +1,185 @@ +/* + * linux/fs/stat.c + * + * (C) 1991 Linus Torvalds + */ + +#include +#include + +#include +#include +#include +#include +#include + +static void cp_stat(struct m_inode * inode, struct stat * statbuf) +{ + struct stat tmp; + int i; + + verify_area(statbuf,sizeof (* statbuf)); + tmp.st_dev = inode->i_dev; + tmp.st_ino = inode->i_num; + tmp.st_mode = inode->i_mode; + tmp.st_nlink = inode->i_nlinks; + tmp.st_uid = inode->i_uid; + tmp.st_gid = inode->i_gid; + tmp.st_rdev = inode->i_zone[0]; + tmp.st_size = inode->i_size; + tmp.st_atime = inode->i_atime; + tmp.st_mtime = inode->i_mtime; + tmp.st_ctime = inode->i_ctime; + for (i=0 ; ii_dev; + tmp.st_ino = inode->i_num; + tmp.st_mode = inode->i_mode; + tmp.st_nlink = inode->i_nlinks; + tmp.st_uid = inode->i_uid; + tmp.st_gid = inode->i_gid; + tmp.st_rdev = inode->i_zone[0]; + tmp.st_size = inode->i_size; + tmp.st_atime = inode->i_atime; + tmp.st_mtime = inode->i_mtime; + tmp.st_ctime = inode->i_ctime; + for (i=0 ; i= NR_OPEN || !(f=current->filp[fd]) || !(inode=f->f_inode)) + return -EBADF; + cp_stat(inode,statbuf); + return 0; +} + +int sys_readlink() +{ + return -ENOSYS; +} + +char *sys_getcwd(char *buf,size_t size) +{ + char path[128], cwd[128], name[16], res[128]; + int dirp; + struct stat sb, sb_d, sb_1; + dev_t dev; + ino_t ino; + int tmp = 0, i; + memset(res, 0, sizeof(res)); + while(1) + { + //将当前目录的字符串"."存入buf中才能作为stat的参数传入使用 + put_fs_byte('.', &buf[0]); + put_fs_byte(0, &buf[1]); + + //获取当前目录的文件信息 + my_stat(buf, &sb); + dev = sb.st_dev; + ino = sb.st_ino; + + //获取父目录的对应的目录流和父目录的文件信息,将父目录".."字符串写入buf中 + put_fs_byte('.', &buf[0]); + put_fs_byte('.', &buf[1]); + put_fs_byte(0, &buf[2]); + + //获取父目录的对应的目录流 + dirp = sys_open(buf, O_RDONLY, 0); + //获取父目录的文件信息 + my_stat(buf, &sb_1); + + //判断当前目录是否与父目录相同,相同则找到当前路径 + if(sb_1.st_dev == dev && sb_1.st_ino == ino) + { + break; + } + + //在父目录对应的目录流读取条目 + //遍历父目录下对应的所有目录与之前获取的当前目录想匹配,匹配成功的目录项对当前目录所在的目录项,即可保存名字后继续向前寻找 + while(my_read(dirp, name, 16) > 0 && name[0]) + { + tmp = 0; + memset(path, 0, sizeof(path)); + path[0] = '.';path[1] = '.';path[2] = '/'; + while(name[tmp+2]) + { + path[tmp+3] = name[tmp+2]; + tmp++; + } + path[tmp+3] = 0; + for(i=0;i