Linux下编程-----文件与IO(二)stat函数

功能:获取文件元数据

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. #include <unistd.h>  
  4. int stat(const char *path, struct stat *buf);  
  5. int fstat(int fd, struct stat *buf);  
  6. int lstat(const char *path, struct stat *buf);  

stat结构体

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct stat  
  2. {  
  3.     dev_t     st_dev;     /* ID of device containing file */  
  4.     ino_t     st_ino;     /* inode number */  
  5.     mode_t    st_mode;    /* protection */  
  6.     nlink_t   st_nlink;   /* number of hard links */  
  7.     uid_t     st_uid;     /* user ID of owner */  
  8.     gid_t     st_gid;     /* group ID of owner */  
  9.     dev_t     st_rdev;    /* device ID (if special file) */  
  10.     off_t     st_size;    /* total size, in bytes */  
  11.     blksize_t st_blksize; /* blocksize for filesystem I/O */  
  12.     blkcnt_t  st_blocks;  /* number of 512B blocks allocated */  
  13.     time_t    st_atime;   /* time of last access */  
  14.     time_t    st_mtime;   /* time of last modification */  
  15.     time_t    st_ctime;   /* time of last status change */  
  16. };  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //示例   - err_exit函数如前  
  2. // 其实可以通过Linux 系统调用major, minor来替换(如下)  
  3. #define MAJOR(a) (int)((unsigned short)a >> 8)  //主设备号: 获取高8位  
  4. #define MINOR(a) (int)((unsigned short)a & 0xFF)//次设备号: 获取低8位  
  5. bool fileType(const struct stat &fileStat);  
  6. void filePerm(const struct stat &fileStat, char *perm);  
  7.   
  8. int main(int argc, char *argv[])  
  9. {  
  10.     if (argc != 2)  
  11.     {  
  12.         cerr << "Usage: " << argv[0] << " <file-name>" << endl;  
  13.         exit(EXIT_FAILURE);  
  14.     }  
  15.   
  16.     struct stat fileStat;  
  17.     if (lstat(argv[1], &fileStat) == -1)  
  18.         err_exit("stat error");  
  19.   
  20.     cout << "file-name: " << argv[1] << endl;  
  21.     cout << "st_ino = " << fileStat.st_ino << endl;  
  22.     cout << "device major: " << major(fileStat.st_dev)  
  23.          << ", minor: " << minor(fileStat.st_dev) << endl;  
  24.     if (fileType(fileStat))  
  25.     {  
  26.         cout << "----------------------------" << endl;  
  27.         cout << "major: " << MAJOR(fileStat.st_dev)  
  28.              << ", minor: " << MINOR(fileStat.st_rdev) << endl;  
  29. //        cout << "major: " << major(fileStat.st_dev)  
  30. //             << ", minor: " << minor(fileStat.st_rdev) << endl;  
  31.     }  
  32.     //获取文件的权限: 但要注意需要&上07777  
  33.     fprintf(stdout, "file permission: %o", fileStat.st_mode&07777);  
  34.     char perm[11];  
  35.     filePerm(fileStat, perm);  
  36.     cout << ", msg: " << perm << endl;  
  37.   
  38.     cout << "st_nlink = " << fileStat.st_nlink << endl;  
  39.     cout << "st_uid = " << fileStat.st_uid << endl;  
  40.     cout << "st_gid = " << fileStat.st_gid << endl;  
  41.     cout << "st_size = " << fileStat.st_size << endl;  
  42.     cout << "st_blksize = " << fileStat.st_blksize << endl;  
  43.     cout << "st_blocks = " << fileStat.st_blocks << endl;  
  44.     cout << "st_atime = " << fileStat.st_atime << endl;  
  45.     cout << "st_ctime = " << fileStat.st_ctime << endl;  
  46.     cout << "st_mtime = " << fileStat.st_mtime << endl;  
  47. }  
  48.   
  49. bool fileType(const struct stat &fileStat)  
  50. {  
  51.     cout << "file-type: ";  
  52.     switch(fileStat.st_mode & S_IFMT)  
  53.     {  
  54.     case S_IFSOCK:  
  55.         cout << "socket";  
  56.         break;  
  57.     case S_IFLNK:  
  58.         cout << "symbolic link";  
  59.         break;  
  60.     case S_IFREG:  
  61.         cout << "regular file";  
  62.         break;  
  63.     case S_IFBLK:  
  64.         cout << "block device" << endl;  
  65.         return true;  
  66.         break;  
  67.     case S_IFDIR:  
  68.         cout << "directory";  
  69.         break;  
  70.     case S_IFCHR:  
  71.         cout << "character device" << endl;  
  72.         return true;  
  73.         break;  
  74.     case S_IFIFO:  
  75.         cout << "FIFO" << endl;  
  76.         break;  
  77.     default:  
  78.         cout << "unknown?";  
  79.         break;  
  80.     }  
  81.     cout << endl;  
  82.     return false;  
  83. }  
  84.   
  85. void filePerm(const struct stat &fileStat, char *perm)  
  86. {  
  87.     strcpy(perm, "----------");  
  88.     switch(fileStat.st_mode & S_IFMT)  
  89.     {  
  90.     case S_IFSOCK:  
  91.         perm[0] = 's';  
  92.         break;  
  93.     case S_IFLNK:  
  94.         perm[0] = 'l';  
  95.         break;  
  96.     case S_IFREG:  
  97.         perm[0] = '-';  
  98.         break;  
  99.     case S_IFBLK:  
  100.         perm[0] = 'b';  
  101.         break;  
  102.     case S_IFDIR:  
  103.         perm[0] = 'd';  
  104.         break;  
  105.     case S_IFCHR:  
  106.         perm[0] = 'c';  
  107.         break;  
  108.     case S_IFIFO:  
  109.         perm[0] = 'p';  
  110.         break;  
  111.     default:  
  112.         perm[0] = '?';  
  113.         break;  
  114.     }  
  115.   
  116.     if (fileStat.st_mode & S_IRUSR)  
  117.         perm[1] = 'r';  
  118.     if (fileStat.st_mode & S_IWUSR)  
  119.         perm[2] = 'w';  
  120.     if (fileStat.st_mode & S_IXUSR)  
  121.         perm[3] = 'x';  
  122.     if (fileStat.st_mode & S_IRGRP)  
  123.         perm[4] = 'r';  
  124.     if (fileStat.st_mode & S_IWGRP)  
  125.         perm[5] = 'w';  
  126.     if (fileStat.st_mode & S_IXGRP)  
  127.         perm[6] = 'x';  
  128.     if (fileStat.st_mode & S_IROTH)  
  129.         perm[7] = 'r';  
  130.     if (fileStat.st_mode & S_IWOTH)  
  131.         perm[8] = 'w';  
  132.     if (fileStat.st_mode & S_IXOTH)  
  133.         perm[9] = 'x';  
  134. }  


