home *** CD-ROM | disk | FTP | other *** search
/ norge.freeshell.org (192.94.73.8) / 192.94.73.8.tar / 192.94.73.8 / pub / computers / pcjr / arc / PAKUTL12.LZH / PAKLIST.C < prev    next >
Text File  |  1988-09-10  |  5KB  |  242 lines

  1. /*
  2.     PAKLIST.C    List contents of .PAK archive.
  3.  
  4.     Copyright 1988, Michael J. Housky
  5.     Released to the Public Domain ... *no* rights reserved.
  6. */
  7.  
  8. /*
  9.     Update log:
  10. ****************************************************************************
  11.     Version 1.2, 10 September 1988: Released to public domain.
  12. ****************************************************************************
  13.     Version 1.0, 27 August 1988: First version.
  14. ****************************************************************************
  15. */
  16.  
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <io.h>
  21.  
  22. #include "dirfn.h"
  23. #include "pakdefn.h"
  24.  
  25. #define BUF_SIZE    4096        /* constant buffer size */
  26.  
  27. /* ------------    Global data: */
  28.  
  29. FILE_TYPE    *file_list = NULL;    /* list of files (malloc'ed) */
  30. unsigned char    pak_buf[BUF_SIZE];
  31. pak_hdr        fhdr;
  32. long        pakloc,paklen;
  33.  
  34.  
  35. /*
  36.     read_pakhdr:    Read header of .PAK member file
  37. */
  38.  
  39. int read_pakhdr( FILE* );
  40.  
  41. int read_pakhdr( fp )
  42.     FILE    *fp;
  43. {
  44.     register int i;
  45.  
  46.     i = fgetc(fp);            /* get file marker */
  47.     if ( i != PAK_MARK )        /* error if not correct */
  48.     return -1;
  49.  
  50.     i = fread( (char*)&fhdr, 1, PAK_HDRLEN, fp );
  51.  
  52.     if ( i>=1 && fhdr.type==0 )
  53.     return 1;            /* end of file mark seen */
  54.  
  55.     if ( i!=PAK_HDRLEN || fhdr.slen<0L || fhdr.olen<fhdr.slen )
  56.     return -1;            /* invalid header seen */
  57.  
  58.     pakloc = ftell(fp);
  59.     if ( pakloc+fhdr.slen > paklen )
  60.     return -1;
  61.     return 0;
  62.  
  63. } /* read_pakhdr */
  64.  
  65.  
  66. /*
  67.     pak_open:    Open a .PAK file, test for valid first header.
  68. */
  69.  
  70. FILE *pak_open( char* );
  71.  
  72. FILE *pak_open( fn )
  73.     char    *fn;
  74. {
  75.     register int
  76.         i;
  77.     FILE    *fp;
  78.  
  79. /* Open the file: */
  80.  
  81.     fp = fopen( fn, "rb" );
  82.     if ( fp==NULL )
  83.     {
  84.     printf("Can't open: %s\n",fn);
  85.     return NULL;
  86.     }
  87.     paklen = filelength(fileno(fp));
  88.  
  89. /* Read the first header: */
  90.  
  91.     i = read_pakhdr(fp);
  92.     if ( i )
  93.     {
  94.     printf("%s is not a .PAK archive\n",fn);
  95.     fclose(fp);
  96.     return NULL;
  97.     }
  98.  
  99.     return fp;
  100.  
  101. } /* pak_open */    
  102.  
  103.  
  104. /*
  105.     pak_check:    Check one .PAK file by reading headers.
  106. */
  107.  
  108. int pak_check( char* );
  109.  
  110. int pak_check( fn )
  111.     char    *fn;
  112. {
  113.     register    int i;
  114.     FILE    *fp;
  115.     long    l;
  116.     static char    mo_tab [13] [4] =
  117.     {
  118.     "???",
  119.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  120.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  121.     };
  122.  
  123.     printf("\nSearching ... %s\n\n", fn);
  124.  
  125.     fp = pak_open( fn );
  126.     if ( fp == NULL )
  127.     return -1;
  128.  
  129.     printf("Filename       Length     Size     Date        Time     CRC\n");
  130.     printf("------------   ------    -----  -----------  --------  ----\n");
  131.     /*      nnnnnnnn.xxx ssssssss ssssssss  dd-mmm-yyyy  hh:mm:ss  xxxx */
  132.     while ( (l = pakloc+fhdr.slen) < paklen )
  133.     {
  134.     printf("%-12s %8ld %8ld  %2d-%3s-%02d  %2d:%02d:%02d  %04X\n",
  135.         fhdr.fname,
  136.         fhdr.olen,
  137.         fhdr.slen,
  138.         ((fhdr.ddate>> 0) & 0x1F),
  139.         mo_tab[((fhdr.ddate>> 5) & 0x0F)],
  140.         ((fhdr.ddate>> 9) & 0x7F)+1980,
  141.         ((fhdr.dtime>>11) & 0x1F),
  142.         ((fhdr.dtime>> 5) & 0x3F),
  143.         ((fhdr.dtime<< 1) & 0x3E),
  144.         fhdr.crc);
  145.  
  146.     i = fseek( fp, l, SEEK_SET );
  147.     if ( i )
  148.     {
  149.         printf("seek error on %s\n",fn);
  150.         return -1;
  151.     }
  152.  
  153.     i = read_pakhdr(fp);
  154.     if ( i==1 )
  155.     {                /* end of file seen */
  156.         fclose(fp);            /* close file */
  157.         return 0;            /* normal return */
  158.     }
  159.     if ( i<0 )
  160.     {                /* invalid header seen */
  161.         fclose(fp);
  162.         printf("invalid header found\n");
  163.         return -1;
  164.     }
  165.     /* repeat if more files possible */
  166.     }
  167.     fclose(fp);
  168.     printf("invalid file, possibly truncated\n");
  169.     return -1;
  170.  
  171. } /* pak_check */
  172.  
  173.  
  174. /*
  175.     PAKLIST.C main program
  176. */
  177.  
  178.  
  179. int main(int,char **);
  180. int main( argc, argv )
  181.     int        argc;
  182.     char    **argv;
  183. {
  184.     register int
  185.         i,j;
  186.     long    len;
  187.     int        addext;
  188.     int        num_files;
  189.     char    fpath[128];        /* full path\name of file */
  190.     char    *fnptr;            /* location of filename in fpath */
  191.  
  192. /* First, identify self and examine paramter: */
  193.  
  194.     printf("\nPAKLIST v1.2\n");
  195.     if ( argc<2 || !strcmp(argv[1],"?") )
  196.     {
  197.     printf("Usage: paklist <fname>\n");
  198.     return 0;
  199.     }
  200.  
  201. /* Parameter valid, find path and decide about default extension */
  202.  
  203.     strcpy( fpath, argv[1] );        /* extract path, find end */
  204.     i = strlen(fpath);            /* index of last character */
  205.     addext = 1;                /* presume no extension */
  206.     while ( --i >= 0 )            /* search backward thru argument */
  207.     {
  208.     j = fpath[i];
  209.     if ( j=='\\' || j==':' )
  210.         break;            /* colon or backslash ends search */
  211.     if ( j=='.' )
  212.         addext = 0;            /* period found, no default */
  213.     }
  214.     fnptr = fpath+i+1;
  215.     strupr(fpath);            /* uppercase for display */
  216.     if ( addext )
  217.     strcat(fpath,".PAK");        /* add default extension */
  218.  
  219.     num_files = get_files(fpath,0,&file_list);
  220.     if (num_files < 0)
  221.     {
  222.     printf("Too many files: out of memory.\n");
  223.     return(1);
  224.     }
  225.  
  226.     if (num_files < 1)
  227.     {
  228.     printf("Cannot find file(s): %s\n",argv[1]);
  229.     return(1);
  230.     }
  231.  
  232. /* Now, list contents of each file: */
  233.  
  234.     for (i = 0; i < num_files; ++i)
  235.     {
  236.     strcpy( fnptr, file_list[i].filename );
  237.     j = pak_check( fpath );
  238.     }
  239.     return 0;
  240.  
  241. } /* main */
  242.