sys/types.h,sys/stat.h與fcntl.h的作用
今天學習了國嵌Linux應用班的視訊,檔案操作。通過Linux系統呼叫(區別於C語言庫函式,系統呼叫依賴於Linux系統,C語言庫函式與作業系統是獨立的)的方式進行檔案操作時,看例子程式用到了這麼幾句 #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h>,找了下它們的作用,備忘。
1. sys/tpyes.h 參考自http://blog.csdn.net/wdy_yx/article/details/8543582
是Unix/Linux系統的基本系統資料型別的標頭檔案,含有size_t,time_t,pid_t等型別。
在應用程式原始檔中包含 <sys/types.h> 以訪問 _LP64 和 _ILP32 的定義。此標頭檔案還包含適當時應使用的多個基本派生型別。尤其是以下型別更為重要:
clock_t 表示系統時間(以時鐘週期為單位)。
dev_t 用於裝置號。
off_t 用於檔案大小和偏移量。
ptrdiff_t 是一種帶符號整型,用於對兩個指標執行減法運算後所得的結果。
size_t 反映記憶體中物件的大小(以位元組為單位)。
ssize_t 供返回位元組計數或錯誤提示的函式使用。
time_t 以秒為單位計時。
所有這些型別在 ILP32 編譯環境中保持為 32 位值,並會在 LP64 編譯環境中增長為 64 位值。
blkcnt_t Used for file block counts.
blksize_t Used for block sizes.
clock_t Used for system times in clock ticks or CLOCKS_PER_SEC; see <time.h> .
clockid_t Used for clock ID type in the clock and timer functions.
dev_t Used for device IDs.
fsblkcnt_t
Used for file system block counts.
fsfilcnt_t
Used for file system file counts.
gid_t Used for group IDs.
id_t Used as a general identifier; can be used to contain at least a pid_t, uid_t, or gid_t.
ino_t Used for file serial numbers.
key_t Used for XSI interprocess communication.
mode_t Used for some file attributes.
nlink_t Used for link counts.
off_t Used for file sizes.
pid_t Used for process IDs and process group IDs.
size_t Used for sizes of objects.
ssize_t Used for a count of bytes or an error indication.
suseconds_t
Used for time in microseconds.
time_t Used for time in seconds.
timer_t Used for timer ID returned by timer_create().
trace_attr_t Used to identify a trace stream attributes object.
trace_event_id_t Used to identify a trace event type.
trace_event_set_t Used to identify a trace event type set.
trace_id_t Used to identify a trace stream.
uid_t Used for user IDs.
useconds_t Used for time in microseconds.
All of the types shall be defined as arithmetic types of an appropriate length, with the following exceptions:
key_t
Additionally:
* mode_t shall be an integer type.
* nlink_t, uid_t, gid_t, and id_t shall be integer types.
* blkcnt_t and off_t shall be signed integer types.
* fsblkcnt_t, fsfilcnt_t, and ino_t shall be defined as unsigned integer types.
* size_t shall be an unsigned integer type.
* blksize_t, pid_t, and ssize_t shall be signed integer types.
* time_t and clock_t shall be integer or real-floating types.
2. sys/stat.h 參考自http://blog.csdn.net/wdy_yx/article/details/8543592
stat.h標頭檔案,輕鬆獲取檔案屬性
以前還為了獲取檔案的長度,費勁從頭讀取一遍,一個一個位元組的算。
做webserver時候,發現原來stat函式可以返回一個結構,裡面包括檔案的全部屬性。
真是曲折啊。】
#i nclude<sys/stat.h>
int stat(const char *restrict pathname,struct stat *restrict buf);
int fstat(int fields,struct stat *buf);
int lstat(const char *restrict pathname,struct stat *restrict buf);
返回值:若成功則返回0,失敗則返回-1
一旦給出pathname,stat函式就返回與此命名檔案有關的資訊結構,fstat函式獲取已在描述符fields上開啟檔案的有關資訊。
lstat函式類似於stat.但是當命名的檔案是一個符號連結時,lstat返回該符號連結的有關資訊,而不是由該符號連結引用檔案
的資訊。第二個引數buf是指標,它指向一個我們必須提供的結構,這些函式填寫由buf指向的結構。該結構的實際定義可能隨實現
有所不同.
struct stat{
mode_t st_mode; //檔案型別和許可權資訊
ino_t st_ino; //i結點標識
dev_t st_dev; //device number (file system)
dev_t st_rdev; //device number for special files
nlink_t st_nlink; //符號連結數
uid_t st_uid; //使用者ID
gid_t st_gid; //組ID
off_t st_size; //size in bytes,for regular files
time_t st_st_atime; //最後一次訪問的時間
time_t st_mtime; //檔案內容最後一次被更改的時間
time_t st_ctime; //檔案結構最後一次被更改的時間
blksize_t st_blksize; //best I/O block size
blkcnt_t st_blocks; //number of disk blocks allocated
};
檔案型別:
普通檔案,目錄檔案,塊特殊檔案,字元特殊檔案,套接字,FIFO,符號連結.
檔案型別資訊包含在stat結構的st_mode成員中,可以用如下的巨集確定檔案型別,這些巨集是stat結構中的st_mode成員.
S_ISREG();S_ISDIR();S_ISCHR();S_ISBLK();S_ISFIFO();S_ISLNK();S_ISSOCK()
示例:
#i nclude<iostream>
int main(int argc,char* argv[])
{
int i;
struct stat buf;
char * ptr;
for(i=1;i<argc;i++)
{
if(lstat(argv[i],&buf)<0)
{
perror(”錯誤原因是:”);
continue;
}
if (S_ISREG(buf.st_mode))
ptr=”普通檔案”;
if (S_ISDIR(buf.st_mode))
ptr=”目錄”;
//……and so on…
cout<<”引數為:”<<argv[i]<<”的標識是一個”<<ptr<<endl;
}
exit(0);
}
<!–
POSIX.1(Portable Operation System Interface)是一組作業系統規範,符合這個規範的作業系統之間行為一致,而且系統呼叫一致。
Unix是AT&T的註冊商標,其他廠商必須付費才可以使用,Solaris是SUN公司的UNIX系統,因為版權問題不能叫Unix
Linux是GNU的作業系統專案,是 一個類Unix作業系統。
–>
POSIX.1允許實現將程序間通訊(IPC)物件(如:訊息佇列和訊號量等)表示為檔案.以下巨集可以用來確定IPC物件的型別.以下巨集與S_ISREG等巨集不同,
它們的引數並非st_mode,而是指向stat結構的指標.
如:S_TYPEISMQ()表示訊息佇列; S_TYPEISSEM()表示訊號量 ; S_TYPEISSHM()表示共享儲存物件.
程序每次開啟,建立或刪除一個檔案時,核心就進行檔案訪問許可權測試,而這種測試可能涉及檔案的所有者(st_uid和st_gid),程序的有效ID(有 效使用者ID或有效組ID)以及程序的附加組ID,兩個所有者ID是檔案的性質,而兩個有效ID和附加組ID則是程序的性質,核心進行的測試是:
(1).若程序的有效使用者ID是0(超級使用者),則允許訪問。
(2).若程序的有效使用者ID等於檔案的有效使用者ID,那麼若所在者適當的訪問許可權被設定,則允許訪問。
(3).若程序的有效組ID或程序的附加組ID之一等於檔案的組ID,那麼組適當的訪問許可權位被設定,則允許訪問。
(4).若其他使用者適當的訪問許可權位被設定,則允許訪問。
按順序執行以上四步。
3, fcntl.h 參考自http://blog.const.net.cn/a/9161.htm
fcntl.h標頭檔案定義了檔案操作等所用到的相關巨集。[喝小酒的網摘]http://blog.const.net.cn/a/9161.htm
如不包含此標頭檔案就會在檔案操作時出現類似下面的錯誤。
error: `O_RDWR' undeclared (first use in this function)
error: `O_APPEND' undeclared (first use in this function)
error: `O_CREAT' undeclared (first use in this function)
/* Specifiy one of these flags to define the access mode. */
#define _O_RDONLY 0
#define _O_WRONLY 1
#define _O_RDWR 2
/* Mask for access mode bits in the _open flags. */
#define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
#define _O_APPEND 0x0008 /* Writes will add to the end of the file. */
#define _O_RANDOM 0x0010
#define _O_SEQUENTIAL 0x0020
#define _O_TEMPORARY 0x0040 /* Make the file dissappear after closing.
* WARNING: Even if not created by _open! */
#define _O_NOINHERIT 0x0080
#define _O_CREAT 0x0100 /* Create the file if it does not exist. */
#define _O_TRUNC 0x0200 /* Truncate the file if it does exist. */
#define _O_EXCL 0x0400 /* Open only if the file does not exist. */
#define _O_SHORT_LIVED 0x1000
/* NOTE: Text is the default even if the given _O_TEXT bit is not on. */
#define _O_TEXT 0x4000 /* CR-LF in file becomes LF in memory. */
#define _O_BINARY 0x8000 /* Input and output is not translated. */
#define _O_RAW _O_BINARY
#if (__MSVCRT_VERSION__ >= 0x0800)
#define _O_WTEXT 0x10000
#define _O_U16TEXT 0x20000
#define _O_U8TEXT 0x40000
#endif
---------------------
作者:ybsun2010
來源:CSDN
原文:https://blog.csdn.net/ybsun2010/article/details/24830441?utm_source=copy
版權宣告:本文為博主原創文章,轉載請附上博文連結!