#include <stdio.h>
#include <errno.h>
#include "libsys.h"

static void __GetLn1()="\tjsr\t$FD6F";

static unsigned char pos=255;

unsigned int __read(int f,char *p,unsigned int len)
{
  if(f<FOPEN_MAX){
    __pdos_cmd.read.cnt=4;
    __pdos_cmd.read.ref_num=f;
    __pdos_cmd.read.data_buffer=p;
    __pdos_cmd.read.request_count=len;
    __pdos_call(PDOS_READ);
    if(__pdos_err){
      if(__pdos_err==0x4c) return 0;
      errno=__pdos_err;
      return -1;
    }
    return __pdos_cmd.read.trans_count;
  }else{
    unsigned char n,c;
    if(pos==255){
      __GetLn1();
      pos=0;
    }
    n=0;
    do{
      c=(((unsigned char*)0x200)[pos])&127;
      if(c=='\r'){c='\n';pos=254;}
      *p++=c;
      n++;pos++;
    }while(n<len&&c!='\n');
    return n;
  }
}
