home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / utils misc / Boxer 0.18 / Boxer018.exe / boxer / PalmBoxer / untar.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-02  |  2.8 KB  |  157 lines

  1. /* based on funzip.c from unzip 5.40 */
  2.  
  3. #define IGNORE_STDIO_STUBS
  4. #define __string_h
  5.  
  6. #ifdef OLDGCC
  7.  
  8. #include <Common.h>
  9. #include <System/SysAll.h>
  10. #include <UI/UIAll.h>
  11. #include <Unix/sys_types.h>
  12.  
  13. #else
  14.  
  15. #include <PalmOS.h>
  16. #include <PalmCompatibility.h>
  17. #include <Unix/sys_types.h>
  18.  
  19. #endif
  20.  
  21. #include <System/SysEvtMgr.h>
  22. #include "stringil.h"
  23. #include "stdio2.h"
  24.  
  25. #ifdef __PALMOS_TRAPS__
  26. Err errno;
  27. #endif
  28.  
  29. extern int fgetc(FILE * fp);
  30. extern FILE *fopen(char *name, char *mode);
  31.  
  32. int untardir(FILE * fd, char *dir, long int *loc)
  33. {
  34.   char ibuf[512], *c;
  35.   unsigned long total, disp=0, i=0;
  36.   int max;
  37.  
  38.   max = 0;
  39.   DmSet(dir,0,2,0);
  40.  
  41.   fseek(fd, 0, SEEK_SET);
  42.  
  43.   fread(ibuf, 1, 512, fd);
  44.   if (strncmp(&ibuf[257], "GNUtar",6) && strncmp(&ibuf[257], "ustar", 5))
  45.     return 0;
  46.  
  47.   fseek(fd, 0, SEEK_SET);
  48.  
  49.   for (;;) {
  50.     fread(ibuf, 1, 512, fd);
  51.  
  52.     if (ibuf[0] == 0)
  53.       fread(ibuf, 1, 512, fd);
  54.  
  55.     if (strncmp(&ibuf[257], "GNUtar",6) && strncmp(&ibuf[257], "ustar", 5))
  56.       return max;
  57.  
  58.     total = ftell(fd) - 512L;
  59.     DmWrite(loc,(i++)*4,&total,4);
  60.  
  61.     DmWrite(dir,disp,ibuf,strlen(ibuf)+1);
  62.     disp += strlen(ibuf)+1;
  63.     DmSet(dir,disp,1,0);
  64.  
  65.     c = &ibuf[124];
  66.     while (*c == ' ')
  67.       c++;
  68.     total = 0;
  69.  
  70.     do {
  71.       total = (total << 3) + *c++ - '0';
  72.     } while (*c != ' ');
  73.  
  74.     // round to the block size
  75.     total += 512;
  76.     total &= ~511;
  77.     fseek(fd, total, SEEK_CUR);
  78.     max++;
  79.  
  80.   }
  81.  
  82.  
  83. }
  84.  
  85. int untar(FILE * fd, long int loc)
  86. {
  87.   char ibuf[512], *c;
  88.   unsigned long total, itot;
  89.   FILE *ofd;
  90.   RectangleType r;
  91.  
  92.   fseek(fd, 0, SEEK_SET);
  93.  
  94.   fread(ibuf, 1, 512, fd);
  95.   if (strncmp(&ibuf[257], "GNUtar",6) && strncmp(&ibuf[257], "ustar", 5))
  96.     return -1;
  97.  
  98.   if( loc == -1 ) {
  99.     fseek(fd, 0, SEEK_SET);
  100.     FileControl(fileOpDestructiveReadMode, fd, NULL, NULL);
  101.   }
  102.   else
  103.     fseek(fd, loc, SEEK_SET);
  104.  
  105.   for (;;) {
  106.  
  107.     fread(ibuf, 1, 512, fd);
  108.  
  109.     if (ibuf[0] == 0)
  110.       fread(ibuf, 1, 512, fd);
  111.  
  112.     if (strncmp(&ibuf[257], "GNUtar",6) && strncmp(&ibuf[257], "ustar", 5))
  113.       return 0;
  114.  
  115.     r.topLeft.x = 0, r.topLeft.y = 15, r.extent.x = 160, r.extent.y = 30;
  116.     WinEraseRectangle(&r, 0);
  117.  
  118.     r.topLeft.x = 0, r.topLeft.y = 30, r.extent.x = 160, r.extent.y = 15;
  119.  
  120.  
  121.     c = &ibuf[124];
  122.     while (*c == ' ')
  123.       c++;
  124.     total = 0;
  125.  
  126.     do {
  127.       total = (total << 3) + *c++ - '0';
  128.     } while (*c != ' ');
  129.  
  130.     ofd = fopen(ibuf, "w");
  131.  
  132.  
  133.     StrIToA(ibuf,total);
  134.     strcat(ibuf,"     ");
  135.     WinDrawChars(ibuf,strlen(ibuf),120,0);
  136.  
  137.  
  138.     itot = total;
  139.     while (total) {
  140.  
  141.       fread(ibuf, 1, 512, fd);
  142.       fwrite(ibuf, 1, total > 512 ? 512 : total, ofd);
  143.       total -= total > 512 ? 512 : total;
  144.       r.extent.x = 160 - (160 * total / itot);
  145.       WinDrawRectangle(&r, 0);
  146.     }
  147.     fclose(ofd);
  148.     EvtResetAutoOffTimer();
  149.  
  150.     if( loc != -1 )
  151.       break;
  152.  
  153.   }
  154.   return 0;
  155.  
  156. }
  157.