XCOPY.C
资源名称:C.rar [点击查看]
上传用户:qq5388545
上传日期:2022-07-04
资源大小:29849k
文件大小:2k
源码类别:

界面编程

开发平台:

C/C++

  1. #include <stdio.h>
  2. #include <alloc.h>
  3. #include <dir.h>
  4. #include <string.h>
  5. #include <io.h>
  6. #include <conio.h>
  7. #include <process.h>
  8. #include <ctype.h>
  9. void copy(void);
  10. void copyaction(void);
  11. int bufcopy(char fromname[13],char toname[MAXPATH]);
  12. char searchstr[MAXPATH],newpath[MAXPATH],newname[MAXPATH],
  13.      drive[MAXPATH],subdir[MAXPATH],pathname[MAXPATH],
  14.      file[MAXFILE],ext[MAXEXT],oldname[MAXPATH];
  15. int done,problem,answer,count;
  16. unsigned char *buffer;
  17. struct ffblk dta;
  18. main(int argc,char *argv[])
  19. {
  20.  buffer=(unsigned char *) calloc(32767,1);
  21.  if(buffer==NULL)
  22. {
  23. printf("Not enough memory to run this utility get rid of");
  24. printf("nmemory resident programs and try again.n");
  25. exit(1);
  26. }
  27.  if(argc>2)
  28. {
  29. strcpy(searchstr,argv[1]);
  30. fnsplit(searchstr,drive,subdir,file,ext);
  31. sprintf(pathname,"%s%s",drive,subdir);
  32. strcpy(newpath,argv[2]);
  33. count=strlen(newpath);
  34. if(newpath[count-1]!=92)
  35. {
  36. newpath[count]=92;
  37. newpath[count+1]=0;
  38. }
  39. }
  40.  else
  41. {
  42. printf("usage: vcopy [filespec topath]n");
  43. free(buffer);
  44. exit(2);
  45. }
  46.  copy();
  47.  free(buffer);
  48.  return(0);
  49. }
  50. void copy(void)
  51. {
  52.  count=0;
  53.  done=findfirst(searchstr,&dta,47);
  54.  while(!done)
  55. {
  56. strcpy(oldname,pathname);
  57. strcat(oldname,dta.ff_name);
  58. strupr(oldname);
  59. strcpy(newname,newpath);
  60. strcat(newname,dta.ff_name);
  61. strupr(newname);
  62. copyaction();
  63. done=findnext(&dta);
  64. }
  65.  if(count>0) printf("nNumber of files copied %3dn",count);
  66.  else printf("nNo files copiedn");
  67. }
  68. void copyaction(void)
  69. {
  70.  printf("copy %-12s ",oldname);
  71.  problem=bufcopy(oldname,newname);
  72.  if(!problem)
  73. {
  74. printf("-copied to %s.n",newname);
  75. count++;
  76. }
  77.  else  printf("-not copied .ERROR!n");
  78. }
  79. int bufcopy(char fromname[MAXPATH],char toname[MAXPATH])
  80. {
  81.  long size,numread,filepos;
  82.  int numtoget;
  83.  FILE *fromfile,*tofile;
  84.  int inhandle,outhandle;
  85.  fromfile=fopen(fromname,"r");
  86.  tofile=fopen(toname,"w");
  87.  if(tofile==NULL||fromfile==NULL)
  88.  return(1);
  89.  inhandle=fileno(fromfile);
  90.  outhandle=fileno(tofile);
  91.  size=filelength(inhandle);
  92.  if(size<=32767) numtoget=size;
  93.  else numtoget=32767;
  94.  numread=0;
  95.  do
  96. {
  97. filepos=ftell(fromfile);
  98. fseek(fromfile,numread,0);
  99. _read(inhandle,buffer,numtoget);
  100. numread+=numtoget;
  101. fseek(tofile,filepos,0);
  102. _write(outhandle,buffer,numtoget);
  103. if(numread+numtoget>size) numtoget=size-numread;
  104. }while(numtoget!=0);
  105.  fclose(fromfile);
  106.  fclose(tofile);
  107.  return(0);
  108. }