
title STDIO Routines for PASCAL
page	62,131
include b:struct.mac
data	segment byte public 'data'
dgroup	group	data
str_area db	64 dup(?)		; Save area for PATH
data	ends

stdio	segment 'code'
	assume	cs:stdio,ds:dgroup
;
; function c_open(const filename : string;access : integer) : integer;
;		 bp+10=size  bp+8=@	bp+6=value
;
cs_open struc
	dw	3 dup(?)
access	dw
const_@ dw
const_$ dw
cs_open ends
	public	c_open
c_open	  proc	  far
	push	bp			; save stack pointer
	mov	bp,sp			; setup addressibility
	lea	bx,[bp].const_@
	call	str_load		; setup the string area with file name
	mov	al,byte ptr [bp].access 	 ; 'access'
	mov	ah,3dH			; 'open' code
	int	21H
	.if	c			; check for error
	  neg	  ax			; set minus on error
	.endif
	pop	bp
	ret	6			; return
c_open	  endp
;
; function c_creat(const filename : string; mode : integer) : integer
;		  bp+10=size bp+8=@	  bp+6=value
;
	public	c_creat
c_creat proc	far
	push	bp			; save stack pointer
	mov	bp,sp
	lea	bx,[bp+8]
	call	str_load
	mov	cx,[bp+6]		; `mode'
	mov	ah,3cH			; `creat' call #
	int	21H
	.if	c
	  neg	  ax			; on error, make negative
	.endif
	pop	bp
	ret	6
c_creat   endp

;
; function c_close(filenum : integer) : integer
;		 bp+6=value
;
	public	c_close
c_close proc	far
	push	bp
	mov	bp,sp
	mov	bx,[bp+6]		; pickup file#
	mov	ah,3eH
	int	21H
	.if	c
	  neg	  ax
	.else
	  xor	  ax,ax 		; clear since not set on normal return
	.endif
	pop	bp
	ret	2
c_close endp
;
; function c_write(filenum : integer; buffer : adsmem; bsize : integer) : integer
;		     bp+12=value     bp+10=# bp+8=@	 bp+6=value
;
	public	c_write
c_write proc	far
	push	bp			; setup stack pointer
	mov	bp,sp
	push	ds
	mov	cx,[bp+6]		; buffer size
	lds	dx,dword ptr [bp+8] ; ADS of buffer
	mov	bx,[bp+12]		; file descriptor
	mov	ah,40H
	int	21H			; WRITE call
	.if	c
	  neg	  ax			; set error flag
	.endif
	pop	ds
	pop	bp
	ret	8
c_write endp
;
; function c_read(filenum : integer; buffer : adsmem; bsize : integer) : integer
;		   bp+12=value	    bp+10=# bp+8=@     bp+6=value
;
	public	c_read
c_read	proc	far
	push	bp
	mov	bp,sp
	push	ds
	mov	cx,[bp+6]		; size
	lds	dx,dword ptr [bp+8] ; ADS of buffer
	mov	bx,[bp+12]		; file descriptor
	mov	ah,3fH
	int	21H			; READ call
	.if	c
	  neg	  ax			; set error flag
	.endif
	pop	ds
	pop	bp
	ret	8
c_read	endp
;
; function c_unlink(filename : string) : integer;
;		    bp+8=# bp+6=@
;
	public	c_unlink
c_unlink proc	far
	push	bp
	mov	bp,sp
	lea	bx,[bp+6]
	call	str_load
	mov	ah,41H
	int	21H
	.if	c
	  neg	  ax
	.endif
	pop	bp
	ret	4
c_unlink endp
;
; function c_mkdir(const dirname : string) : integer
;		   bp+8=# bp+6=@
;
	public	c_mkdir
c_mkdir proc	far
	push	bp
	mov	bp,sp			; setup stack pointer
	lea	bx,[bp+6]
	call	str_load		; setup ASCIZ string
	mov	ah,39H			; MKDIR
	int	21H
	.if	c
	  neg	  ax			; set negative for error
	.endif
	pop	bp
	ret	4
c_mkdir endp
;
; function c_rmdir(const dirname : string) : integer
;		     bp+8=#  bp+6=@
;
	public	c_rmdir
c_rmdir proc	far
	push	bp			; setup stack pointer
	mov	bp,sp
	lea	bx,[bp+6]		; setup ACSIZ string
	call	str_load
	mov	ah,3AH			; RMDIR
	int	21H
	.if	c
	  neg	  ax
	.endif
	pop	bp
	ret	4
c_rmdir endp
;
; function c_chdir(const dirname : string) : integer
;		     bp+8=#  bp+6=@
;
	public	c_chdir
c_chdir proc	far
	push	bp
	mov	bp,sp			; setup ASCIZ string
	lea	bx,[bp+6]
	call	str_load
	mov	ah,3BH			; CHDIR
	int	21H
	.if	c
	  neg	  ax
	.endif
	pop	bp
	ret	4
c_chdir endp
;
; setup the ASCIZ string needed on most calls
;   returns with DS:DX pointing to the string area
;
str_load proc	near
	cld
	push	es
	push	ds
	pop	es			; setup ES for MOVS
	mov	si,[bx] 		; @ of string
	mov	cx,[bx+2]		; size
	lea	di,str_area
	mov	dx,di			; save to return to caller
rep	movsb
	mov	byte ptr[di],0		; set the end of string
	pop	es
	ret
str_load endp

stdio	ends
	end
