linux下的乙個目錄掃瞄程式

2021-08-19 08:45:52 字數 3093 閱讀 9314

程式的開始是一些必要的標頭檔案,接下來是乙個printdir()函式,它的作用是輸出當前目錄的內容,該程式採用遞迴遍歷各級子目錄,使用depth來控制縮排。下面就是程式的**:

#include

#include

#include

#include

#include

#include

void printdir(char *dir,int depth)

dir *dp;

struct dirent *entry;

struct stat statbuf;

if((dp = opendir(dir)) == null)

fprintf(stderr,"can not open this directory: %s\n",dir);

return ;

chdir(dir);//進入指定目錄

while((entry = readdir(dp)) != null)

lstat(entry->d_name,&statbuf);

if(s_isdir(statbuf.st_mode))//查詢乙個目錄但是忽略.和..

//s_isdir查詢是否為目錄

if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)

continue;

printdir(entry->d_name,depth+4);

else

printf("%*s%s\n",depth,"",entry->d_name);

chdir("..");

closedir(dp);

int main()

printf("directory scan of:\n");

printdir("./",0);

printf("done.\n");

exit(0);

return 0;

程式的結果是輸出當前目錄的所有子目錄

在這個程式中主要要介紹一下struct stat結構體感覺用這個結構體比我之前寫的掃瞄目錄簡單多了,**量也少很多:

在使用這個結構體和方法時,需要引入:

struct stat這個結構體是用來描述乙個linux系統檔案系統中的檔案屬性的結構。

可以有兩種方法來獲取乙個檔案的屬性:

1、通過路徑:

int stat(const char *path, struct stat *struct_stat);

int lstat(const char *path,struct stat *struct_stat);

兩個函式的第乙個引數都是檔案的路徑,第二個引數是struct stat的指標。返回值為0,表示成功執行。

執行失敗是,error被自動設定為下面的值:

ebadf: 檔案描述詞無效

efault: 位址空間不可訪問

eloop: 遍歷路徑時遇到太多的符號連線

enametoolong:檔案路徑名太長

enoent:路徑名的部分元件不存在,或路徑名是空字串

enomem:記憶體不足

enotdir:路徑名的部分元件不是目錄

這兩個方法區別在於stat沒有處理字元鏈結(軟鏈結)的能力,如果乙個檔案是符號鏈結,stat會直接返回它所指向的檔案的屬性;而lstat返回的就是這個符號鏈結的內容。這裡需要說明一下的是軟鏈結和硬鏈結的含義。我們知道目錄在linux中也是乙個檔案,檔案的內容就是這這個目錄下面所有檔案與inode的對應關係。那麼所謂的硬鏈結就是在某乙個目錄下面將乙個檔名與乙個inode關聯起來,其實就是新增一條記錄!而軟鏈結也叫符號鏈結更加簡單了,這個檔案的內容就是乙個字串,這個字串就是它所鏈結的檔案的絕對或者相對位址。

下面是這個結構的結構

struct stat ;

stat結構體中的st_mode 則定義了下列數種情況:

s_ifmt   0170000    檔案型別的位遮罩

s_ifsock 0140000    scoket

s_iflnk 0120000     符號連線

s_ifreg 0100000     一般檔案

s_ifblk 0060000     區塊裝置

s_ifdir 0040000     目錄

s_ifchr 0020000     字元裝置

s_ififo 0010000     先進先出

s_isuid 04000     檔案的(set user-id on execution)位

s_isgid 02000     檔案的(set group-id on execution)位

s_isvtx 01000     檔案的sticky位

s_irusr(s_iread) 00400     檔案所有者具可讀取許可權

s_iwusr(s_iwrite)00200     檔案所有者具可寫入許可權

s_ixusr(s_iexec) 00100     檔案所有者具可執行許可權

s_irgrp 00040             使用者組具可讀取許可權

s_iwgrp 00020             使用者組具可寫入許可權

s_ixgrp 00010             使用者組具可執行許可權

s_iroth 00004             其他使用者具可讀取許可權

s_iwoth 00002             其他使用者具可寫入許可權

s_ixoth 00001             其他使用者具可執行許可權

上述的檔案型別在posix中定義了檢查這些型別的巨集定義:

s_islnk (st_mode)    判斷是否為符號連線

s_isreg (st_mode)    是否為一般檔案

s_isdir (st_mode)    是否為目錄

s_ischr (st_mode)    是否為字元裝置檔案

s_isblk (s3e)        是否為先進先出

s_issock (st_mode)   是否為socket

關於struct stat摘自

LINUX讀取乙個目錄

使用的函式 gnu命令列處理函式getopt getopt long opendir readdir closedir 熟悉gnu命令列處理函式,以及linux目錄函式。include include include include include define err quit printf de...

騰訊面試 Linux下如何掃瞄目錄

問題 在linux下,輸出某個資料夾下的檔案即其子目錄,以及子目錄下的檔案和資料夾。如下 include include include include include include include 值得注意的是 struct stat statbuf 1.如果宣告為 struct stat st...

乙個簡單的埠掃瞄程式題

一 tcp掃瞄技術 為了提高掃瞄速度,本程式採用了多執行緒技術和非阻塞i o的技術。程式的主介面是乙個對話方塊,下面是程式框架示意圖 1 全域性變數 以下是所有全域性變數的定義 hwnd g hwnd null 處理訊息的視窗控制代碼 unsigned long g uladdr inaddr no...