# include <stdio.h>
# include <stdlib.h>
# include <sys/stat.h>
# include <unistd.h>
# include <sys/types.h>
# include <dirent.h>
ino_t get_inode(char * name);
void pwd_dir(ino_t node);
int main(void)
{
pwd_dir(get_inode("."));
printf("\n");
return 0;
}
void pwd_dir(ino_t node)
{
struct dirent * dir;
DIR * buf_dir;
chdir("..");
if(node == get_inode("."))
return;
buf_dir = opendir(".");
while((dir = readdir(buf_dir)) != NULL)
{
if(dir->d_ino == node)
{
pwd_dir(get_inode("."));
printf("/%s", dir->d_name);
}
}
}
ino_t get_inode(char * name)
{
struct stat current_name;
if(stat(name, ¤t_name) == -1)
{
fprintf(stderr, "Cannot stat");
perror(name);
exit(1);
}
return current_name.st_ino;
}
类似pwd命令程序
最新推荐文章于 2022-08-26 17:31:43 发布