linux pwd指令C實現
整體思路
-
實現pwd命令:
-
在命令行輸入pwd:顯示了現在所在路徑。
-
研究實現pwd所需的系統調用
-
我們可以通過man命令和grep命令來獲取我們所需要的系統調用函數信息
-
man chdir
-
目錄是一種文件,這種文件比較特殊,它里面存儲的是一張對應表,即文件名和i節點的對應關系表,而i節點才是記錄此文件詳細信息的結構,如文件大小,屬性,權限,存在硬盤的那個塊等。在一個目錄創建文件就是在這張表里添加對應關系,使用某個文件時也是根據i節點確定在硬盤的實際存儲位置的。
- 可以通過
readdir(".")
來獲取當前的目錄。
cd ..
cd
命令返回父級目錄,然后在
readdir(".");
來讀取當前目錄,依次執行下去。
- 所以通過實現
cd
命令,即可解決
pwd
命令
- 通過
man -k chage | grep dir
可用chdir實現
- 在根目錄下運行查看
"."
".."
的i結點節點號一樣的。即可作為循環的終止條件。
ls -i -a .
來查看i結點值。
概括來講,就是:
1.找到本目錄的i-節點
2.進入父目錄,找到i-節點對應的文件名
3.循環以上過程,直到到達根目
偽代碼
定義一個char數組用來保存當前目錄的絕對路徑;
遞歸:
調用函數chdir()來改變當前程序的工作路徑;(返回上一級)
if(返回的指針==NULL)
調用函數中存在錯誤,輸出錯誤警告;
else
直接打印結果
代碼實現
詳細代碼:
#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(".")); //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(".."); //進入上級目錄
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) //找到i-節點對應的文件名,並放在字符數組里
{
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) //根據文件名,返回-i節點
{
struct stat info;
if ( stat( fname, &info) == -1){
fprintf( stderr , "Cannot stat ");
perror(fname);
exit (1);
}
return info.st_ino;
}