title MSDOS 2.00 Function Library for Lattice C
;
;........................................
;.					.
;.	DOS 2.00 Functions for		.
;.	       Lattice			.
;.					.
;.	T. Jennings 19 Nov 91		.
;.	  created 13 Sept 82		.
;.					.
;........................................
;
include mc.ash
.model small
;
;MSDOS 2.00 support for Lattice. These will NOT
;work for version 1.xx of DOS. All support full
;paths. 
;
;NOTE: You cannot mix these calls with the 
;Lattice library calls: i.e. open with open()
;and write with open(). You must use ALL or
;NONE.
;
;These calls are the underscored normal
;names for doing buffered I/O.
;
;handle= _open(pathname,access);
;int handle,access;
;char *pathname;
;
;	Open a file. handle returns either 
;the DOS handle, or -1 if error (file not 
;found). Access is: 0 == read only, 1 == write
;only, 2 == read/write.
;
;handle= _creat(pathname,access);
;
;	Create a new file, delete any existing
;copy. Access is not used: use 0. Returns the
;handle or -1 if error.
;
;v= _read(handle,buffer,count);
;v= _write(handle,buffer,count);
;int v,handle,count;
;char *buffer;
;
;	File read or write to an opened or
;created file. reads or writes 'count' bytes
;to the file 'handle', to or from 'buffer'.
;Returns the number of bytes processed: equal
;to 'count' if sucessful.
;
;error= _close(handle)
;int error;
;
;	Close an open file. Returns -1 if 
;error. Any buffers are flushed at this point.
;
;
;pos= _lseek(handle,distance,flag);
;long pos;	new position, else -1
;int handle;
;long distance;	displacement
;int flag;	0 == from BOF, 
;		1 == from current,
;		2 == from EOF,
;
;	Seek to a position within a file.
page
.code
;;
;;	handle= creat(name,access)
;;	handle= open(name,access)
;;
;;	int handle;		-1 if error,
;;	int access;		0=r, 1=w, 2=r/w
;;	char *name;		null terminated
;;
;;Open and create functions. The name is a null
;;terminated string. The access byte is passed
;;directly to DOS. All errors are translated to
;;a -1 return value.
;;
func __open 
	mov	ah,61
	call	opncrt
endf __open

func __creat 
	mov	ah,60
	call	opncrt
endf __creat

opncrt:
if long
	lds	dx,dword ptr arg0
	mov	al,arg2
else
	mov	dx,arg0		;pathname,
	mov	al,arg1		;access,
endif
	xor	bx,bx
	xor	cx,cx
	int	21h		;do it,
	jnc	opncrt1
	mov	ax,-1		;error!
opncrt1:ret
;;
;;_close(handle)
;;int handle;
;;
;;Close a handle opened by XOPEN or XCREATE.
;;Returns -1 if close error.
;;
func __close 
	mov	ah,62
	mov	bx,arg0		;handle,
	int	21h
	mov	ax,0
	jnc	_cl1
	dec	ax
_cl1:
endf __close 
page
;;
;;	count= _read(handle,buffer,size)
;;	count= _write(handle,buffer,size)
;;
;;	int count;	bytes actually r/w
;;	int handle;
;;	char *buffer;
;;	int size;	byte count,
;;
;;	Read or write (size) bytes from the
;;file (handle). The return value is the number 
;;of bytes actually processed.
;;
;;	No text translation is done. All
;;bytes are processed as read or written. No
;;check is done (or is possible) on the buffer
;;size.
;;
func __read 
	mov	ah,63
	call	rdwrt
endf __read

func __write 
	mov	ah,64
	call	rdwrt
endf __write

rdwrt:	mov	bx,arg0		;handle,
if long
	lds	dx,dword ptr arg1 ;buffer
	mov	cx,arg3		;count
else
	mov	dx,arg1		;buffer,
	mov	cx,arg2		;count,
endif
	int	21h
	ret
page
;;
;;	pos= _lseek(handle,distance,flag);
;;	long pos;	new position, else -1
;;	int handle;
;;	long distance;	displacement
;;	int flag;	0 == from BOF, 
;;			1 == from current,
;;			2 == from EOF,
;;
func __lseek 
	mov	bx,arg0		;BX == handle,
	mov	dx,arg1		;CX:DX == disp,
	mov	cx,arg2
	mov	ah,42h
	mov	al,arg3		;unless error,
	int	21h		;DX:AX == pos,
	mov	bx,ax
	mov	ax,dx
	jnc	_x1
	mov	ax,-1
	mov	bx,-1
_x1:
endf __lseek 

;;
;;error= unlink(path);
;;int error;
;;char *path;
;;
;; Delete the file from the system.
;;
func __unlink
if long
	lds	dx,dword ptr arg0
else
	mov	dx,arg0
endif
	mov	ah,41h
	int	21h
	mov	ax,0
	sbb	ax,0
endf __unlink
;;
;;rename(oldname,newname);
;;char *oldname,*newname;
;;
;;Change the pathname of a file.
;;Returns -1 if error.
;;
func __rename
if long
	lds	dx,arg0
	les	di,arg2
else
	mov	dx,arg0
	mov	di,arg1
endif
	mov	ah,56h
	int	21h
	mov	ax,0	
	sbb	ax,0

endf __rename

	end

