linux下檔案類型的擷取

來源:互聯網
上載者:User

檔案類型擷取一、stat、fstat 和lstat 函數#include <sys/types.h>
  #include <sys/stat.h>
  int stat(const char * pathname, struct stat * buf);
  int fstat(int filedes,struct stat * buf);
  int lstat(const char * pathname, struct stat *buf);
  三個函數的返回:若成功則為0,若出錯則為-1。
  給定一個p a t h n a m e,s t a t函數返回一個與此命名檔案有關的資訊結構, f s t a t函數獲得已在描述符f i l e d e s上開啟的檔案的有關資訊。l s t a t函數類似於s t a t,但是當命名的檔案是一個符號串連時,l s t a t返回該符號串連的有關資訊,而不是由該符號串連引用的檔案的資訊。第二個參數是個指標,它指向一個我們應提供的結構。這些函數填寫由b u f指向的結構。該結構的實際定義可能隨實現而有所不同,但其基本形式是:
  struct stat {
   unsigned short st_dev;
   unsigned short __pad1;
   unsigned long st_ino;
   unsigned short st_mode;
   unsigned short st_nlink;
   unsigned short st_uid;
   unsigned short st_gid;
   unsigned short st_rdev;
   unsigned short __pad2;
   unsigned long st_size;
   unsigned long st_blksize;
   unsigned long st_blocks;
   unsigned long st_atime;
   unsigned long __unused1;
   unsigned long st_mtime;
   unsigned long __unused2;
   unsigned long st_ctime;
   unsigned long __unused3;
   unsigned long __unused4;
   unsigned long __unused5;
  };
  二、檔案類型LINUX系統的大多數檔案是普通檔案或目錄,但是也有另外一些檔案類型:
  1. 普通檔案(regular file)。這是最常見的檔案類型,這種檔案包含了某種形式的資料。至
  於這種資料是文本還是位元據對於核心而言並無區別。對普通檔案內容的解釋由處理該文
  件的應用程式進行。
  2. 目錄檔案(directory file)。這種檔案包含了其他檔案的名字以及指向與這些檔案有關信
  息的指標。對一個目錄檔案具有讀許可權的任一進程都可以讀該目錄的內容,但只有核心可以
  寫目錄檔案。
  3. 字元特殊檔案(character special file)。這種檔案用於系統中某些類型的裝置。
  4. 塊特殊檔案(block special file)。這種檔案典型地用於磁碟裝置。系統中的所有裝置或者是字元特殊檔案,或者是塊特殊檔案
  5. F I F O。這種檔案用於進程間的通訊,有時也將其稱為具名管道。1 4 . 5節將對其進行說明。
  6. 套介面( s o c k e t )。這種檔案用於進程間的網路通訊。套介面也可用於在一台宿主機上的進程之間的非網路通訊。
  7. 符號串連(symbolic link)。這種檔案指向另一個檔案。4 . 1 6節將更多地述及符號串連。檔案類型資訊包含在s t a t結構的s t _ m o d e成員中。可以用表4 - 1中的宏確定檔案類型。這些宏的參數都是s t a t結構中的s t _ m o d e成員。
  在< s y s / s t a t . h >中的檔案類型宏
  宏
  檔案類型
  S _ I S R E G ( )
  普通檔案
  S _ I S D I R ( )
  目錄檔案
  S _ I S C H R ( )
  字元特殊檔案
  S _ I S B L K ( )
  塊特殊檔案
  S _ I S F I F O ( )
  管道或FIFO
  S _ I S L N K ( )
  符號串連
  S _ I S S O C K ( )
  套結字
  三、例子程式1、代碼程式取其命令列參數,然後針對每一個命令列參數列印其檔案類型。
  FileType.c:
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <stdio.h>
  #include <stdlib.h>
  int main(int argc, char *argv[])
  {
   int i;
   struct stat buf;
   char *ptr;
  
   for(i = 1; i < argc; i++)
   {
   printf("%s: ", argv[i]);
   if(lstat(argv[i], &buf) < 0)
   {
   perror("lstat error");
   continue;
   }
  
   if(S_ISREG(buf.st_mode))
   ptr = "regular";
   else if(S_ISDIR(buf.st_mode))
   ptr = "directory";
   else if (S_ISCHR(buf.st_mode))
   ptr = "character special";
   else if (S_ISBLK(buf.st_mode))
   ptr = "block special";
   else if (S_ISFIFO(buf.st_mode))
   ptr = "fifo";
  #ifdef S_ISLNK
   else if (S_ISLNK(buf.st_mode))
   ptr = "symbolic link";
  #endif
  #ifdef S_ISSOCK
   else if (S_ISSOCK(buf.st_mode))
   ptr = "socket";
  #endif
   else
   ptr = "***unknown mode ***";
  
   printf("%s/n", ptr);
   }
  }
  2、編譯Make FileType
  3、執行./FileType FileType.c /home/
  4、輸出
  FileType.c: regular
  /home/: directory

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.