home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Utilities / UnRAR / src_amiga / extract.c < prev    next >
Encoding:
Text File  |  1999-11-15  |  8.6 KB  |  340 lines

  1. void ExtrFile(void)
  2. {
  3.   struct FileStat FS;
  4.   char DestFileName[NM],*ChPtr;
  5.   long FileCount=0,TotalFileCount,ErrCount=0;
  6.   int SkipSolid,ExtrFile,MDCode,Size,AllArgsUsed;
  7.   int UserReject,TmpPassword=0,BrokenFile;
  8.   int FirstFile;
  9.  
  10.  
  11. GetNextArchive:
  12.   if (UnpMemory!=NULL)
  13.   {
  14.     free(UnpMemory);
  15.     UnpMemory=NULL;
  16.   }
  17.  
  18.   while (ReadArcName())
  19.   {
  20.  
  21. SkipToFirstVol:
  22.  
  23.     TotalFileCount=ArgUsed=AllArgsUsed=0;
  24.     FirstFile=1;
  25.     if ((ArcPtr=wopen(ArcName,READBINARY,M_DENYWRITE))==NULL)
  26.       continue;
  27.  
  28.     if (TmpPassword!=0)
  29.       TmpPassword=Password[0]=0;
  30.     if (!IsArchive())
  31.     {
  32.       mprintf(MNotRAR,ArcName);
  33.       tclose(ArcPtr);
  34.       continue;
  35.     }
  36.  
  37.     if (MainCommand[0]=='T')
  38.       mprintf(MExtrTest,ArcName);
  39.     else
  40.       mprintf(MExtracting,ArcName);
  41.  
  42.     ViewComment();
  43.  
  44.     if ((UnpMemory=malloc(UNP_MEMORY))==NULL)
  45.       ErrExit(EMEMORY,MEMORY_ERROR);
  46.  
  47.     tseek(ArcPtr,NewMhd.HeadSize-MainHeadSize,SEEK_CUR);
  48.     UnpVolume=0;
  49.  
  50.     while (1)
  51.     {
  52.       Size=ReadBlock(FILE_HEAD | READSUBBLOCK);
  53.       if (Size<=0 && UnpVolume==0)
  54.         break;
  55.       if (BlockHead.HeadType==SUB_HEAD)
  56.       {
  57.         tseek(ArcPtr,NextBlockPos,SEEK_SET);
  58.         continue;
  59.       }
  60.  
  61.       if (AllArgsUsed)
  62.         break;
  63.  
  64.       ConvertPath(ArcFileName,ArcFileName);
  65.  
  66.       ConvertFlags();
  67.  
  68.       if ((NewLhd.Flags & LHD_SPLIT_BEFORE) && SolidType && FirstFile)
  69.       {
  70.         tclose(ArcPtr);
  71.         ChPtr=strrchr(ArcName,'.');
  72.         if (ChPtr==NULL || stricomp(ChPtr,".rar")!=0)
  73.         {
  74.           SetExt(ArcName,"rar");
  75.           if (FileExist(ArcName))
  76.             goto SkipToFirstVol;
  77.           SetExt(ArcName,"exe");
  78.           if (FileExist(ArcName))
  79.             goto SkipToFirstVol;
  80.         }
  81.         mprintf(MNeedFirstVol);
  82.         if (TotalArcCount>1)
  83.           goto GetNextArchive;
  84.         else
  85.           ErrExit(EEMPTY,FATAL_ERROR);
  86.       }
  87.       FirstFile=0;
  88.       if (UnpVolume && Size==0 && !MergeArchive(0))
  89.       {
  90.         ErrCount++;
  91.         ExitCode=WARNING;
  92.         goto GetNextArchive;
  93.       }
  94.       UnpVolume=(NewLhd.Flags & LHD_SPLIT_AFTER);
  95.  
  96.       tseek(ArcPtr,NextBlockPos-NewLhd.PackSize,SEEK_SET);
  97.  
  98.       TestMode=0;
  99.       ExtrFile=0;
  100.       SkipSolid=0;
  101.  
  102.       if (IsProcessFile(COMPARE_PATH) && (NewLhd.Flags & LHD_SPLIT_BEFORE)==0
  103.           || (SkipSolid=SolidType)!=0)
  104.       {
  105.  
  106.         if (NewLhd.Flags & LHD_PASSWORD)
  107.         {
  108.           if (*Password==0)
  109.           {
  110.             if (GetPassword(1)!=1)
  111.               ErrExit(EEMPTY,USER_BREAK);
  112.             TmpPassword=(SolidType) ? 2 : 1;
  113.           }
  114.           else
  115.             if (TmpPassword==1)
  116.             {
  117.               mprintf(MUseCurPsw,ArcFileName);
  118.               Ask(MYesNoAll);
  119.               switch(Choice)
  120.               {
  121.                 case -1:
  122.                   ErrExit(EEMPTY,USER_BREAK);
  123.                 case 2:
  124.                   if (GetPassword(1)!=1)
  125.                     ErrExit(EEMPTY,USER_BREAK);
  126.                   break;
  127.                 case 3:
  128.                   TmpPassword=2;
  129.                   break;
  130.               }
  131.             }
  132.         }
  133.  
  134.         strcpy(DestFileName,ExtrPath);
  135.  
  136.         if (MainCommand[0]=='E')
  137.           strcat(DestFileName,PointToName(ArcFileName));
  138.         else
  139.           strcat(DestFileName,ArcFileName);
  140.  
  141.         ExtrFile=!SkipSolid;
  142.         if ((Opt.FreshFiles || Opt.UpdateFiles) && (MainCommand[0]=='E' || MainCommand[0]=='X'))
  143.         {
  144.           if (GetFileStat(DestFileName,&FS))
  145.           {
  146.             if ( FS.FileTime >= NewLhd.FileTime)
  147.               ExtrFile=0;
  148.           }
  149.           else
  150.             if (Opt.FreshFiles)
  151.               ExtrFile=0;
  152.         }
  153.  
  154.         if (NewLhd.UnpVer<13 || NewLhd.UnpVer>UNP_VER)
  155.         {
  156.           mprintf(MUnknownMeth,ArcFileName);
  157.           ExtrFile=0;
  158.           ErrCount++;
  159.           ExitCode=WARNING;
  160.         }
  161.  
  162.         if (IsLabel(NewLhd.FileAttr))
  163.           continue;
  164.         if (IsDir(NewLhd.FileAttr))
  165.         {
  166.           if (MainCommand[0]=='P' || MainCommand[0]=='E')
  167.             continue;
  168.           if (SkipSolid)
  169.           {
  170.             mprintf(MExtrSkipDir,ArcFileName);
  171.             continue;
  172.           }
  173.           FileCount++;
  174.           if (MainCommand[0]=='T')
  175.           {
  176.             mprintf(MExtrTestDir,ArcFileName);
  177.             continue;
  178.           }
  179.           if ((MDCode=MakeDir(DestFileName,NewLhd.FileAttr))==-1 && errno==ENOENT)
  180.           {
  181.             CreatePath(DestFileName);
  182.             if ((MDCode=MakeDir(DestFileName,NewLhd.FileAttr))==-1 && errno!=EEXIST)
  183.             {
  184.               mprintf(MExtrErrMkDir,ArcFileName);
  185.               ExitCode=WARNING;
  186.             }
  187.           }
  188.           if (MDCode==0)
  189.             mprintf(MCreatDir,ArcFileName);
  190.           continue;
  191.         }
  192.         else
  193.         {
  194.           if (MainCommand[0]=='T' && ExtrFile)
  195.             TestMode=1;
  196.           if (MainCommand[0]=='P' && ExtrFile)
  197.             FilePtr=stdout;
  198.           if ((MainCommand[0]=='E' || MainCommand[0]=='X') && ExtrFile)
  199.           {
  200.             FilePtr=FileCreate(DestFileName,Opt.Overwrite,&UserReject);
  201.             if (FilePtr==NULL)
  202.             {
  203.               if (!UserReject)
  204.               {
  205.                 mprintf(MCannotCreate,DestFileName);
  206.                 ErrCount++;
  207.                 ExitCode=WARNING;
  208.               }
  209.               ExtrFile=0;
  210.               AllArgsUsed=IsAllArgsUsed();
  211.             }
  212.           }
  213.         }
  214.  
  215.         if (!ExtrFile && SolidType)
  216.         {
  217.           SkipSolid=1;
  218.           TestMode=1;
  219.           ExtrFile=1;
  220.         }
  221.         if (ExtrFile)
  222.         {
  223.           if (!SkipSolid)
  224.           {
  225.             if (!TestMode && CheckForDevice(FilePtr))
  226.               ErrExit(EWRITE,WRITE_ERROR);
  227.             FileCount++;
  228.           }
  229.           TotalFileCount++;
  230.           if (SkipSolid)
  231.             mprintf(MExtrSkipFile,ArcFileName);
  232.           else
  233.             switch(MainCommand[0])
  234.             {
  235.               case 'T':
  236.                 mprintf(MExtrTestFile,ArcFileName);
  237.                 break;
  238.               case 'P':
  239.                 mprintf(MExtrPrinting,ArcFileName);
  240.                 CheckWriteSize=0;
  241.                 break;
  242.               case 'X':
  243.               case 'E':
  244.                 mprintf(MExtrFile,DestFileName);
  245.                 break;
  246.             }
  247.           strcpy(CurExtrFile,SkipSolid ? "":DestFileName);
  248.           CurUnpRead=CurUnpWrite=0;
  249.           UnpFileCRC=(ArcFormat==OLD) ? 0 : 0xFFFFFFFFL;
  250.           PackedCRC=0xFFFFFFFFL;
  251.           if ((*Password!=0) && (NewLhd.Flags & LHD_PASSWORD))
  252.             Encryption=NewLhd.UnpVer;
  253.           else
  254.             Encryption=0;
  255.           if (Encryption)
  256.             SetCryptKeys(Password);
  257.           UnpPackedSize=NewLhd.PackSize;
  258.           DestUnpSize=NewLhd.UnpSize;
  259.           RdUnpPtr=ArcPtr;
  260.           WrUnpPtr=FilePtr;
  261.           Suspend=0;
  262.           Repack=0;
  263.           SkipUnpCRC=SkipSolid;
  264.           if (NewLhd.Method==0x30)
  265.             UnstoreFile();
  266.           else
  267.             if (NewLhd.UnpVer<=15)
  268.               tunpack(UnpMemory,TotalFileCount>1 && SolidType,OLD_UNPACK);
  269.             else
  270.               tunpack(UnpMemory,NewLhd.Flags & LHD_SOLID,NEW_UNPACK);
  271.           if (!SkipSolid)
  272.             AllArgsUsed=IsAllArgsUsed();
  273.  
  274.           if (MainCommand[0]=='P')
  275.             CheckWriteSize=1;
  276.           if (ArcPtr!=NULL)
  277.             tseek(ArcPtr,NextBlockPos,SEEK_SET);
  278.           if (!SkipSolid)
  279.             if ((ArcFormat==OLD && UnpFileCRC==NewLhd.FileCRC) || (ArcFormat==NEW && UnpFileCRC==~NewLhd.FileCRC))
  280.             {
  281.               if (MainCommand[0]!='P')
  282.                 mprintf(MOk);
  283.               BrokenFile=0;
  284.             }
  285.             else
  286.             {
  287.               if (NewLhd.Flags & LHD_PASSWORD)
  288.                 mprintf(MEncrBadCRC,ArcFileName);
  289.               else
  290.                 mprintf("\n%-20s - %s",ArcFileName,MCRCFailed);
  291.               ExitCode=CRC_ERROR;
  292.               ErrCount++;
  293.               BrokenFile=1;
  294.             }
  295.           if (TestMode==1)
  296.             TestMode=0;
  297.           else
  298.           {
  299.             if (MainCommand[0]=='X' || MainCommand[0]=='E')
  300.             {
  301.               SetOpenFileStat(FilePtr);
  302.               tclose(FilePtr);
  303.               SetCloseFileStat(DestFileName);
  304.               if (BrokenFile && !Opt.KeepBroken)
  305.                 remove(DestFileName);
  306.             }
  307.           }
  308.           *CurExtrFile=0;
  309.         }
  310.       }
  311.       if (ArcPtr==NULL)
  312.         goto GetNextArchive;
  313.       if (!ExtrFile)
  314.         if (!SolidType)
  315.           tseek(ArcPtr,NextBlockPos,SEEK_SET);
  316.         else
  317.           if (!SkipSolid)
  318.             break;
  319.       if (AllArgsUsed)
  320.         break;
  321.     }
  322.     free(UnpMemory);
  323.     UnpMemory=NULL;
  324.     tclose(ArcPtr);
  325.   }
  326.   if (FileCount==0)
  327.   {
  328.     mprintf(MExtrNoFiles);
  329.     ExitCode=WARNING;
  330.   }
  331.   else
  332.     if (ErrCount==0)
  333.       mprintf(MExtrAllOk);
  334.     else
  335.       mprintf(MExtrTotalErr,ErrCount);
  336.   if (TmpPassword!=0)
  337.     memset(Password,0,sizeof(Password));
  338. }
  339.  
  340.