home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d569 / trackmaster.lha / TrackMaster / src / tm.c < prev    next >
C/C++ Source or Header  |  1991-11-21  |  25KB  |  570 lines

  1. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  2. //
  3. //  Soucecode:  TrackMaster
  4. //
  5. //  © by BrainTrace Design    / Carsten Schlote, Egelseeweg 52,
  6. //                              6302 Lich 1
  7. //
  8. //  Date          Author         Comment
  9. //  =========     =========      ========================
  10. //  26-Sep-91     Schlote        Offset & Directory Offset / Rev. Specialmdir
  11. //  12-Sep-91     Schlote & MB   Relocator & Link feature
  12. //  10-Aug-91     Schlote        Large-Size Dirtable
  13. //  25-Jul-91     Schlote        Added lzh-coding of data
  14. //  22-Jul-91     Schlote        Created this file!
  15. //
  16. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  17.  
  18. #include "tm.h"
  19.  
  20. #define BLKLENGTH( real )  ((real & 0xffffe00)+0x200)
  21. #define COMPBLKLEN( real )  ( BLKLENGTH( real + ENCODEEXTRA( real ) ) )
  22.  
  23. #define  MAXENTRIES (4*64)                /* Maximale Anzahl von Files  */
  24. #define  DIRBLOCK   (2*512)               /* Offset to DirBlock         */
  25.  
  26. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  27. // The following stuff defines some code & data into glues.a
  28.  
  29. extern LONG __asm BBChkSum( register __a0 LONG * bootblock );
  30. extern LONG __chip BootBlock[];
  31. extern LONG __chip LoaderOffset, __chip LoaderLength;
  32. extern LONG __chip NumFiles;
  33.  
  34. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  35.  
  36. OPAIR __chip dirfeld[MAXENTRIES];   // Dieses Feld enthält die Offsets/Längen für Trackdisk.device
  37.                                     // Wird während des Schreibens erstellt.
  38.  
  39.  
  40. TEXT  filefeld[MAXENTRIES][30];     /* Filename                         */
  41. LONG  realfilelen[MAXENTRIES];      /* Die echte Filelänge in Bytes !!  */
  42. BOOL  compressfile[MAXENTRIES];     /* Dieses File comprimieren ?       */
  43. BOOL  relocatefile[MAXENTRIES];     /* File relocieren und wohin        */
  44. ULONG reloaddrfile[MAXENTRIES];
  45. LONG  newoffset[MAXENTRIES];        /* Neuer BlockOffset o. -1          */
  46.  
  47. LONG  numfiles;                     /* Wieviele Files zu Zeit           */
  48. LONG  curroffset;                   /* Actueller Blockoffset            */
  49.  
  50. TEXT *hunkfmt = "HunkWizard -a%lx -o%s %s\n";
  51.  
  52. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  53.  
  54. ULONG dospacket(struct MsgPort *aprt,LONG typ,
  55.                 LONG arg1,LONG arg2,LONG arg3,LONG arg4,LONG arg5,LONG arg6,LONG arg7)
  56. {
  57. struct StandardPacket *sp;
  58. struct MsgPort* rp;
  59. ULONG ret;
  60.  
  61.    if ( (rp = CreatePort(NULL,0))==NULL ) {
  62.       return(-1);
  63.    }else{
  64.        if ( (sp = AllocMem(sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR))==0) {
  65.            DeletePort(rp);
  66.            return(-1);
  67.       }else{
  68.            sp->sp_Msg.mn_Node.ln_Name = (char*)&(sp->sp_Pkt);
  69.            sp->sp_Pkt.dp_Link = &(sp->sp_Msg);
  70.            sp->sp_Pkt.dp_Port = rp;
  71.            sp->sp_Pkt.dp_Type = typ;
  72.            sp->sp_Pkt.dp_Arg1 = arg1;
  73.            sp->sp_Pkt.dp_Arg2 = arg2;
  74.            sp->sp_Pkt.dp_Arg3 = arg3;
  75.            sp->sp_Pkt.dp_Arg4 = arg4;
  76.            sp->sp_Pkt.dp_Arg5 = arg5;
  77.            sp->sp_Pkt.dp_Arg6 = arg6;
  78.            sp->sp_Pkt.dp_Arg7 = arg7;
  79.            PutMsg(aprt, &sp->sp_Msg); WaitPort(rp); GetMsg(rp);
  80.            ret = sp->sp_Pkt.dp_Res1;
  81.            FreeMem( sp, sizeof(struct StandardPacket));
  82.            DeletePort(rp);
  83.       }
  84.    }
  85.    return(ret);
  86. }
  87.  
  88. void inhibitdrive(WORD drnum, BOOL state)
  89. {
  90. TEXT*tp;
  91. struct MsgPort *mp;
  92.    switch( drnum ) {
  93.    case  0 : tp = "df0:"; break;
  94.    case  1 : tp = "df1:"; break;
  95.    case  2 : tp = "df2:"; break;
  96.    case  3 : tp = "df3:"; break;
  97.    }
  98.    mp = DeviceProc( tp );
  99.    if ( mp ) {
  100.         dospacket( mp, ACTION_INHIBIT,state,0,0,0,0,0,0);
  101.    }
  102. }
  103.  
  104. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  105.  
  106. LONG calcrating( struct LhBuffer *lh )
  107. {
  108. LONG ret;
  109.    ret =  ( (lh->lh_SrcSize - lh->lh_DstSize) * 100 ) / lh->lh_SrcSize;
  110.    return( ret );
  111. }
  112.  
  113. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  114.  
  115. BOOL CheckDisk( struct IOStdReq* ior)
  116. {
  117. BOOL err = FALSE;
  118.     ior->io_Command = TD_CHANGESTATE;
  119.     DoIO( ior );
  120.     if ( ior->io_Actual != FALSE ) {
  121.         printf("Insert a blank,formatted disk in drive before !\n");
  122.       err = TRUE;
  123.     } else {
  124.        ior->io_Command = TD_PROTSTATUS;
  125.        err = DoIO( ior );
  126.        if ( ior->io_Actual != FALSE ) {
  127.            printf("Disk in selected drive is write-protected !\n");
  128.          err = TRUE;
  129.        }
  130.    }
  131.     return( err );
  132. }
  133.  
  134. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  135.  
  136. struct Library *LhBase;
  137. struct LhBuffer *lhbuffer;
  138.  
  139. void openlibs(void) {
  140.    if ( !(LhBase = OpenLibrary( LH_NAME, LH_VERSION ) )) {
  141.       printf("\nI can't find the %s\nCompression disabled.\n",LH_NAME);
  142.    }else{
  143.       if ( !(lhbuffer = CreateBuffer(FALSE))) {
  144.          printf("Unable to allocate auxilary buffers for compression.\n"
  145.                 "Terminating.\n");
  146.          exit( RETURN_FAIL );
  147.       }
  148.    }
  149. }
  150. void closelibs(void) {
  151.    if ( lhbuffer ) { DeleteBuffer(lhbuffer); lhbuffer = NULL; }
  152.    if ( LhBase ) { CloseLibrary( LhBase ); LhBase = NULL; }
  153. }
  154.  
  155. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  156.  
  157. void Usage(TEXT*pn) {
  158.   printf(
  159.    "Usage: %s scriptfile <0..3>\n\n"
  160.    "This tool was designed to copy some workfiles to a non-dos disk\n"
  161.    "using trackdisk.device.\n"
  162.    "'scriptfile' is a file, which contains the names of the file to\n"
  163.    "store. See TrackMaster Docfile for more information\n\n",pn);
  164. }
  165.  
  166. //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  167.  
  168. int main(int argc,char *argv[])
  169. {
  170. struct   MsgPort *myport;           /* Mein MessagePort           */
  171. struct   IOStdReq *myio;            /* Mein Standard IO Block     */
  172. LONG     unitnum;                   /* Nummer des Disklaufwerkes  */
  173.  
  174.  
  175. struct   FileInfoBlock *fib;        /* Für Filelängen etc         */
  176. BPTR     lock;
  177.  
  178. FILE     *fp;
  179. TEXT     buffer[256];               /* Eingabepuffer für Zeile    */
  180. BYTE     *filebuff;                 /* DateiPuffer                */
  181. BYTE     *compbuff;                 /* Compress Dest-Buffer       */
  182.  
  183. LONG     i,j;                       /* Laufvariable               */
  184. BOOL     breakloop = FALSE;         /* Falls ein Fehler auftritt  */
  185. BOOL     firstline = FALSE;         /* schon erste Zeile bearbeit.*/
  186. BOOL     compresson = FALSE;        /* Default is aus !!!         */
  187. BOOL     relocateon = FALSE;        /*       "                    */
  188. ULONG    relocateaddr;
  189. BOOL     specialon = FALSE;         /* Special command            */
  190. TEXT     specialcom[80];
  191. BOOL     patchdir = FALSE;          /* Skip some leading files on */
  192. LONG     patchfrom;                 /* disk, yet.                 */
  193.  
  194.  
  195.    printf("\nTrackMaster V1.7   Freeware ©1990 Carsten Schlote\n"
  196.              "                                    BrainTrace Design Software\n\n");
  197.  
  198.    //­­­­­­­ Startup-Stuff for lhlib ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  199.  
  200.    onexit( closelibs );
  201.    openlibs();
  202.  
  203.    //­­­­­­­ Argumente auswerten ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  204.  
  205.    if ( argc != 3 ) {
  206.       Usage(argv[0]); exit(RETURN_ERROR);
  207.    }else{
  208.       if ( sscanf( argv[2],"%ld\n", &unitnum )!=1 ) {
  209.          printf("The drive number is invalid !\n\n"); breakloop = TRUE;
  210.       }else{
  211.          if ( (fp = fopen( argv[1],"r" ))==NULL ) {
  212.             printf("Can't open scriptfile\n\n"); breakloop = TRUE;
  213.          }else{
  214.             if ( !(fib = AllocMem( sizeof(*fib),MEMF_PUBLIC ))) {
  215.                printf("Can't allocate FIB.\n\n"); breakloop = TRUE;
  216.             }else{
  217.                numfiles = 0;
  218.  
  219.                //­­­­Scriptzeile lesen und auswerten­­­­­­­­­­­­­­­­­­­­­­­­­­­
  220.  
  221.                while ( !breakloop && (fgets( buffer,256,fp ) != NULL) ) {
  222.  
  223.                   if ( buffer[0]=='\n' ) continue;    /* Commentar            */
  224.  
  225.                   if (newoffset[numfiles]<=0)
  226.                      newoffset[ numfiles ] = -1;      /* No new offset, yet   */
  227.  
  228.                   //­­­­Commandosequence auswerten­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  229.  
  230.                   if ( buffer[0]=='#' ) {
  231.                      if ( buffer[1]=='\n' ) continue;    /* Commentar         */
  232.  
  233.                      switch ( toupper( buffer[1] ) ) {
  234.                      case '@' : if ( patchdir || firstline ) {
  235.                                    printf("*** #@ may be used only once and the beginning of the script.\n");
  236.                                    breakloop = TRUE;
  237.                                 }else{
  238.                                    if ( sscanf( &buffer[3],"%ld",&patchfrom ) != 1 ) {
  239.                                       printf("*** Dir Entry Number wrong or non-decimal.\n");
  240.                                       breakloop = TRUE;
  241.                                    }
  242.                                    patchdir = TRUE;
  243.                                    numfiles = patchfrom;
  244.                                 }
  245.                                 break;
  246.                      case 'O' : if ( sscanf( &buffer[3],"%ld",&newoffset[numfiles] ) != 1 ) {
  247.                                    printf("*** Offset Number wrong or non-decimal.\n");
  248.                                    breakloop = TRUE;
  249.                                 }
  250.                                 break;
  251.                      case 'C' : if ( LhBase ) compresson = ( buffer[2]=='1' )?TRUE:FALSE; break;
  252.                      case 'R' : relocateon = (buffer[2]== '1')?TRUE:FALSE;
  253.                                 if ( buffer[2] == '1' ) {
  254.                                    if ( sscanf( &buffer[3],"%lx", &relocateaddr ) != 1 ) {
  255.                                        printf("*** Hexaddr for relocate is wrong\n");
  256.                                        breakloop = TRUE;
  257.                                    }
  258.                                 } break;
  259.                      case 'S' : specialon = (buffer[2]== '1')?TRUE:FALSE;
  260.                                 strcpy( specialcom ,&buffer[3] );
  261.                                 break;
  262.                      case 'L' : {
  263.                                 TEXT tempfname[30];
  264.                                 TEXT linkfiles[30*30] = "\0";
  265.                                 TEXT doscom[256];
  266.                                    if ( sscanf( &buffer[2],"%d %s",&i,tempfname ) != 2 ) {
  267.                                       printf("*** Link parameters wrong or missing.\n");
  268.                                       breakloop = TRUE;
  269.                                    }
  270.                                    if ( i <= 30 && i > 1 ) {
  271.                                       while ( i > 0 ) {
  272.                                          if ( fgets( buffer,256,fp ) != NULL ) {
  273.                                             if ( buffer[0] == '\n' ) continue;
  274.                                             strcat( linkfiles, buffer );
  275.                                             linkfiles[ strlen(linkfiles)-1 ] = ' ';
  276.                                          }
  277.                                          --i;
  278.                                       }
  279.                                       sprintf(doscom,"Join %s to %s", linkfiles, tempfname );
  280.                                       if ( Execute( doscom, NULL,NULL ) == FALSE ) {
  281.                                          printf("*** Linkage failed.\n"); breakloop=TRUE; continue;
  282.                                       }
  283.                                       strcpy(buffer, tempfname );
  284.                                       goto getlinkfilename;
  285.                                    }else{
  286.                                       printf("*** Too many/less files for link option.\n");
  287.                                       breakloop = TRUE;
  288.                                    }
  289.                                 }
  290.                                 break;
  291.                      }
  292.                      continue;
  293.                   }
  294.                   buffer[ strlen( buffer ) -1 ] = 0;
  295. getlinkfilename:
  296.                   //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  297.  
  298.                   if ( relocateon & specialon ) {
  299.                      printf("Script options conflict\n");
  300.                      breakloop = TRUE; continue;
  301.                   }
  302.  
  303.                   //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  304.  
  305.                   if ( specialon ) {
  306.                   TEXT tempfname[30], doscom[256], *p;
  307.                   TEXT speccom[160];
  308.                      strcpy( speccom, specialcom );
  309.                      strcpy( tempfname, buffer ); strcat( tempfname, ".rel" );
  310.                      while ( p = strchr( speccom, '@' ) ) {
  311.                         p[0] = '%';
  312.                         switch ( p[1] ) {
  313.                         case 's' : sprintf( doscom, speccom, buffer ); strcpy( speccom, doscom ); break;
  314.                         case 'd' : p[1] = 's'; sprintf( doscom, speccom, tempfname); strcpy( speccom, doscom ); break;
  315.                         }
  316.                      }
  317.                      strcpy( doscom, speccom );
  318.                      printf("%s",doscom );
  319.                      if ( Execute( doscom, NULL,NULL ) == FALSE ) {
  320.                         printf("Can't start requested command or prg failed.\n");
  321.                         breakloop = TRUE; continue;
  322.                      }
  323.                      strcpy( buffer, tempfname );
  324.                   }
  325.  
  326.                   //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  327.  
  328.                   if ( relocateon ) {
  329.                   TEXT tempfname[30];
  330.                   TEXT doscom[256];
  331.                      strcpy( tempfname, buffer );
  332.                      strcat( tempfname, ".rel" );
  333.                      sprintf( doscom, hunkfmt, relocateaddr, tempfname,buffer );
  334.                      if ( Execute( doscom, NULL,NULL ) == FALSE ) {
  335.                         printf("Can't start hunkwizard or prg failed.\n");
  336.                         breakloop = TRUE; continue;
  337.                      }
  338.                      strcpy( buffer, tempfname );
  339.                   }
  340.  
  341.                   //­­­­File locken und auswerten­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  342.  
  343.                   if ( sscanf( buffer,"%30s\n",filefeld[numfiles]) != 1 ) {
  344.                      printf("Error in scriptfile.\n"); breakloop = TRUE;
  345.                   } else {
  346.                      if ( (lock=Lock(filefeld[numfiles],ACCESS_READ))==0 ) {
  347.                         printf("Can't find file %s.\n\n",filefeld[numfiles]);
  348.                         breakloop = TRUE;
  349.                      }else{
  350.                         if ( Examine( lock, fib ) == FALSE ) {
  351.                            printf("Can't examine file %s.\n\n",filefeld[numfiles]);
  352.                            breakloop = TRUE;
  353.                         }else{
  354.                            realfilelen[numfiles]=fib->fib_Size;
  355.                            compressfile[numfiles] = (numfiles==0)?FALSE:compresson;
  356.                            relocatefile[numfiles] = relocateon;
  357.                            reloaddrfile[numfiles] = relocateaddr;
  358.                         }
  359.                         UnLock(lock);
  360.                      }
  361.                   }
  362.                   if ( ++numfiles >= MAXENTRIES-1 ) { // Wegen filenamesfile => - 1 !
  363.                      printf("Table overflow. Too many files in script !\n");
  364.                      breakloop=TRUE;
  365.                   }
  366.                   firstline = TRUE;
  367.                }
  368.                FreeMem(fib,sizeof(fib));
  369.             }
  370.             fclose(fp);             /* scriptfile schließen */
  371.          }
  372.       }
  373.    }
  374.    if ( breakloop ) exit( RETURN_FAIL );
  375.  
  376.    //­­­­ Filenamen abspeichern ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  377.  
  378.    {
  379.    LONG fnamelen = numfiles * 30;
  380.    BPTR fh;
  381.       if ( fh = Open( "tm.dir", MODE_NEWFILE ) ) {
  382.          if ( Write( fh, filefeld, fnamelen ) != fnamelen ) exit(RETURN_FAIL);
  383.          Close(fh);
  384.       } else {
  385.          exit( RETURN_FAIL );
  386.       }
  387.       strcpy( filefeld[numfiles], "tm.dir" );
  388.       realfilelen[numfiles]= fnamelen;
  389.       compressfile[numfiles] = TRUE;
  390.       relocatefile[numfiles] = FALSE;
  391.       reloaddrfile[numfiles] = NULL;
  392.       newoffset[numfiles] = -1;
  393.       ++numfiles;
  394.    }
  395.  
  396.    //­­­­­­­ Daten lesen und schreiben ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  397.  
  398. #define TDCOM( com, data, offset, len ) { myio->io_Command = com; myio->io_Data = data; myio->io_Offset = offset; myio->io_Length = len; DoIO(myio); }
  399. #define TDCOMX( com, data, offset, len ) { TDCOM( com,data, offset,len); if (myio->io_Error) { printf("*** IO-Error %ld. Terminating.\n",myio->io_Error); goto error; } }
  400.  
  401.    if(numfiles==0) {
  402.       printf("Nothing to write !?\n\n"); exit(RETURN_WARN);
  403.    }
  404.    if ( (myport = CreatePort( NULL,0 ))==NULL ) {
  405.       printf("Can't create port.\n\n"); exit(RETURN_ERROR);
  406.    }else{
  407.       if ( (myio = CreateStdIO( myport ))==NULL ) {
  408.          printf("Can't create std io.\n\n"); exit(RETURN_ERROR);
  409.       }else{
  410.          if ( OpenDevice( TD_NAME, unitnum, myio, 0 )!=0 ) {
  411.             printf("Can't open %s unit %ld.\n\n",TD_NAME,unitnum); exit(RETURN_ERROR);
  412.          }else{
  413.             inhibitdrive( unitnum, TRUE );
  414.             printf("Please insert disk in Drive df%ld: and hit return.",unitnum);
  415.             Read( Input(), buffer, sizeof(buffer) );
  416.  
  417.             if ( (i = CheckDisk( myio )) ) {
  418.                printf("*** IO-Error %ld. Terminating.\n",i);
  419.                goto nodisk;
  420.             }
  421.  
  422.             //­­­­Files lesen, bearbeiten und schreiben ­­­­­­­­­­­­­­­­­­­­­­­
  423.  
  424.             curroffset = DIRBLOCK + BLKLENGTH( (numfiles*8) );
  425.  
  426.             printf("---------------------------------------------------------------------\n");
  427.             for ( i= (patchdir)?patchfrom:0 ; i<numfiles ; ++i )
  428.             {
  429.             static OPAIR olddirfeld[MAXENTRIES];
  430.             static TEXT oldfilefeld[MAXENTRIES][30];
  431.             static TEXT packedfiles[MAXENTRIES][30];
  432.             static LONG * lp = (LONG*)packedfiles;
  433.  
  434.                if ( patchdir && (i==patchfrom) ) {
  435.                   printf("patching old entries...\r");
  436.                   TDCOMX( CMD_READ, olddirfeld, DIRBLOCK, BLKLENGTH( (numfiles*8) ) );
  437.                   for ( j=0; j<patchfrom; ++j ) dirfeld[j] = olddirfeld[j];
  438.  
  439.                   for ( j = MAXENTRIES-1; j > 0 && !olddirfeld[j].offset && !olddirfeld[j].length ; --j )
  440.                      ;
  441.                   TDCOMX( CMD_READ, packedfiles, olddirfeld[j].offset, olddirfeld[j].length );
  442.                   if ( lp[0] == 'PACK' ) {
  443.                      lhbuffer->lh_Src = &lp[3]; lhbuffer->lh_SrcSize = lp[1];
  444.                      lhbuffer->lh_Dst = oldfilefeld; lhbuffer->lh_DstSize = lp[2];
  445.                      LhDecode( lhbuffer );
  446.                   }else
  447.                      memcpy( oldfilefeld, packedfiles, sizeof(packedfiles) );
  448.                }
  449.  
  450.                if ( newoffset[i] != -1 ) {
  451.                   if ( newoffset[i]*512 < curroffset ) {
  452.                      printf("*** Warning : Seeking to lower disk offset.\n");
  453.                   }
  454.                   curroffset = newoffset[i] * 512;
  455.                   printf("Seek to Diskblock %ld and continue writing.\n",newoffset[i] );
  456.                }
  457.  
  458.                if ( curroffset < (DIRBLOCK + BLKLENGTH( (numfiles*8) )) ||
  459.                     curroffset > ( 160*11*512 ) ) {
  460.                   printf("*** Seek offset invalid.\n\n");
  461.                   break;
  462.                }
  463.  
  464.                if( !(filebuff = AllocMem( BLKLENGTH(realfilelen[i]), MEMF_CHIP|MEMF_PUBLIC|MEMF_CLEAR ))) {
  465.                   printf("*** Can't allocate buffer for file %s.\n\n",filefeld[i]);
  466.                   breakloop = TRUE;
  467.                }else{
  468.                   if( !(lock = Open( filefeld[i], MODE_OLDFILE ))) {
  469.                      printf("*** Can't open file %s.\n\n",filefeld[i]);
  470.                      breakloop = TRUE;
  471.                   } else {
  472.                      if ( Read(lock, filebuff, realfilelen[i]) != realfilelen[i] ) {
  473.                         printf("*** Can't open file %s.\n\n",filefeld[i]);
  474.                         breakloop = TRUE;
  475.                      }else{
  476.                         if ( compressfile[i] ) {
  477.                            if ( !(compbuff = AllocMem( COMPBLKLEN( realfilelen[i] ), MEMF_CHIP|MEMF_CLEAR|MEMF_PUBLIC ))) {
  478.                               printf("*** Can't allocate compression buffer.\nTerminate.\n");
  479.                               breakloop = TRUE;
  480.                            }else{
  481.  
  482.                               if ( i == numfiles-1 && patchdir ) {
  483.                                  for ( j=0; j<patchfrom; ++j ) {
  484.                                     memcpy( filebuff+(j*30),&oldfilefeld[j], 30 );
  485.                                  }
  486.                               }
  487.  
  488.                               lhbuffer->lh_Src = filebuff; lhbuffer->lh_SrcSize = realfilelen[i];
  489.                               lhbuffer->lh_Dst = compbuff + ( 3*4 );
  490.                               printf(" Compress data - stand by.");
  491.                               if ( LhEncode( lhbuffer )==0 ) {
  492.                                  printf("\r*** Compression failed.\nTerminating.\n");
  493.                                  breakloop = TRUE;
  494.                               }else{
  495.                                  if ( lhbuffer->lh_DstSize >= lhbuffer->lh_SrcSize ) {
  496.                                     printf("\r*** uncompressable data. Check file %s\nTerminate.\n",filefeld[i] );
  497.                                     breakloop = TRUE;
  498.                                  }else{
  499.                                     {
  500.                                     LONG * tp;
  501.                                        tp = (LONG*)(compbuff);
  502.                                        tp[0] = 'PACK' ;
  503.                                        tp[1] = lhbuffer->lh_SrcSize;
  504.                                        tp[2] = lhbuffer->lh_DstSize;
  505.                                     }
  506.                                     dirfeld[i].offset = curroffset;
  507.                                     dirfeld[i].length = BLKLENGTH( lhbuffer->lh_DstSize );
  508.                                     printf("\r%3ld. Offset: %6ld(%4ld)  Length: %6ld(%4ld) Rate:%ld%% File: %s\n",
  509.                                             i,
  510.                                             dirfeld[i].offset,dirfeld[i].offset/512,
  511.                                             dirfeld[i].length,dirfeld[i].length/512,calcrating( lhbuffer ),filefeld[i] );
  512.                                     TDCOMX( CMD_WRITE, compbuff, dirfeld[i].offset, dirfeld[i].length);
  513.                                     curroffset += dirfeld[i].length;
  514.                                  }
  515.                               }
  516.                               FreeMem( compbuff, COMPBLKLEN(realfilelen[i]) );
  517.                            }
  518.                         }else{
  519.                            dirfeld[i].offset = curroffset;
  520.                            dirfeld[i].length = BLKLENGTH( realfilelen[i] );
  521.                            printf("%3ld. Offset: %6ld(%4ld)  Length: %6ld(%4ld) File: %s\n",
  522.                                    i,
  523.                                    dirfeld[i].offset,dirfeld[i].offset/512,
  524.                                    dirfeld[i].length,dirfeld[i].length/512,filefeld[i] );
  525.                            TDCOMX( CMD_WRITE, filebuff, dirfeld[i].offset, dirfeld[i].length);
  526.                            curroffset += dirfeld[i].length;
  527.                         }
  528.                         Close(lock);
  529.                      }
  530.                   }
  531.                   FreeMem(filebuff, BLKLENGTH( realfilelen[i] ) );
  532.                }
  533.                if ( breakloop ) break;
  534.             }
  535.             printf("---------------------------------------------------------------------\n");
  536.             printf("Writing %ld direntrie(s) to offset %ld(%ld).\n",numfiles,DIRBLOCK,DIRBLOCK/512);
  537.             TDCOMX( CMD_WRITE, dirfeld, DIRBLOCK, BLKLENGTH( (numfiles*8) ) );
  538.  
  539.             printf("---------------------------------------------------------------------\n");
  540.             printf("Writing BootLoader for first, unpacked & pc-relative program !\n");
  541.             LoaderOffset = dirfeld[0].offset;
  542.             LoaderLength = dirfeld[0].length;
  543.             NumFiles = numfiles;
  544.             BBChkSum( BootBlock );
  545.             TDCOMX( CMD_WRITE, BootBlock, 0, 1024 );
  546.  
  547. error:      printf("---------------------------------------------------------------------\n");
  548.             TDCOM( CMD_UPDATE, 0,0,0 );
  549.             TDCOM( TD_MOTOR, 0,0,0 );       // Motor aus
  550.             printf("Please remove disk from Drive df%ld: and hit return.",unitnum);
  551.             Read( Input(), buffer, sizeof(buffer) );
  552. nodisk:     inhibitdrive( unitnum, FALSE );
  553.             printf("%s",(breakloop==FALSE)?"All done.\n":"Operation failed !\n");
  554.             CloseDevice( myio );
  555.          }
  556.          DeleteStdIO( myio );
  557.       }
  558.       DeletePort( myport );
  559.    }
  560.  
  561.    //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  562.  
  563.    closelibs();
  564.  
  565.    //­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
  566.  
  567.    return( breakloop?RETURN_FAIL:RETURN_OK );
  568. }
  569.  
  570.