home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / ARCHIVE / PKZDATE.ZIP / PKZDATE.C next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  10.2 KB  |  486 lines

  1. /******************************************************************************
  2.  *
  3.  *    Syntax:
  4.  *
  5.  *     PKZDATE Zip1[.ZIP] Zip2[.ZIP] Zip3.[ZIP]
  6.  *
  7.  *    Updates:
  8.  *
  9.  ******************************************************************************
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <alloc.h>
  16. #include <conio.h>
  17. #include <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <bios.h>
  20. #include <dir.h>
  21. #include <dos.h>
  22. #include <io.h>
  23. #include <share.h>
  24. #include <fcntl.h>
  25. #include "pkzip.h"
  26.  
  27. #define RELEASE_LEVEL    "0.03"
  28.  
  29. /* Global variables */
  30.  
  31. struct ZIPSTATS
  32.   {
  33.   int  Handle;
  34.   long FilePos;
  35.   struct CENTRAL_RECORD *CDRTable;
  36.   struct END_CENTRAL_RECORD *ECDPtr;
  37.   struct CENTRAL_RECORD *CurCDROff;
  38.   char Name[64];
  39.   };
  40.  
  41. struct ZIPSTATS Zip;
  42. int    RecurseSubdirs;
  43. static int   TotalZips;
  44.  
  45. /* Function prototypes */
  46.  
  47. extern    int main99(int argc, char * *argv);
  48.  
  49. int main(int argc, char *argv[])
  50. {
  51. int   rc;
  52.  
  53.   rc = main99(argc, argv);
  54.   return(rc);
  55. }
  56.  
  57. void DisplayError(int ErrorType)
  58. {
  59.   switch (ErrorType)
  60.     {
  61.     case -1:
  62.       printf("\nPKZDATE %s (TC 2.0L) - Latest File Dater", RELEASE_LEVEL );
  63.       printf("\n\nSyntax: PKZDATE Zip1[.ZIP] Zip2[.ZIP] .... ....");
  64.       printf("\n - Zipnames can be up 63 chars in length");
  65.       printf("\n - # of Zips limited only by command line length");
  66.       printf("\n - DOS d:[\path\]filename.ext fully supported");
  67.       printf("\n - Wildcards ok");
  68.       printf("\n - /S or -S to search all subdirs below specified one");
  69.       printf("\n");
  70.       printf("\nReturn Codes:");
  71.       printf("\n   0 - Successful completion");
  72.       printf("\n 255 - Help Screen / Incorrect parms specified");
  73.       printf("\n   1 - Unable to OPEN Zip");
  74.       printf("\n   2 - Unable to LSEEK in Zip");
  75.       printf("\n   3 - Unable to READ Zip");
  76.       printf("\n   4 - Unable to WRITE Zip");
  77.       printf("\n   5 - Unable to ALLOCATE for Zip");
  78.       printf("\n   6 - Unable to CREATE Zip");
  79.       printf("\n");
  80.       break;
  81.     case 1:
  82.       printf("\nCannot open: ");
  83.       break;
  84.     case 2:
  85.       printf("\nNot able to LSEEK in: ");
  86.       break;
  87.     case 3:
  88.       printf("\nNot able to READ in: ");
  89.       break;
  90.     case 4:
  91.       printf("\nNot able to WRITE in: ");
  92.       break;
  93.     case 5:
  94.       printf("\nOut of memory in: ");
  95.       break;
  96.     case 6:
  97.       printf("\nUnable to CREATE file: ");
  98.       break;
  99.     }
  100.  
  101.   if (ErrorType >= 0)
  102.     {
  103.     printf("%s", Zip.Name);
  104.     }
  105.   else
  106.     exit( ErrorType );
  107. }
  108.  
  109. int OpenZipFile( void )
  110. {
  111.  
  112.   if(_osmajor >= 3)
  113.     Zip.Handle = sopen(Zip.Name, O_RDONLY | O_BINARY, SH_DENYWR, S_IREAD);
  114.   else
  115.     Zip.Handle = open(Zip.Name, O_RDONLY | O_BINARY);
  116.  
  117.   if(Zip.Handle == -1)
  118.     {
  119.     DisplayError(2);
  120.     return(2);
  121.     }
  122.  
  123.   return(0);
  124. }
  125.  
  126. long LseekZip( long FilePos )
  127. {
  128. long rc, bytes2move;
  129.  
  130.   Zip.FilePos = tell( Zip.Handle );
  131.   bytes2move = FilePos - Zip.FilePos;
  132.  
  133.   rc = lseek( Zip.Handle, bytes2move, SEEK_CUR );
  134.   if ( rc <= -1L )
  135.     {
  136.     DisplayError(2);
  137.     return(2);
  138.     }
  139.   Zip.FilePos = rc;
  140.  
  141.   return(0);
  142. }
  143.  
  144. int ReadZip( char *ReadBuf, int ReadSize )
  145. {
  146. int BytesRead;
  147.  
  148.   BytesRead = _read( Zip.Handle, ReadBuf, ReadSize );
  149.   if ( BytesRead != ReadSize )
  150.     {
  151.     DisplayError(3);
  152.     return(3);
  153.     }
  154.  
  155.   Zip.FilePos += ReadSize;
  156.  
  157.   return(0);
  158. }
  159.  
  160. int WriteZip( char *WriteBuf, int WriteSize )
  161. {
  162. int BytesWritten;
  163.  
  164.   BytesWritten = _write( Zip.Handle, WriteBuf, WriteSize );
  165.   if ( BytesWritten != WriteSize )
  166.     {
  167.     DisplayError(4);
  168.     return(4);
  169.     }
  170.  
  171.   Zip.FilePos += WriteSize;
  172.  
  173.   return(0);
  174. }
  175.  
  176. int FindZipCentralDir( void )
  177. {
  178. int rc, break_flag, loop_counter, ZipCmtLngth;
  179. char Buffer[sizeof(struct END_CENTRAL_RECORD)];
  180. long *MatchPtr;
  181. long EndRecordOffset;
  182. long ReadSize, LseekSize;
  183. long allocate_size;
  184.  
  185.   Zip.FilePos = lseek(Zip.Handle, 0L, SEEK_END);
  186.   if ( Zip.FilePos == -1 || Zip.FilePos < sizeof(struct END_CENTRAL_RECORD) )
  187.     {
  188.     DisplayError(2);
  189.     return(2);
  190.     }
  191.  
  192.   ReadSize = (long)sizeof(struct END_CENTRAL_RECORD);
  193.   LseekSize = ReadSize;
  194.  
  195.   LseekZip( Zip.FilePos - ReadSize );
  196.  
  197.   /* Read 1st block into memory */
  198.  
  199.   ReadZip( (char *)Buffer, (int)ReadSize );
  200.  
  201.   while(1)
  202.     {
  203.     break_flag = 0;
  204.     loop_counter = 0;
  205.     for (MatchPtr = (long *)Buffer; MatchPtr < (long *)(Buffer + (int)ReadSize); ((char *)MatchPtr)++)
  206.       {
  207.       if (*MatchPtr == ENDCENTRALSIG )
  208.     {
  209.     break_flag = 1;
  210.     break;
  211.     }
  212.       loop_counter++;
  213.       }
  214.  
  215.     if (break_flag)
  216.       break;
  217.  
  218.     /* Move 1st 3 bytes to end of buffer */
  219.  
  220.     for (rc = 0; rc < 3; rc++)
  221.       Buffer[sizeof(struct END_CENTRAL_RECORD) - 3 + rc] = Buffer[rc];
  222.  
  223.     ReadSize = (long)(sizeof(struct END_CENTRAL_RECORD) - 3);
  224.  
  225.     LseekZip( Zip.FilePos -(ReadSize + LseekSize) );
  226.  
  227.     LseekSize = ReadSize;
  228.  
  229.     /* Read next block into memory */
  230.  
  231.     ReadZip( (char *)Buffer, (int)ReadSize );
  232.     }
  233.  
  234.   EndRecordOffset = Zip.FilePos + loop_counter - ReadSize;
  235.  
  236.   LseekZip( EndRecordOffset + sizeof(struct END_CENTRAL_RECORD) -
  237.      sizeof(int) );
  238.  
  239.   ReadZip( (char *)&ZipCmtLngth, sizeof(int) );
  240.  
  241.   allocate_size = (long)(sizeof(struct END_CENTRAL_RECORD) + ZipCmtLngth);
  242.   Zip.ECDPtr = (struct END_CENTRAL_RECORD *)calloc( (int)allocate_size, 1 );
  243.   if ( Zip.ECDPtr == NULL )
  244.     {
  245.     DisplayError(5);
  246.     return(5);
  247.     }
  248.  
  249.   LseekZip( EndRecordOffset );
  250.  
  251.   ReadSize = allocate_size;
  252.  
  253.   /* Read ECR into memory */
  254.  
  255.   ReadZip( (char *)Zip.ECDPtr, (int)ReadSize );
  256.  
  257. #if 0
  258.   printf("\n%s -> Located ECR at offset %ld!", Zip.Name,
  259.      EndRecordOffset);
  260.   printf("\n%s -> Central Dir at offset %ld has %d entries occupying %d bytes!",
  261.      Zip.Name,
  262.      Zip.ECDPtr->CentralDirOffset,
  263.      Zip.ECDPtr->CentralDirEntries,
  264.      Zip.ECDPtr->CentralDirSize );
  265. #endif
  266.   return(0);
  267. }
  268.  
  269. int LoadCentralDir( void )
  270. {
  271. long allocate_size;
  272. char *CharPtr;
  273.  
  274.   allocate_size = Zip.ECDPtr->CentralDirSize;
  275.   Zip.CDRTable =
  276.      (struct CENTRAL_RECORD *)calloc( (int)allocate_size, 1 );
  277.   if ( Zip.CDRTable == NULL )
  278.     {
  279.     DisplayError(5);
  280.     return(5);
  281.     }
  282.  
  283.   LseekZip( Zip.ECDPtr->CentralDirOffset );
  284.  
  285.   CharPtr = (char *)Zip.CDRTable;
  286.   ReadZip( CharPtr, (int)allocate_size );
  287.  
  288.   return(0);
  289. }
  290.  
  291. int FindLatestDate( struct ftime *ft )
  292. {
  293. struct CENTRAL_RECORD *SourceCDR;
  294. int rc, SourceCD;
  295. unsigned LatestDate, LatestTime;
  296.  
  297.   rc = LatestDate = LatestTime = 0;
  298.   for (SourceCD = 0; SourceCD < Zip.ECDPtr->CentralDirEntries; SourceCD++)
  299.     {
  300.  
  301.     if (SourceCD == 0)
  302.       SourceCDR = Zip.CDRTable;
  303.     else
  304.       (char *)SourceCDR += ( SIZECDR + SourceCDR->CommentFieldLength +
  305.       SourceCDR->FileNameLength );
  306.  
  307.     Zip.CurCDROff = SourceCDR;
  308.  
  309.     if ( SourceCDR->FileDate > LatestDate )
  310.       {
  311.       LatestDate = SourceCDR->FileDate;
  312.       LatestTime = SourceCDR->FileTime;
  313.       }
  314.     else
  315.       {
  316.       if ( SourceCDR->FileDate == LatestDate )
  317.     {
  318.     if ( SourceCDR->FileTime > LatestTime )
  319.       {
  320.       LatestDate = SourceCDR->FileDate;
  321.       LatestTime = SourceCDR->FileTime;
  322.       }
  323.     }
  324.       }
  325.     }
  326.   ft->ft_tsec  = LatestTime & 0x1f;
  327.   ft->ft_min   = ( LatestTime & 0x7e0 ) >> 5;
  328.   ft->ft_hour  = ( LatestTime & 0xf800 ) >> 11;
  329.   ft->ft_day   = LatestDate & 0x1f;
  330.   ft->ft_month = ( LatestDate & 0x1e0 ) >> 5;
  331.   ft->ft_year  = ( LatestDate & 0xfe00 ) >> 9;
  332.  
  333.   return( rc );
  334. }
  335.  
  336. int SetLatestZipDate( char *ZipName )
  337. {
  338. int rc;
  339. struct ftime ftimep;
  340.  
  341.   rc = 0;
  342.  
  343.   strcpy( Zip.Name, ZipName );
  344.  
  345.   rc = OpenZipFile();
  346.   if ( !rc )
  347.     {
  348.     rc = FindZipCentralDir();
  349.     if ( !rc )
  350.       {
  351.       rc = LoadCentralDir();
  352.       if ( !rc )
  353.     {
  354.     FindLatestDate( &ftimep );
  355.     setftime( Zip.Handle, &ftimep );
  356.     TotalZips++;
  357.     printf( "\nDated->%s.", Zip.Name );
  358.     }
  359.       free( Zip.CDRTable );
  360.       }
  361.     close( Zip.Handle );
  362.     }
  363.  
  364.   return( rc );
  365. }
  366.  
  367. ProcessFile(char *FileMask)
  368. {
  369.   struct   ffblk c_file;
  370.   char       full_file[65];
  371.   int       rc;
  372.   char       Drive[3];
  373.   char       Path[65];
  374.   char       Fname[13];
  375.   char       Ext[5];
  376.   unsigned AttributeMask;
  377.  
  378.   fnsplit( FileMask, Drive, Path, Fname, Ext );
  379.  
  380.   if ( Fname == NULL )
  381.     return(1);
  382.  
  383.   rc = 0;
  384.   AttributeMask = FA_ARCH;
  385.   if ( !findfirst( FileMask, &c_file, AttributeMask ) )
  386.     {
  387.     do
  388.       {
  389.       strcpy( full_file, Drive );
  390.       strcat( full_file, Path );
  391.       strcat( full_file, c_file.ff_name );
  392.       SetLatestZipDate( full_file );
  393.       if ( kbhit() )
  394.     break;
  395.       } while ( findnext( &c_file ) == 0 );
  396.     }
  397.  
  398.   if ( RecurseSubdirs )
  399.     {
  400.     strcpy( full_file, Drive );
  401.     strcat( full_file, Path );
  402.     strcat( full_file, "*.*" );
  403.  
  404.     AttributeMask = FA_DIREC;
  405.     if ( !findfirst( full_file, &c_file, AttributeMask ) )
  406.       {
  407.       do
  408.     {
  409.     if ( strcmpi( c_file.ff_name, ".") != 0 &&
  410.        strcmpi( c_file.ff_name, ".." ) != 0 )
  411.       {
  412.       strcpy( full_file, Drive );
  413.       strcat( full_file, Path );
  414.       strcat( full_file, c_file.ff_name );
  415.       strcat( full_file, "\\" );
  416.       strcat( full_file, Fname );
  417.       strcat( full_file, Ext );
  418.       ProcessFile( full_file );
  419.       if ( kbhit() )
  420.         break;
  421.       }
  422.     } while ( findnext( &c_file ) == 0 );
  423.       }
  424.     }
  425.  
  426.   return(rc);
  427. }
  428.  
  429. void ParseSwitchesOffCommandline( int argc, char *argv[] )
  430. {
  431. int index;
  432.  
  433.   for( index = 1; index < argc; index++)
  434.     {
  435.     strupr( argv[ index ] );
  436.     if ( *argv[ index ] == '-' || *argv[ index ] == '/' )
  437.       {
  438.       switch( argv[ index ][ 1 ] )
  439.     {
  440.     case 'S':
  441.       RecurseSubdirs++;
  442.       argv[ index ] = NULL;
  443.       break;
  444.     }
  445.       }
  446.     }
  447. }
  448.  
  449. int main99(int argc, char *argv[])
  450. {
  451. int rc, loop_counter;
  452. char FileMask[64];
  453.  
  454.   rc = 0;
  455.   if( argc < 2 )
  456.     DisplayError(-1);
  457.  
  458.   ParseSwitchesOffCommandline( argc, argv );
  459.  
  460.   printf("\nPKZDATE %s - Zip File Dater", RELEASE_LEVEL );
  461.   printf("\n - processing %d filemask(s) ....\n", argc - 1);
  462.  
  463.   TotalZips = 0;
  464.   for (loop_counter = 1; loop_counter < argc; loop_counter++)
  465.     {
  466.  
  467.     if ( strlen( argv[ loop_counter ] ) > 63 )
  468.       DisplayError(-1);
  469.  
  470.     strcpy( FileMask, argv[ loop_counter ] );
  471.     strupr( FileMask );
  472.  
  473.     /* If the .ZIP extension is missing add it. */
  474.  
  475.     if ( strchr( FileMask, '.') == NULL )
  476.       strcat( FileMask, ".ZIP" );
  477.  
  478.     ProcessFile( FileMask );
  479.     }
  480.  
  481.   printf("\n%d Zip(s) dated.\n", TotalZips );
  482.  
  483.   return(rc);
  484. }
  485.  
  486.