实现ls -l的功能:

【拓展函数】

1.getpwuid 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct passwd *getpwuid(uid_t uid);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //passwd结构体  
  2. struct passwd  
  3. {  
  4.     char   *pw_name;       /* username */  
  5.     char   *pw_passwd;     /* user password */  
  6.     uid_t   pw_uid;        /* user ID */  
  7.     gid_t   pw_gid;        /* group ID */  
  8.     char   *pw_gecos;      /* user information */  
  9.     char   *pw_dir;        /* home directory */  
  10.     char   *pw_shell;      /* shell program */  
  11. };  

2.getgrgid

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct group *getgrgid(gid_t gid);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //group结构体  
  2. struct group  
  3. {  
  4.     char   *gr_name;       /* group name */  
  5.     char   *gr_passwd;     /* group password */  
  6.     gid_t   gr_gid;        /* group ID */  
  7.     char  **gr_mem;        /* group members */  
  8. };  

3. readlink

扫描二维码关注公众号,回复: 889918 查看本文章
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ssize_t readlink(const char *path, char *buf, size_t bufsiz);  

4. localtime

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct tm *localtime(const time_t *timep);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //tm结构体  
  2. struct tm  
  3. {  
  4.     int tm_sec;         /* seconds */  
  5.     int tm_min;         /* minutes */  
  6.     int tm_hour;        /* hours */  
  7.     int tm_mday;        /* day of the month */  
  8.     int tm_mon;         /* month */  
  9.     int tm_year;        /* year */  
  10.     int tm_wday;        /* day of the week */  
  11.     int tm_yday;        /* day in the year */  
  12.     int tm_isdst;       /* daylight saving time */  
  13. };  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //示例: 实现简单的ls -l功能  
  2. #include <iostream>  
  3.   
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6. #include <string.h>  
  7. #include <string.h>  
  8.   
  9. #include <sys/types.h>  
  10. #include <sys/stat.h>  
  11. #include <fcntl.h>  
  12. #include <unistd.h>  
  13. #include <grp.h>  
  14. #include <pwd.h>  
  15. #include <time.h>  
  16.   
  17. using namespace std;  
  18.   
  19. inline void err_exit(std::string message);  
  20. bool isDevice(const struct stat &fileStat);  
  21. bool isLink(const struct stat &fileStat);  
  22. void filePerm(const struct stat &fileStat, char *perm);  
  23.   
  24. int main(int argc, char *argv[])  
  25. {  
  26.     if (argc != 2)  
  27.     {  
  28.         cerr << "Usage: " << argv[0] << " <file-name>" << endl;  
  29.         exit(EXIT_FAILURE);  
  30.     }  
  31.   
  32.     struct stat fileStat;  
  33.     if (lstat(argv[1], &fileStat) == -1)  
  34.         err_exit("lstat error");  
  35.   
  36.     //获取权限  
  37.     char perm[11];  
  38.     filePerm(fileStat, perm);  
  39.     cout << perm << ' ';  
  40.   
  41.     //获取文件链接数  
  42.     cout << fileStat.st_nlink << ' ';  
  43.   
  44.     //获取文件所有者  
  45.     struct passwd *ps = getpwuid(fileStat.st_uid);  
  46.     cout << ps->pw_name << ' ';  
  47.   
  48.     //获取文件所属组  
  49.     struct group *gp = getgrgid(fileStat.st_gid);  
  50.     cout << gp->gr_name << ' ';  
  51.   
  52.     if (isDevice(fileStat))  
  53.         cout << major(fileStat.st_dev) << ", "  
  54.              << minor(fileStat.st_rdev) << ' ';  
  55.     else  
  56.         cout << fileStat.st_size << ' ';  
  57.   
  58.     // 打印最后一次修改时间  
  59.     time_t mtime = fileStat.st_mtime;  
  60.     struct tm *pTime = localtime(&mtime);  
  61.     cout << pTime->tm_mon+1 << "月 " << pTime->tm_mday << ' '  
  62.          << pTime->tm_hour << ':' << pTime->tm_min << ' ';  
  63.   
  64.     // 打印文件名  
  65.     cout << argv[1];  
  66.     if (isLink(fileStat))  
  67.     {  
  68.         cout << " -> ";  
  69.         char name[1024] = {0};  
  70.         if (readlink(argv[1], name, sizeof(name)) == -1)  
  71.             err_exit("readlink error");  
  72.         cout << name;  
  73.     }  
  74.     cout << endl;  
  75.   
  76. }  
  77.   
  78. inline void err_exit(std::string message)  
  79. {  
  80.     perror(message.c_str());  
  81.     exit(EXIT_FAILURE);  
  82. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. bool isDevice(const struct stat &fileStat)  
  2. {  
  3.     switch(fileStat.st_mode & S_IFMT)  
  4.     {  
  5.     case S_IFBLK:  
  6.     case S_IFCHR:  
  7.         return true;  
  8.         break;  
  9.     default:  
  10.         return false;  
  11.         break;  
  12.     }  
  13.     return false;  
  14. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. bool isLink(const struct stat &fileStat)  
  2. {  
  3.     if ((fileStat.st_mode & S_IFMT) == S_IFLNK)  
  4.         return true;  
  5.     return false;  
  6. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void filePerm(const struct stat &fileStat, char *perm)  
  2. {  
  3.     strcpy(perm, "----------");  
  4.     switch(fileStat.st_mode & S_IFMT)  
  5.     {  
  6.     case S_IFSOCK:  
  7.         perm[0] = 's';  
  8.         break;  
  9.     case S_IFLNK:  
  10.         perm[0] = 'l';  
  11.         break;  
  12.     case S_IFREG:  
  13.         perm[0] = '-';  
  14.         break;  
  15.     case S_IFBLK:  
  16.         perm[0] = 'b';  
  17.         break;  
  18.     case S_IFDIR:  
  19.         perm[0] = 'd';  
  20.         break;  
  21.     case S_IFCHR:  
  22.         perm[0] = 'c';  
  23.         break;  
  24.     case S_IFIFO:  
  25.         perm[0] = 'p';  
  26.         break;  
  27.     default:  
  28.         perm[0] = '?';  
  29.         break;  
  30.     }  
  31.   
  32.     if (fileStat.st_mode & S_IRUSR)  
  33.         perm[1] = 'r';  
  34.     if (fileStat.st_mode & S_IWUSR)  
  35.         perm[2] = 'w';  
  36.     if (fileStat.st_mode & S_IXUSR)  
  37.         perm[3] = 'x';  
  38.     if (fileStat.st_mode & S_IRGRP)  
  39.         perm[4] = 'r';  
  40.     if (fileStat.st_mode & S_IWGRP)  
  41.         perm[5] = 'w';  
  42.     if (fileStat.st_mode & S_IXGRP)  
  43.         perm[6] = 'x';  
  44.     if (fileStat.st_mode & S_IROTH)  
  45.         perm[7] = 'r';  
  46.     if (fileStat.st_mode & S_IWOTH)  
  47.         perm[8] = 'w';  
  48.     if (fileStat.st_mode & S_IXOTH)  
  49.         perm[9] = 'x';  
  50. }  
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NK_test/article/details/48215061

功能:获取文件元数据

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. #include <unistd.h>  
  4. int stat(const char *path, struct stat *buf);  
  5. int fstat(int fd, struct stat *buf);  
  6. int lstat(const char *path, struct stat *buf);  

stat结构体

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct stat  
  2. {  
  3.     dev_t     st_dev;     /* ID of device containing file */  
  4.     ino_t     st_ino;     /* inode number */  
  5.     mode_t    st_mode;    /* protection */  
  6.     nlink_t   st_nlink;   /* number of hard links */  
  7.     uid_t     st_uid;     /* user ID of owner */  
  8.     gid_t     st_gid;     /* group ID of owner */  
  9.     dev_t     st_rdev;    /* device ID (if special file) */  
  10.     off_t     st_size;    /* total size, in bytes */  
  11.     blksize_t st_blksize; /* blocksize for filesystem I/O */  
  12.     blkcnt_t  st_blocks;  /* number of 512B blocks allocated */  
  13.     time_t    st_atime;   /* time of last access */  
  14.     time_t    st_mtime;   /* time of last modification */  
  15.     time_t    st_ctime;   /* time of last status change */  
  16. };  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //示例   - err_exit函数如前  
  2. // 其实可以通过Linux 系统调用major, minor来替换(如下)  
  3. #define MAJOR(a) (int)((unsigned short)a >> 8)  //主设备号: 获取高8位  
  4. #define MINOR(a) (int)((unsigned short)a & 0xFF)//次设备号: 获取低8位  
  5. bool fileType(const struct stat &fileStat);  
  6. void filePerm(const struct stat &fileStat, char *perm);  
  7.   
  8. int main(int argc, char *argv[])  
  9. {  
  10.     if (argc != 2)  
  11.     {  
  12.         cerr << "Usage: " << argv[0] << " <file-name>" << endl;  
  13.         exit(EXIT_FAILURE);  
  14.     }  
  15.   
  16.     struct stat fileStat;  
  17.     if (lstat(argv[1], &fileStat) == -1)  
  18.         err_exit("stat error");  
  19.   
  20.     cout << "file-name: " << argv[1] << endl;  
  21.     cout << "st_ino = " << fileStat.st_ino << endl;  
  22.     cout << "device major: " << major(fileStat.st_dev)  
  23.          << ", minor: " << minor(fileStat.st_dev) << endl;  
  24.     if (fileType(fileStat))  
  25.     {  
  26.         cout << "----------------------------" << endl;  
  27.         cout << "major: " << MAJOR(fileStat.st_dev)  
  28.              << ", minor: " << MINOR(fileStat.st_rdev) << endl;  
  29. //        cout << "major: " << major(fileStat.st_dev)  
  30. //             << ", minor: " << minor(fileStat.st_rdev) << endl;  
  31.     }  
  32.     //获取文件的权限: 但要注意需要&上07777  
  33.     fprintf(stdout, "file permission: %o", fileStat.st_mode&07777);  
  34.     char perm[11];  
  35.     filePerm(fileStat, perm);  
  36.     cout << ", msg: " << perm << endl;  
  37.   
  38.     cout << "st_nlink = " << fileStat.st_nlink << endl;  
  39.     cout << "st_uid = " << fileStat.st_uid << endl;  
  40.     cout << "st_gid = " << fileStat.st_gid << endl;  
  41.     cout << "st_size = " << fileStat.st_size << endl;  
  42.     cout << "st_blksize = " << fileStat.st_blksize << endl;  
  43.     cout << "st_blocks = " << fileStat.st_blocks << endl;  
  44.     cout << "st_atime = " << fileStat.st_atime << endl;  
  45.     cout << "st_ctime = " << fileStat.st_ctime << endl;  
  46.     cout << "st_mtime = " << fileStat.st_mtime << endl;  
  47. }  
  48.   
  49. bool fileType(const struct stat &fileStat)  
  50. {  
  51.     cout << "file-type: ";  
  52.     switch(fileStat.st_mode & S_IFMT)  
  53.     {  
  54.     case S_IFSOCK:  
  55.         cout << "socket";  
  56.         break;  
  57.     case S_IFLNK:  
  58.         cout << "symbolic link";  
  59.         break;  
  60.     case S_IFREG:  
  61.         cout << "regular file";  
  62.         break;  
  63.     case S_IFBLK:  
  64.         cout << "block device" << endl;  
  65.         return true;  
  66.         break;  
  67.     case S_IFDIR:  
  68.         cout << "directory";  
  69.         break;  
  70.     case S_IFCHR:  
  71.         cout << "character device" << endl;  
  72.         return true;  
  73.         break;  
  74.     case S_IFIFO:  
  75.         cout << "FIFO" << endl;  
  76.         break;  
  77.     default:  
  78.         cout << "unknown?";  
  79.         break;  
  80.     }  
  81.     cout << endl;  
  82.     return false;  
  83. }  
  84.   
  85. void filePerm(const struct stat &fileStat, char *perm)  
  86. {  
  87.     strcpy(perm, "----------");  
  88.     switch(fileStat.st_mode & S_IFMT)  
  89.     {  
  90.     case S_IFSOCK:  
  91.         perm[0] = 's';  
  92.         break;  
  93.     case S_IFLNK:  
  94.         perm[0] = 'l';  
  95.         break;  
  96.     case S_IFREG:  
  97.         perm[0] = '-';  
  98.         break;  
  99.     case S_IFBLK:  
  100.         perm[0] = 'b';  
  101.         break;  
  102.     case S_IFDIR:  
  103.         perm[0] = 'd';  
  104.         break;  
  105.     case S_IFCHR:  
  106.         perm[0] = 'c';  
  107.         break;  
  108.     case S_IFIFO:  
  109.         perm[0] = 'p';  
  110.         break;  
  111.     default:  
  112.         perm[0] = '?';  
  113.         break;  
  114.     }  
  115.   
  116.     if (fileStat.st_mode & S_IRUSR)  
  117.         perm[1] = 'r';  
  118.     if (fileStat.st_mode & S_IWUSR)  
  119.         perm[2] = 'w';  
  120.     if (fileStat.st_mode & S_IXUSR)  
  121.         perm[3] = 'x';  
  122.     if (fileStat.st_mode & S_IRGRP)  
  123.         perm[4] = 'r';  
  124.     if (fileStat.st_mode & S_IWGRP)  
  125.         perm[5] = 'w';  
  126.     if (fileStat.st_mode & S_IXGRP)  
  127.         perm[6] = 'x';  
  128.     if (fileStat.st_mode & S_IROTH)  
  129.         perm[7] = 'r';  
  130.     if (fileStat.st_mode & S_IWOTH)  
  131.         perm[8] = 'w';  
  132.     if (fileStat.st_mode & S_IXOTH)  
  133.         perm[9] = 'x';  
  134. }  


实现ls -l的功能:

【拓展函数】

1.getpwuid 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct passwd *getpwuid(uid_t uid);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //passwd结构体  
  2. struct passwd  
  3. {  
  4.     char   *pw_name;       /* username */  
  5.     char   *pw_passwd;     /* user password */  
  6.     uid_t   pw_uid;        /* user ID */  
  7.     gid_t   pw_gid;        /* group ID */  
  8.     char   *pw_gecos;      /* user information */  
  9.     char   *pw_dir;        /* home directory */  
  10.     char   *pw_shell;      /* shell program */  
  11. };  

2.getgrgid

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct group *getgrgid(gid_t gid);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //group结构体  
  2. struct group  
  3. {  
  4.     char   *gr_name;       /* group name */  
  5.     char   *gr_passwd;     /* group password */  
  6.     gid_t   gr_gid;        /* group ID */  
  7.     char  **gr_mem;        /* group members */  
  8. };  

3. readlink

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ssize_t readlink(const char *path, char *buf, size_t bufsiz);  

4. localtime

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct tm *localtime(const time_t *timep);  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //tm结构体  
  2. struct tm  
  3. {  
  4.     int tm_sec;         /* seconds */  
  5.     int tm_min;         /* minutes */  
  6.     int tm_hour;        /* hours */  
  7.     int tm_mday;        /* day of the month */  
  8.     int tm_mon;         /* month */  
  9.     int tm_year;        /* year */  
  10.     int tm_wday;        /* day of the week */  
  11.     int tm_yday;        /* day in the year */  
  12.     int tm_isdst;       /* daylight saving time */  
  13. };  

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //示例: 实现简单的ls -l功能  
  2. #include <iostream>  
  3.   
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6. #include <string.h>  
  7. #include <string.h>  
  8.   
  9. #include <sys/types.h>  
  10. #include <sys/stat.h>  
  11. #include <fcntl.h>  
  12. #include <unistd.h>  
  13. #include <grp.h>  
  14. #include <pwd.h>  
  15. #include <time.h>  
  16.   
  17. using namespace std;  
  18.   
  19. inline void err_exit(std::string message);  
  20. bool isDevice(const struct stat &fileStat);  
  21. bool isLink(const struct stat &fileStat);  
  22. void filePerm(const struct stat &fileStat, char *perm);  
  23.   
  24. int main(int argc, char *argv[])  
  25. {  
  26.     if (argc != 2)  
  27.     {  
  28.         cerr << "Usage: " << argv[0] << " <file-name>" << endl;  
  29.         exit(EXIT_FAILURE);  
  30.     }  
  31.   
  32.     struct stat fileStat;  
  33.     if (lstat(argv[1], &fileStat) == -1)  
  34.         err_exit("lstat error");  
  35.   
  36.     //获取权限  
  37.     char perm[11];  
  38.     filePerm(fileStat, perm);  
  39.     cout << perm << ' ';  
  40.   
  41.     //获取文件链接数  
  42.     cout << fileStat.st_nlink << ' ';  
  43.   
  44.     //获取文件所有者  
  45.     struct passwd *ps = getpwuid(fileStat.st_uid);  
  46.     cout << ps->pw_name << ' ';  
  47.   
  48.     //获取文件所属组  
  49.     struct group *gp = getgrgid(fileStat.st_gid);  
  50.     cout << gp->gr_name << ' ';  
  51.   
  52.     if (isDevice(fileStat))  
  53.         cout << major(fileStat.st_dev) << ", "  
  54.              << minor(fileStat.st_rdev) << ' ';  
  55.     else  
  56.         cout << fileStat.st_size << ' ';  
  57.   
  58.     // 打印最后一次修改时间  
  59.     time_t mtime = fileStat.st_mtime;  
  60.     struct tm *pTime = localtime(&mtime);  
  61.     cout << pTime->tm_mon+1 << "月 " << pTime->tm_mday << ' '  
  62.          << pTime->tm_hour << ':' << pTime->tm_min << ' ';  
  63.   
  64.     // 打印文件名  
  65.     cout << argv[1];  
  66.     if (isLink(fileStat))  
  67.     {  
  68.         cout << " -> ";  
  69.         char name[1024] = {0};  
  70.         if (readlink(argv[1], name, sizeof(name)) == -1)  
  71.             err_exit("readlink error");  
  72.         cout << name;  
  73.     }  
  74.     cout << endl;  
  75.   
  76. }  
  77.   
  78. inline void err_exit(std::string message)  
  79. {  
  80.     perror(message.c_str());  
  81.     exit(EXIT_FAILURE);  
  82. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. bool isDevice(const struct stat &fileStat)  
  2. {  
  3.     switch(fileStat.st_mode & S_IFMT)  
  4.     {  
  5.     case S_IFBLK:  
  6.     case S_IFCHR:  
  7.         return true;  
  8.         break;  
  9.     default:  
  10.         return false;  
  11.         break;  
  12.     }  
  13.     return false;  
  14. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. bool isLink(const struct stat &fileStat)  
  2. {  
  3.     if ((fileStat.st_mode & S_IFMT) == S_IFLNK)  
  4.         return true;  
  5.     return false;  
  6. }  
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void filePerm(const struct stat &fileStat, char *perm)  
  2. {  
  3.     strcpy(perm, "----------");  
  4.     switch(fileStat.st_mode & S_IFMT)  
  5.     {  
  6.     case S_IFSOCK:  
  7.         perm[0] = 's';  
  8.         break;  
  9.     case S_IFLNK:  
  10.         perm[0] = 'l';  
  11.         break;  
  12.     case S_IFREG:  
  13.         perm[0] = '-';  
  14.         break;  
  15.     case S_IFBLK:  
  16.         perm[0] = 'b';  
  17.         break;  
  18.     case S_IFDIR:  
  19.         perm[0] = 'd';  
  20.         break;  
  21.     case S_IFCHR:  
  22.         perm[0] = 'c';  
  23.         break;  
  24.     case S_IFIFO:  
  25.         perm[0] = 'p';  
  26.         break;  
  27.     default:  
  28.         perm[0] = '?';  
  29.         break;  
  30.     }  
  31.   
  32.     if (fileStat.st_mode & S_IRUSR)  
  33.         perm[1] = 'r';  
  34.     if (fileStat.st_mode & S_IWUSR)  
  35.         perm[2] = 'w';  
  36.     if (fileStat.st_mode & S_IXUSR)  
  37.         perm[3] = 'x';  
  38.     if (fileStat.st_mode & S_IRGRP)  
  39.         perm[4] = 'r';  
  40.     if (fileStat.st_mode & S_IWGRP)  
  41.         perm[5] = 'w';  
  42.     if (fileStat.st_mode & S_IXGRP)  
  43.         perm[6] = 'x';  
  44.     if (fileStat.st_mode & S_IROTH)  
  45.         perm[7] = 'r';  
  46.     if (fileStat.st_mode & S_IWOTH)  
  47.         perm[8] = 'w';  
  48.     if (fileStat.st_mode & S_IXOTH)  
  49.         perm[9] = 'x';  
  50. }  

猜你喜欢

转载自blog.csdn.net/zjy900507/article/details/80309902