apue学习之文件IO第二部分
學習apue課程已經有一個多月了,有很多知識點需要自己去總結:
下面是主要的幾個函數的介紹:
1-stat()函數是獲得文件的主要信息。在linux當中主要以一個結構體來保存文件的信息,其中每一個字段都代表著文件的一個信息量:
struct stat {dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */mode_t st_mode; /* protection */ /*文件的權限狀態值,是一個數值*/nlink_t st_nlink; /* number of hard links */uid_t st_uid; /* user ID of owner */gid_t st_gid; /* group ID of owner */dev_t st_rdev; /* device ID (if special file) */off_t st_size; /* total size, in bytes */ /* 文件所占的磁盤空間的大小 */blksize_t st_blksize; /* blocksize for file system I/O */blkcnt_t st_blocks; /* number of 512B blocks allocated */time_t st_atime; /* time of last access */time_t st_mtime; /* time of last modification */time_t st_ctime; /* time of last status change */ /* 最后一次修改的狀態*/};
在使用這個結構體的時候我們會用到許多的宏定義,比如st_mode的值要做一些判斷。詳細的介紹請參照man手冊對此函數的定義。
stat()函數:
stat, fstat, lstat - get file status #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf); //主要使用stat()函數
2-opendir
opendir, fdopendir - open a directory#include <sys/types.h> #include <dirent.h>DIR *opendir(const char *name);DIR *fdopendir(int fd);
打開一個目錄,返回一個文件指針,DIR *dir ; 返回的結果記錄著目錄的信息,一般和readdir搭配使用做為其參數。
3-readdir
NAMEreaddir, readdir_r - read a directory //讀取一個目錄SYNOPSIS#include <dirent.h>struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
讀取一個目錄,每次只能讀一個目錄的信息,readdir()參數為opendir返回的文件指針DIR* dir ,返回值為一個結構體struct dirent *指針類型。
這個結構體如下:
主要包括著目錄的信息:
linux當中一切都為文件,那么目錄也是屬于一種文件,所以stat對其也是適用的。
struct dirent {ino_t d_ino; /* inode number */off_t d_off; /* offset to the next dirent */unsigned short d_reclen; /* length of this record */unsigned char d_type; /* type of file; not supportedby all file system types */char d_name[256]; /* filename */};
在stat結構體當中有兩個字段,一個是st_uid , 一個是st_gid 兩個變量都是數值類型,要獲得具體的所屬用戶的名字和所屬組的名字,則需要使用getpwuid()和getgrgid()這兩個函數。
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry#include <sys/types.h>#include <pwd.h>struct passwd *getpwnam(const char *name);struct passwd *getpwuid(uid_t uid);int getpwnam_r(const char *name, struct passwd *pwd,char *buf, size_t buflen, struct passwd **result);int getpwuid_r(uid_t uid, struct passwd *pwd,char *buf, size_t buflen, struct passwd **result);
這組函數和getpwuid()所實現的功能都是類似的。
這里面有一個結構體struct passwd:
struct passwd {char *pw_name; /* username */char *pw_passwd; /* user password */uid_t pw_uid; /* user ID */gid_t pw_gid; /* group ID */char *pw_gecos; /* user information */char *pw_dir; /* home directory */char *pw_shell; /* shell program */};
這樣就可以得到此用戶的所有信息。包括用戶密碼。
還有一個函數和他類似:getgrgid()
NAMEgetgrnam, getgrnam_r, getgrgid, getgrgid_r - get group file entrySYNOPSIS#include <sys/types.h>#include <grp.h>struct group *getgrnam(const char *name);struct group *getgrgid(gid_t gid);int getgrnam_r(const char *name, struct group *grp,char *buf, size_t buflen, struct group **result);int getgrgid_r(gid_t gid, struct group *grp,char *buf, size_t buflen, struct group **result);
返回的是一個struct group
struct group {char *gr_name; /* group name */char *gr_passwd; /* group password */gid_t gr_gid; /* group ID */char **gr_mem; /* group members */};
現在我們來實現一個在linux下面常用的一個功能:
即:ls -l -i?
大家都知道ls 是顯示所有當前目錄下的所有文件的信息,包括目錄。所以我們要打開的目錄就是openddir(".");然后讀取其信息,并對其分解。
1 #include <time.h>2 #include <stdio.h>3 #include <stdlib.h>4 #include <unistd.h>5 #include <sys/types.h>6 #include <sys/stat.h>7 #include <string.h>8 #include <dirent.h>9 #include <pwd.h>10 #include <grp.h>11 12 void get_type( int );13 void get_mode( int );14 15 int main()16 {17 DIR * dir ;18 dir = opendir(".");19 if( dir == NULL )20 {21 perror("opendir");22 exit(1);23 }24 struct dirent *cur ;25 while( ( cur = readdir( dir ) ) != NULL )26 {27 struct stat st ;28 /* get states of file from d_name */29 int ret = stat( cur->d_name , &st );30 if( ret == -1 )31 {32 perror("stat");33 exit(1);34 }35 printf("%ld " , st.st_ino );36 get_type( st.st_mode );37 get_mode( st.st_mode );38 /* getpwuid getgrgid*/39 /* get namenam and group name through the st_uid and st_gid */40 struct passwd * pwd = getpwuid( st.st_uid );41 printf("%s " , pwd->pw_name );42 // printf("%s " , pwd->pw_passwd );43 // printf("%s " , pwd->pw_gecos );44 struct group *grp = getgrgid( st.st_gid );45 printf("%s " , grp->gr_name );46 printf("%ld\t" , st.st_size );47 /* ctime */48 char *time = ctime( &st.st_ctime );49 printf("%.12s " , time+4 );50 printf("%s " , cur->d_name );51 printf("\n");52 }53 return 0;54 }55 56 /* get the type of current files */57 void get_type( int m )58 {59 if( S_ISREG(m))60 printf("-");61 else if( S_ISDIR(m))62 printf("d");63 else if( S_ISCHR(m))64 printf("c");65 else if( S_ISBLK(m))66 printf("b");67 else if( S_ISFIFO(m))68 printf("p");69 else if( S_ISLNK(m))70 printf("l");71 else //if( S_ISSOCK(m))72 printf("s");73 }74 75 /* get the modition of current files */76 void get_mode( int m )77 {78 char str[10];79 memset( str , '-' , 10 ); // littal bug80 str[9] = '\0';81 /* onwer user's mode */82 if( ( m&S_IRUSR ) == S_IRUSR )83 str[0] = 'r';84 if( ( m&S_IWUSR ) == S_IWUSR )85 str[1] = 'w';86 if( ( m&S_IXUSR ) == S_IXUSR )87 str[2] = 'x';88 /* group user's mode */89 if( ( m&S_IRGRP) == S_IRGRP )90 str[3] = 'r';91 if( ( m&S_IWGRP) == S_IWGRP )92 str[4] = 'w';93 if( ( m&S_IXGRP) == S_IXGRP )94 str[5] = 'x';95 /* other user's mode */96 97 if( (m&S_IROTH) == S_IROTH )98 str[6] = 'r';99 if( (m&S_IWOTH) == S_IWOTH ) 100 str[7] = 'w'; 101 if( (m&S_IXOTH) == S_IXOTH ) 102 str[8] = 'x'; 103 printf("%s " , str ); 104 } 105
以上代碼我在使用memset()的時候第二個參數報錯了,后來找了半天的原因,原來是因為使用‘-’這個字符覆蓋數組的時候我弄成了“-”這是字符串,對應的ASCII的值是不一樣的。這個例子的關鍵是如何實現權限的打印。通過stat結構體的中的st_mode 并對其判斷。
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/jamesduan/archive/2013/01/26/2877899.html
總結
以上是生活随笔為你收集整理的apue学习之文件IO第二部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 沉默的誓言是什么歌呢?
- 下一篇: Web开发的那点事--数据持久层常用功能