home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / ARCHIVE / REDATE2.ZIP / REDATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-22  |  8.6 KB  |  276 lines

  1. /*--------------------------------------------------------------------------*/
  2. /* Yet another semi-useful utility: REDATE *.PAK                            */
  3. /* Sets the file time and date for the output of NoGate's PAK.EXE files to  */
  4. /* the time and date of newest member file in a PAK archive.                */
  5. /*                                                                          */
  6. /* If you break it, you can keep both parts. This just fixes a small hassle */
  7. /* for people converting from SEA ARC format to NoGate's PAK format.        */
  8. /*                                                                          */
  9. /* Copyright 1989, Doug Boone. FidoNet 119/5                                */
  10. /*      bcc -a- -f- -G -K -lt -mt -Z redate.c                               */
  11. /*                                                                          */
  12. /*--------------------------------------------------------------------------*/
  13. /* March 22, 1991  REDATE2.ZIP                                              */
  14. /* Fixed bug in LZH files that were padded with ^Z.                         */
  15. /* Fixed PAK type 12 header to be included                                  */
  16. /*--------------------------------------------------------------------------*/
  17.  
  18. #include    <stdio.h>
  19. #include    <string.h>
  20. #include    <ctype.h>
  21. #include    <time.h>
  22. #include    <dos.h>
  23. #include    <dir.h>
  24. #include    <io.h>
  25. #include    <fcntl.h>
  26. #include    <dos.h>
  27. #include   <stdlib.h>
  28. #include    <sys\types.h>
  29. #include    <sys\stat.h>
  30. #include    <errno.h>
  31. /* You don't need everything in archdr.h, but I don't want to trim it. */
  32.  
  33. #include    "archdr.h"
  34.  
  35.  
  36. unsigned    int     mbrtime;
  37. unsigned    int     mbrdate;
  38.  
  39. int checkpak(int arc);        /* list files in archive */
  40. int checkzip(int infile);
  41. int checklzh(int infile);
  42. int checkarj(int fh);        /* list files in archive */
  43.  
  44. void cdecl main(int argc, char *argv[])
  45. {
  46.     int     i;
  47.     int     done;
  48.     struct  ffblk   ffblk;
  49.     char    name[80];
  50.     int     infile;        /* file handle */
  51.     char    *firstchar;
  52.     char    apath[80];
  53.     char    *result;
  54.     int     success;
  55.     union    REGS    regs;
  56.  
  57.     if (argc < 2) {
  58.         printf ("\nFixes the date for PAK, ZIP, ARJ and LZH files\n\n");
  59.         exit(1);
  60.         }
  61.     firstchar = (char *) malloc(1);
  62.  
  63.     for (i=1; i<argc; i++) {
  64.         strcpy(apath,argv[i]);
  65.         result = strrchr(apath,'\\');
  66.         if (result != NULL)
  67.             *++result = '\0';
  68.         else {
  69.             result = strrchr(apath,':');
  70.             if (result !=NULL)
  71.                 *++result = '\0';
  72.             else
  73.                 apath[0] = '\0';
  74.             }
  75.         if (strncmp(argv[i],".",1)==0)
  76.             strcpy(argv[i],"*.*");
  77.         done = findfirst(argv[i],&ffblk,0);
  78.         while (done == 0) {
  79.             strcpy(name,apath);
  80.             strcat(name,ffblk.ff_name);
  81.             printf("Resetting file date on %s",name);
  82.             infile = open(name,O_RDONLY|O_BINARY);
  83.             if (infile > 0)
  84.                 success = read(infile,firstchar,1);
  85.             firstchar[0] = toupper(firstchar[0]);
  86.             mbrtime = 0;
  87.             mbrdate = 0;
  88.            switch(firstchar[0]) {
  89.                case    0x1a:    success = checkpak(infile); break;
  90.                case    'P':     success = checkzip(infile); break;
  91.                case    '`':     success = checkarj(infile); break;
  92.                default:         success = checklzh(infile); break;
  93.                }
  94.             if (success<0)
  95.                 printf("\n%s Not an archive file\n\n",name);
  96.             else {
  97.                 regs.h.ah = 0x57;
  98.                 regs.h.al = 0x01;
  99.                 regs.x.bx = infile;
  100.                 regs.x.cx = mbrtime;
  101.                 regs.x.dx = mbrdate;
  102.                 int86(0x21,®s,®s);
  103.                 printf(" to %2d/%02d/%02d  %02d:%02d:%02d\n",
  104.                     ((mbrdate >> MONTH_SHIFT) & MONTH_MASK),
  105.                     (mbrdate & DAY_MASK),
  106.                     ((mbrdate >> YEAR_SHIFT) + DOS_EPOCH),
  107.                     ((mbrtime >> 11) & 0x1f),
  108.                     ((mbrtime >> 5) & 0x3f),
  109.                     (mbrtime &0x1f)*2);
  110.                 }
  111.             close(infile);
  112.             done = findnext(&ffblk);
  113.             }        /* end of findfirst/findnext loop */
  114.         }        /* end of for loop */
  115.     free(firstchar);
  116.     exit(0);
  117. }
  118.  
  119.  
  120. /* ====================================================================
  121.  * start of list arc contents processing
  122.  * ====================================================================
  123.  */
  124. int checkpak(int arc)        /* list files in archive */
  125. {
  126.     struct     heads *hdr;        /* header data */
  127.     char    *ver = " ";
  128.  
  129.  
  130.     hdr = (struct heads *) malloc(sizeof(struct heads));
  131.  
  132.     lseek(arc,1L,SEEK_SET);
  133.     read(arc,ver,1);
  134.     while (*ver > 0 && *ver < 12) {
  135.  
  136.         read(arc,hdr,sizeof(struct heads));
  137.         if (mbrdate < hdr->mbrdate) {
  138.             mbrdate = hdr->mbrdate;
  139.             mbrtime = hdr->mbrtime;
  140.             }
  141.         if (mbrdate == hdr->mbrdate &&
  142.             mbrtime < hdr->mbrtime)
  143.             mbrtime = hdr->mbrtime;
  144.  
  145.         lseek(arc,hdr->mbrsize+1L,SEEK_CUR);
  146.         read(arc,ver,1);
  147.         };        /* End of while loop */
  148.     free(hdr);
  149.     return(0);
  150. }
  151.  
  152. int checkzip(int infile)
  153. {
  154.     struct  ID_Hdr          *ID;
  155.     struct  Local_Hdr       *local;
  156.     int        check;
  157.  
  158.  
  159.     ID = (struct ID_Hdr *) malloc(sizeof(struct ID_Hdr));
  160.     local = (struct Local_Hdr *) malloc(sizeof(struct Local_Hdr));
  161.     lseek(infile,0,SEEK_SET);
  162.     do {
  163.         check = read(infile,(void *)ID,sizeof(struct ID_Hdr));
  164.         if (ID->Head_Type == LOCAL_HEADER) {
  165.             if ((check = read(infile,(void *)local,sizeof(struct Local_Hdr))) != -1) {
  166.                 if (mbrdate < local->mod_date) {
  167.                     mbrdate = local->mod_date;
  168.                     mbrtime = local->mod_time;
  169.                     }
  170.                 if (mbrdate == local->mod_date &&
  171.                     mbrtime < local->mod_time)
  172.                     mbrtime = local->mod_time;
  173.  
  174.                 lseek(infile,local->size_now+((long)(local->name_length + local->Xfield_length)),SEEK_CUR);
  175.                 }        /* End of one entry */
  176.             }        /* End of grabbing local directory entries */
  177.         else
  178.             check = 0;
  179.         } while(check >0 && !eof(infile));        /* End of file */
  180.     free(ID);
  181.     free(local);
  182.     return(0);
  183. }
  184.  
  185. int checklzh(int infile)
  186. {
  187.    struct  Lharc_Hdr   *local;
  188.     int        check;
  189.    long    where;
  190.    unsigned    time;
  191.    unsigned    date;
  192.    struct  tm *tm;
  193.  
  194. /*--------------------------------------------------------------------------*/
  195.     lseek(infile,0L,SEEK_SET);
  196.    local = (struct Lharc_Hdr *) malloc(sizeof(struct Lharc_Hdr));
  197.  
  198.     if ((check = read(infile,(void *)local,sizeof(struct Lharc_Hdr))) ==
  199.         sizeof(struct Lharc_Hdr)) {
  200.             if (!((strncmp(local->type,"-lh",3) == 0) &&
  201.                 (local->type[4] == '-'))) {
  202.                 free(local);
  203.                 return(-1);
  204.                 }
  205.             }
  206.     else {
  207.         free(local);
  208.         return(-1);
  209.         }
  210.     where = 0L;
  211.     do {
  212.         switch(local->level) {
  213.             case 0: 
  214.             case 1: where += local->size_now + 2L + (local->size_header & 0xff);
  215.                    if (mbrdate < local->orig.dtime.date) {
  216.                        mbrdate = local->orig.dtime.date;
  217.                        mbrtime = local->orig.dtime.time;
  218.                        }
  219.                    else if ((mbrdate == local->orig.dtime.date) &&
  220.                        (mbrtime < local->orig.dtime.time))
  221.                        mbrtime = local->orig.dtime.time;
  222.                    break;
  223.  
  224.             case 2:     where += local->size_now + local->size_header;
  225.                         tm = localtime(&(local->orig.utc));
  226.  
  227.                        date =  (tm->tm_year - 80) << 9;
  228.                        date += (tm->tm_mon +1) << 5;
  229.                        date += tm->tm_mday;
  230.  
  231.                        time =  tm->tm_hour << 11;
  232.                        time += tm->tm_min << 5;
  233.                        time += tm->tm_sec/2;
  234.  
  235.                    if (mbrdate < date) {
  236.                        mbrdate = date;
  237.                        mbrtime = time;
  238.                        }
  239.                    else if ((mbrdate == date) && (mbrtime < time))
  240.                        mbrtime = time;
  241.                    break;
  242.             }
  243.         where = lseek(infile,where,SEEK_SET);
  244.         check = read(infile,(void *)local,sizeof(struct Lharc_Hdr));
  245.         } while(check >1 && !strncmp(local->type,"-lh",3));        /* End of file */
  246.     free(local);
  247.     return(0);
  248. }
  249.  
  250. int checkarj(int fh)        /* list files in archive */
  251. {
  252.    struct  _ARJ_main   ahead;
  253.    word    ID;
  254.    word    len;
  255.  
  256.     lseek(fh,0L,SEEK_SET);
  257.    while ((read(fh,(char *)&ID,sizeof(word))) && (ID == 0xea60)) {
  258.         if (read(fh,(char *)&ahead,sizeof(struct _ARJ_main)) == sizeof(struct _ARJ_main)) {
  259.           len = ahead.base_size - sizeof(struct _ARJ_main);
  260.           lseek(fh,((long)len),SEEK_CUR);
  261.            if (ahead.c_size > 0L) {
  262.                if (mbrdate < ahead.stamp.date) {
  263.                    mbrdate = ahead.stamp.date;
  264.                    mbrtime = ahead.stamp.time;
  265.                    }
  266.                if (mbrdate == ahead.stamp.date && mbrtime < ahead.stamp.time)
  267.                    mbrtime = ahead.stamp.time;
  268.                    }
  269.  
  270.            lseek(fh,(8L+ahead.c_size),SEEK_CUR);
  271.            }
  272.         };        /* End of while loop */
  273.     return(0);
  274. }
  275.  
  276.