Linux编程实践Day4
pwd的工作原理
- 通过stat函数获得“.”的ino,根据ino与其上级目录的ino是否相同判断是否为根目录
- 若不是根目录,则进入上一级目录
- 根据ino获得文件的名字
- 重复直至抵达根目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
ino_t get_inode(char*);
void printpathto(ino_t);
void inum_to_name(ino_t,char*,int);
int main()
{
printpathto(get_inode(".")); //打印当前目录绝对路径
putchar('\n');
return 0;
}
void printpathto(ino_t this_inode)
{
ino_t my_inode;
char its_name[BUFSIZ];
/*如果本目录的inode-number与上级目录不同,即本目录不是根目录*/
if (get_inode("..")!=this_inode)
{
chdir(".."); //进入上级目录
inum_to_name(this_inode,its_name,BUFSIZ);
my_inode = get_inode(".");
printpathto(my_inode);
printf("/%s",its_name);
}
}
void inum_to_name(ino_t inode_to_find,char* namebuf,int buflen) //找到inode-number节点对应的文件名,并放在字符数组里
{
DIR* dir_ptr;
struct dirent* direntp;
dir_ptr = opendir(".");
if (dir_ptr == NULL)
{
perror(".");
exit(1);
}
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) //根据文件名,返回inode-number
{
struct stat info;
if ( stat( fname, &info) == -1){
fprintf( stderr , "Cannot stat ");
perror(fname);
exit (1);
}
return info.st_ino;
}
结果展示
![](/2023/09/12/Linux%E7%BC%96%E7%A8%8B%E5%AE%9E%E8%B7%B5Day4/pic1.png)
参考
[1] cnblog
Linux编程实践Day4
http://bugeater.space/2023/09/12/Linux编程实践Day4/