//盗墓者是个丑奴儿,原
//博主个人网站 :
//打完一波小广告,进入正题
//头文件引入
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include <dirent.h>
//函数声明
int opend(const char * pathname);
int judge(char * pathname);
char * str(const char * str1,const char * str2);
void readf(const char * pathname);
DIR * dir=NULL;
int in=0; //打开参数目录,记录的目录个数
char * arr[100]={0}; //打开输出参数,对目录路径收集
//参数:pathname,作用:opendir打开pathname。判断:如果是文件,调用judge()统计代码。如果是目录,调用存储进入arr数组
int opend(const char * pathname){
dir=opendir(pathname);
if(dir==NULL)
return -1;
struct dirent * reado=NULL;
while((reado=readdir(dir))!=NULL){
char * tmps=NULL;
if(reado->d_name[0]=='.')
continue;
tmps=str(pathname,reado->d_name);
if(reado->d_type==DT_DIR){
arr[in++]=tmps;
// readf(tmps);
}else{
judge(tmps);
}
if(reado==NULL)
free(tmps);
}
}
int y=0;
pthread_mutex_t s; //互斥锁
void * run(void * arg){ //给创建线程,所绑定的方法
pthread_mutex_lock(&s); //加锁
readf(arr[y++]);
pthread_mutex_unlock(&s); //解锁,因为下面用了join阻塞回收子线程,这里也可有可无
}
//参数:pathname,作用,递归目录,如果发现递归到文件了,调用judge()统计代码。如果是目录,自调
void readf(const char * pathname){
DIR * dir=opendir(pathname);
struct dirent * reado=NULL;
while((reado=readdir(dir))!=NULL){
char * tmps=NULL;
if(reado->d_name[0]=='.')
continue;
tmps=str(pathname,reado->d_name);
if(reado->d_type==DT_DIR){
readf(tmps);
}else{
judge(tmps);
}
if(reado==NULL)
free(tmps);
}
}
//count_num统计字数
//rows统计行数
//统计的.c文件个数
int count_num=0;
int rows=0;
int gs=0;
//参数:pathname(文件路径),作用,过滤出.c文件,统计.c文件的字数,行数
//第一步,先等于"