1 #include <sys/types.h> 2 #include <unistd.h> 3 #include <sys/stat.h> 4 #include <fcntl.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 int main() 8 { 9 10 int fd = open("/bin/about.elf", O_RDONLY); 11 if (fd == -1) 12 return 0; 13 printf("fd = %d\n", fd); 14 struct stat *st = (struct stat *)malloc(sizeof(struct stat)); 15 fstat(fd, st); 16 printf("stat size = %d\n", sizeof(struct stat)); 17 // FIXME 打印数据时内存出错 18 printf("====================\n"); 19 printf("st address: %#018lx\n", st); 20 printf("st_dev = %d\n", (*st).st_dev); 21 printf("st_ino = %d\n", (*st).st_ino); 22 printf("st_mode = %d\n", (*st).st_mode); 23 printf("st_nlink = %d\n", (*st).st_nlink); 24 printf("st_uid = %d\n", (*st).st_uid); 25 printf("st_gid = %d\n", (*st).st_gid); 26 printf("st_rdev = %d\n", (*st).st_rdev); 27 printf("st_size = %d\n", (*st).st_size); 28 printf("st_blksize = %d\n", (*st).st_blksize); 29 printf("st_blocks = %d\n", (*st).st_blocks); 30 printf("st_atim.sec= %d\tst_atim.nsec= %d\n", (*st).st_atim.tv_sec, (*st).st_atim.tv_nsec); 31 printf("st_mtim.sec= %d\tst_mtim.nsec= %d\n", (*st).st_mtim.tv_sec, (*st).st_mtim.tv_nsec); 32 printf("st_ctim.sec= %d\tst_ctim.nsec= %d\n", (*st).st_ctim.tv_sec, (*st).st_ctim.tv_nsec); 33 34 return 0; 35 }