        page    ,132
        title   FCBIO -

;
;   Revisions:
;
;Local  DCR/
;Tag    PTM(R)  Who     When/What
;               danno   09-Mar-1989
;                       Created for MVDM
;***********************************************************************;BN001;
;  31Oct91 - G.L.Fulk                                                   ;BN001;
;  Pass the user environment to the file system delete worker.          ;BN001;
;***********************************************************************;BN001;
;

.xlist
include v86seg.inc

.xcref
include basemaca.inc
include osmaca.inc
include demmac.inc
include dossymc.inc
include mvdmexp.inc
include demsvc.inc
include cpmfcb.inc
include error.inc
include opncreat.inc
include devhdr.inc
include dos3dev.inc
include struc30.inc
.cref
.list

Random  =   2                           ; random operation
FCBRead =   4                           ; doing a read
Block   =   8                           ; doing a block I/O


        CpuMode 386


V86CODE SEGMENT

        ReferV86   DMAADD
        ReferV86    CurrentPDB,WORD                                     ;BN001;

        EXTRN   FCB_Ret_Err:NEAR
        EXTRN   FCB_Ret_OK:NEAR
        EXTRN   FCB_Return:NEAR
        EXTRN   RMDevCheckNameFCB:NEAR
        EXTRN   RMDevCheckFCB:NEAR
        EXTRN   RMDevCloseDI:NEAR
        EXTRN   RMDevXFLAGS:NEAR
        EXTRN   RMDevAllocDI:NEAR
        EXTRN   RMDevOpen:NEAR
        EXTRN   RMDevClose:NEAR
        EXTRN   RMDevRead:NEAR
        EXTRN   RMDevWrite:NEAR
        EXTRN   SFFromSFN:NEAR
        EXTRN   get_user_stack:NEAR

        ASSUME  CS:V86GROUP,DS:NOTHING,ES:NOTHING,SS:NOTHING


;***    $Get_FCB_Position - set random record fields to current pos
;
;   $Get_FCB_Position - look at an FCB, retrieve the current position from the
;       extent and next record field and set the random record field to point
;       to that record
;
;   Inputs:     DS:DX point to a possible extended FCB
;   Outputs:    The random record field of the FCB is set to the current record
;   Registers modified: AX,DX,SI
;
        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING

Procedure $Get_FCB_Position,NEAR
        call    GetExtended             ; point to FCB
        call    GetExtent               ; DX:AX is current record
        mov     word ptr [SI.fcb_RR],ax ; drop in low order piece
        mov     [SI+fcb_RR+2],DL        ; drop in high order piece
        .if     <<[SI.fcb_RECSIZ]> b 64>
          mov     [SI+fcb_RR+2+1],DH      ; set 4th byte only if record size < 64
        .endif
        jmp     FCB_Ret_OK
EndProc $Get_FCB_Position


;***    $FCB_Delete - remove several files that match the input FCB
;
;       Given an FCB, remove all directory entries in the current
;       directory that have names that match the FCB's ?  marks.
;
;       ENTRY   DS:DX - point to an FCB
;       EXIT    directory entries matching the FCB are deleted
;               AL = FF if no entries were deleted.
;       USES    all
;
        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING

Procedure $FCB_Delete,NEAR
        push    es                                                      ;BN001;
        mov     es,[CurrentPDB]                                         ;BN001;
        mov     es,es:[002Ch]                                           ;BN001;
        SVC     SVC_DEMDELETEFCB
        pop     es                                                      ;BN001;
        jmp     FCB_Return
EndProc   $FCB_Delete


;***    $Get_FCB_File_Length - return the length of a file
;
;       Set the random record field to the length of the file in
;       records (rounded up if partial).
;
;       ENTRY   DS:DX - point to a possible extended FCB
;       EXIT    Random record field updated to reflect the number of records
;       USES    all
;
        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING

Procedure $Get_FCB_File_Length,NEAR
        SVC     SVC_DEMFCBLENGTH
        jmp     FCB_Return
EndProc   $Get_FCB_File_Length


