#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <pthread.h> #include <dirent.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> void sys_err(const char *err,int code) { perror(err); if (errno == EACCES) return; exit(code); } typedef struct thread_arg{ char filename[256]; char ext[32]; }*pthread_arg,thread_arg; typedef struct extension{ char filename[4096]; struct extension *pnext; }*pext,ext; pext phead = NULL; void create_list(pext *phead,char *filename) { if (*phead == NULL) { pext node = (pext)malloc(sizeof(ext)); if (!node) return; strcpy(node->filename,filename); *phead = node; node->pnext = NULL; } else { pext ptr = *phead; while(ptr->pnext != NULL) ptr = ptr->pnext; pext node = (pext)malloc(sizeof(ext)); if (!node) return; strcpy(node->filename,filename); ptr->pnext = node; node->pnext = NULL; } return; } char *get_ext(const char *filename) { char *ptr = strchr(filename,'.'); if (!ptr) return NULL; return ptr; } void *traverse(void *arg) { pthread_arg parg = (pthread_arg)arg; DIR *dp; struct dirent *dirp; if ((dp = opendir(parg->filename)) == NULL) { sys_err("opendir",1); return (void *)NULL; } chdir(parg->filename); while((dirp = readdir(dp)) != NULL) { if (!strcmp(dirp->d_name,".") || !strcmp(dirp->d_name,"..")) continue; char *ptr = get_ext(dirp->d_name); if (ptr != NULL) { if (!strcmp(ptr,parg->ext)) { char temp[4096]; getcwd(temp,4096); //get the full path strcat(temp,dirp->d_name); create_list(&phead,temp); } } if (dirp->d_type == DT_DIR) { strcpy(parg->filename,dirp->d_name); traverse((void *)parg); } } chdir(".."); closedir(dp); return (void *)NULL; } char **split(ssize_t count,ssize_t size,char data[],const char *spl) { char **buff = (char **)calloc(count,sizeof(char*)); if (!buff) return NULL; for (ssize_t i = 0;i<count;i++) buff[i] = (char *)calloc(size,sizeof(char)); char *token = strtok(data,spl); int i = 0; while(token) { strcpy(buff[i],token); token = strtok(NULL,spl); i++; } return buff; } int get_spl_count(char *str) { int i = 0; while(*str != '\0') { if (*str == '|') i++; str++; } if (!i) return -1; return i+1; } char *get_file_content(const char *filepath) { int fd = open(filepath,O_RDONLY); if (fd == -1) sys_err("open",1); long size = lseek(fd,SEEK_SET,SEEK_END); lseek(fd,SEEK_SET,SEEK_SET); if (size == -1) sys_err("lseek",1); char *buff = (char *)calloc(size,sizeof(char)); if (!buff) return NULL; int result = read(fd,buff,size); if (result == -1) return NULL; close(fd); return buff; } void show_list(pext head) { while(head != NULL) { puts(head->filename); // do evil head = head->pnext; } } void destory_list(pext *head) { if (*head == NULL) return; pext p1,p2; p1 = p2 = NULL; p1 = *head; while(p1->pnext != NULL) { p2 = p1->pnext; p1->pnext = p2->pnext; free(p2); if (p2 != NULL) p2 = NULL; } free(*head); if (*head != NULL) *head = NULL; return; } void execute(char *str) { int spl_count = get_spl_count(str); if (spl_count == -1) { printf("file content:.doc|.exe|.jpg|.png\n"); exit(EXIT_FAILURE); } pthread_t *thread = (pthread_t *)calloc(spl_count,sizeof(pthread_t)); char **result = split(spl_count,32,str,"|"); for (ssize_t i = 0;i< spl_count;i++) { thread_arg arg; strcpy(arg.filename,"/"); strcpy(arg.ext,result[i]); pthread_create(thread+i,NULL,(void *)traverse,(void *)&arg); pthread_join(thread[i],NULL); } free(result); } int main(int argc,char **argv) { if (argc == 2) execute(argv[1]); if (argc == 3 && *(*(argv+1)+1) == 'f') { char *buff = get_file_content(argv[2]); if (buff != NULL && strcmp(buff,"")) { execute(buff); free(buff); } } show_list(phead); destory_list(&phead); return 0; }
linux c遍历文件夹
猜你喜欢
转载自blog.csdn.net/qq_31629063/article/details/80433495
今日推荐
周排行