home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / util / mbq319 / unpack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-27  |  2.5 KB  |  133 lines

  1. #include "cmdlib.h"
  2. #include <sys/types.h>
  3. //#include <sys/dir.h>
  4.  
  5. typedef struct
  6. {
  7.     char    name[56];
  8.     int        filepos, filelen;
  9. } packfile_t;
  10.  
  11. typedef struct
  12. {
  13.     char    id[4];
  14.     int        dirofs;
  15.     int        dirlen;
  16. } packheader_t;
  17.  
  18. int        packhandle;
  19. packfile_t    *packfiles;
  20. char    packfilename[256];
  21. int        numpackfiles;
  22.  
  23. /*
  24. =================
  25. LoadPackFile
  26. =================
  27. */
  28. void LoadPackFile (char *packfile)
  29. {
  30.     packheader_t        header;
  31.     int                    i;
  32.     
  33.     packhandle = SafeOpenRead (packfile);
  34.     SafeRead (packhandle, (void *)&header, sizeof(header));
  35.     if (header.id[0] != 'P' || header.id[1] != 'A'
  36.     || header.id[2] != 'C' || header.id[3] != 'K')
  37.         Error ("%s is not a packfile", packfile);
  38.     header.dirofs = LittleLong (header.dirofs);
  39.     header.dirlen = LittleLong (header.dirlen);
  40.     
  41.     lseek (packhandle, header.dirofs, SEEK_SET);
  42.     packfiles = malloc (header.dirlen);
  43.     SafeRead (packhandle, (void *)packfiles, header.dirlen);
  44.     numpackfiles = header.dirlen / sizeof(packfile_t);
  45.     for (i=0 ; i<numpackfiles ; i++)
  46.     {
  47.         packfiles[i].filepos = LittleLong(packfiles[i].filepos);
  48.         packfiles[i].filelen = LittleLong(packfiles[i].filelen);
  49.     }
  50. }
  51.  
  52. void Sys_mkdir (char *path)
  53. {
  54.     if (mkdir (path, 0777) != -1)
  55.         return;
  56.     if (errno != EEXIST)
  57.         Error ("mkdir %s: %s",path, strerror(errno)); 
  58. }
  59.  
  60. /*
  61. ============
  62. CreatePath
  63. ============
  64. */
  65. void    CreatePath (char *path)
  66. {
  67.     char    *ofs;
  68.     
  69.     for (ofs = path+1 ; *ofs ; ofs++)
  70.     {
  71.         if (*ofs == '/')
  72.         {    // create the directory
  73.             *ofs = 0;
  74.             Sys_mkdir (path);
  75.             *ofs = '/';
  76.         }
  77.     }
  78. }
  79.  
  80. /*
  81. ===========
  82. CopyFile
  83.  
  84. Copies a file, creating any directories needed
  85. ===========
  86. */
  87. void CopyFile (char *src, char *dest)
  88. {
  89. }
  90.  
  91.  
  92. /*
  93. ============
  94. main
  95. ============
  96. */
  97. void main (int argc, char **argv)
  98. {
  99.     int        i;
  100.     char    dest[1024];
  101.     int        out;
  102.     int        remaining, count;
  103.     char    buf[4096];
  104.  
  105.     if (argc != 3)
  106.         Error ("usage: unpack <packfile> <dirbase>\nexample: unpack id1.pak /raid/quake/id1\n");
  107.  
  108.     LoadPackFile (argv[1]);
  109.  
  110.     for (i=0 ; i<numpackfiles ; i++)
  111.     {
  112.         sprintf (dest, "%s/%s", argv[2], packfiles[i].name);
  113.         lseek (packhandle, packfiles[i].filepos, SEEK_SET);
  114.         printf ("writing %s\n", dest);
  115.         remaining = packfiles[i].filelen;
  116.         CreatePath (dest);
  117.         out = SafeOpenWrite (dest);
  118.         
  119.         while (remaining)
  120.         {
  121.             if (remaining < sizeof(buf))
  122.                 count = remaining;
  123.             else
  124.                 count = sizeof(buf);
  125.             SafeRead (packhandle, buf, count);
  126.             SafeWrite (out, buf, count);
  127.             remaining -= count;
  128.         }    
  129.         close (out);    
  130.     }
  131. }
  132.  
  133.