;***    $FCB_Close - close a file
;
;       Given an FCB, look up the SFN and close it.  Do not free it
;       as the FCB may be used for further I/O
;
;       ENTRY   DS:DX point to FCB
;       EXIT    AL = FF if file was not found on disk
;       USES    all
;
        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING

Procedure $FCB_Close,NEAR
        call    GetExtended
        call    RMDevCheckFCB           ; Local device FCB?
        jnc     short fcl1              ; Skip if so
        SVC     SVC_DEMFCBCLOSE
        jmp     FCB_Return

fcl1:   xor     bh,bh
        mov     bl,ds:[si].fcb_sfn
        call    RMDevCloseDI
        jmp     FCB_Return

EndProc   $FCB_Close


;***    $FCB_Rename - change names in place
;
;       Rename a file in place within a directory.  Renames multiple
;       files copying from the meta characters.
;
;       ENTRY   DS:DX point to an FCB.  The normal name field is the source
;                   name of the files to be renamed.  Starting at offset 11h
;                   in the FCB is the destination name.
;       EXIT    AL = 0 -> no error occurred and all files were renamed
;               AL = FF -> some files may have been renamed but:
;                   rename to existing file or source file not found
;       USES    all
;
        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING

Procedure $FCB_Rename,NEAR
        SVC     SVC_DEMRENAMEFCB
        jmp     FCB_Return
EndProc   $FCB_Rename


;***    GetExtended - Make DS:SI point to FCB from DS:DX
;
;       ENTRY   DS:DX point to a possible extended FCB
;       EXIT    DS:SI point to the FCB part
;               zeroflag set if not extended fcb
;       USES    SI

        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING
Procedure GetExtended
        mov     si,dx                   ; point to Something
        .if     <<byte ptr ds:[si]> eq -1>   ; look for extention
          add     si,7                  ; point to FCB
        .endif
        cmp     si,dx                   ; set condition codes
        ret
EndProc   GetExtended

;***    GetExtent - retrieve next location for sequential IO
;
;       Construct the next record to perform I/O from the EXTENT and
;       NR fields in the FCB.
;
;       ENTRY   DS:SI - point to FCB
;       EXIT    DX:AX contain the contents of the random record field
;       USES    AX,DX
;
        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING

Procedure GetExtent,NEAR
        MOV     AL,[SI.fcb_NR]          ; get low order piece
        MOV     DX,[SI.fcb_EXTENT]      ; get high order piece
        SHL     AL,1
        SHR     DX,1
        RCR     AL,1            ; move low order bit of DL to high order of AH
        MOV     AH,DL
        MOV     DL,DH
        XOR     DH,DH
        RET
EndProc   GetExtent

;***    SetExtent - update next location for sequential IO
;
;       Chnage the position of an FCB by filling in the extent/NR fields.
;
;       ENTRY   DS:SI - point to FCB
;               DX:AX contain the contents of the random record field
;       EXIT    Extent/NR field set
;
;       USES    CX,AX,DX
;
        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING

Procedure SetExtent,NEAR
        MOV     CX,AX
        AND     AL,7FH
        MOV     [SI.fcb_NR],AL
        AND     CL,80H
        SHL     CX,1
        RCL     DX,1
        MOV     AL,CH
        MOV     AH,DL
        MOV     [SI.fcb_EXTENT],AX
        RET

EndProc   SetExtent

;   GetRecSize - return in BX the record size from the FCB at DS:SI
;
;   Inputs:     DS:SI point to a non-extended FCB
;   Outputs:    BX contains the record size
;   Registers modified: None
;
        ASSUME  DS:NOTHING,ES:NOTHING,SS:NOTHING

Procedure   GetRecSize,NEAR
        mov     bx,[SI.fcb_RECSIZ]      ; get his record size
        .if     <ZERO bx>               ; is it nul?
          mov     bx,128                ; use default size
          mov     [SI.fcb_RECSIZ],BX    ; stuff it back
        .endif
        ret
EndProc GetRecSize

V86CODE ENDS

        END
