20155222 c語言實現pwd命令

來源:互聯網
上載者:User

標籤:利用   另一個   pre   結構   name   main   class   目錄檔案   print   

20155222 c語言實現linux的pwd命令
  • 1.學習pwd命令在Linux階層中,使用者可以在被授權的任意目錄下利用mkdir命令建立新目錄,也可以利用cd命令從一個目錄轉換到另一個目錄。然而,沒有提示符來告知使用者目前處於哪一個目錄中。想要知道當前所處的目錄,可以用pwd命令,該命令顯示整個路徑名。

  • 2.研究實現pwd所需的系統調用

opendir(開啟目錄)
相關函數 open,readdir,closedir,rewinddir,seekdir,telldir,scandir
表標頭檔

#include<sys/types.h>#include<dirent.h>

定義函數

DIR * opendir(const char * name);

函數說明 opendir()用來開啟參數name指定的目錄,並返回DIR形態的目錄流,和open()類似,接下來對目錄的讀取和搜尋都要使用此傳回值。
傳回值 成功則返回DIR
型態的目錄流,開啟失敗則返回NULL。
錯誤碼 EACCESS 許可權不足
EMFILE 已達到進程可同時開啟的檔案數上限。
ENFILE 已達到系統可同時開啟的檔案數上限。
ENOTDIR 參數name非真正的目錄
ENOENT 參數name 指定的目錄不存在,或是參數name 為一Null 字元
串。
ENOMEM 核心記憶體不足。

readdir(讀取目錄)
相關函數 open,opendir,closedir,rewinddir,seekdir,telldir,scandir
表標頭檔

#include <sys/types.h>#include <dirent.h>

定義函數 struct dirent * readdir(DIR * dir);
函數說明 readdir()返回參數dir目錄流的下個目錄進入點。
結構dirent定義如下

struct dirent{ino_t d_ino;ff_t d_off;signed short int d_reclen;unsigned char d_type;har d_name[256;};

d_ino 此目錄進入點的inode
d_off 目錄檔案開頭至此目錄進入點的位移
d_reclen _name的長度,不包含NULL字元
d_type d_name 所指的檔案類型
d_name 檔案名稱
傳回值 成功則返回下個目錄進入點。有錯誤發生或讀取到目錄檔案尾則返回NULL。
附加說明 EBADF參數dir為無效的目錄流。
 
範例:

#include<sys/types.h>#include<dirent.h>#include<unistd.h>main(){DIR * dir;struct dirent * ptr;int i;dir =opendir(“/etc/rc.d”);while((ptr = readdir(dir))!=NULL){printf(“d_name: %s\n”,ptr->d_name);}closedir(dir);}執行d_name:.d_name:..d_name:init.dd_name:rc0.dd_name:rc1.dd_name:rc2.dd_name:rc3.dd_name:rc4.dd_name:rc5.dd_name:rc6.dd_name:rcd_name:rc.locald_name:rc.sysinit

closedir(關閉目錄)
相關函數 opendir
表標頭檔

#include<sys/types.h>#include<dirent.h>

定義函數 int closedir(DIR *dir);
函數說明 closedir()關閉參數dir所指的目錄流。
傳回值 關閉成功則返回0,失敗返回-1,錯誤原因存於errno 中。
錯誤碼 EBADF 參數dir為無效的目錄流

  • 3.實現步驟:
    步驟一:查看目前的目錄"."檔案的inode-number
    步驟二:查看目前的目錄“..”檔案的inode-number
    步驟三:比較兩個inode-number,若相等,已到根目錄,進入步驟四,否則進入“..”,根據步驟一中擷取的inode-number,在目錄中尋找相應的檔案名稱並將其入棧,回到步驟一。
    步驟四:依次出棧輸出路徑名

  • 4.實現代碼

#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<dirent.h>#include<sys/stat.h>#include<fcntl.h>#include<string.h>int main(){                       char this_name[20]=".",father_name[20]="..";        char name[16][32];        struct dirent *this_dir=NULL,*father_dir=NULL,*temp=NULL;        DIR *dir=NULL;        int i=0;        unsigned long int this_ino;        while(1)        {                dir = opendir(this_name);                if(dir == NULL)                printf("error open");                while(1)                {                        this_dir = readdir(dir);                        if(strcmp(this_dir->d_name,".")==0)                        break;                }                this_ino=this_dir->d_ino;                closedir(dir);                dir = opendir(father_name);                while(1)                {                        father_dir = readdir(dir);                        if(father_dir->d_name[0]==‘.‘&&father_dir->d_name[1]==‘.‘)                        break;                }                if(this_dir->d_ino==father_dir->d_ino)                break;                closedir(dir);                dir=opendir(father_name);                while((temp=readdir(dir))!=NULL)                {                        if(temp->d_ino==this_ino)                        {                            bzero(name[i],32);                    strcat(name[i++],temp->d_name);                                break;                        }                }                closedir(dir);                strcat(this_name,"/..");                strcat(father_name,"/..");        }        for(i--;i>=0;i--)    printf("/%s",name[i]);    printf("\n");}
  • 5.測試代碼

20155222 c語言實現pwd命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.