{ Zip file comment adder 1.0                   }
{ Copyright (c) 1990 McBrine Computer Products }
{ See below for more info                      }

program zipcomment(zipfile,cmntfile,output);
uses
 dos;
type                              { Comment is stored on the heap }
 ptr=^foo;                        { Note TP4.0 will not accept 0..65535 }
 foo=record                       { as a range for an array! }
  bar:array[0..65534] of byte
 end;                                  
var
 zipfile,cmntfile:file of byte;
 zippat,cmntname:string[80];
 x,y,z:longint;
 m,n,o,clength:word;
 a,b,t,e:byte;
 comment:ptr;
 scanz:searchrec;

procedure sz;
 begin                            { Too simple to be a procedure in its }
  seek(zipfile,x)                 { own right, it seems; but it results in }
 end;                             { a much shorter compiled program }

procedure rnp;
 begin
  read(zipfile,a,b)
 end;

function rno:word;
 begin
  rnp;
  rno:=a+256*b
 end;

procedure gettype;
 begin
  x:=x+4;
  t:=0;
  rnp;
  if (a=80) and (b=75) then
   begin
    rnp;
    case a+b of
      7:t:=1;
      3:t:=2;
     11:t:=3
    end
   end
 end;

procedure error1;
 begin
  writeln('Zip File Comment Adder 1.0');
  writeln('by William J. McBrine III');
  writeln('Copyright (c) 1990 McBrine Computer Products');
  writeln;
  writeln('Syntax is:');
  writeln;
  writeln('ZIPCMNT zipfile[.zip] cmntfile.ext [/l]');
  writeln;
  writeln('"Zipfile" is the filespec to add a comment to, and may include wildcards. The');
  writeln('extension ".ZIP" is assumed if none is given. "Cmntfile.ext" is the file con-');
  writeln('taining the comment; can be any file under 64K (use "NUL" to strip comments).');
  writeln('"/l" option (must be third parameter) adds in two <CR><LF>s before the comment.');
  writeln;
  writeln('This program is free software, distributed under the terms of the GNU General');
  writeln('Public License as published by the Free Software Foundation, version 1. It is');
  writeln('distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. See');
  writeln('LICENSE.DOC for details. It should have come with this program; if not, write:');
  writeln;
  writeln('Free Software Foundation, Inc. / 675 Massachusetts Avenue / Cambridge, MA 02139');
  writeln;
  writeln('author: William J. McBrine III / 514 S. Jackson St. / Salisbury, NC 28144-5428');
  writeln('email : WILLIAM MCBRINE on HomeBoy''s Digital Undergound, (704) 637-2342  3-24');
  writeln('                         & The Big Byte, (704) 279-2295  3-96 dual');
  halt
 end;

procedure skipfile;
 begin
  x:=x+14;
  sz;
  y:=rno;
  z:=rno;
  y:=y+65536*z;
  x:=x+8;
  sz;
  m:=rno;
  n:=rno;
  x:=x+y+m+n+4;
  sz
 end;

procedure skipdir;
 begin
  x:=x+24;
  sz;
  m:=rno;
  n:=rno;
  o:=rno;
  x:=x+m+n+o+18;
  sz
 end;

procedure readcmnt;
 begin
  writeln('Reading comment file: ',cmntname);
  assign(cmntfile,cmntname);
  reset(cmntfile);
  clength:=filesize(cmntfile);
  mark(comment);
  getmem(comment,clength);
  if clength>0 then
   with comment^ do for m:=0 to clength-1 do read(cmntfile,bar[m]);
  close(cmntfile)
 end;

procedure onezip;
 begin
  writeln('Commenting: ',scanz.name);
  assign(zipfile,scanz.name);
  reset(zipfile);
  if filesize(zipfile)<4 then t:=0
  else
   begin
    x:=0;
    repeat
     gettype;
     case t of
      1:skipfile;
      2:skipdir
     end
    until (t=3) or (t=0)
   end;
  if t=0 then
   begin
    writeln(' I can''t read this ZIP file!');
    close(zipfile)
   end
  else
   begin
    x:=x+16;
    sz;

{ We're now at the zipfile comment length field }

    m:=clength+e;
    a:=lo(m);
    b:=hi(m);
    write(zipfile,a,b);
    if e=4 then
     begin
      a:=13;
      b:=10;
      write(zipfile,a,b,a,b)
     end;
    if clength>0 then
     with comment^ do for n:=0 to clength-1 do write(zipfile,bar[n]);
    truncate(zipfile);
    close(zipfile)
   end
 end;

begin
 e:=0;
 if paramcount=3 then
  if paramstr(3)='/l' then e:=4
  else
 else if paramcount<>2 then error1;
 zippat:=paramstr(1);
 cmntname:=paramstr(2);
 if pos('.',zippat)=0 then zippat:=concat(zippat,'.zip');
 readcmnt;
 findfirst(zippat,anyfile,scanz);
 if doserror=0 then
  repeat
   onezip;
   findnext(scanz)
  until doserror<>0
end.
