home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / xpk_source / shell / xup.c < prev   
C/C++ Source or Header  |  1996-11-12  |  3KB  |  155 lines

  1. #define NAME     "xUp"
  2. #define REVISION "1"
  3.  
  4. /* Routinesheader
  5.  
  6.     Name:        xUp
  7.     Author:        SDI
  8.     Distribution:    PD
  9.     Description:    General XPK file-to-file unpacker
  10.  
  11.  1.1   09.10.96 : fixed error with new 3.10 xpkmaster.library (A4 problem)
  12. */
  13.  
  14. #include "SDI_defines.h"
  15. #define SDI_TO_ANSI
  16. #include "SDI_ASM_STD_protos.h"
  17. #include <pragma/exec_lib.h>
  18. #include <pragma/dos_lib.h>
  19. #include <pragma/xpkmaster_lib.h>
  20.  
  21. #ifdef __MAXON__
  22.   #define __asm
  23.   #define __saveds
  24. #endif
  25.  
  26. struct Library *XpkBase = 0;
  27.  
  28. UBYTE errbuf[XPKERRMSGSIZE + 1];    /* +1 to make room for '\n'*/
  29.  
  30. ULONG __asm _saveds chunkfunc(register __a1 struct XpkProgress *prog)
  31. {
  32.   ULONG out = Output();
  33.   UBYTE buf[120];
  34.  
  35.   if(prog->xp_Type == XPKPROG_START)
  36.     Write(out, "\033[0 p", 5);
  37.  
  38.   if(prog->xp_Type != XPKPROG_END)
  39.     sprintf(buf,
  40.          "\r%4s: %-8s (%3ld%% done, %2ld%% CF, %6ld cps) %s\033[K",
  41.          prog->xp_PackerName, prog->xp_Activity, prog->xp_Done,
  42.          prog->xp_CF, prog->xp_Speed, prog->xp_FileName);
  43.   else
  44.     sprintf(buf,
  45.          "\r%4s: %-8s (%3ldK, %2ld%% CF, %6ld cps) %s\033[K\n",
  46.          prog->xp_PackerName, prog->xp_Activity, prog->xp_ULen >> 10,
  47.          prog->xp_CF, prog->xp_Speed, prog->xp_FileName);
  48.  
  49.   Write(out, buf, strlen(buf));
  50.  
  51.   if(prog->xp_Type == XPKPROG_END)
  52.     Write(out, "\033[1 p", 5);
  53.  
  54.   return SetSignal(0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C;
  55. }
  56.  
  57. struct Hook chunkhook = {{0}, (ULONG (*) ()) chunkfunc};
  58.  
  59. UBYTE namebuf[200];
  60.  
  61. STRPTR tempname(STRPTR name)
  62. {
  63.   ULONG i = strlen(name);
  64.   CopyMem(name, namebuf, i);
  65.   for(name = namebuf + i; name > namebuf; name--)
  66.     if(name[-1] == '/' || name[-1] == ':')
  67.       break;
  68.  
  69.   sprintf(name, "tmp%lx", &name);
  70.   return namebuf;
  71. }
  72.  
  73. void end(STRPTR text)
  74. {
  75.   if(text)    Write(Output(), text, strlen(text));
  76.   if(XpkBase)    CloseLibrary(XpkBase);
  77.   exit(text ? RETURN_ERROR : 0);
  78. }
  79.  
  80. void main(int argc, char **argv)
  81. {
  82.   LONG res, i = 1, suffix, len;
  83.   STRPTR password = 0;
  84.  
  85.   if(!(XpkBase = OpenLibrary(XPKNAME, 0)))
  86.     end("Cannot open " XPKNAME "\n");
  87.  
  88.   if(argc == 1 || !strcmp (argv[1], "?"))
  89.     end("Usage: " NAME " [-s][-p password] files\n");
  90.  
  91.   if(i + 1 < argc && !strcmp(argv[i], "-p"))
  92.     password = argv[++i], i++;
  93.  
  94.   for(; i < argc; i++)
  95.   {
  96.     tempname(argv[i]);
  97.     len = strlen(argv[i]);
  98.     suffix = 0;
  99.     if(len > 4 && !stricmp (argv[i] + len - 5, ".xpk"))
  100.     {
  101.       CopyMem(argv[i], namebuf, strlen(argv[i]));
  102.       namebuf[len - 5] = 0;
  103.       suffix = 1;
  104.     }
  105.  
  106.     {
  107. #ifdef __MAXON__            /* Maxon has no tagcall pragma now */
  108.       struct TagItem t[] = {        /* and I don't want to use a link */
  109. #else                    /* library */
  110.       if((res = XpkUnpackTags(
  111. #endif
  112.     XPK_InName, (ULONG) argv[i],
  113.     XPK_FileName, (ULONG) argv[i],
  114.     XPK_OutName, (ULONG) namebuf,
  115.     XPK_ChunkHook, (ULONG) &chunkhook,
  116.     XPK_Password, (ULONG) password,
  117.     XPK_GetError, (ULONG) errbuf,
  118.     XPK_NoClobber, TRUE,
  119.     TAG_DONE
  120. #ifdef __MAXON__
  121.       , 0 };
  122.       if((res = XpkUnpack(t
  123. #endif
  124.       )) && res != XPKERR_NOTPACKED)
  125.       {
  126.         if(errbuf)
  127.         {
  128.           ULONG i = strlen(errbuf);
  129.           errbuf[i++] = '\n';
  130.           errbuf[i] = 0;
  131.         }
  132.         end(errbuf);
  133.       }
  134.     }
  135.  
  136.     if(res)
  137.     {
  138.       Write(Output(), errbuf, strlen(errbuf));
  139.       Write(Output(), "\n", 1);
  140.       if(!DeleteFile(namebuf))
  141.     end("Cannot delete outfile\n");
  142.     }
  143.  
  144.     else if(!suffix)
  145.     {
  146.       if(!DeleteFile(argv[i]))
  147.     end("Cannot delete input file\n");
  148.       if(!Rename(namebuf, argv[i]))
  149.     end("Cannot rename tempfile\n");
  150.     }
  151.   }
  152.  
  153.   end(0);
  154. }
  155.