1、可执行程序有一个参数,如果这个参数是一个文件名,我们输出这个文件名的大小,如果是一个目录,(含多级子目录)
的如下信息:
1)该目录中普通文件数量
2)该目录中链接文件数量
3)该目录中子目录数量,每个子目录的目录深度,每个子目录中普通文件
的数量,每个子目录中链接文件的数量
4)每个文件的大小(字节数)
5)将上述结果发送到服务器,服务器接收到数据后,把数据写入一个文本文件
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3333//端口号
#define MAXDATASIZE 100
#define MAX_SIZE 30
#define NAME_SIZE 20
int all_num,all[20];
int deep=-1;
char buff[1000];
struct names//统计文件名称和文件的大小
{
char name[NAME_SIZE];//文件名
long int n_size; //文件的大小
};
struct types//统计文件类型和该类型函数的个数
{
char type[NAME_SIZE];//文件的类型
int t_size;//该文件类型的个数
};
int figure_up(struct names head[],struct types heads[3],char *d)
{
char p[100];//获取绝对路径
char *str;
struct stat statBuf;
DIR *dir;
struct dirent *dirp;
if(!(dir=opendir(d)))//打开异常
{
perror("open fail");
exit(-1);
}
int i;
for(i=0;i<3;i++)
{
heads[i].t_size=0;
}
i=0;
while((dirp=readdir(dir))!=NULL)//读取
{
sprintf(p,"%s/%s",d,dirp->d_name);
lstat(p,&statBuf);
if(dirp->d_name[0]=='.')//隐藏文件忽略
{
continue;
}
lstat(p,&statBuf);
if(S_ISDIR(statBuf.st_mode))//目录文件
{
deep++;
//printf("dir directory file:%s\n",dirp->d_name);
sprintf(head[i].name,"%s",dirp->d_name);
head[i].n_size=statBuf.st_size;
heads[0].t_size++;
}
else if(S_ISREG(statBuf.st_mode))//普通文件
{
//printf("dir regular file:%s\n",dirp->d_name);
sprintf(head[i].name,"%s",dirp->d_name);
head[i].n_size=statBuf.st_size;
heads[1].t_size++;
}
else if(S_ISLNK(statBuf.st_mode))//链接文件
{
//printf("dir link file:%s\n",dirp->d_name);
sprintf(head[i].name,"%s",dirp->d_name);
head[i].n_size=statBuf.st_size;
heads[2].t_size++;
}
i++;
}
deep--;
return deep;
}
void child_figure_up(struct names f_head[],struct names head[][MAX_SIZE],struct types heads[][3],int h_size,char *d)
{
char p[100],str[50];//获取绝对路径
struct stat statBuf;
DIR *dir;
struct dirent *dirp;
int i,j;
for(i=0;i
for(j=0;j<3;j++)
{
heads[i][j].t_size=0;
}
}
for(i=0;i
sprintf(str,"%s/%s",d,f_head[i].name);
lstat(str,&statBuf);
if(!S_ISDIR(statBuf.st_mode))//目录文件
{
continue;
}
else
if(!(dir=opendir(str)))//打开异常
{
perror("open fail");
exit(-1);
}
j=0;
while((dirp=readdir(dir))!=NULL)//读取
{
sprintf(p,"%s/%s",d,dirp->d_name);
lstat(p,&statBuf);
if(dirp->d_name[0]=='.')//隐藏文件忽略
{
continue;
}
lstat(p,&statBuf);
if(S_ISDIR(statBuf.st_mode))//目录文件
{
//printf("dir directory file:%s\n",dirp->d_name);
sprintf(head[i][j].name,"%s",dirp->d_name);
head[i][j].n_size=statBuf.st_size;
heads[i][0].t_size++;
}
else if(S_ISREG(statBuf.st_mode))//普通文件
{
//printf("dir regular file:%s\n",dirp->d_name);
sprintf(head[i][j].name,"%s",dirp->d_name);
head[i][j].n_size=statBuf.st_size;
heads[i][1].t_size++;
}
else if(S_ISLNK(statBuf.st_mode))//链接文件
{
//printf("dir link file:%s\n",dirp->d_name);
sprintf(head[i][j].name,"%s",dirp->d_name);
head[i][j].n_size=statBuf.st_size;
heads[i][2].t_size++;
}
j++;
}
}
}
void head_type(struct types heads[])
{
int i;
sprintf(heads[0].type,"directory file");
sprintf(heads[1].type,"regular file");
sprintf(heads[2].type,"link file");
all[0]=heads[0].t_size;
printf("figure_up\n");
}
void show_head(struct names head[],struct types heads[],int h_size)
{
int i;
for(i=0;i
printf("name:%s\tn_size:%ld\n",head[i].name,head[i].n_size);
}
for(i=0;i<3;i++)
{
printf("type:%s\tt_size:%d\n",heads[i].type,heads[i].t_size);
}
printf("统计成功!\n");
}
void child_head_type(struct types heads[][3],int h_size)
{
int i,j;
for(i=0;i
for(j=0;j<3;j++)
{
if(j==0)
sprintf(heads[i][j].type,"directory file");
else if(j==1)
sprintf(heads[i][j].type,"regular file");
else if(j==2)
sprintf(heads[i][j].type,"link file");
}
}
for(i=0;i
for(j=0;j<3;j++)
{
all[i+1]+=heads[i][j].t_size;
}
}
printf("child_figure_up\n");
}
int show_child_head(struct names child_head[][MAX_SIZE],struct types child_heads[][3],int h_size)
{
int i,j;
printf("统计子目录\n");
for(i=0;i
printf("all:%d\n",all[i+1]);
for(j=0;j
printf("name:%s\tn_size:%ld\n",child_head[i][j].name,child_head[i][j].n_size);
}
}
for(i=0;i
for(j=0;j<3;j++)
{
printf("type:%s\tt_size:%d\n",child_heads[i][j].type,child_heads[i][j].t_size);
}
}
}
int client()//客户端
{
int sockfd;printf("1. 输入的是文件\n");
printf("输入的是目录\n");
/*创建socket*/
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
perror("socket");
exit(1);
}
/*设置sockaddr_in 结构体中相关参数*/
struct sockaddr_in my_addr;
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(MYPORT);
my_addr.sin_addr.s_addr=inet_addr("10.215.15.76");
//bind(sockfd,(struct sockaddr*)(&my_addr),sizeof(struct sockaddr));
int con=connect(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr));
if(con==-1)
{
perror("connect:");
exit(1);
}
printf("sockfd:%d\n",sockfd);
return sockfd;
}
void write_head(struct names head[],struct types heads[],int sockfd,int num)
{
int i;
char str[20];
for(i=0;i
sprintf(str,"%s:%d\n",head[i].name,head[i].n_size);
write(sockfd,str,strlen(str));
}
for(i=0;i<3;i++)
{
sprintf(str,"%s:%d\n",heads[i].type,heads[i].t_size);
write(sockfd,str,strlen(str));
}
}
void write_childhead(struct names child_head[][MAX_SIZE],struct types child_heads[][3],int sockfd)
{
int i,j;
char str[20];
for(i=0;i
for(j=0;j
printf("name:%s\tn_size:%ld\n",child_head[i][j].name,child_head[i][j].n_size);
sprintf(str,"%s:%d\n",child_head[i][j].name,child_head[i][j].n_size);
write(sockfd,str,strlen(str));
}
}
for(i=0;i
for(j=0;j<3;j++)
{ printf("type:%s\tt_size:%d\n",child_heads[i][j].type,child_heads[i][j].t_size);
sprintf(str,"%s:%d\n",child_heads[i][j].type,child_heads[i][j].t_size);
write(sockfd,str,strlen(str));
}
}
}
void file_all(char *file_name,int sockfd)
{
struct stat statBuf;
lstat(file_name,&statBuf);
printf("%d\n",statBuf.st_size);
char str[30];
sprintf(str,"%s:%d",file_name,statBuf.st_size);
write(sockfd,str,strlen(str));
}
int main()
{
int n;
int sockfd=client();
char str[20],file_name[20],path[20];
while(1)
{
printf("1.输入的是文件\n");
printf("2.输入的是目录\n");
printf("3.退出\n");
printf("请选择\n");
scanf("%d",&n);
switch(n)
{
case 1:printf("请输入文件名\n");
scanf("%s",&file_name);
file_all(file_name,sockfd);
break;
case 2:{printf("请输入目录\n");
scanf("%s",&path);
printf("%s\n",path);
struct names head[MAX_SIZE];
struct types heads[3];
int h_size,hdir_num=heads[0].t_size;//目录中子目录的个数
struct names child_head[hdir_num][MAX_SIZE];
struct types child_heads[hdir_num][3];
int deep=figure_up(head,heads,path);
printf("deep:%d\n",deep);
head_type(heads);
h_size=all[0];
show_head(head,heads,h_size);
child_figure_up(head,child_head,child_heads,h_size,path);
child_head_type(child_heads,h_size);
show_child_head(child_head,child_heads,h_size);
sprintf(str,"deep: %d\n",deep);
write(sockfd,str,strlen(str));
write_head(head,heads,sockfd,all[0]);
write_childhead(child_head,child_heads,sockfd);
break;
}
case 3:read(sockfd,str,20);
printf("%s\n",str);
return 0;
default:printf("选择错误,请重新选择\n");
break;
}
}
return 0;
}