/* **** Includes *********************************************************** */

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>

/* **** Variables ********************************************************** */


char   outstr[1024];

/* **** Functions ********************************************************** */


#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) {}
#endif

BOOL FGetsR(BPTR FH,UBYTE *Buffer,LONG BufferLen)
{

  LONG nr,startpos,where,back,foundpos=0;
  BOOL retval=TRUE,sof=FALSE,error=FALSE;

  UBYTE *strptr=NULL;


  Buffer[0]=0;
  BufferLen--;

  where=Seek(FH,0,OFFSET_CURRENT);  // start of file already ?
  if (where<=0) //start of file or error
  {
    retval=FALSE;
  }
  else
  {
    startpos=where; // make a note of where we started from.
    do
    {
      back=255;
      if (where<255) back=where;

      if (back>BufferLen) back=BufferLen;

      if ((where=Seek(FH,0-back,OFFSET_CURRENT))!=-1)
      {
        where-=back;

        if (nr=Read(FH,Buffer,back))
        {
          Buffer[nr]=0;
          if (strptr=strrchr(Buffer,'\n'))
          {
            foundpos=where+(strptr-Buffer);


            if (Seek(FH,foundpos+1,OFFSET_BEGINNING)==-1)
            {
              error=TRUE;
            }
          }
          else
          {
            if (where==0)
            {
              if (Seek(FH,0,OFFSET_BEGINNING)==-1)
              {
                error=TRUE;
              }
              else
              {
                sof=TRUE;
                foundpos=1;
              }
            }
            else
            {

              if (Seek(FH,0-nr,OFFSET_CURRENT)!=-1)
              {
                where-=nr;
              }
              else
              {
                error=TRUE;
              }
            }

          }
        }
        else
        {
          error=TRUE;
        }

      }
    } while (strptr==NULL && !error && !sof);

    Buffer[0]=0;
    if (strptr || sof)
    {
      FGets(FH,Buffer,BufferLen);
      Seek(FH,(foundpos) ? foundpos-1 : 0 ,OFFSET_BEGINNING);
    }
  }

  return(retval);

}

void stripCR(UBYTE *String)
{
  ULONG Len;
  if (String && (Len=strlen(String)))
  {
    Len--; // Len now = character index..

    while (Len && (String[Len]=='\r' || String[Len]=='\n'))
    {
      String[Len--]=0;
    }
  }
}



int main(int argc,char **argv)
{
  BPTR FH;
  UBYTE Buffer[255];

  if (argc==2)
  {
    if (argv[1][0]=='?')
    {
      puts("R-Type (C) 1996 Dominic Clifton - Hydra/LSD\n\n"
           "this small util just reads file backwards line by line\n"
           "and prints it on the screen\n\n"
           "usage: RType <filename>");
    }
    else
    {
      if (FH=Open(argv[1],MODE_OLDFILE))
      {
        Seek(FH,0,OFFSET_END);
        while (FGetsR(FH,Buffer,255))
        {
          stripCR(Buffer);
          puts(Buffer);
        }
        Close(FH);
      }
    }
  }
}
