- 论坛徽章:
- 0
|
这是我的代码
#include <stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<limits.h>
#ifdef PATH_MAX
static int pathmax=PATH_MAX;
#endif
int copyf(FILE *,FILE *);
int copyf_d(char *,char *,char *);
int copyd_d(char *,char *);
int main(int argc,char *argv[])
{
int i;
char *d1;
char *d2;
d1=argv[1];
d2=argv[2];
i=copyd_d(d1,d2);
if(i==0) printf("copy %s to %s success\n",argv[1],argv[2]);
return 0;
}
int copyd_d(char *cpd1,char *cpd2){
static DIR *dp1;
static DIR *dp2;
char *d_path1,*d_path2;
char *path;
path=malloc(pathmax+1);
d_path1=malloc(pathmax+1);
d_path2=malloc(pathmax+1);
char b[200];
static struct dirent *dir;
static struct stat buf;
if((dp1=opendir(cpd1))==NULL)
{
printf("\nCannot open the directory %s strike any key exit!",cpd1);
getchar();
exit(1);
}
if((dp2=opendir(cpd2))==NULL)
{
if(mkdir(cpd2,S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)<0)
printf("\nCannot make the directory %s strike any key exit!",cpd2);
}
while((dir=readdir(dp1))!=NULL)
{
if(strcmp(dir->d_name,".")==0||strcmp(dir->d_name,"..")==0)
continue;
strcpy(path,cpd1);
strcat(path,"/");
strcat(path,dir->d_name);
if(stat(path,&buf)<0)
{ printf("%s stat error\n",path);
exit(1);
}
if(S_ISREG(buf.st_mode))
{
printf("%s is a file.\n",path);
copyf_d(path,dir->d_name,cpd2);
}
else if(S_ISDIR(buf.st_mode))
{
printf("%s is directory\n",path);
strcpy(d_path1,path);
strcpy(d_path2,cpd2);
strcat(d_path2,"/");
strcat(d_path2,dir->d_name);
copyd_d(d_path1,d_path2);
}
}
printf("copyd_d %s to %s is ok\n",d_path1,d_path2);
return 0;
}
int copyf_d(char *pathfile,char *filename,char *dir)
{
FILE *fp1;
FILE *fp2;
char buffer[MAXPATH];
char *cwd;
char *rdir;
rdir=malloc(pathmax+1);
if((fp1=fopen(pathfile,"r"))==NULL)
{
printf("\nCannot open the file1 %s strike any key exit!",pathfile);
getchar();
exit(1);
}
strcpy(rdir,dir);
strcat(rdir,"/");
strcat(rdir,filename);
if((fp2=fopen(rdir,"w+"))==NULL)
{
printf("\nCannot open the file2 %s strike any key exit!",rdir);
getchar();
exit(0);
}
copyf(fp1,fp2);
return 0;
}
int copyf(FILE *f1,FILE *f2)
{
char ch;
ch=fgetc(f1);
while(ch!=EOF)
{
fputc(ch,f2);
ch=fgetc(f1);
}
return 0;
} |
|