1 Star 0 Fork 0

20145221高其 / linux-besti-is-2016-2017-1-20145221

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
spwd.c 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
20145221高其 提交于 2016-11-18 18:52 . 显示当期目录路径
#include <stdio.h>
#include <stdlib.h>
#include <string.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];
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)
{
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", (int) inode_to_find);
exit(1);
}
ino_t get_inode( char *fname )
{
struct stat info;
if ( stat( fname , &info ) == -1 ){
fprintf(stderr, "Cannot stat ");
perror(fname);
exit(1);
}
return info.st_ino;
}
C
1
https://gitee.com/20145221/linux-besti-is-2016-2017-1-20145221.git
git@gitee.com:20145221/linux-besti-is-2016-2017-1-20145221.git
20145221
linux-besti-is-2016-2017-1-20145221
linux-besti-is-2016-2017-1-20145221
master

搜索帮助