/*
 * LPatch.c - C. Scheppner  03/87
 *
 *    Corrects the startup code of some 1.0/1.1 programs such as
 *    Atom so that they don't abort during startup with the Alert
 *    00038007. (can't open dos library).  The problem is caused
 *    by d0 (version) being uninitialized prior to the OpenLibrary.
 *
 *   LINKAGE INFO:
 *     Compile with -v flag on LC2
 *     Link with AStartup.obj ... LIBRARY Amiga.lib, LC.lib
 */

#include <exec/types.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>

/* Patch Offsets, Old Values, New Values */

ULONG  offset[] = {0x196,0x197, 0x213, 0x23E,0x23F,0x240,0x241,0x242,0x243};
UBYTE  oldval[] = {0x01, 0x4E,  0xD2,  0x24, 0x3C, 0x00, 0x00, 0x03, 0xED};
UBYTE  newval[] = {0x00, 0xA8,  0x2C,  0x70, 0x00, 0x60, 0x00, 0x00, 0xA2};

LONG file;

main(argc, argv)
int argc;
char **argv;
   {
   LONG            rLen, wLen;
   char            *filename;
   UBYTE  buf[4];
   int k;

   if (argc==0) cleanexit("");
   if ((argc==1)||(argc>2)||(*argv[1]=='?'))
      cleanexit("Usage: LPatch filename");

   filename = argv[1];

   if(!(file = Open(filename, MODE_OLDFILE)))
      cleanexit("File not found");

   for(k=0;k<9;k++)
      {
      Seek(file,offset[k],OFFSET_BEGINING);
      if((rLen = Read(file,buf,1)) < 1)  cleanexit("Read error");
      if(buf[0] != oldval[k])  cleanexit("File not correct for patch");
      }

   for(k=0;k<9;k++)
      {
      Seek(file,offset[k],OFFSET_BEGINING);
      if((wLen = Write(file,&newval[k],1)) < 1)  cleanexit("Write error");
      }

   cleanup();
   }


cleanexit(s)
   char  *s;
   {
   if (*s)
      {
      printf("%s \n",s);
      }
   cleanup();
   exit();
   }

cleanup()
   {
   if(file)  Close(file);
   }

/* end */


