嵌入式开发记录-day11 man命令、文件目录 显示文件、文件夹基本信息
1、man命令:
在linux下比较方编程便的写程序,在用到什么函数的时候,可以很方便的查询函数使用方法及其相关的函数;
man 命令总结:
往下翻可以按enter键,或者方向键,按q键退出当前文档;
man 1:一般命令,常见的Linux命令man 1 ls
man 2:linux 内核提供的函数查询,man 2 open
man 3:C库中的函数,man 3 printf
man 4:特殊文件,设备和驱动程序
man 5:文件格式,映射文件
man 6:游戏和屏幕保护程序
man 7:杂类文件
man 8:系统管理命令
基于以上解释,目前在linux下编程,可能用的最多的就是man 2和man 3了;linux内核以及C库函数;
1.1、使用man命令查询完成后,需要查一些字符串、函数名、变量等待
可以输入/(斜杠) 然后输入查询的东西
man 2 open /open // 按回车键
2、显示文件状态 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); // 根据文件路径输出文件信息所包含的结构体stat int fstat(int fd, struct stat *buf); // 根据文件句柄输出文件信息 int lstat(const char *path, struct stat *buf);
3、相关测试
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <unistd.h> 4 #include <stdio.h> 5 #include <fcntl.h> 6 7 int main(int argc,char* argv[]) 8 { 9 struct stat groupstat; 10 int fd,ret; 11 if(argc < 2){ // 对输入参数个数判断 12 printf("\n please input file path\n"); 13 return 0; 14 } 15 ret = stat(argv[1],&groupstat); 16 if(ret != 0){ 17 printf("Plase make sure file path\n"); 18 return 0; 19 } 20 printf("stat function test,%s of st_ino is %ld\n",argv[1],groupstat.st_ino); // 输出传入文件的inode号 21 22 /// fstat 23 fd = open(argv[1],O_RDWR|O_NOCTTY|O_NDELAY); 24 if(fd < 0){ 25 printf("please make sure file path\n"); 26 return 0; 27 } 28 ret = fstat(fd,&groupstat); 29 if(ret != 0){ 30 printf("please make sure file path\n"); 31 return 0; 32 } 33 printf("fstat function test,%s of st_ino is %ld\n",argv[1],groupstat.st_ino); 34 ///////lstat 35 ret = lstat(argv[1],&groupstat); 36 if(ret != 0){ 37 printf("Please make sure file path\n"); 38 return 0; 39 } 40 printf("lstat function test,%s of st_ino is %ld\n",argv[1],groupstat.st_ino); 41 return 0; 42 }
// 运行 目录下有open文件 ./mnt/disk/open /mnt/disk/open // 执行+文件路径 [root@iTOP-4412]# ./mnt/disk/stat /mnt/disk/open stat function test,/mnt/disk/open of st_ino is 26 fstat function test,/mnt/disk/open of st_ino is 26 lstat function test,/mnt/disk/open of st_ino is 26 [root@iTOP-4412]# ls -i /mnt/disk/open // 查看文件的inode号 26 /mnt/disk/open
4、结构体struct stat{}
struct stat { dev_t st_dev; // ID of device containing file 设备ID号 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) 该字段描述此文件(inode)代表的设备。 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 文件所占512byte块的数量 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 文件最后状态修改时间 };

浙公网安备 33010602011771号