home *** CD-ROM | disk | FTP | other *** search
/ ftp.rarlab.com / 2014.05.ftp.rarlab.com.tar / ftp.rarlab.com / rar / sunrar.zip / unrar.c < prev   
C/C++ Source or Header  |  1998-07-01  |  6KB  |  296 lines

  1. #include "const.h"
  2. #include "lochead.eng"
  3. #include "loctoup.eng"
  4.  
  5. #include "global.c"
  6. #include "os.c"
  7. #include "rdwrfn.c"
  8. #include "others.c"
  9. #include "smallfn.c"
  10. #include "block.c"
  11. #include "getargs.c"
  12. #include "comment.c"
  13. #include "getopt.c"
  14. #include "extract.c"
  15. #include "somefn.c"
  16. #include "list.c"
  17. #include "compr.c"
  18.  
  19. main(int Argc, char *Argv[])
  20. {
  21.   setbuf(stdout,NULL);
  22.   InitCRC();
  23.   InitSomeVars();
  24.   if (signal(SIGINT,SIG_IGN)!=SIG_IGN)
  25.     signal(SIGINT,ProcessSignal);
  26.   if (signal(SIGTERM,SIG_IGN)!=SIG_IGN)
  27.     signal(SIGTERM,ProcessSignal);
  28. #ifdef SIGBREAK
  29.   if (signal(SIGBREAK,SIG_IGN)!=SIG_IGN)
  30.     signal(SIGBREAK,ProcessSignal);
  31. #endif
  32. #ifdef _WIN_32
  33.   {
  34.     int I;
  35.     if (signal(SIGBREAK,SIG_IGN)!=SIG_IGN)
  36.       signal(SIGBREAK,ProcessSignal);
  37.     SetFileApisToOEM();
  38.     for (I=0;I<Argc;I++)
  39.       CharToOem(Argv[I],Argv[I]);
  40.   }
  41. #endif
  42.  
  43.   SplitCommandLine(Argc,Argv);
  44.   ExecuteCommand();
  45.   ShutDown(SD_MEMORY);
  46.   exit(ExitCode);
  47. }
  48.  
  49.  
  50. int CmpName(char *Wildcard,char *Name,int CmpPath)
  51. {
  52.   int Wildcards;
  53.   char Path1[NM],Name1[NM],*NamePtr1;
  54.   char Path2[NM],Name2[NM],*NamePtr2;
  55.   Wildcards=SplitPath(Wildcard,Path1,Name1,1);
  56.   SplitPath(Name,Path2,Name2,1);
  57.   if (CmpPath==1 && stricomp(Path1,Path2)!=0)
  58.     return(0);
  59.   if (CmpPath==2)
  60.     if (Wildcards || Opt.Recurse)
  61.     {
  62.       if (strnicomp(Path1,Path2,strlen(Path1))!=0)
  63.         return(0);
  64.     }
  65.     else
  66.       if (stricomp(Path1,Path2)!=0)
  67.         return(0);
  68.  
  69.   NamePtr1=Name1;
  70.   NamePtr2=Name2;
  71.  
  72.   while (*NamePtr1)
  73.   {
  74.     if (*NamePtr1=='*')
  75.     {
  76.       if ((NamePtr1=strchr(NamePtr1,'.'))!=NULL)
  77.         NamePtr1++;
  78.       else
  79.         break;
  80.  
  81.       if ((NamePtr2=strchr(NamePtr2,'.'))!=NULL)
  82.       {
  83.         NamePtr2++;
  84.         if (*NamePtr1==0 && *NamePtr2)
  85.           return(0);
  86.       }
  87.       else
  88.         return(*NamePtr1==0 || *NamePtr1=='*');
  89.       continue;
  90.     }
  91.     if (*NamePtr1=='?')
  92.     {
  93.       NamePtr1++;
  94.       if (NamePtr2!=NULL && *NamePtr2)
  95.         NamePtr2++;
  96.       continue;
  97.     }
  98.  
  99.     if (NamePtr2!=NULL && toupper(*NamePtr1)==toupper(*NamePtr2))
  100.     {
  101.       NamePtr1++;
  102.       NamePtr2++;
  103.     }
  104.     else
  105.       return(0);
  106.   }
  107.   if (NamePtr1!=NULL && *NamePtr1==0 && *NamePtr2)
  108.     return(0);
  109.   return(1);
  110. }
  111.  
  112.  
  113. void ErrExit(int ErrCode,int Code)
  114. {
  115.   char ErrMsg[200];
  116.   switch(ErrCode)
  117.   {
  118.     case EEMPTY:
  119.       strcpy(ErrMsg,"");
  120.       break;
  121.     case EWRITE:
  122.       strcpy(ErrMsg,MErrWrite);
  123.       break;
  124.     case EREAD:
  125.       strcpy(ErrMsg,MErrRead);
  126.       break;
  127.     case ESEEK:
  128.       strcpy(ErrMsg,MErrSeek);
  129.       break;
  130.     case EOPEN:
  131.       strcpy(ErrMsg,MErrFOpen);
  132.       break;
  133.     case ECREAT:
  134.       strcpy(ErrMsg,MErrFCreat);
  135.       break;
  136.     case ECLOSE:
  137.       strcpy(ErrMsg,MErrFClose);
  138.       break;
  139.     case EMEMORY:
  140.       strcpy(ErrMsg,MErrOutMem);
  141.       break;
  142.     case EARCH:
  143.       strcpy(ErrMsg,MErrBrokenArc);
  144.       break;
  145.   }
  146.   if (ErrCode!=EEMPTY)
  147.     mprintf("\n%s\n%s\n",ErrMsg,MProgAborted);
  148.   ShutDown(SD_FILES | SD_MEMORY);
  149.   exit(Code);
  150. }
  151.  
  152.  
  153. void ProcessSignal(int SigType)
  154. {
  155.   if (SigType!=SIGTERM)
  156.     mprintf(MBreak);
  157.   ShutDown(SD_FILES | SD_MEMORY);
  158.   exit(USER_BREAK);
  159. }
  160.  
  161.  
  162. void CreatePath(char *fpath)
  163. {
  164.   char *ChPtr;
  165.   ChPtr=fpath;
  166.   while(*ChPtr!=0)
  167.   {
  168.     if (IsPathDiv(*ChPtr))
  169.     {
  170.       *ChPtr=0;
  171.       if (MakeDir(fpath,0777)==0)
  172.         mprintf(MCreatDir,fpath);
  173.       *ChPtr=CPATHDIVIDER;
  174.     }
  175.     ChPtr++;
  176.   }
  177. }
  178.  
  179.  
  180. void NextVolumeName(void)
  181. {
  182.   char *ChPtr;
  183.   if ((ChPtr=strrchr(ArcName,'.'))==NULL)
  184.   {
  185.     strcat(ArcName,".rar");
  186.     ChPtr=strrchr(ArcName,'.');
  187.   }
  188.   else
  189.     if (ChPtr[1]==0 || stricomp(ChPtr+1,"exe")==0)
  190.       strcpy(ChPtr+1,"rar");
  191.   if (!isdigit(*(ChPtr+2)) || !isdigit(*(ChPtr+3)))
  192.     strcpy(ChPtr+2,"00");
  193.   else
  194.   {
  195.     ChPtr+=3;
  196.     while ((++(*ChPtr))=='9'+1)
  197.     {
  198.       if (*(ChPtr-1)=='.')
  199.       {
  200.         *ChPtr='A';
  201.         break;
  202.       }
  203.       else
  204.       {
  205.         *ChPtr='0';
  206.         ChPtr--;
  207.       }
  208.     }
  209.   }
  210. }
  211.  
  212.  
  213. int MergeArchive(int ShowFileName)
  214. {
  215.   tclose(ArcPtr);
  216.   NextVolumeName();
  217.   while ((ArcPtr=fopen(ArcName,READBINARY))==NULL)
  218.   {
  219.     if (NumArcDrive==-1 || !IsRemovable(NumArcDrive))
  220.     {
  221.       mprintf(MAbsNextVol,ArcName);
  222.       return(0);
  223.     }
  224.     AskNextVol();
  225.   }
  226.   if (!IsArchive())
  227.   {
  228.     mprintf(MBadArc,ArcName);
  229.     return(0);
  230.   }
  231.   if (MainCommand[0]=='T')
  232.     mprintf(MTestVol,ArcName);
  233.   else
  234.     if (MainCommand[0]=='X' || MainCommand[0]=='E')
  235.       mprintf(MExtrVol,ArcName);
  236.   tseek(ArcPtr,NewMhd.HeadSize-MainHeadSize,SEEK_CUR);
  237.   ReadBlock(FILE_HEAD);
  238.   if (ShowFileName)
  239.     mprintf(MExtrPoints,ArcFileName);
  240.   UnpVolume=(NewLhd.Flags & LHD_SPLIT_AFTER);
  241.   tseek(ArcPtr,NextBlockPos-NewLhd.PackSize,SEEK_SET);
  242.   UnpPackedSize=NewLhd.PackSize;
  243.   RdUnpPtr=ArcPtr;
  244.   return(1);
  245. }
  246.  
  247.  
  248. void UnstoreFile(void)
  249. {
  250.   int Code;
  251.   if ((TempMemory=malloc(0x8000))==NULL)
  252.     ErrExit(EMEMORY,MEMORY_ERROR);
  253.   while (1)
  254.   {
  255.     if ((Code=UnpRead(TempMemory,0x8000))==-1)
  256.       ErrExit(EWRITE,WRITE_ERROR);
  257.     if (Code==0)
  258.       break;
  259.     Code=(int)Min((long)Code,DestUnpSize);
  260.     if (UnpWrite(TempMemory,Code)==-1U)
  261.       ErrExit(EWRITE,WRITE_ERROR);
  262.     if (DestUnpSize>=0)
  263.       DestUnpSize-=Code;
  264.   }
  265.   free(TempMemory);
  266.   TempMemory=NULL;
  267. }
  268.  
  269.  
  270. void ConvertPath(char *OutPath, char *InPath)
  271. {
  272.   char TmpStr[NM],*OutPathPtr;
  273.   if (InPath[0] && IsDriveDiv(InPath[1]))
  274.     OutPathPtr=&InPath[2];
  275.   else
  276.     OutPathPtr=InPath;
  277.   if (OutPathPtr[0]=='.' && OutPathPtr[1]==CPATHDIVIDER)
  278.     OutPathPtr++;
  279.   if (OutPathPtr[0]=='.' && OutPathPtr[1]=='.' && OutPathPtr[2]==CPATHDIVIDER)
  280.     OutPathPtr+=2;
  281.   if (*OutPathPtr==CPATHDIVIDER)
  282.     OutPathPtr++;
  283.   strcpy(TmpStr,OutPathPtr);
  284.   strcpy(OutPath,TmpStr);
  285. }
  286.  
  287.  
  288. void AskNextVol(void)
  289. {
  290.   mprintf(MAskNextVol,ArcName);
  291.   Ask(MContinueQuit);
  292.   if (Choice==2)
  293.     ErrExit(EEMPTY,USER_BREAK);
  294. }
  295.  
  296.