/*
 * SCC standard I/O
 *
 * (c)2001 Snakessoft
 * This will be improved upon, someday?
 *
 * exit(x),fclose(x),=fopen(x,x),=fgetc(x),=fputc(x,x),=getc()
 * putc(x),=outtext(x),outint(x)
 */

#ifndef __scc_include_stdio_h_
#define __scc_include_stdio_h_

#define FILE    unsigned
#define EOF     -1
#define NULL    0

void exit(int val){
 asm{
    mov  ah,4ch
    mov  al,[val]
    int  21h
    }
return; }

void fclose(FILE *hnd){
 asm {
       mov ah,3eh
       mov bx,[hnd]
       int 21h
             }
return; }

FILE *fopen(char *name, char *type){
 FILE *hnd;

 if(type[0]=='w'){
     asm{
        mov ax,3c00h
        mov cx,0
        mov dx,[name]
        int 21h
        jc   OpenError
        mov [hnd],ax
                }
            }
 else{
   asm{
     mov ax,3d00h
     mov cx,0
     mov dx,[name]
     int 21h
     jc   OpenError
     mov [hnd],ax
     jmp OpenEnd
     OpenError:
     mov word [hnd],0
     OpenEnd:
      }
    }
return hnd; }

char fgetc(FILE *hnd){
 char *k=" ";
 asm{
     mov ax,3f00h
     mov bx,[hnd]
     mov cx,1
     mov dx,[k]
     int 21h
        }
return k[0]; }

int fputc(FILE *hnd,char hh){
 int ret;
 char *k=" ";
 k[0]=hh;
 asm{
     mov ah,40h
     mov bx,[hnd]
     mov cx,1
     mov dx,[k]
     int 21h
     jc   WriteError
     mov [ret],cx
     jmp WriteEnd
     WriteError:
     mov word [ret],0
     WriteEnd:
        }
return ret; }

char getc(){
 char *k=" ";
 asm{
        mov ah,01
        int 21h
        mov [k],al
        }
return k[0]; }

void putc(char k){
 asm{
    mov ah,02
    mov dl,[k]
    int 21h
     }
 if(k=='\n'){
      asm{
       mov ah,02
       mov dl,0dh
       int 21h
        }
     }
return; }

int outtext(char *str){
  long num=0;
  int cnt=0;
  char k;
  for(;;cnt++){
    k=str[cnt];
    if(k==0)break;
    putc(k);
       }
return cnt; }

void outint(long num){
 long baknum;
 long d=1;
 char k;

 if(num<0){ putc('-'); num=-num; }
 baknum=num;
 for(;;){
     if(num>10){
                num=num/10;
                d=d*10;
                }
     else break;
        }
 
 for(;;){
      num=baknum;
      num=num/d;
      putc(num+'0');
      num=num*d;
      if(num<10)break;
      baknum=baknum-num;
      d=d/10;
        }    
return; }

#endif
