home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol10n19.zip / BIGCOPY.C next >
Text File  |  1991-10-15  |  5KB  |  197 lines

  1. // bigcopy.c RHS 3/29/90
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<ctype.h>
  6. #include<dos.h>
  7. #include<malloc.h>
  8. #include<string.h>
  9. #include<conio.h>
  10. #include<io.h>
  11.  
  12. #define MAXBUFSIZE  32767
  13. #define MAXFNAME    80
  14. #define NUMELEMENTS(item)   (sizeof(item)/sizeof(item[0]))
  15.  
  16. char infname[MAXFNAME];
  17. char drive[2];
  18. char curdrive;
  19.   
  20. long length;
  21.  
  22. struct _disks
  23.     {
  24.     long inbytes;
  25.     char *name;
  26.     } disks[4] =
  27.         {
  28.         {  362496L, "360k" },
  29.         {  730112L, "720k" },
  30.         { 1213952L, "1.22meg" },
  31.         { 1457664L, "1.44meg" }
  32.         };
  33.  
  34. char *bigcopy = "BIGCOPY.";
  35. char bigcopyfname[14];
  36. int bigcopyfileno = 0;
  37.  
  38. long diskfreespace(char drive);
  39.  
  40.  
  41. void main(int argc, char **argv)
  42.     {
  43.     FILE *hin, *hout;
  44.     long bytesfree, totread, bytestoread, bytesremain, chunksize = 0L, temp;
  45.     int i,numdisks;
  46.     char *bufptr;
  47.     unsigned bytesread;
  48.  
  49.     if(argc < 3 || argc > 4)
  50.         {
  51.         printf("Usage: BIGCOPY <filename> <drive> [chunksize]\n");
  52.         exit(1);
  53.         }
  54.     strcpy(infname,strupr(argv[1]));
  55.  
  56. #if defined(__BORLANDC__)
  57.     curdrive = getdisk();
  58.     curdrive++;
  59. #else
  60.     _dos_getdrive((unsigned *)&curdrive);
  61. #endif
  62.  
  63.     drive[0] = *(argv[2]);
  64.     drive[0] = (char)toupper(drive[0]);
  65.     drive[1] = (char)(drive[0]-'A'+1);
  66.  
  67.     if(argc == 4)
  68.         chunksize = atol(argv[3]);
  69.  
  70.     if(!(hin = fopen(infname,"rb")))
  71.         {
  72.         printf("Unable to open %s\n",infname);
  73.         exit(2);
  74.         }
  75.  
  76.     if(!(bufptr = malloc(MAXBUFSIZE)))
  77.         {
  78.         printf("Unable allocate copy buffer\n");
  79.         exit(3);
  80.         }
  81.  
  82.     length = filelength(fileno(hin));
  83.     bytesfree = diskfreespace(drive[1]);
  84.  
  85.     if(bytesfree < length)
  86.         {
  87.         printf("%s is %ld bytes long...",infname,length);
  88.         printf("Copying it will require:\n");
  89.         printf(
  90.             "Disks   Disk Size\n"
  91.             "=========================\n");
  92.  
  93.         for(i = 0; i < NUMELEMENTS(disks); i++)
  94.             {
  95.             if(disks[i].inbytes > length)
  96.                 numdisks = 1;
  97.             else
  98.                 {
  99.                 unsigned remain;
  100.  
  101.                 numdisks = (int)(length / disks[i].inbytes);
  102.                 if(numdisks == 0)
  103.                     numdisks++;
  104.                 if((remain = (unsigned)(length % disks[i].inbytes)) > 0)
  105.                     numdisks++;
  106.                 }
  107.             printf("%7s %5d disk%s\n",disks[i].name,numdisks,
  108.                 (numdisks > 1 ? "s" : ""));
  109.             }
  110.         }
  111.  
  112.     for(bytesremain = length; bytesremain != 0L; )
  113.         {
  114.         if(drive[1] != curdrive)
  115.             {
  116.             printf("Insert disk %d in drive %c\n",bigcopyfileno,drive[0]);
  117.             printf("Press any key to begin copying, or ^C to exit...\n");
  118.             if(getch() == 3)
  119.                 exit(0);
  120.             }
  121.  
  122.         bytesfree = diskfreespace(drive[1]);
  123.  
  124.             // bytes to read should be lesser of free space or unread bytes
  125.         bytestoread = (bytesfree < bytesremain ? bytesfree : bytesremain);
  126.             // bytes to read should be chunksize if chunksize is smaller
  127.         if(chunksize > 0L && chunksize < bytestoread)
  128.             bytestoread = chunksize;
  129.  
  130.         sprintf(bigcopyfname,"%c:%s%03d",drive[0],bigcopy,bigcopyfileno++);
  131.         printf("\tCopying %ld bytes from %s to %s",bytestoread,
  132.             infname,bigcopyfname);
  133.  
  134.         if(!(hout = fopen(bigcopyfname,"wb")))
  135.             {
  136.             printf("\nUnable to open output file: %s\n",bigcopyfname);
  137.             exit(5);
  138.             }
  139.  
  140.         for(totread = 0L; totread < bytestoread; totread += bytesread )
  141.             {
  142.             temp = MAXBUFSIZE;
  143.             bytesread = (bytestoread-totread > temp
  144.                 ? MAXBUFSIZE : (unsigned)(bytestoread-totread));
  145.             
  146.             if((bytesread = fread(bufptr,sizeof(char),bytesread,hin)) == 0)
  147.                 break;
  148.  
  149.             if((fwrite(bufptr,sizeof(char),bytesread,hout)) != bytesread)
  150.                 {
  151.                 printf("\nError writing %s",bigcopyfname);
  152.                 exit(6);
  153.                 }
  154.             printf(".");
  155.             }
  156.         printf("\n");
  157.         bytesremain -= totread;
  158.         fclose(hout);
  159.         }
  160.     fclose(hin);
  161.     free(bufptr);
  162.     exit(0);
  163.     }
  164.  
  165.  
  166. long diskfreespace(char drive)
  167.     {
  168.     long bfree;
  169. #if defined(__BORLANDC__)
  170.     struct dfree diskfree;
  171.  
  172.     getdfree(drive, &diskfree);
  173.     if(diskfree.df_sclus == 0xffff)
  174.         {
  175.         printf("Invalid drive: %c\n",drive-1+'A');
  176.         exit(4);
  177.         }
  178.  
  179.     bfree = (long)diskfree.df_avail;
  180.     bfree *= diskfree.df_sclus;
  181.     bfree *= diskfree.df_bsec;
  182. #else
  183.     struct diskfree_t diskfree;
  184.  
  185.     if(_dos_getdiskfree(drive,&diskfree))
  186.         {
  187.         printf("Invalid drive: %c\n",drive);
  188.         exit(4);
  189.         }
  190.  
  191.     bfree = (long)diskfree.avail_clusters;
  192.     bfree *= diskfree.sectors_per_cluster;
  193.     bfree *= diskfree.bytes_per_sector;
  194. #endif
  195.     return bfree;
  196.     }
  197.