1 Star 0 Fork 0

20165235qy/20165235-c

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mypwd.c 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
20165235qy 提交于 2018-11-25 16:05 . pwd
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
ino_t get_inode(char*);
void printpathto(ino_t);
void inode_to_name(ino_t,char*,int);
int main()
{
printpathto(get_inode(".")); //print path to here
putchar('\n');
return 0;
}
void printpathto(ino_t this_inode)
{
ino_t my_inode;
char its_name[BUFSIZ];
/*如果本目录的i-节点与上级目录不同,即本目录不是根目录*/
if (get_inode("..")!=this_inode)
{
chdir(".."); //进入上级目录
inode_to_name(this_inode,its_name,BUFSIZ);
my_inode = get_inode(".");
printpathto(my_inode);
printf("/%s",its_name);
}
}
void inode_to_name(ino_t inode_to_find,char* namebuf,int buflen) //找到i-节点对应的文件名,并放在字符数组里
{
DIR* dir_ptr;
struct dirent* direntp;
dir_ptr = opendir(".");
if (dir_ptr == NULL)
{
perror(".");
exit(1);
}
/*上面是打开一个目录流,然后下面是通过目录流读取来查找当前目录的与要找的i-node相对应的文件名,之后关闭目录流*/
while((direntp = readdir(dir_ptr)) != NULL)
{
if(direntp->d_ino == inode_to_find)
{
strncpy(namebuf,direntp->d_name,buflen);
namebuf[buflen-1] = '\0';
closedir( dir_ptr);
return;
}
}
fprintf( stderr , "error looking for inum % d\n" ,inode_to_find);
exit (1) ;
}
ino_t get_inode(char* fname) //根据文件名,返回-i节点
{
struct stat info;
if ( stat( fname, &info) == -1){
fprintf( stderr , "Cannot stat ");
perror(fname);
exit (1);
}
return info.st_ino;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qy20165235/20165235_this.git
git@gitee.com:qy20165235/20165235_this.git
qy20165235
20165235_this
20165235-c
master

搜索帮助