2013-02-19 23 views
0

我想使用函數S_ISLNK(file.st_mode)來檢查某個文件是符號鏈接還是目錄或常規文件。然而,當我用符號鏈接文件檢查它時,它似乎不起作用。確定unix系統上的符號鏈接

這裏是我的代碼:

  if(S_ISDIR(fileStat.st_mode)) 
      { 
       // DIR - display files in the directory 
       printf(" DIR   "); 
       fileType = 2; 


      }else if(S_ISLNK(fileStat.st_mode)){ 
       // LNK - display the name of the file the link is pointing to 
       printf(" LNK   "); 
       fileType = 3; 
      }else{ 
       // Display general info only 
       printf(" REG   "); 
       fileType = 1; 
      } 

一個目錄的檢查工作正常,但是當我在一個符號鏈接運行我的程序文件,它顯示爲一個普通的文件。任何人都知道我可能做錯了什麼?

回答

3

stat統計鏈接的目標。使用lstat如果你想告訴一個文件是否是一個符號鏈接:

lstat()是相同的stat(),不同之處在於,如果路徑是一個符號鏈接,則該鏈接本身是STAT-ED,而不是文件,它指的是。