-
函数:
Linux – stat, fstat, lstat, fstatat函数
Linux – asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r函数 -
效果:
-
file_mode.h文件
#ifndef __FILE_MODE_H #define __FILE_MODE_H void stat_mode(const char *pathname); #endif
-
file_mode.c文件
#include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> #include "../include/file_mode.h" #include <stdio.h> #include <pwd.h> #include <grp.h> #include <time.h> /** * 函数名:stat_mode * 功能: 显示文件的类型和权限信息 * 参数: const char *pathname -- 文件标识 * 返回: 无 */ void stat_mode(const char *pathname){ struct stat sb; struct passwd *getuser=NULL; struct group *getgroup=NULL; struct tm *status_tm=NULL; char rwx[]={'x','w','r'}; char buf[1024]; ssize_t len; if(-1==lstat(pathname, &sb)){ perror("lstat"); exit(EXIT_FAILURE); } switch(sb.st_mode&S_IFMT){ /*判断文件类型*/ case S_IFSOCK : fprintf(stderr , "%c" , 's'); break; /*套接字文件*/ case S_IFLNK : fprintf(stderr , "%c" , 'l'); break; /*符号链接文件*/ case S_IFREG : fprintf(stderr , "%c" , '-'); break; /*普通文件*/ case S_IFDIR : fprintf(stderr , "%c" , 'd'); break; /*目录文件*/ case S_IFCHR : fprintf(stderr , "%c" , 'c'); break; /*字符设备文件*/ case S_IFIFO : fprintf(stderr , "%c" , 'p'); break; /*管道文件*/ case S_IFBLK : fprintf(stderr , "%c" , 'b'); break; /*块设备文件*/ default : fprintf(stderr , "%s" , "unknown?"); /*未知文件*/ } for(int i=8 ; i>=0 ; i--){ /*文件权限*/ if(sb.st_mode&0x1<<i) fprintf(stderr,"%c",rwx[i%3]); else fprintf(stderr,"%c",'-'); } fprintf(stderr," %2lu",sb.st_nlink); /*硬链接数*/ if(NULL==(getuser=getpwuid(sb.st_uid))){ perror("getpwuid"); exit(EXIT_FAILURE); } fprintf(stderr," %s",getuser->pw_name); /*用户名称*/ if(NULL==(getgroup=getgrgid(sb.st_gid))){ perror("getgrgid"); exit(EXIT_FAILURE); } fprintf(stderr," %s",getgroup->gr_name);/*组名称*/ fprintf(stderr," %5ld",sb.st_size); /*文件的大小*/ //status_tm=localtime(&sb.st_ctime) -- 另一种方式 if(NULL==(status_tm=localtime(&sb.st_ctim.tv_sec))){ /*获取状态改变的时间*/ perror("localtime"); exit(EXIT_FAILURE); } fprintf(stderr,"%2d月 %2d %02d:%02d %s", status_tm->tm_mon+1 , status_tm->tm_mday , status_tm->tm_hour , status_tm->tm_min , pathname ); /* 月份 (0-11) */ if ((len = readlink(pathname, buf, sizeof(buf)-1)) != -1){ /*读取符号链接的内容*/ buf[len] = '\0'; printf(" -> %s",buf); } putchar('\n'); }
-
user_main.c文件
#include <stdio.h> #include "../include/file_mode.h" #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> #include <string.h> int main(int argc,char *argv[]) { struct stat sb; DIR *dirp=NULL; struct dirent *dir=NULL; char pathname[1024]; if(argc == 2 && !strcmp(argv[1],"-l")) /*判断命令参数*/ { strcpy(pathname , "."); } else if(argc != 3){ printf("Usage : %s -l [文件]\n", argv[0]); exit(EXIT_FAILURE); } else { strcpy(pathname,argv[2]); } if(-1==lstat(pathname, &sb)){ perror("lstat"); exit(EXIT_FAILURE); } if(S_ISDIR(sb.st_mode)){ /*判断是否是目录文件*/ if(NULL==(dirp=opendir(pathname))){ perror("opendir"); exit(EXIT_FAILURE); } if(-1==chdir(pathname)) /*改变当前目录已让lstat读取到目录下的文件*/ { perror("chdir"); exit(EXIT_FAILURE); } while(NULL!=(dir=readdir(dirp))){ if(*(dir->d_name)!='.') /*不显示.和..目录文件和隐藏文件*/ stat_mode(dir->d_name); /*不是目录文件*/ } } else{ stat_mode(pathname); /*不是目录文件*/ } return 0; }