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

  1. int MakeDir(char *Name,UDWORD Attr)
  2. {
  3. #ifdef _WIN_32
  4.   if (mkdir(Name)==0)
  5.   {
  6.     SetFileAttributes(Name,Attr);
  7.     return(0);
  8.   }
  9.   return(-1);
  10. #endif
  11. #ifdef _UNIX
  12.   return(mkdir((Name),(mode_t)(Attr)));
  13. #endif
  14. }
  15.  
  16.  
  17. /* Generating sound MSec milliseconds duration and Freq frequency
  18. */
  19.  
  20. void MakeSound(int MSec,int Freq)
  21. {
  22.   printf("\007");
  23. }
  24.  
  25. /* Return date format:
  26.    0 - Month-Day-Year
  27.    1 - Day-Month-Year
  28.    2 - Year-Month-Day
  29. */
  30.  
  31. int DateFormat(void)
  32. {
  33.   return(1);
  34. }
  35.  
  36. /* Check is drive removable
  37.    NumDrive: 0 - A, 1 - B, ...
  38. */
  39.  
  40.  
  41. int IsRemovable(int NumDrive)
  42. {
  43. #ifdef _WIN_32
  44.   char Root[10];
  45.   sprintf(Root,"%c:\\",NumDrive+'A');
  46.   return(GetDriveType(Root)==DRIVE_REMOVABLE);
  47. #else
  48.   return(0);
  49. #endif
  50. }
  51.  
  52.  
  53. /* Display ANSI comments
  54.    Addr - comment address
  55.    Size - comment length
  56. */
  57.  
  58. void ShowAnsiComment(UBYTE *Addr,unsigned int Size)
  59. {
  60.   fwrite(Addr,1,Size,stdout);
  61.   fflush(stdout);
  62. }
  63.  
  64.  
  65. /* Input string for password
  66. */
  67.  
  68. void GetPswStr(char *Str)
  69. {
  70.   gets(Str);
  71. }
  72.  
  73.  
  74. /* return current disk number
  75.    0 - A, 1 - B, ...
  76.    or return -1 if unknown
  77. */
  78.  
  79. int GetCurDisk(void)
  80. {
  81. #ifdef _WIN_32
  82.   unsigned int Drive;
  83.   _dos_getdrive(&Drive);
  84.   return(Drive-1);
  85. #endif
  86.  
  87. #ifdef _UNIX
  88.   return(-1);
  89. #endif
  90. }
  91.  
  92.  
  93. int GetFileStat(char *Name,struct FileStat *FS)
  94. {
  95. #ifdef _WIN_32
  96.   struct find_t ff;
  97.   if (_dos_findfirst(Name,FM_ARCH|FM_DIREC|FM_RDONLY,&ff)!=0)
  98.     return(0);
  99.   FS->FileAttr=ff.attrib;
  100.   FS->IsDir=IsDir(ff.attrib);
  101.   FS->FileTime=ff.wr_time+((UDWORD)ff.wr_date<<16);
  102.   FS->FileSize=ff.size;
  103.   return(1);
  104. #endif
  105. #ifdef _UNIX
  106.   struct stat st;
  107.   if (stat(Name,&st)!=0)
  108.     return(0);
  109.   FS->FileAttr=st.st_mode;
  110.   FS->IsDir=st.st_mode & S_IFDIR;
  111.   FS->FileSize=st.st_size;
  112.   FS->FileTime=UnixTimeToDos(st.st_mtime);
  113.   return(1);
  114. #endif
  115. }
  116.  
  117.  
  118. #ifdef _UNIX
  119. UDWORD UnixTimeToDos(time_t UnixTime)
  120. {
  121.   struct tm *t;
  122.   UDWORD DosTime;
  123.   t=localtime(&UnixTime);
  124.   DosTime=(t->tm_sec/2)|(t->tm_min<<5)|(t->tm_hour<<11)|
  125.           (t->tm_mday<<16)|((t->tm_mon+1)<<21)|((t->tm_year-80)<<25);
  126.   return(DosTime);
  127. }
  128.  
  129.  
  130. time_t DosTimeToUnix(UDWORD DosTime)
  131. {
  132.   struct tm t;
  133.  
  134.   t.tm_sec=(DosTime & 0x1f)*2;
  135.   t.tm_min=(DosTime>>5) & 0x3f;
  136.   t.tm_hour=(DosTime>>11) & 0x1f;
  137.   t.tm_mday=(DosTime>>16) & 0x1f;
  138.   t.tm_mon=((DosTime>>21)-1) & 0x0f;
  139.   t.tm_year=(DosTime>>25)+80;
  140.   t.tm_isdst=-1;
  141.   return(mktime(&t));
  142. }
  143. #endif
  144.  
  145.  
  146. void ConvertDate(UDWORD ft)
  147. {
  148.   static int Format=-1;
  149.   int Year;
  150.  
  151.   Year=(int)(((ft>>25)+1980)%100);
  152.   if (Format==-1)
  153.     Format=DateFormat();
  154.   switch (Format)
  155.   {
  156.     case 0:
  157.       sprintf(DateStr,"%02d%02d%02d%02d%02d",
  158.               (int)(ft>>21)&0xf,(int)(ft>>16)&0x1f,Year,
  159.               (int)(ft>>11)&0x1f,(int)(ft>>5)&0x3f);
  160.       break;
  161.     case 1:
  162.       sprintf(DateStr,"%02d%02d%02d%02d%02d",
  163.               (int)(ft>>16)&0x1f,(int)(ft>>21)&0xf,Year,
  164.               (int)(ft>>11)&0x1f,(int)(ft>>5)&0x3f);
  165.       break;
  166.     default:
  167.       sprintf(DateStr,"%02d%02d%02d%02d%02d",
  168.               Year,(int)(ft>>21)&0xf,(int)(ft>>16)&0x1f,
  169.               (int)(ft>>11)&0x1f,(int)(ft>>5)&0x3f);
  170.       break;
  171.   }
  172. }
  173.  
  174.  
  175. void SetOpenFileStat(FILE *FPtr)
  176. {
  177. #ifdef _WIN_32
  178.   SetOpenFileTime(FPtr,NewLhd.FileTime);
  179. #endif
  180. }
  181.  
  182.  
  183. void SetCloseFileStat(char *Name)
  184. {
  185. #ifdef _WIN_32
  186.   _dos_setfileattr(Name,NewLhd.FileAttr);
  187. #endif
  188. #ifdef _UNIX
  189.   struct utimbuf ut;
  190.   chmod(Name,(mode_t)NewLhd.FileAttr);
  191.   SetCloseFileTime(Name,NewLhd.FileTime);
  192. #endif
  193. }
  194.  
  195.  
  196. void SetOpenFileTime(FILE *FPtr,UDWORD ft)
  197. {
  198. #ifdef _WIN_32
  199.   fflush(FPtr);
  200.   _dos_setftime(fileno(FPtr),ft>>16,ft);
  201. #endif
  202. }
  203.  
  204.  
  205. void SetCloseFileTime(char *Name,UDWORD ft)
  206. {
  207. #ifdef _UNIX
  208.   struct utimbuf ut;
  209.   ut.actime=ut.modtime=DosTimeToUnix(ft);
  210.   utime(Name,&ut);
  211. #endif
  212. }
  213.  
  214.  
  215. UDWORD GetOpenFileTime(FILE *FPtr)
  216. {
  217. #ifdef _WIN_32
  218.   unsigned int FileDate,FileTime;
  219.   _dos_getftime(fileno(FPtr),&FileDate,&FileTime);
  220.   return(((UDWORD)FileDate<<16)+FileTime);
  221. #endif
  222. #ifdef _UNIX
  223.   struct stat st;
  224.   fstat(fileno(FPtr),&st);
  225.   return(UnixTimeToDos(st.st_mtime));
  226. #endif
  227. }
  228.  
  229.  
  230. int FileExist(char *FileName)
  231. {
  232. #ifdef ENABLE_ACCESS
  233.   return(access(FileName,0)==0);
  234. #else
  235.   FILE *FPtr;
  236.   if ((FPtr=fopen(FileName,READBINARY))==NULL)
  237.   {
  238.     if (errno==ENOENT)
  239.       return(0);
  240.   }
  241.   else
  242.     fclose(FPtr);
  243.   return(1);
  244. #endif
  245. }
  246.  
  247.  
  248. FILE* topen(char *Name,char *Mode,unsigned int Sharing)
  249. {
  250.   FILE *FPtr;
  251.   FPtr=ShareOpen(Name,Mode,Sharing);
  252.   if (FPtr==NULL)
  253.   {
  254.     mprintf(MCannotFOpen,Name);
  255.     ErrExit(EEMPTY,OPEN_ERROR);
  256.   }
  257.   return(FPtr);
  258. }
  259.  
  260.  
  261. FILE* wopen(char *Name,char *Mode,unsigned int Sharing)
  262. {
  263.   FILE *FPtr;
  264.   FPtr=ShareOpen(Name,Mode,Sharing);
  265.   if (FPtr==NULL)
  266.     mprintf(MCannotFOpen,Name);
  267.   return(FPtr);
  268. }
  269.  
  270.  
  271. FILE* ShareOpen(char *Name,char *Mode,unsigned int Sharing)
  272. {
  273.   FILE *FPtr;
  274. #ifdef _WIN_32
  275.   unsigned int ShFlags;
  276.   switch (Sharing)
  277.   {
  278.     case M_DENYREAD:
  279.       ShFlags=SH_DENYRD;
  280.       break;
  281.     case M_DENYWRITE:
  282.       ShFlags=SH_DENYWR;
  283.       break;
  284.     case M_DENYNONE:
  285.       ShFlags=SH_DENYNONE;
  286.       break;
  287.     case M_DENYALL:
  288.       ShFlags=SH_DENYNO;
  289.       break;
  290.   }
  291.   FPtr=_fsopen(Name,Mode,ShFlags);
  292. #else
  293.   FPtr=fopen(Name,Mode);
  294. #endif
  295.   return(FPtr);
  296. }
  297.  
  298.  
  299. int SplitPath(char *FullName,char *Path,char *Name,int RemoveDrive)
  300. {
  301.   char *ChPtr;
  302.   if (RemoveDrive && (ChPtr=strchr(FullName,':'))!=NULL)
  303.     ChPtr++;
  304.   else
  305.     ChPtr=FullName;
  306.   strcpy(Name,PointToName(ChPtr));
  307.   strcpy(Path,ChPtr);
  308.   Path[strlen(ChPtr)-strlen(Name)]=0;
  309.   if (strchr(ChPtr,'?')!=NULL || strchr(ChPtr,'*')!=NULL)
  310.     return(1);
  311.   else
  312.     return(0);
  313. }
  314.  
  315.  
  316. int GetPathDisk(char *Path)
  317. {
  318.   if (isalpha(*Path) && Path[1]==':')
  319.     return(toupper(*Path)-'A');
  320.   else
  321.     return(-1);
  322. }
  323.  
  324.  
  325. void ShowAttr(void)
  326. {
  327.   UDWORD A;
  328.   A=NewLhd.FileAttr;
  329.   switch(NewLhd.HostOS)
  330.   {
  331.     case MS_DOS:
  332.     case OS2:
  333.     case WIN_32:
  334.       mprintf("  %c%c%c%c%c%c  ",
  335.               (A & 0x08) ? 'V' : '.',
  336.               (A & 0x10) ? 'D' : '.',
  337.               (A & 0x01) ? 'R' : '.',
  338.               (A & 0x02) ? 'H' : '.',
  339.               (A & 0x04) ? 'S' : '.',
  340.               (A & 0x20) ? 'A' : '.');
  341.       break;
  342.     case UNIX:
  343.       mprintf("%c%c%c%c%c%c%c%c%c%c",
  344.               (A & 0x4000) ? 'd' : '-',
  345.               (A & 0x0100) ? 'r' : '-',
  346.               (A & 0x0080) ? 'w' : '-',
  347.               (A & 0x0040) ? ((A & 0x0800) ? 's':'x'):((A & 0x0800) ? 'S':'-'),
  348.               (A & 0x0020) ? 'r' : '-',
  349.               (A & 0x0010) ? 'w' : '-',
  350.               (A & 0x0008) ? ((A & 0x0400) ? 's':'x'):((A & 0x0400) ? 'S':'-'),
  351.               (A & 0x0004) ? 'r' : '-',
  352.               (A & 0x0002) ? 'w' : '-',
  353.               (A & 0x0001) ? 'x' : '-');
  354.       break;
  355.   }
  356. }
  357.  
  358.  
  359. int IsDir(UDWORD Attr)
  360. {
  361.   return(Attr & FM_DIREC);
  362. }
  363.  
  364.  
  365. int IsLabel(UDWORD Attr)
  366. {
  367.   return(Attr & FM_LABEL);
  368. }
  369.  
  370.  
  371. void ConvertFlags(void)
  372. {
  373. #ifdef _WIN_32
  374.   switch(NewLhd.HostOS)
  375.   {
  376.     case MS_DOS:
  377.     case OS2:
  378.     case WIN_32:
  379.       break;
  380.     case UNIX:
  381.       if ((NewLhd.Flags & LHD_WINDOWMASK)==LHD_DIRECTORY)
  382.         NewLhd.FileAttr=FM_DIREC;
  383.       else
  384.         NewLhd.FileAttr=FM_ARCH;
  385.       break;
  386.     default:
  387.       if ((NewLhd.Flags & LHD_WINDOWMASK)==LHD_DIRECTORY)
  388.         NewLhd.FileAttr=FM_DIREC;
  389.       else
  390.         NewLhd.FileAttr=FM_ARCH;
  391.       break;
  392.   }
  393. #endif
  394. #ifdef _UNIX
  395.   switch(NewLhd.HostOS)
  396.   {
  397.     case MS_DOS:
  398.     case OS2:
  399.     case WIN_32:
  400.       if (NewLhd.FileAttr & 0x10)
  401.         NewLhd.FileAttr=0x41ff;
  402.       else
  403.         if (NewLhd.FileAttr & 1)
  404.           NewLhd.FileAttr=0x816d;
  405.         else
  406.           NewLhd.FileAttr=0x81ff;
  407.       break;
  408.     case UNIX:
  409.       break;
  410.     default:
  411.       if ((NewLhd.Flags & LHD_WINDOWMASK)==LHD_DIRECTORY)
  412.         NewLhd.FileAttr=0x41ff;
  413.       else
  414.         NewLhd.FileAttr=0x81ff;
  415.       break;
  416.   }
  417. #endif
  418. }
  419.  
  420.  
  421. DIR * DirOpen(char *Name)
  422. {
  423.   char DirName[NM],*NPtr,*DPtr;
  424.   int I;
  425.   if (Name[0] && Name[1]==':')
  426.   {
  427.     strncpy(DirName,Name,2);
  428.     DirName[2]=0;
  429.     NPtr=&Name[2];
  430.     DPtr=&DirName[2];
  431.   }
  432.   else
  433.   {
  434.     NPtr=Name;
  435.     DPtr=DirName;
  436.   }
  437.   if (*NPtr)
  438.   {
  439.     strcpy(DPtr,NPtr);
  440.     I=strlen(DPtr)-1;
  441.     if (IsPathDiv(DPtr[I]))
  442.       if (I>0)
  443.         DPtr[I]=0;
  444.       else
  445.         strcat(DPtr,".");
  446.   }
  447.   else
  448.     *DPtr=0;
  449.  
  450.   if (*DPtr==0)
  451.     strcpy(DPtr,".");
  452.  
  453.   return(opendir(DirName));
  454. }
  455.  
  456.  
  457. int IsPathDiv(int Ch)
  458. {
  459.   if (Ch=='\\' || Ch=='/')
  460.     return(1);
  461.   return(0);
  462. }
  463.  
  464.  
  465. int IsDriveDiv(int Ch)
  466. {
  467. #ifdef _UNIX
  468.   return(0);
  469. #else
  470.   return(Ch==':');
  471. #endif
  472. }
  473.  
  474.  
  475. void ConvertUnknownHeader(void)
  476. {
  477.   int I;
  478.   if (NewLhd.HostOS>UNIX)
  479.   {
  480.     NewLhd.HostOS=MS_DOS;
  481.     if ((NewLhd.Flags & LHD_WINDOWMASK)==LHD_DIRECTORY)
  482.       NewLhd.FileAttr=0x10;
  483.     else
  484.       NewLhd.FileAttr=0x20;
  485.   }
  486.   for (I=0;ArcFileName[I];I++)
  487.     if (IsPathDiv(ArcFileName[I]))
  488.       ArcFileName[I]=CPATHDIVIDER;
  489. }
  490.  
  491.  
  492. int CheckForDevice(FILE *CheckFile)
  493. {
  494. #ifdef _UNIX
  495.   return(0);
  496. #else
  497.   return(isatty(fileno(CheckFile)));
  498. #endif
  499. }
  500.  
  501.