lstat函数检查文件有关的信息
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <pthread.h> #include <sys/stat.h> #include <time.h> void PTS(time_t t) { struct tm* _tm=localtime(&t); printf("%04d-%02d-%02d ",_tm->tm_year+1900,_tm->tm_mon+1,_tm->tm_mday); printf("%02d:%02d:%02d\n",_tm->tm_hour,_tm->tm_min,_tm->tm_sec); } int main(int argc,char* argv[]) { struct stat buf; if(argc!=2) { printf("usage %s filename\n",argv[0]); return 1; } if(access(argv[1],F_OK)!=0) { printf("'%s' doesn't exist,quit.\n",argv[1]); return 2; } if(lstat(argv[1],&buf)<0) { printf("function 'lstat' error.\n"); return 3; } if(S_ISREG(buf.st_mode)) printf("Regular.\n"); if(S_ISDIR(buf.st_mode)) printf("Directory.\n"); if(S_ISCHR(buf.st_mode)) printf("Character special.\n"); if(S_ISBLK(buf.st_mode)) printf("Block special.\n"); if(S_ISFIFO(buf.st_mode)) printf("FIFO.\n"); if(S_ISLNK(buf.st_mode)) printf("Symbolic link.\n"); if(S_ISSOCK(buf.st_mode)) printf("Socket.\n"); //////// printf("i-node number: %ul\n",buf.st_ino); printf("device number: %ul\n",buf.st_dev); printf("number of links: %d\n",buf.st_nlink); printf("UID=%d, GID=%d\n",buf.st_uid,buf.st_gid); if(S_ISREG(buf.st_mode)||S_ISDIR(buf.st_mode)||S_ISLNK(buf.st_mode)) printf("file size: %ld bytes\n",buf.st_size); printf("last access time: "); PTS(buf.st_atime); printf("last modify time: "); PTS(buf.st_mtime); printf("last file status change time: "); PTS(buf.st_ctime); return 0; }