home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #3 / amigamamagazinepolishissue1998.iso / blabla / xpkcut / xpkcut.c < prev    next >
C/C++ Source or Header  |  1998-01-24  |  2KB  |  136 lines

  1. /*
  2.  * Program: XPK Cut
  3.  * Author : LeMUr/Fire & blabla
  4.  * Version: 0.1 ßeta
  5.  * Date   : 01.03.1996
  6.  *
  7.  * See doc for more!
  8.  *
  9.  * Informations about changes:
  10.  * - 01.03.1996 - 1st version, CLI use only
  11.  *
  12.  */
  13.  
  14.  
  15. #include <exec/memory.h>
  16.  
  17. #include <proto/exec.h>
  18. #include <proto/dos.h>
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24.  
  25. #define VER_STR "$VER: XPK-Cut v0.1ß by LeMUr/Fire & blabla"
  26.  
  27.  
  28. // memory
  29. UBYTE *Bufor = NULL;
  30. UBYTE *OldBufor = NULL;
  31. ULONG Size = NULL;
  32.  
  33.  
  34. void out(int code);
  35.  
  36.  
  37. void main(int argc, char *argv[])
  38. {
  39.     // files locks
  40.     BPTR lock = NULL;
  41.     BPTR file = NULL;
  42.  
  43.     // indicators
  44.     ULONG i;
  45.     ULONG ind = 1;
  46.     ULONG len;
  47.  
  48.     // file name
  49.     UBYTE name[50];
  50.  
  51.     struct FileInfoBlock __aligned fib;
  52.  
  53.  
  54.     if(argc != 2)
  55.     {
  56.         printf("I need a file name!\n");
  57.         out(20);
  58.     }
  59.  
  60.     if(!(lock = Lock(argv[1], ACCESS_READ)))
  61.     {
  62.         printf("Unable to lock \"%s\"\n", argv[1]);
  63.         out(25);
  64.     }
  65.  
  66.     if(!Examine(lock, &fib))
  67.     {
  68.         printf("Unable to examine \"%s\"\n", argv[1]);
  69.         UnLock(lock);
  70.         out(30);
  71.     }
  72.  
  73.     if(!(Size = fib.fib_Size))
  74.     {
  75.         printf("File couldn't be 0 bytes long!\n");
  76.         UnLock(lock);
  77.         out(32);
  78.     }
  79.  
  80.     if(!(Bufor = AllocMem(Size, MEMF_ANY | MEMF_CLEAR)))
  81.     {
  82.         printf("Unable to allocate needed memory!\n");
  83.         UnLock(lock);
  84.         out(35);
  85.     }
  86.  
  87.     if(!(file = Open(argv[1], MODE_OLDFILE)))
  88.     {
  89.         printf("Unable to open file \"%s\"\n", argv[1]);
  90.         UnLock(lock);
  91.         out(40);
  92.     }
  93.  
  94.     Read(file, Bufor, Size);
  95.     Close(file);
  96.     UnLock(lock);
  97.  
  98.  
  99.     OldBufor = Bufor;
  100.  
  101.     // searching with saving (if found 'coz)
  102.     for(i=0; i != Size; i++)
  103.     {
  104.         if(*Bufor == 'X' && *(++Bufor) == 'P' && *(++Bufor) == 'K' && *(++Bufor) == 'F')
  105.         {
  106.             sprintf(name, "File_%d", ind++);
  107.             if(!(file = Open(name, MODE_NEWFILE)))
  108.             {
  109.                 printf("Unable to open file \"%s\"\n", name);
  110.                 out(40);
  111.             }
  112.             len = *((ULONG *)++Bufor) + 4;    /* lenght of packed file
  113.                     ('+1' because of ULONG type); '+4' - security margin
  114.                     ignored by decrunchers */
  115.  
  116.             printf("File: %s  Len: %ld (0x%X)\n", name, len, len);
  117.             Write(file, "XPKF", 4);
  118.             Write(file, Bufor, len);
  119.             Close(file);
  120.         }
  121.         Bufor++;
  122.     }
  123.  
  124.     out(0);
  125. }    // of main()
  126.  
  127.  
  128.  
  129. void out(int code)
  130. {
  131.     if(Size && OldBufor)
  132.         FreeMem(OldBufor, Size);
  133.  
  134.     exit(code);
  135. }
  136.