home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Cruncher / XPK416SR.LHA / xpk_Source / shell / xUp.c < prev   
Encoding:
C/C++ Source or Header  |  1997-09-06  |  3.6 KB  |  165 lines

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