#include "scan.h" //轮循读取文件名 void read_filename(char *dir) { DIR *dfd; struct dirent *dp; char name[MAX_NAME]; if ((dfd = opendir(dir)) == NULL) { printf("dir_order:can't open %s\n %s",dir,strerror(errno)); return ; } while ((dp = readdir(dfd)) != NULL) { if (strncmp(dp->d_name, ".", 1) == 0) continue; if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name))//确定申请的空间够大 { printf("dir_order:name %s %s too long\n",dir,dp->d_name); return ; } else { memset(name,0,sizeof(char)*MAX_NAME); sprintf(name,"%s/%s",dir,dp->d_name); sprintf(file.name,name); if(4 == dp->d_type)//判断文件为目录,则递归查询 { read_filename(name); printf("%s\n",name); } printf("%s\n" ,name); } } closedir(dfd); return ; } //获取文件部分信息 void read_filestat(struct fileinfo file) { struct stat filestat; if(lstat(file.name,&filestat) <= 0) { printf("cannot access the file %s",file.name); return; } //获取文件类型 switch(filestat.st_mode & S_IFMT){ case S_IFDIR : sprintf(file.type,"目录"); break; case S_IFSOCK : sprintf(file.type," 套接字 "); break; case S_IFLNK : sprintf(file.type," 软链接 "); break; case S_IFREG : sprintf(file.type," 文件 "); break; case S_IFCHR : sprintf(file.type," 字符设备 "); break; case S_IFIFO : sprintf(file.type," 管道 "); break; } //获取文件大小 //printf("%8ld\n" ,file.st_size);//打印文件名和文件大小。 file.size = filestat.st_size; //获取文件权限 int i = 8; while(i >= 0) { if((filestat.st_mode)& 1<pw_name); //获取文件属组 sprintf(file.gid,"%s",getgrgid(filestat.st_gid)->gr_name); } #if 0 void read_filemd5(struct fileinfo file) { } #endif #if 0 int main(int argc, const char *argv[]) { char *dir = NULL; char *name = NULL; dir = (char *)malloc(MAX_FILE); printf("请输入要读取的目录\n"); scanf("%s",dir); read_filename(dir); printf("文件名:%s\n",file.name); return 0; } #endif