MODULE lview;

USE
  system,fio,iostd,param,putnum,math;

CONST
  buf = 10000;


SUB readlib VAR h;
  @h = fopen(paramstr(1));
  fread(h buf 0h7FFF);
  fclose(h);
RET


SUB checklib VAR ptr<0>,str<'lib2d'>;
  REPEAT
    IF byte(str+ptr) <> byte(buf+ptr) THEN
      FALSE;
      EXIT
    FI
    inc(@ptr)
  UNTIL ptr==length(str) LOOP
  TRUE
RET


SUB viewhdr VAR pos*;
  puthex(pos-buf);
  bl;
  putword(byte(pos+2));
  bl;
  putword(byte(pos+3));
  bl;
  putword(byte(pos+4));
  bl;
  putstring(pos+5);
  cr
RET

SUB showlib VAR next,old;
  @next=word(buf+5);
  WHILE next<>0 DO
    @old=next;
    @next=word(buf+next);
    viewhdr(old+buf);
  WEND
RET


SUB main
  IF paramcount THEN
    readlib
    IF checklib THEN
      showlib
    ELSE
      putstring 'unknown library format'
    FI
  ELSE
    cr;
    putstring 'usage: lview [filename''.''ext]';
    cr;
    cr
  FI
RET

END
