www.pudn.com > os2design4file.rar > mycopy.h


#include
#include
#include
#include
int isdir(char *str)
{
	DIR *dir;
	if((dir=opendir(str))==NULL)
		return 0;
	else
		closedir(dir);
}

int isfile(char *str)
{
	FILE * fp;
	fp=fopen(str,"r+");
	if(fp==NULL) 
		return 0;
	else
	{
		fclose(fp);
		return 1;
	}
}


int copyfile(char*sourcefile,char*targetfile)
{
	FILE *source,*target;
	if((source=fopen(sourcefile,"r+"))==NULL)		
	{
		printf("open source file error\n");
		return 0;
	}
	if((target=fopen(targetfile,"w"))==NULL)			
	{
		printf("open target file error\n");
		return 0;
	}
	char buf[80];
	while(fgets(buf,80,source))				
		fputs(buf,target);
	fclose(target);
	fclose(source);	
	printf("%s\n","copy success");
	return 1;
}

int filetodir(char *source,char *target)
{
	char name[80];
	strcpy(name,source);
	strcpy(name,strcat(name,"/"));
	strcpy(name,strcat(name,target));
	copyfile(source,name);
}

int dirtodir(char *source,char *target)
{
	char *childdir;
	DIR *dp;
	struct dirent *dirp;
	dp = opendir(source);
	while( (dirp = readdir(dp) )!=NULL)
	{
		if(strcmp(".",dirp->d_name)!=0&&strcmp("..",dirp->d_name)!=0)
			filetodir(dirp->d_name,target);
	}
}

void mycopy(char *source,char *target)
{
	if(isfile(source)==1&&isfile(target)==0&&isdir(target)==0)
		copyfile(source,target);
	else if(isfile(source)==1&&isfile(target)==1&&isdir(target)==0)
		copyfile(source,target);
	else if(isfile(source)==1&&isdir(target)==1)
		filetodir(source,target);
	else if(isdir(source)==1&&isdir(target)==1)
		dirtodir(source,target);
	else
		printf("Instruction is wrong!\n");
}