判断文件或者目录,打印其详细信息 lstat
2017-11-30 17:26
218 查看
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include <grp.h> #include <pwd.h> #include <dirent.h> //判断文件或者目录,打印其详细信息 //lstat传入一个文件,显示文件详细信息 //-rwxrwxrwx 1 root root 1723 Nov 30 09:49 27_lstat.c //权限 链接数 所有者 用户组 文件容量 修改日期 文件名 //文件所有者权限 文件所属用户组的权限 其他人对此文件的权限 #if 0 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 所有者ID*/ gid_t st_gid; /* group ID of owner 用户组ID*/ 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 修改日期*/ }; #endif #if 0 struct group *getgrgid(gid_t gid); struct group { char *gr_name; /* group name */ char *gr_passwd; /* group password */ gid_t gr_gid; /* group ID */ char **gr_mem; /* group members */ }; #endif #if 0 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 supported by all file system types */ char d_name[256]; /* filename 需要*/ }; #endif void fun_lstat(char *filename); int main(int argc,char *argv[]) { DIR *dp; struct stat st;//文件结构体 char buf[BUFSIZ]; if(lstat(argv[1],&st) < 0)//lstat获取文件或者目录信息 { perror("main lstat failed");//需要加"" exit(1); } //判断确定是文件 if(S_ISREG(st.st_mode)) fun_lstat(argv[1]); //判断确定是目录 else if(S_ISDIR(st.st_mode))// 不是buf.st_mode { dp = opendir(argv[1]); if(dp == NULL) { perror("opendir failed"); exit(1); } struct dirent *dir; dir = readdir(dp); while(dir == NULL) { printf("%s\n",dir->d_name); } } return 0; } void fun_lstat(char *filename) { struct stat buf; if(lstat(filename,&buf) < 0)//lstat获取文件信息 { perror("lstat failed");//需要加"" exit(1); } //打印文件类型 if(S_ISREG(buf.st_mode)) printf("-"); if(S_ISDIR(buf.st_mode)) printf("d"); if(S_ISCHR(buf.st_mode)) printf("c"); if(S_ISBLK(buf.st_mode)) printf("b"); if(S_ISFIFO(buf.st_mode)) printf("p"); if(S_ISLNK(buf.st_mode)) printf("l"); if(S_ISSOCK(buf.st_mode)) printf("s"); //打印文件权限 -rwxrwxrwx 421421421 char str[] = "xwr-"; int i; for(i=8;i>0;i--) { if(buf.st_mode & 1 << i)//左移 printf("%c",str[i%3]); else printf("%c",str[3]); } //打印文件链接数 printf(" %d ",buf.st_nlink); //打印文件所有者 printf("%s ",getpwuid(buf.st_uid)->pw_name);// //打印文件用户组 printf("%s ",getgrgid(buf.st_gid)->gr_name); //打印文件容量 printf("%ld ",buf.st_size); //打印修改日期 struct tm *tp; tp = localtime(&buf.st_ctime);// printf("%d-%d-%d %d:%d:%d\n",tp->tm_year+1900, tp->tm_mon+1, \ tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec); //打印文件名 printf("%s\n",filename); }
相关文章推荐
- 目录操作/打印一个文件或目录的详细信息
- C语言判断文件夹或者文件是否存在以及权限等信息access函数和删除文件或目录 remove()函数使用实例
- shell判断文件,目录是否存在或者具有权限
- [zz] shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- Linux stat函数获取文件属性(文件大小,创建时间等,判断普通文件或者目录等)
- linux shell判断文件,目录是否存在或者具有权限
- php打印当前目录文件信息
- shell判断文件,目录是否存在或者具有权限的代码
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- 如何查看目录下文件的行或者字数等信息
- shell判断文件,目录是否存在或者具有权限
- iOS6.1 & iOS 7 & iOS8 判断沙盒文件或者目录是否存在,以及判断是文件还是目录的一个隐藏问题
- linux+笔记:linux下判断一个文件或者目录是否存在
- shell判断文件,目录是否存在或者具有权限
- [IO]判断文件或者目录是否存在
- 由于文件不可访问,或者内存或磁盘空间不足,所以无法打开数据库 'msdb'。有关详细信息,请参阅 SQL Server 错误日志。 (Microsoft SQL Server,错误: 945)
- shell判断文件,目录是否存在或者具有权限