home *** CD-ROM | disk | FTP | other *** search
/ The People Multimedia CD-Plus / VolumeOne.iso / LHARC / LVIEW.C < prev    next >
Text File  |  1989-03-28  |  4KB  |  159 lines

  1. /******************************************************************************
  2.  * File: lview.c
  3.  * Desc: view contents of .lzh files
  4.  * Date: 03/28/89
  5.  *****************************************************************************/
  6.  
  7. /*
  8.  
  9.     Daniel Durbin___________________________________________________
  10.     SysOp: Cygnus X-1 BBS        | CIS: 73447,1744
  11.     (805) 541-8505 (data)        | GEnie: D.DURBIN
  12.     EL major at PolySlo        | ddurbin@polyslo.CalPoly.EDU 
  13.  
  14.     This code is a result of looking at the header information
  15.     for an example .lzh file shown in the lzh.h file included.
  16.  
  17.     This code is released to the public and is freely copyable
  18.     and usable for any use
  19.  
  20. */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <dir.h>
  27.  
  28. #include "lzh.h"
  29.  
  30. char lview(FILE *stream);
  31. void nice(char *s);
  32.  
  33. void main(int argc, char *argv[])
  34. {
  35.     FILE *arc;
  36.     struct ffblk ffblk;
  37.     char string[81];
  38.     char ok;
  39.  
  40.     if (argc < 2) {
  41.         puts("Syntax: lview filename[.lzh]");
  42.         return;
  43.     }
  44.     strcpy(string, argv[1]);
  45.     if (strchr(string, '.') == NULL)
  46.         strcat(string, ".lzh");
  47.     if (strchr(string, '*') || strchr(string, '?')) {
  48.         ok = findfirst(string, &ffblk, 0) == 0;
  49.         while (ok) {
  50.             printf("Found %s\n", ffblk.ff_name);
  51.                 arc = fopen(ffblk.ff_name, "rb");
  52.             if (arc == NULL) {
  53.                 printf("Cannot open %s\n", ffblk.ff_name);
  54.                 return;
  55.             }
  56.             lview(arc);
  57.             ok = findnext(&ffblk) == 0;
  58.         }
  59.     }
  60.     else {
  61.             arc = fopen(string, "rb");
  62.         if (arc == NULL) {
  63.             printf("Cannot open %s\n", string);
  64.             return;
  65.         }
  66.         lview(arc);
  67.         fclose(arc);
  68.     }
  69. }
  70.  
  71. char lview(FILE *arc)
  72. {
  73.     struct lzhlfh header;        /* Local file header */
  74.     char *fname;            /* filename */
  75.     int crc;            /* crc-16 */
  76.  
  77.     char string[96];        /* output */
  78.     char method[7];            /* compression method */
  79.     char s1[9];            /* date */
  80.     char s2[9];            /* time */
  81.     char ok = 1;            /* while */
  82.     char i;                /* loop variable */
  83.     int num;            /* number of files in archive */
  84.     char pct;            /* percent compressed */
  85.     long t_fsize = 0;        /* unarched file size */
  86.     long t_csize = 0;        /* arched file size */
  87.  
  88.     fseek(arc, 0L, SEEK_SET);
  89.     num = 0;
  90.         puts("  #  FileName      Stowage    UnLZH Sz  LZH Size  Pct  Date      Time      CRC");
  91.     puts("═══  ════════════  ═════════  ════════  ════════  ═══  ════════  ════════  ════");
  92.     while (ok) {
  93.         ok = fread(&header, sizeof(header), 1, arc) == 1;
  94.         if (!ok || !header.namelen) break;
  95.                 fname = malloc(header.namelen);
  96.         if (fname == NULL) {
  97.             puts("memory allocation error");
  98.             return(num);
  99.         }
  100.         fread(fname, header.namelen, 1, arc);
  101.         *(fname+header.namelen) = 0;
  102.         fread(&crc, sizeof(short), 1, arc);
  103.         for (i = 0; i < 5; i++)
  104.             *(method + i) = *(header.method + i);
  105.         num++;    /* increment total number of files */
  106.         sprintf(s1, "%02.2d/%02d/%02d",
  107.             (header.fdate & 0x01e0) / 32,
  108.             (header.fdate & 0x001f),
  109.             (header.fdate & 0xfe00) / 512 + 80
  110.         );
  111.         sprintf(s2, "%02d:%02d:%02d",
  112.             (header.ftime & 0xfc00) / 2048,
  113.             (header.ftime & 0x07e0) / 32,
  114.             (header.ftime & 0x001f)
  115.         );
  116.         t_fsize += header.fsize;
  117.         t_csize += header.csize;
  118.         if (header.fsize == 0L)
  119.             header.fsize = 1L;
  120.         pct = 100 - (char)((header.csize * 100) / header.fsize);
  121.         nice(fname);
  122.         sprintf(string, "%3d  %-12s  %9.5s  %8ld  %8ld  %2d%c  %s  %s  %04X",
  123.             num,
  124.             fname,
  125.             method,
  126.             header.fsize,
  127.             header.csize,
  128.             pct,
  129.             '%',
  130.             s1,
  131.             s2,
  132.             crc
  133.         );
  134.         puts(string);
  135.         if (fname != NULL) free(fname);
  136.         fseek(arc, header.csize, SEEK_CUR);
  137.     }
  138.     puts("───  ────────────             ────────  ────────  ───");
  139.     sprintf(string, "%3d  File(s)       Arc Total  %8ld  %8ld  %2d%c",
  140.         num,
  141.         t_fsize,
  142.         t_csize,
  143.         100 - (char)((t_csize * 100) / t_fsize),
  144.         '%'
  145.     );
  146.     puts(string);
  147.     return(num);
  148. }
  149.  
  150. void nice(char *s)
  151. {
  152.     char *p;
  153.  
  154.     strlwr(s);
  155.     *s -= 32;
  156.     p = strchr(s, ' ');
  157.     if (p != NULL) *(p+1) -= 32;
  158. }
  159.