home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / xpk_source / shell / xdir.c < prev    next >
C/C++ Source or Header  |  1996-10-19  |  4KB  |  217 lines

  1.  
  2. #include <proto/exec.h>
  3. #include <proto/dos.h>
  4. #include <proto/xpk.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <dos.h>
  9.  
  10. #define MAXLINES 2000
  11.  
  12. static char ver[]= "$VER: xDir 1.1 (" __DATE__ ")\r\n";
  13.  
  14. struct Library *XpkBase;
  15. char errbuf[XPKERRMSGSIZE + 1], *Line[MAXLINES];
  16. struct FileInfoBlock *Fib;
  17. long utot, ctot, Lines;
  18. char buf[300];
  19.  
  20. void exitem (char *name);
  21. void exfile (struct FileInfoBlock *fib);
  22. void print (char *str);
  23. void showlines (void);
  24.  
  25. void 
  26. end (char *text)
  27. {
  28.   if (text)
  29.     print (text);
  30.   if (XpkBase)
  31.     CloseLibrary (XpkBase);
  32.   _exit (text ? 10 : 0);
  33. }
  34.  
  35. int 
  36. breakfunc (void)
  37. {
  38.   end ("***Break\n");
  39.   return 0;
  40. }
  41.  
  42. char usage[]= "Usage: xDir files/dirs\n";
  43.  
  44. void 
  45. main (int argc, char *argv[])
  46. {
  47.   int i, ratio = 0;
  48.  
  49.   onbreak (breakfunc);
  50.  
  51.   if (argc == 2 && !stricmp (argv[1], "?"))
  52.     end (usage);
  53.  
  54.   if (!(XpkBase = OpenLibrary (XPKNAME, 0)))
  55.     end ("Cannot open " XPKNAME "\n");
  56.  
  57.   if (argc < 2)
  58.     argv[1] = "", argc = 2;
  59.  
  60.   if (!(Fib = AllocMem (sizeof (*Fib), 0)))
  61.     end ("Not enough memory\n");
  62.  
  63.   if (*argv[1] == '-')
  64.     end (usage);
  65.  
  66.   print ("Original  Packed  Ratio Type Protection Name\n");
  67.   print ("-------- -------- ----- ---- ---------- ----\n");
  68.   for (i = 1; i < argc; i++)
  69.     exitem (argv[i]);
  70.  
  71.   showlines ();
  72.  
  73.   if (utot)
  74.     ratio = 100 - 100 * ctot / utot;
  75.   print ("-------- -------- -----\n");
  76.   sprintf (buf, "%8ld %8ld  %2ld%%\n", utot, ctot, ratio);
  77.   print (buf);
  78.  
  79.   end (NULL);
  80. }
  81.  
  82. void 
  83. showlines (void)
  84. {
  85.   int i;
  86.  
  87.   for (i = 0; i < Lines; i++) {
  88.     Write (Output (), Line[i], strlen (Line[i]));
  89.     if (SetSignal (0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  90.       end (" *** Break\n");
  91.   }
  92.   Lines = 0;
  93. }
  94.  
  95. void 
  96. print (char *str)
  97. {
  98.   Write (Output (), str, strlen (str));
  99. }
  100.  
  101. char *
  102. dirname (char *rp)
  103. {
  104.   static char dir[200];
  105.   char *wp = dir, *lp = dir;
  106.  
  107.   while (*rp) {
  108.     if (*rp == ':')
  109.       lp = wp + 1;
  110.     if (*rp == '/')
  111.       lp = wp;
  112.     *wp++ = *rp++;
  113.   }
  114.   *lp = 0;
  115.   return dir;
  116. }
  117.  
  118. void
  119. exitem (char *name)
  120. {
  121.   BPTR lock, prev;
  122.  
  123.   if (!(lock = Lock (name, ACCESS_READ))) {
  124.     sprintf (buf, "Error %ld reading %s\n", IoErr (), name);
  125.     print (buf);
  126.     return;
  127.   }
  128.  
  129.   if (!Examine (lock, Fib)) {
  130.     UnLock (lock);
  131.     sprintf (buf, "Error %ld reading %s\n", IoErr (), name);
  132.     print (buf);
  133.     return;
  134.   }
  135.  
  136.   if (Fib->fib_DirEntryType < 0) {
  137.     UnLock (lock);
  138.     if (!(lock = Lock (dirname (name), ACCESS_READ))) {
  139.       sprintf (buf, "Error locking %s\n", name);
  140.       print (buf);
  141.       return;
  142.     }
  143.  
  144.     prev = CurrentDir (lock);
  145.     exfile (Fib);
  146.     UnLock (CurrentDir (prev));
  147.   }
  148.   else {
  149.  
  150.     prev = CurrentDir (lock);
  151.  
  152.     if (Lines) {
  153.       showlines ();
  154.       print ("\n");
  155.     }
  156.  
  157.     while (ExNext (lock, Fib))
  158.       exfile (Fib);
  159.     UnLock (CurrentDir (prev));
  160.   }
  161. }
  162.  
  163. struct TagItem tags[]=
  164. {XPK_InName, NULL, XPK_PassThru, TRUE, TAG_DONE};
  165.  
  166. void
  167. exfile (struct FileInfoBlock *fib)
  168. {
  169.   struct XpkFib xfib;
  170.   char prot[10];
  171.   int i, clen = 0, ulen = 0;
  172.   char buf[200];
  173.  
  174.   if (SetSignal (0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  175.     end (" *** Break\n");
  176.  
  177.   prot[0] = fib->fib_DirEntryType < 0 ? '-' : 'd';
  178.   for (i = 0; i < 8; i++)
  179.     prot[8 - i] = (Fib->fib_Protection ^ 0x0f) & (1 << i) ? "dewrapsh"[i] : '-';
  180.   prot[9] = 0;
  181.  
  182.   if (XpkExamineTags (
  183.                &xfib,
  184.                XPK_InName, (long) fib->fib_FileName,
  185.                TAG_DONE
  186.       )|| xfib.Type != XPKTYPE_PACKED) {
  187.     if (fib->fib_EntryType < 0)
  188.       sprintf (buf, "%8ld                      %9s %-20s\n",
  189.            clen = ulen = fib->fib_Size,
  190.            prot,
  191.            fib->fib_FileName);
  192.     else
  193.       sprintf (buf, "%8s                      %9s %-20s\n",
  194.            "<Dir>",
  195.            prot,
  196.            fib->fib_FileName);
  197.   }
  198.   else
  199.     sprintf (buf, "%8ld %8ld  %2ld%%  %4s  %9s %-20s\n",
  200.          ulen = xfib.ULen,
  201.          clen = xfib.CLen,
  202.          xfib.Ratio,
  203.          xfib.Packer,
  204.          prot,
  205.          fib->fib_FileName);
  206.  
  207.   for (i = Lines - 1; i >= 0 && stricmp (Line[i] + 40, buf + 40) > 0; i--)
  208.     Line[i + 1] = Line[i];
  209.   Line[i + 1] = strcpy (malloc (strlen (buf) + 1), buf);
  210.  
  211.   if (++Lines >= MAXLINES)
  212.     end ("Buffer overflow\n");
  213.  
  214.   utot += ulen;
  215.   ctot += clen;
  216. }
  217.