www.pudn.com > c.rar > FCOPY.C, change:1997-01-26,size:2750b


#include <stdio.h> 
#include <alloc.h> 
#include <dir.h> 
#include <string.h> 
#include <io.h> 
#include <conio.h> 
#include <process.h> 
#include <ctype.h> 
 
 
 
main(int argc,char *argv[]) 
{ 
 
 int copy(char *,char *); 
 char searchstr[MAXPATH],newpath[MAXPATH]; 
  
  
 if(argc>2) 
	{ 
	strcpy(searchstr,argv[1]); 
	strcpy(newpath,argv[2]); 
	} 
 else 
	{ 
	printf("usage: vcopy [filespec topath]\n"); 
	exit(2); 
	} 
 copy(searchstr,newpath); 
 return(0); 
} 
 
 
int copy(char searchstr[],char newpath[]) 
{ 
 int bufcopy(char fromname[13],char toname[MAXPATH]); 
 char newname[MAXPATH], 
     drive[MAXPATH],subdir[MAXPATH],pathname[MAXPATH], 
     file[MAXFILE],ext[MAXEXT],oldname[MAXPATH]; 
 int done,problem,count; 
 
 
 struct ffblk dta; 
 
 
 fnsplit(searchstr,drive,subdir,file,ext); 
 sprintf(pathname,"%s%s",drive,subdir); 
 
  
 if(chdir(newpath)) /*not exist this dir*/ 
	{ 
	if(mkdir(newpath)) 
		{ 
		printf("\nfaile to make newparh\n"); 
		return 0; 
		} 
	else printf("\nsuccess to make newpath\n"); 
	getch(); 
	} 
 else chdir(".."); 
 
 count=strlen(newpath); 
 if(newpath[count-1]!=92) 
	{ 
	newpath[count]=92; 
	newpath[count+1]=0; 
	} 
 
 count=0; 
 done=findfirst(searchstr,&dta,47); 
 
 while(!done) 
	{ 
	strcpy(oldname,pathname); 
	strcat(oldname,dta.ff_name); 
	strupr(oldname); 
 
	strcpy(newname,newpath); 
	strcat(newname,dta.ff_name); 
	strupr(newname); 
 
	/*copyaction();*/ 
 
	printf("copy %-12s ",oldname); 
 
	problem=bufcopy(oldname,newname); 
	if(!problem) 
		{ 
		printf("-copied to %s.\n",newname); 
		count++; 
		} 
	else  printf("-not copied .ERROR!\n"); 
 
	done=findnext(&dta); 
	} 
 
 if(count>0) printf("\nNumber of files copied %3d\n",count); 
 else printf("\nNo files copied\n"); 
 return 1; 
} 
 
int bufcopy(char fromname[MAXPATH],char toname[MAXPATH]) 
{ 
 unsigned char *buffer; 
 long size,numread,filepos; 
 int numtoget; 
 FILE *fromfile,*tofile; 
 int inhandle,outhandle; 
 
 fromfile=fopen(fromname,"r"); 
 tofile=fopen(toname,"w"); 
 if(tofile==NULL||fromfile==NULL) 
	return(1); 
 inhandle=fileno(fromfile); 
 outhandle=fileno(tofile); 
 
 size=filelength(inhandle); 
 
 if(size<=32767) numtoget=size; 
 else numtoget=32767; 
  
 buffer=(unsigned char *) calloc(32767,1); 
 if(buffer==NULL) 
	{ 
	printf("\Not enough memory to run this utility get rid of"); 
	printf("\nmemory resident programs and try again.\n"); 
	return 1; 
	} 
  
 numread=0; 
 
 do 
	{ 
	filepos=ftell(fromfile); 
	fseek(fromfile,numread,0); 
	_read(inhandle,buffer,numtoget); 
 
	numread+=numtoget; 
 
	fseek(tofile,filepos,0); 
	_write(outhandle,buffer,numtoget); 
	if(numread+numtoget>size) numtoget=size-numread; 
	}while(numtoget!=0); 
 fclose(fromfile); 
 fclose(tofile); 
 free(buffer); 
 return(0); 
}