; ****************************************************************************
; TRDOS386.ASM (TRDOS 386 Kernel - v2.0.0) - MAIN PROGRAM : trdosk3.s
; ----------------------------------------------------------------------------
; Last Update: 29/02/2016
; ----------------------------------------------------------------------------
; Beginning: 06/01/2016
; ----------------------------------------------------------------------------
; Assembler: NASM version 2.11 (trdos386.s)
; ----------------------------------------------------------------------------
; Derived from TRDOS Operating System v1.0 (8086) source code by Erdogan Tan
; MAINPROG.ASM (09/11/2011)
; ****************************************************************************
; MAINPROG.ASM [ TRDOS KERNEL - COMMAND EXECUTER SECTION - MAIN PROGRAM ]
; (c) 2004-2011  Erdogan TAN  [ 17/01/2004 ]  Last Update: 09/11/2011
; CMD_INTR.ASM [ TRDOS Command Interpreter Procedure ] Last Update: 09/11/2011
; DIR.ASM [ DIRECTORY FUNCTIONS ] Last Update: 09/10/2011
; FILE.ASM [ FILE FUNCTIONS ] Last Update: 09/10/2011

change_current_drive:
	; 02/02/2016
	; 15/01/2016 (TRDOS 386 = TRDOS v2.0)
	; 18/08/2011
	; 09/09/2009
	; INPUT:
	;   DL = Logical DOS Drive Number
	; OUTPUT:
	;  cf=1 -> Not successful
	;   EAX = Error code
	;  cf=0 ->
	;   EAX = 0 (successful)

	xor	ebx, ebx
	mov	bh, dl

	;cmp	dl, 1
	;jna	short loc_ccdrv_initial_media_change_check
	;cmp	bh, [Last_Dos_DiskNo]
	;ja	short loc_ccdrv_drive_not_ready_err

loc_ccdrv_initial_media_change_check:
	mov	esi, Logical_DOSDisks
	add	esi, ebx
loc_ccdrv_dos_drive_name_check:
	cmp	dl, 2
	jb	short loc_ccdrv_dos_drive_name_check_ok

	mov	al, [esi+LD_Name]
	sub	al, 'A'
	cmp	al, dl
	je	short loc_ccdrv_dos_drive_name_check_ok

loc_ccdrv_drive_not_ready_err:
	mov	eax, 15h ; Drive not ready
loc_change_current_drive_stc_retn:
	stc
	retn  

loc_ccdrv_dos_drive_name_check_ok:
	mov	ah, [esi+LD_MediaChanged]
	cmp	ah, 6  ; VOLUME NAME CHECK/MOVE SIGN
	je	short loc_ccdrv_get_FAT_volume_name_0

	cmp	dl, 1
	ja	short loc_gmcs_init_drv_hd

loc_gmcs_init_drv_fd:
	or	ah, ah 
	; AH = 1 is initialization sign (invalid_fd_parameter)
	jnz	short loc_ccdrv_call_fd_init

	call	get_media_change_status
	jc	short loc_ccdrv_drive_not_ready_err

	and	ah, ah
	jz	short loc_change_current_drv3

	xor	ah, 6
	jnz	short loc_ccdrv_drive_not_ready_err

loc_ccdrv_call_fd_init_check_vol_id:
	call	get_volume_serial_number
	jnc	short loc_ccdrv_check_vol_serial

loc_ccdrv_call_fd_init:
	call	floppy_drv_init
	jnc	short loc_reset_drv_fd_current_dir

loc_ccdrv_fdinit_fail_retn:
	retn

loc_ccdrv_check_vol_serial:
	mov	[Current_VolSerial], eax
	;mov	dl, bh
	call	floppy_drv_init
	jc	short loc_ccdrv_fdinit_fail_retn

	cmp	eax, [Current_VolSerial]
	je	short loc_change_current_drv2

loc_reset_drv_fd_current_dir:
	xor	eax, eax              
        mov	[esi+LD_CDirLevel], al
	mov	edi, esi
	add	edi, LD_CurrentDirectory
	mov	ecx, 32
	rep	stosd   
 
loc_ccdrv_get_FAT_volume_name_0:
	mov	al, [esi+LD_FATType]
	or	al, al
	jz	short loc_change_current_drv2

	push	esi 
	cmp	al, 2
	ja	short loc_ccdrv_get_FAT32_vol_name
             
loc_ccdrv_get_FAT2_16_vol_name:
	add	esi, LD_BPB + VolumeLabel
	jmp	short loc_ccdrv_get_FAT_volume_name_1

loc_ccdrv_get_FAT32_vol_name:
	add	esi, LD_BPB + FAT32_VolLab
loc_ccdrv_get_FAT_volume_name_1:
	push	ebx
	push	esi
	call	get_FAT_volume_name
	pop	edi
	pop	ebx
	; BL = 0
	jc	short loc_change_current_drv1
	and	al, al
	jz	short loc_change_current_drv1

loc_ccdrv_move_FAT_volume_name:
	mov	ecx, 11
	rep	movsb

loc_change_current_drv1:
	pop	esi
	jmp	short loc_change_current_drv2

loc_gmcs_init_drv_hd:
	or	ah, ah
	jz	short loc_change_current_drv3
	; BL = 0, BH = Logical DOS drive number
loc_change_current_drv2:
	mov	byte [esi+LD_MediaChanged], 0
loc_change_current_drv3:
	mov	[Current_Drv], bh

	;call	restore_current_directory
	;retn

restore_current_directory:
	; 11/02/2016
	; 15/01/2016 (TRDOS 386 = TRDOS v2.0)
	; 25/01/2010
	; 12/10/2009
	;
	; INPUT:
	;   ESI = Logical DOS Drive Description Table
	;
	; OUTPUT:
	;   ESI = Logical DOS Drive Description Table
	;   EDI = offset Current_Dir_Drv 

	mov	al, [esi+LD_FATType]
	mov	[Current_FATType], al

	mov	ah, [esi+LD_Name] 
	mov	[Current_Dir_Drv], ah

	and	al, al
	jz	short loc_restore_FS_current_directory

loc_restore_FAT_current_directory:
	mov	ah, [esi+LD_CDirLevel]
	mov	[Current_Dir_Level], ah
	or	ah, ah
        jz	short loc_ccdrv_reset_cdir_FAT_12_16_32_fcluster

	movzx	edx, ah
	shl	dl, 4 ; * 16
        add	edx, esi
	mov	eax, [edx+LD_CurrentDirectory+12]
	jmp	short loc_ccdrv_reset_cdir_FAT_fcluster

loc_restore_FS_current_directory:
	call	load_current_FS_directory 
	retn 

loc_ccdrv_reset_cdir_FAT_12_16_32_fcluster:
	cmp	al, 3
	jb	short loc_ccdrv_reset_cdir_FAT_12_16_fcluster
loc_ccdrv_reset_cdir_FAT32_fcluster:
	mov	eax, [esi+LD_BPB+FAT32_RootFClust]
	jmp	short loc_ccdrv_check_rootdir_sign
loc_ccdrv_reset_cdir_FAT_12_16_fcluster:   
	xor	al, al  ; xor eax, eax
	xor	edx, edx
loc_ccdrv_check_rootdir_sign:
	cmp	byte [esi+LD_CurrentDirectory], 0
	jne	short loc_ccdrv_reset_cdir_FAT_fcluster
loc_ccdrv_set_rootdir_FAT_fcluster:
        mov     [esi+LD_CurrentDirectory+12], eax
	mov	dword [esi+LD_CurrentDirectory], 'ROOT'

loc_ccdrv_reset_cdir_FAT_fcluster:
	mov	[Current_Dir_FCluster], eax

	mov	edi, PATH_Array
	mov	edx, esi
	add	esi, LD_CurrentDirectory
	mov	ecx, 32
	rep	movsd

	call	change_prompt_dir_string
               
	mov	esi, edx
               
        sub	eax, eax
       ;sub	edx, edx
	mov	edi, Current_Dir_Drv

	mov	[Restore_CDIR], al ; 0
	retn

dos_prompt:
	; 30/01/2016
	; 29/01/2016
	; 16/01/2016 (TRDOS 386 = TRDOS v2.0)
	; 15/09/2011
	; 13/09/2009
	; 2004-2005

loc_TRDOS_prompt:
	mov	edi, TextBuffer
	mov	byte [edi], "["
	inc	edi
	mov	esi, TRDOSPromptLabel
get_next_prompt_label_char:
	cmp	byte [esi], 20h
	jb	short pass_prompt_label
	movsb
	jmp	short get_next_prompt_label_char
pass_prompt_label:
	mov	byte [edi], "]"
	inc	edi
	mov	byte [edi], 20h
	inc	edi
	mov	esi, Current_Dir_Drv
	movsw
	movsb 
loc_prompt_current_directory:
	cmp	byte [esi], 20h
	jb	short pass_prompt_current_directory
	movsb
	jmp	short loc_prompt_current_directory  
pass_prompt_current_directory:
	mov	byte [edi], '>'
	inc	edi
	mov	byte [edi], 0  
	mov	esi, TextBuffer
	call	print_msg
        
	;sub	bl, bl ; video page = 0
	;call	get_cpos ; get cursor position
	mov	dx, [CURSOR_POSN] ; video page 0
	mov	[CursorColumn], dl

	; 30/01/2016 (to show cursor on the row, again)
	; (Initial color attributes of video page 0 is 0)
	; (see: 'StartPMP' in trdos386.s)
	; 
	;mov	edi, 0B8000h ; start of video page 0
	;movzx	ecx, dl ; column	 
	;mov	al, 80
	;mul	dh
	;add	ax, cx
	;shl	ax, 1 ; character + attribute
	;add	di, ax ; (2*80*row) + (2*column)
	;neg	cl
	;add	cl, 80
	;mov	ax, 700h ;  ah = 7 (color attribute)
	;rep	stosw	

loc_rw_char:
	call	rw_char
loc_move_command:
	mov	esi, CommandBuffer
	mov	edi, esi
	xor	ecx, ecx
first_command_char:
	lodsb
	cmp	al, 20h
	ja	short pass_space_control
	jb	short loc_move_cmd_arguments_ok
	cmp	esi, CommandBuffer + 79
	jb	short first_command_char
	jmp	short loc_move_cmd_arguments_ok

next_command_char:
	lodsb
	cmp	al, 20h
	ja	short pass_space_control
	jb	short loc_move_cmd_arguments_ok

loc_1st_cmd_arg: ; 30/01/2016
	lodsb
	cmp	al, 20h
	je	short loc_1st_cmd_arg
	jb	short loc_move_cmd_arguments_ok
	
        mov     byte [edi], 0
	inc	edi

loc_move_cmd_arguments:
	stosb
	cmp	esi, CommandBuffer + 79
	jnb	short loc_move_cmd_arguments_ok
        lodsb
	cmp	al, 20h
	jnb	short loc_move_cmd_arguments
	jmp	short loc_move_cmd_arguments_ok

pass_space_control:
	cmp	al, 61h
	jb	short pass_capitalize
	cmp	al, 7Ah
	ja	short pass_capitalize
	and	al, 0DFh
pass_capitalize:
	stosb   
	inc     cl
        cmp     esi, CommandBuffer + 79
	jb      short next_command_char 

loc_move_cmd_arguments_ok:
        mov     byte [edi], 0
       
call_command_intepreter:
	call    command_interpreter

        mov	ecx, 80
	;mov	cx, 80
	mov	edi, CommandBuffer
	xor	al, al
	rep	stosb
	;cmp	byte [Program_Exit], 0
	;ja	short loc_terminate_trdos
        
	; 16/01/2016
	cmp	byte [CRT_MODE], 3 ; 80*25 color
	je	short pass_set_txt_mode

	call	set_txt_mode ; set vide mode to 03h

loc_check_active_page:
	xor	al, al
	cmp	[ACTIVE_PAGE], al ; 0
        je      loc_TRDOS_prompt 
	; AL = 0 = video page 0
	call	set_active_page
        jmp     loc_TRDOS_prompt ; infinitive loop

pass_set_txt_mode: 
	mov	esi, nextline
	call	print_msg
	jmp     short loc_check_active_page

rw_char:
	; 30/01/2016
	; 29/01/2016
	; 17/01/2016 (TRDOS 386 = TRDOS v2.0)
	; 2004-2005
	
	; DH = cursor row, DL = cursor column
	; BL = 0 = video page number (active page)

readnextchar:
	xor     ah, ah
	call	int16h
	and	al, al
	jz	short loc_arrow    
	cmp	al, 0E0h          
	je	short loc_arrow
	cmp	al, 08h             
	jne	short char_return
loc_back:
	cmp	dl, [CursorColumn]
	jna     short readnextchar
prev_column:
	dec	dl
set_cursor_pos:
	push	dx
	xor	bl, bl ; 0 = video page 0
	; DH = Row, DL = Column
	call	_set_cpos ; 17/01/2016
        pop	dx
	movzx	ebx, dl
	sub	bl, [CursorColumn] 
	mov	ah, 7 ; color attribute
	mov	al, 20h
	mov	[CommandBuffer+ebx], al
	sub	bl, bl ; video page 0
	;mov	cx, 1
	call	_write_c_current ; 17/01/2016
	;mov	dx, [CURSOR_POSN]
	jmp	short readnextchar
loc_arrow:    
	cmp	ah, 4Bh
	je	short loc_back
	cmp	ah, 53h
	je      short loc_back
	cmp	ah, 4Dh
	jne	short readnextchar
	cmp	dl, 79
	jnb	short readnextchar
	inc	dl
	jmp	short set_cursor_pos
char_return:
	movzx	ebx, dl
	sub	bl, [CursorColumn] 
	cmp	al, 20h
	jb	short loc_escape
	mov	[CommandBuffer+ebx], al
	sub	bl, bl ; 0
	cmp	dl, 79
	jnb	short readnextchar
	mov	ah, 7 ; color attribute
	call	WRITE_TTY
	mov	dx, [CURSOR_POSN] ; video page 0
        jmp     readnextchar
loc_escape:
	cmp	al, 1Bh
	je	short rw_char_retn
	;
	cmp	al, 0Dh ; CR
        jne     readnextchar
	xor	bl, bl ; 0
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
	mov	ah, 7 ; attribute/color
	mov	al, 0Ah ; LF
	call	WRITE_TTY
rw_char_retn:
	retn

show_date:
	; 18/01/2016 (TRDOS 386 = TRDOS v2.0)
        ; 2004-2005

	;mov	ah, 04h
	;call	int1Ah
	call	RTC_40	; GET RTC DATE

	mov	al, dl
  	call	bcd_to_ascii
	mov	[Day], ax

	mov	al, dh
  	call	bcd_to_ascii
	mov	[Month], ax

	mov	al, ch
  	call	bcd_to_ascii
	mov	[Century], ax

	mov	al, cl
  	call	bcd_to_ascii
	mov	word [Year], ax

	mov	esi, Msg_Show_Date
	call	print_msg

	retn

set_date:
	; 18/01/2016 (TRDOS 386 = TRDOS v2.0)
        ; 2004-2005

	mov	esi, Msg_Enter_Date
	call	print_msg

loc_enter_day_1:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 13
	je	loc_set_date_retn
	cmp	al, 27
	je	loc_set_date_retn
	mov	[Day], al
	cmp	al, '0'
	jb	loc_set_date_stc_0
	cmp	al, '3'
	ja	loc_set_date_stc_0
	;sub	bl, bl ; 0
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_day_2:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_date_retn
	mov	[Day+1], al
	cmp	al, '0'
        jb      loc_set_date_stc_1
	cmp	al, '9'
        ja      loc_set_date_stc_1
	cmp	byte [Day], '3'
	jb	short pass_set_day_31
	cmp	al, '1'
        ja      loc_set_date_stc_1
pass_set_day_31:
	;sub	bl, bl ; 0
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_separator_1:
	sub     ah, ah ; 0
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_date_retn
	cmp	al, '-'
	je	short pass_set_date_separator_1
	cmp	al, '/'
        jne     loc_set_date_stc_2
pass_set_date_separator_1:
	;xor	bl, bl ; 0
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_month_1:
	xor     ah, ah ; 0
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_date_retn
	mov	[Month], al
	cmp	al, '0'
        jb      loc_set_date_stc_3
	cmp	al, '1'
        ja      loc_set_date_stc_3
	;sub	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_month_2:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_date_retn
	mov	[Month+1], al
	cmp	al, '0'
        jb      loc_set_date_stc_4
	cmp	al, '9'
        ja      loc_set_date_stc_4
	cmp	byte [Month], '1'
	jb	short pass_set_month_12
	cmp	al, '2'
        ja      loc_set_date_stc_4
pass_set_month_12:
	;sub	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_separator_2:
	sub     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_date_retn
	cmp	al, '-'
	je	short pass_set_date_separator_2
	cmp	al, '/'
        jne     loc_set_date_stc_5
pass_set_date_separator_2:
	;xor	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_year_1:
	xor    ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_date_retn
	mov	[Year], al
	cmp	al, '0'
        jb      loc_set_date_stc_6
	cmp	al, '9'
        ja      loc_set_date_stc_6
	;sub	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_year_2:
	xor	ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
	je	short loc_set_date_retn
	mov	byte [Year+1], al
	cmp	al, '0'
        jb      loc_set_date_stc_7
	cmp	al, '9'
        ja      loc_set_date_stc_7
	;sub	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_set_date_get_lchar_again:
	sub	ah, ah ; 0
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 13 ; ENTER key
	je	short loc_set_date_progress
	cmp	al, 27 ; ESC key
	je	short loc_set_date_retn
	;
	call	check_for_backspace
	jne	short loc_set_date_get_lchar_again

loc_set_date_bs_8:
	call	write_backspace
	jmp	short loc_enter_year_2

loc_set_date_progress:
	; Get Current Date
	;mov	ah, 04h
	;call	int1Ah
	call	RTC_40	; GET RTC DATE
	; CH = century (in BCD)

	mov	ax, [Year]
	sub	ax, '00'
	shl	al, 4 ; * 16
	mov	cl, al
	add	cl, ah
	mov	ax, [Month]
	sub	ax, '00'
	shl	al, 4 ; * 16
	mov	dh, al
	add	dh, ah
	mov	ax, [Day]
	sub	ax, '00'
	shl	al, 4 ; * 16
	mov	dl, al
	add	dl, ah

	;mov	ah, 05h
	;call	int1Ah
	call	RTC_50	; SET RTC DATE

loc_set_date_retn:
	mov	esi, nextline
	call	print_msg
	retn

loc_set_date_stc_0:
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_day_1
loc_set_date_stc_1:
	call	check_for_backspace
	je	short loc_set_date_bs_1
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_day_2
loc_set_date_bs_1:
	call	write_backspace
        jmp     loc_enter_day_1
loc_set_date_stc_2:
	call	check_for_backspace
	je	short loc_set_date_bs_2
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_separator_1
loc_set_date_bs_2:
	call	write_backspace
        jmp     loc_enter_day_2
loc_set_date_stc_3:
	call	check_for_backspace	
	je short loc_set_date_bs_3
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_month_1
loc_set_date_bs_3:
	call	write_backspace
        jmp     loc_enter_separator_1
loc_set_date_stc_4:
	call	check_for_backspace	
	je	short loc_set_date_bs_4
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_month_2
loc_set_date_bs_4:
	call	write_backspace
        jmp     loc_enter_month_1
loc_set_date_stc_5:
	call	check_for_backspace
	je	short loc_set_date_bs_5
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_separator_2
loc_set_date_bs_5:
	call	write_backspace
        jmp     loc_enter_month_2
loc_set_date_stc_6:
	call	check_for_backspace
        je      short  loc_set_date_bs_6
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_year_1
loc_set_date_bs_6:
	call	write_backspace
        jmp     loc_enter_separator_2
loc_set_date_stc_7:
	call	check_for_backspace
	je	short loc_set_date_bs_7
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_year_2
loc_set_date_bs_7:
	call	write_backspace
        jmp     loc_enter_year_1

write_backspace:
	; 18/01/2016 (TRDOS 386 = TRDOS v2.0)
	mov	al, 08h ; BACKSPACE
	;xor	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
	mov	al, 20h ; BLANK/SPACE char 
	;xor	bl, bl
	mov	ah, 7 ; attribute/color
	;call	_write_c_current
	;retn
	jmp	_write_c_current

check_for_backspace:
	; 18/01/2016 (TRDOS 386 = TRDOS v2.0)
	cmp	ax, 0E08h
	je	short cfbs_retn
	cmp	ax, 4BE0h
	je	short cfbs_retn
	cmp	ax, 4B00h
	je	short cfbs_retn
	cmp	ax, 53E0h
cfbs_retn:
	retn

show_time:
	; 18/01/2016 (TRDOS 386 = TRDOS v2.0)
        ; 2004-2005

	;mov	ah, 02h
	;call	int1Ah
	call	RTC_20	; GET RTC TIME
	
	mov	al, ch
	call	bcd_to_ascii
	mov	[Hour], ax

	mov	al, cl
	call	bcd_to_ascii
	mov	[Minute], ax

	mov	al, dh
	call	bcd_to_ascii
	mov	[Second], ax

	mov	esi, Msg_Show_Time
	call	print_msg
	retn

set_time:
	; 18/01/2016 (TRDOS 386 = TRDOS v2.0)
        ; 2004-2005

	mov 	esi, Msg_Enter_Time
	call	print_msg

loc_enter_hour_1:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 13 ; ENTER key
        je      loc_set_time_retn
	cmp	al, 27 ; ESC key
        je      loc_set_time_retn
	mov	[Hour], al
	cmp	al, '0'
        jb      loc_set_time_stc_0
	cmp	al, '2'
        ja      loc_set_time_stc_0
	;sub	bl, bl ; 0
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_hour_2:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_time_retn
	mov	[Hour+1], al
	cmp	al, '0'
        jb      loc_set_time_stc_1
	cmp	al, '9'
	ja	loc_set_time_stc_1
        cmp     byte [Hour], '2'
	jb	short pass_set_time_24
	cmp	al, '4'
        ja      loc_set_time_stc_1
pass_set_time_24:
	;sub	bl, bl ; 0
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_time_separator_1:
	sub    ah, ah ; 0
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_time_retn
	cmp	al, ':'
        jne     loc_set_time_stc_2
	;xor	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_minute_1:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_time_retn
	mov	[Minute], al
	cmp	al, '0'
        jb      loc_set_time_stc_3
	cmp	al, '5'
        ja      loc_set_time_stc_3
	;sub	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_minute_2:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
        je      loc_set_time_retn
	mov	[Minute+1], al
	cmp	al, '0'
        jb      loc_set_time_stc_4
	cmp	al, '9'
        ja      loc_set_time_stc_4
	;sub	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_time_separator_2:
	mov	word [Second], 3030h
	sub     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 13
        je      loc_set_time_progress
	cmp	al, 27
        je      loc_set_time_retn
	cmp	al, ':'
        jne     loc_set_time_stc_5
	;xor	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_second_1:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 13
	je	short loc_set_time_progress
	cmp	al, 27
        je      loc_set_time_retn
	mov	[Second], al
	cmp	al, '0'
        jb      loc_set_time_stc_6
	cmp	al, '5'
        ja      loc_set_time_stc_6
	;sub	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_enter_second_2:
	xor     ah, ah
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 27
	je	short loc_set_time_retn
	cmp	al, '0'
        jb      loc_set_time_stc_7
	cmp	al, '9'
        ja      loc_set_time_stc_7
	;sub	bl, bl
	mov	ah, 7 ; attribute/color
	call	WRITE_TTY
loc_set_time_get_lchar_again:
	sub	ah, ah ; 0
	call	int16h
	; AL = ASCII Code of the Character
	cmp	al, 13
	je	short loc_set_time_progress
	cmp	al, 27
	je	short loc_set_time_retn
	;
	call	check_for_backspace
	jne	short loc_set_time_get_lchar_again

loc_set_time_bs_8:
	call	write_backspace
	jmp	short loc_enter_second_2

loc_set_time_progress:
	; Get Current Time
	;mov 	ah, 02h
	;call	int1Ah
	call	RTC_20	; GET RTC TIME
	;DL = Daylight Savings Enable option (0-1)	

	mov	ax, [Hour]
	sub	ax, '00'
	shl	al, 4 ; * 16
	mov	ch, al
	add	ch, ah
	mov	ax, [Minute]
	sub	ax, '00'
	shl	al, 4 ; * 16
	mov	cl, al
	add	cl, ah
	mov	ax, [Second]
	sub	ax, '00'
	shl	al, 4 ; * 16
	mov	dh, al
	add	dh, ah
	
	;mov	ah, 03h
	;call	int1Ah
	call	RTC_30	; SET RTC TIME

loc_set_time_retn:
	mov 	esi, nextline
	call	print_msg
	retn

loc_set_time_stc_0:
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_hour_1
loc_set_time_stc_1:
	call	check_for_backspace
	je	short loc_set_time_bs_1
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_hour_2
loc_set_time_bs_1:
	call	write_backspace
        jmp     loc_enter_hour_1
loc_set_time_stc_2:
	call	check_for_backspace
	je	short loc_set_time_bs_2
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_time_separator_1
loc_set_time_bs_2:
	call	write_backspace
        jmp     loc_enter_hour_2
loc_set_time_stc_3:
	call	check_for_backspace
	je	short loc_set_time_bs_3
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !6
        jmp     loc_enter_minute_1
loc_set_time_bs_3:
	call	write_backspace
        jmp     loc_enter_time_separator_1
loc_set_time_stc_4:
	call	check_for_backspace
	je	short loc_set_time_bs_4
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_minute_2
loc_set_time_bs_4:
	call	write_backspace
        jmp     loc_enter_minute_1
loc_set_time_stc_5:
	call	check_for_backspace
	je	short loc_set_time_bs_5
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_time_separator_2
loc_set_time_bs_5:
	call	write_backspace
        jmp     loc_enter_minute_2
loc_set_time_stc_6:
	call	check_for_backspace
	je	short loc_set_time_bs_6
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
	mov	word [Second], 3030h
        jmp     loc_enter_second_1
loc_set_time_bs_6:
	call	write_backspace
        jmp     loc_enter_time_separator_2
loc_set_time_stc_7:
	call	check_for_backspace
	je	short loc_set_time_bs_7
	;xor	bl, bl ; video page 0
	call	beeper ; BEEP !
        jmp     loc_enter_second_2
loc_set_time_bs_7:
	call	write_backspace
        jmp     loc_enter_second_1

print_volume_info:
	; 08/02/2016
	; 06/02/2016
	; 04/02/2016
	; 18/01/2016 (TRDOS 386 = TRDOS v2.0)
	; 25/10/2009
	;
	; "Volume Serial No: "
 	;
	; INPUT  : AL = DOS Drive Number
	; OUTPUT : AH = FS Type
	;          AL = DOS Drive Name
	; CF = 0 -> OK
	; CF = 1 -> Drive not ready 

	mov	ah, al
	sub	al, al
	movzx	esi, ax	
	add	esi, Logical_DOSDisks
	mov	al, [esi]
	cmp	al, 'A'  
	jnb	short loc_pvi_set_vol_name
	mov	ah, [esi+LD_FSType]
	retn

loc_pvi_set_vol_name:
	mov	[Vol_Drv_Name], al
	push	esi
	call	move_volume_name_and_serial_no ;;;
	jnc	short loc_pvi_mvn_ok
	pop	esi
	retn

loc_pvi_mvn_ok:
	mov	esi, [esp]
	cmp	byte [esi+LD_FSType], 0A1h
	jne	short loc_pvi_fat_vol_size
	mov	eax, [esi+LD_FS_VolumeSize]
	movzx	ebx, word [esi+LD_FS_BytesPerSec]
	jmp	short loc_vol_size_mul32
loc_pvi_fat_vol_size:
	mov	eax, [esi+LD_TotalSectors]
	movzx	ebx, word [esi+LD_BPB+BPB_BytsPerSec]
loc_vol_size_mul32:
	mul	ebx
	or	edx, edx
	jnz	short loc_vol_size_in_kbytes
loc_vol_size_in_bytes:
	mov	ecx, VolSize_Bytes
	jmp	short loc_write_vol_size_str
loc_vol_size_in_kbytes:
	mov	bx, 1024
	div	ebx
	mov 	ecx, VolSize_KiloBytes
	xor	edx, edx ; 0
loc_write_vol_size_str:
	mov	[VolSize_Unit1], ecx
	; 
	mov	edi, Vol_Tot_Sec_Str_End
        ;mov	byte [edi], 0
	mov	ecx, 10
loc_write_vol_size_chr:
	div	ecx
	add	dl, '0'
	dec	edi	
	mov	[edi], dl
	test	eax, eax
	jz	short loc_write_vol_size_str_ok
	sub	dl, dl ; 0
	jmp	short loc_write_vol_size_chr

loc_write_vol_size_str_ok:
	mov	[Vol_Tot_Sec_Str_Start], edi
	;
	mov	edi, Vol_FS_Name
	mov	cl, [esi+LD_FATType]
	and	cl, cl ; 0 ?
	jnz	short loc_write_vol_FAT_str_1
	mov	word [edi], 'TR'
	mov	dword [edi+4], ' FS1'
	;movzx	ebx, word [esi+LD_FS_BytesPerSec]
	mov	bx, [esi+LD_FS_BytesPerSec]
	mov	eax, [esi+LD_FS_FreeSectors]
	jmp	short loc_vol_freespace_mul32

loc_write_vol_FAT_str_1:
	mov	ax, '32' ; FAT32
	cmp	cl, 2 ; [esi+LD_FATType]
	ja	short loc_write_vol_FAT_str_2
	mov	ax, '12' ; FAT12
	jb	short loc_write_vol_FAT_str_2
	mov	ah, '6'  ; FAT16
loc_write_vol_FAT_str_2:
	mov	dword [edi], 'FAT '
	mov	word [edi+4], ax
	;
	;movzx	ebx, word [esi+LD_BPB+BPB_BytsPerSec]
	mov	bx, [esi+LD_BPB+BPB_BytsPerSec]
	mov	eax, [esi+LD_FreeSectors]

loc_vol_freespace_mul32:
	mul	ebx
	or	edx, edx
	jnz	short loc_vol_fspace_in_kbytes
loc_vol_fspace_in_bytes:
	mov	ecx, VolSize_Bytes
	jmp	short loc_write_vol_fspace_str
loc_vol_fspace_in_kbytes:
	mov	bx, 1024
	div	ebx
	mov 	ecx, VolSize_KiloBytes
	xor	edx, edx ; 0
loc_write_vol_fspace_str:
	mov	[VolSize_Unit2], ecx
	;	
	mov	edi, Vol_Free_Sectors_Str_End
        ;mov	byte [edi], 0
	mov	ecx, 10
loc_write_vol_fspace_chr:
	div	ecx
	add	dl, '0'
	dec	edi	
	mov	[edi], dl
	test	eax, eax
	jz	short loc_write_vol_fspace_str_ok
	sub	dl, dl ; 0
	jmp	short loc_write_vol_fspace_chr

loc_write_vol_fspace_str_ok:
	mov	[Vol_Free_Sectors_Str_Start], edi
	;
	mov	esi, Volume_in_drive
	call	print_msg
	mov	esi, Vol_Name
	call	print_msg
	mov	esi, nextline
	call	print_msg
	;
	mov	esi, Vol_Total_Sector_Header
	call	print_msg
	mov	esi, [Vol_Tot_Sec_Str_Start]
	call	print_msg
	mov	esi, [VolSize_Unit1]
	call	print_msg
	;
	mov	esi, Vol_Free_Sectors_Header
	call	print_msg
	mov	esi, [Vol_Free_Sectors_Str_Start]
	call	print_msg
	mov	esi, [VolSize_Unit2]
	call	print_msg
	;
	pop	esi
	
	;mov	ah, [esi+LD_FSType]
	;mov	al, [esi+LD_FATType]
	mov	ax, [esi+LD_FATType]

	retn

move_volume_name_and_serial_no:
	; 08/02/2016  (TRDOS 386 = TRDOS v2.0)
	; this routine will be called by
	; "print_volume_info" and "print_directory"
	; INPUT ->
	;	ESI = Logical DOS drv descripton table address
	; OUTPUT ->
	;	*Volume name will be moved to text area
	;	*Volume serial number will be converted to
	;	 text and will be moved to text area
	;   cf = 1 -> invalid/unknown dos drive
	;   cf = 0 -> ecx = 0
	;
	; (eax, edx, ecx, esi, edi will be changed)

	mov 	edi, Vol_Name

	;mov	ah, [esi+LD_FSType]
	;mov	al, [esi+LD_FATType]
	mov	ax, [esi+LD_FATType]
	cmp	ah, 0A1h
	je	short mvn_2
	or	ah, ah
	jz	short mvn_0
	or	al, al
	jnz	short mvn_1
mvn_0:
	mov	al, [esi]
	stc
	retn
mvn_1:
	cmp	al, 2
	ja	short mvn_3 
	;or	al, al
	;jz	short mvn_2
	mov	eax, [esi+LD_BPB+VolumeID]
	add	esi, LD_BPB+VolumeLabel
	jmp	short mvn_4
mvn_2:
	mov	eax, [esi+LD_FS_VolumeSerial]
	add	esi, LD_FS_VolumeName
	mov	ecx, 16
	rep	movsd
	jmp	short mvn_5
mvn_3:
	mov	eax, [esi+LD_BPB+FAT32_VolID]
	add	esi, LD_BPB+FAT32_VolLab
mvn_4:
	mov	ecx, 11
	rep	movsb
	mov	byte [edi], 0
mvn_5:
	;mov	[Current_VolSerial], eax  
	call	dwordtohex
	mov	[Vol_Serial1], edx
	mov	[Vol_Serial2], eax
	; ecx = 0
	retn

get_volume_serial_number:
	; 19/01/2016 (TRDOS 386 = TRDOS v2.0)
	; 08/08/2010
	;
	; INPUT -> DL = Logical DOS Drive number
	; OUTPUT -> EAX = Volume serial number
	;          BL= FAT Type	    
	;          BH = Logical DOS drv Number (DL input)
	; cf = 1 -> Drive not ready

	xor	ebx, ebx
	mov	bh, dl
	cmp	[Last_DOS_DiskNo], dl
	jnb	short loc_gvsn_start
loc_gvsn_stc_retn:
	xor	eax, eax
	stc 
        retn 
loc_gvsn_start:
	push	esi
	mov	esi, Logical_DOSDisks
	add	esi, ebx
	mov	bl, [esi+LD_FATType]
	and	bl, bl
	jz	short loc_gvsn_fs
	cmp	bl, 2
	ja	short loc_gvsn_fat32
loc_gvsn_fat:
	add	esi, LD_BPB + VolumeID
	jmp	short loc_gvsn_return
loc_gvsn_fat32: 
	add	esi, LD_BPB + FAT32_VolID
	jmp	short loc_gvsn_return 
loc_gvsn_fs:
	cmp	byte [esi+LD_FSType], 0A1h
	jne	short loc_gvsn_stc_retn 
	add	esi, LD_FS_VolumeSerial
loc_gvsn_return:
	mov	eax, [esi]
	pop	esi
	retn

; CMD_INTR.ASM [ TRDOS Command Interpreter Procedure ]
; 09/11/2011
; 29/01/2005

command_interpreter:
	; 04/02/2016
	; 03/02/2016
	; 30/01/2016
	; 29/01/2016 (TRDOS 386 = TRDOS 2.0)
	; 15/09/2011         
	; 29/01/2005
        
	; Input: ecx = command word length (CL)
	;	 CommandBuffer = Command string offset
 
	mov	byte [Program_Exit],0
	cmp	cl, 4
        ja      c_6
        jb      c_2
c_4:

cmp_cmd_exit:
	mov	edi, Cmd_Exit
	call	cmp_cmd	
	jc	short cmp_cmd_date

        mov     byte [Program_Exit], 1
        retn

cmp_cmd_date:
	mov	cl, 4
	mov	edi, Cmd_Date
	call	cmp_cmd	
        jc	short cmp_cmd_time
	
	call	show_date
	call	set_date
	retn

cmp_cmd_time:
	mov	cl, 4
	mov	edi, Cmd_Time
   	call	cmp_cmd	
	jc	short cmp_cmd_show

	call	show_time
	call	set_time
	retn

cmp_cmd_show:
	mov	cl, 4
	mov	edi, Cmd_Show
   	call	cmp_cmd	
        jnc     show_file

cmp_cmd_echo:
	mov	cl, 4
	mov	edi, Cmd_Echo
   	call	cmp_cmd	
	jc	short cmp_cmd_copy

	cmp	byte [esi], 20h
	jnb	print_msg
	retn

cmp_cmd_copy:
	mov	cl, 4
	mov	edi, Cmd_Copy
   	call	cmp_cmd	
	jnc	copy_file

cmp_cmd_move:
	mov	cl, 4
	mov	edi, Cmd_Move
   	call	cmp_cmd	
	jnc	move_file

cmp_cmd_path:
	mov	cl, 4
	mov	edi, Cmd_Path
   	call	cmp_cmd	
	jnc	set_get_path

cmp_cmd_beep:
	mov	cl, 4
	mov	edi, Cmd_Beep
   	call	cmp_cmd	
	jnc	beeper

cmp_cmd_find:
	mov	cl, 4
	mov	edi, Cmd_Find
   	call	cmp_cmd	
        jc      loc_cmd_failed

	;call	find_and_list_files
	jmp	find_and_list_files
	;retn

c_1:
	lodsd
cmp_cmd_help:
	cmp	al, '?'
        jne     short cmp_cmd_remark

	mov	esi, Command_List
cmd_help_next_w:
	call	print_msg

	cmp	byte [esi], 20h ; 0
	jb	short cmd_help_retn
	
	push	esi
	mov	esi, nextline
	call	print_msg
	pop	esi
	jmp	short cmd_help_next_w	

cmp_cmd_remark:
	cmp	al, '*'
        jne     loc_cmd_failed
	inc	esi
	mov	edi, Remark
	mov	al, [esi]
	cmp	al, 20h
	ja	short cmd_remark_write
	mov	esi, edi ; Remark
	jmp	print_msg

cmd_remark_write:
	stosb
	lodsb
	cmp	al, 20h
	jnb	short cmd_remark_write
	mov	byte [edi], 0

cmd_help_retn:
cmd_remark_retn:
cd_retn:
	retn

c_2:
	cmp	cl, 2
        ja      c_3
	mov	esi, CommandBuffer
	jb	short c_1

cmp_cmd_cd:
	lodsw
	cmp	ax, 'CD'
	jne	short cmp_cmd_drive
        inc	esi
cd_0:
	mov	ax, [esi]	
	cmp	al, 20h
	jna	short cd_retn
	; 10/02/2016
	cmp	ah, ':'
	jne	short cd_1
	inc	esi
	inc	esi
	jmp	short cd_2

cd_1:	; change current directory
	; 29/11/2009
	; AH = CDh	; to separate 'CD' command from others
			; for restoring current directory
			; 0CDh sign is for saving cdir into 
			; DOS drv description table cdir area
	
	mov	ah, 0CDh ; mov byte [CD_COMMAND], 0CDh 

	call	change_current_directory
        jnc     change_prompt_dir_string

cd_error_messages:
	cmp	al, 3
	je	short cd_path_not_found
	cmp	al, 15h 
	je	short cd_drive_not_ready
	cmp	al, 18h ; Bad request structure length 
	je	short cd_command_failed
	cmp	al, 1Ah ; Unknown media type, non-DOS disk
        je      short cd_command_failed

cd_path_not_found:
	push	ax	
	mov	esi, Msg_Dir_Not_Found
	call	print_msg
	pop	ax
	cmp	ah, [Current_Dir_Level]
        jnb     change_prompt_dir_string
	mov	[Current_Dir_Level], ah
        jmp     change_prompt_dir_string   

cmp_cmd_drive: ; change current drive
	; C:, D:, E: etc.
	cmp	ah, ':'
        jne     loc_cmd_failed

cd_2:	; 'CD C:', 'CD D:' ...
	cmp	byte [esi], 20h
        ja      loc_cmd_failed

	and	al, 0DFh
	sub	al, 'A'
        jc      loc_cmd_failed

        cmp     al, [Last_DOS_DiskNo]
        ja	short cd_drive_not_ready
	
	mov	dl, al
	call 	change_current_drive
	jc	short cd_drive_not_ready	
	retn

cd_drive_not_ready:
	mov	esi, Msg_Not_Ready_Read_Err
	call	print_msg

cd_fail_drive_restart:
	mov	dl, [Current_Drv]
	;call 	change_current_drive
        jmp     change_current_drive
	;retn

cd_command_failed:
	mov	esi, Msg_Bad_Command
	call	print_msg
	jmp	short cd_fail_drive_restart

c_3:
cmp_cmd_dir:

	mov	edi, Cmd_Dir
	call	cmp_cmd	
	jnc	print_directory_list

cmp_cmd_cls:
	mov	cl, 3
	mov	edi, Cmd_Cls
	call	cmp_cmd	
        jnc	clear_screen

cmp_cmd_ver:
	mov	cl, 3
	mov	edi, Cmd_Ver
	call	cmp_cmd	
	jc	short cmp_cmd_mem

	mov	esi, mainprog_Version
	;call	print_msg
	jmp	print_msg
	;retn

cmp_cmd_mem:
	mov	cl, 3
	mov	edi, Cmd_Mem
	call	cmp_cmd	
	jnc	memory_info

cmp_cmd_del:
	mov	cl, 3
	mov	edi, Cmd_Del
	call	cmp_cmd	
        jnc     delete_file

cmp_cmd_set:
	mov	cl, 3
	mov	edi, Cmd_Set
	call	cmp_cmd	
        jnc     set_get_env

cmp_cmd_run:
	mov	cl, 3
	mov	edi, Cmd_Run
	call	cmp_cmd	
        jc      loc_cmd_failed

	call	set_exec_arguments
        jc      loc_cmd_failed

	call	load_and_execute_file
        jc      loc_run_cmd_failed
	retn

c_5:
cmp_cmd_attrib:
	mov	edi, Cmd_Attrib
	call	cmp_cmd	
        jnc     set_file_attributes

cmp_cmd_mkdir:
	mov	cl, 5
	mov	edi, Cmd_Mkdir
	call	cmp_cmd	
        jnc     make_directory

cmp_cmd_rmdir:
	mov	cl, 5
	mov	edi, Cmd_Rmdir
	call	cmp_cmd	
        jnc     delete_directory

cmp_cmd_chdir:
	mov	cl, 5
	mov	edi, Cmd_Chdir
	call	cmp_cmd	
	jc	loc_cmd_failed

	jmp	cd_0

c_6:
	cmp	cl, 6
        ja      c_8
	jb	short c_5
cmp_cmd_prompt:
	mov	edi, Cmd_Prompt
	call	cmp_cmd	
        jc	short cmp_cmd_volume
get_prompt_name_fchar:
	lodsb
	cmp	al, 20h
	je	short get_prompt_name_fchar
	ja	short loc_change_prompt_label
	mov	esi, TRDOSPromptLabel
	mov	dword [esi], "TRDO"
       	mov	word [esi+4], "S" 
loc_cmd_prompt_return:
	retn
loc_change_prompt_label:
	mov	cx, 11
	mov	edi, TRDOSPromptLabel
put_char_new_prompt_label:
	stosb
	lodsb
	cmp	al, 20h
	jb	short pass_put_new_prompt_label
	loop	put_char_new_prompt_label
pass_put_new_prompt_label:
	mov	byte [edi], 0
	retn

cmp_cmd_volume:
	mov	cl, 6
	mov	edi, Cmd_Volume
	call	cmp_cmd	
        jc	short cmp_cmd_rename

cmd_vol1:
	lodsb
	cmp	al, 20h
	ja	short cmd_vol2
	mov	al, [Current_Drv]
	jmp	short cmd_vol4
cmd_vol2:
	cmp	al, 'A'
        jb      loc_cmd_failed
	cmp	al, 'z'
        ja      loc_cmd_failed
	cmp	al, 'Z'
	jna	short cmd_vol3
	cmp	al, 'a'
	jb	short loc_cmd_failed
	and	al, 0DFh
cmd_vol3:
	mov	ah, [esi]
	cmp	ah, ':'
	jne	short loc_cmd_failed
	sub	al, 'A'
        cmp     al, [Last_DOS_DiskNo]
	jna	short cmd_vol4

	mov	esi, Msg_Not_Ready_Read_Err
	jmp	print_msg
	
cmd_vol4:
	call	print_volume_info
        jc      cd_drive_not_ready
	retn

cmp_cmd_rename:
	mov	cl, 6
	mov	edi, Cmd_Rename
	call	cmp_cmd	
        jnc     rename_file

cmp_cmd_device:
	mov	cl, 6
	mov	edi, Cmd_Device
	call	cmp_cmd	
        jc	short loc_cmd_failed

	retn

c_7:
cmp_cmd_devlist:
	mov	edi, Cmd_DevList
	call	cmp_cmd	
        jc	short loc_cmd_failed

	retn

c_8:
        cmp	cl, 8
	ja	short cmp_cmd_external
	jb	short c_7

cmp_cmd_longname:
	mov	edi, Cmd_LongName
	call	cmp_cmd	
        jnc     get_and_print_longname

cmp_cmd_readfile:
	mov	cl, 8
	mov	edi, Cmd_ReadFile
	call	cmp_cmd	
        jc	short loc_cmd_failed

loc_cmd_return:
	retn

cmp_cmd_external:
loc_cmd_failed:
	cmp	byte [CommandBuffer], 20h
	jna	short loc_cmd_return
	mov	esi, Msg_Bad_Command
;	call	print_msg
;loc_cmd_return:
;	retn
	jmp	print_msg

cmp_cmd:
	 ; 29/01/2016 (TRDOS 386 = TRDOS v2.0)
         mov  esi, CommandBuffer
         ; edi = internal command word (ASCIIZ)
	 ; ecx = command length (<=8)
cmp_cmd_1:
	lodsb
	scasb
	jne	short cmp_cmd_3
	loop	cmp_cmd_1
 	lodsb
	cmp	al, 20h
	ja	short cmp_cmd_2
	xor	al, al
	; ZF = 1 -> internal command word matches
	retn
cmp_cmd_2:
	; ZF = 0 (CF = 0) -> external command word 	
	pop	eax ; no return to the caller from here 
	jmp	cmp_cmd_external	
cmp_cmd_3:
	stc
	; CF = 1 -> internal command word does not match
	retn

loc_run_cmd_failed:
	; 15/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 07/12/2009 (CMD_INTR.ASM)	
	; 29/11/2009

	call	restore_cdir_after_cmd_fail

loc_run_cmd_failed_cmp_al:
	; End of Restore_CDIR code (29/11/2009)

	cmp	al, 1
	je	loc_cmd_failed
loc_run_dir_not_found:
	cmp	al, 3
	jne	short loc_run_file_notfound_msg
	; Path not found (MS-DOS Error Code = 3)
	mov	esi, Msg_Dir_Not_Found
	jmp	print_msg

loc_run_file_notfound_msg:
	cmp	al, 2 ; File not found
	jne	short loc_run_file_drv_read_err

loc_print_file_notfound_msg: 
        mov     esi, Msg_File_Not_Found
	;call	proc_printmsg
	;retn
	jmp	print_msg

loc_run_file_drv_read_err:
	; 16/05/2010 -> Err: 1Eh (Read fault)
	cmp	al, 1Eh ; Drive not ready or read error
	je	short loc_run_file_print_drv_read_err
	;
	cmp	al, 15h ; Drive not ready (or read error)
	jne	short loc_run_file_toobig

loc_run_file_print_drv_read_err:
	mov	esi, Msg_Not_Ready_Read_Err
	jmp	print_msg

loc_run_file_toobig:
	cmp	al, 8 ; Not enough free memory to load&run file
	jne	short loc_run_misc_error
	mov	esi, Msg_Insufficient_Memory
	jmp	print_msg

loc_run_misc_error:
	call	bytetohex ; 15/02/2016
	mov	[Error_Code], ax
	
	mov	esi, Msg_Error_Code 
	jmp	print_msg

restore_cdir_after_cmd_fail:
	; 15/02/2016 (TRDOS 386 =  TRDOS v2.0)
	push	eax
	mov	bh, [RUN_CDRV] ; it is set at the beginning
				; of the 'run' command.
	cmp	bh, [Current_Drv]
	je	short loc_run_restore_cdir
	mov	dl, bh
	call	change_current_drive 
	jmp	short loc_run_err_pass_restore_cdir

loc_run_restore_cdir:
	cmp	byte [Restore_CDIR], 0
	jna	short loc_run_err_pass_restore_cdir
	xor	bl, bl
	movzx	esi, bx
	add	esi, Logical_DOSDisks
	call	restore_current_directory

loc_run_err_pass_restore_cdir:
	pop	eax
	retn

print_directory_list:
	; 10/02/2016
	; 08/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 06/12/2009 ('cmp_cmd_dir')	
	;
	mov	word [AttributesMask], 0800h ; ..except volume names..
	mov	al, [Current_Drv]
	mov	[RUN_CDRV], al
get_dfname_fchar:
	lodsb
	cmp	al, 20h
	je	short get_dfname_fchar
        jb      loc_print_dir_call_all
	cmp	al, '-'
	jne	short loc_print_dir_call_flt
get_next_attr_char:
	lodsb
	cmp	al, 20h
	je	short get_next_attr_char
        jb      loc_cmd_failed
	and	al, 0DFh
	cmp	al, 'D' ; directories only ?
	jne	short pass_only_directories
	lodsb
	cmp	al, 20h
        ja      loc_cmd_failed
	or	byte [AttributesMask], 10h ; ..directory..
	jmp	short get_dfname_fchar_attr
pass_only_directories:
	cmp	al, 'F'	; files only ?
        jne     check_attr_s
	lodsb
	cmp	al, 20h
        ja      loc_cmd_failed
	or	byte [AttributesMask+1], 10h ; ..except directories..
get_dfname_fchar_attr:
	lodsb
	cmp	al, 20h
	je	short get_dfname_fchar_attr
	jb	short loc_print_dir_call_all

loc_print_dir_call_flt:
	dec	esi
	mov	edi, FindFile_Drv
	call	parse_path_name
 	jnc	short loc_print_dir_change_drv_1
	cmp	al, 1
        ja      loc_run_cmd_failed

loc_print_dir_change_drv_1:
	mov	dl, [FindFile_Drv]
loc_print_dir_change_drv_2:
	cmp	dl, [RUN_CDRV]
	je	short loc_print_dir_change_directory 
	call	change_current_drive
        jc      loc_run_cmd_failed
loc_print_dir_change_directory:
	cmp	byte [FindFile_Directory], 20h ; 0 or 20h ?
	jna	short pass_print_dir_change_directory

	inc	byte [Restore_CDIR]
	mov	esi, FindFile_Directory
	xor	ah, ah ; CD_COMMAND sign -> 0 
	call	change_current_directory
        jc      loc_run_cmd_failed

loc_print_dir_change_prompt_dir_string:
	call	change_prompt_dir_string

pass_print_dir_change_directory:
	mov	esi, FindFile_Name
	cmp	byte [esi], 20h ; ; 0 or 20h ?
	ja	short loc_print_dir_call

loc_print_dir_call_all:
	mov	dword [esi], '*.*'
loc_print_dir_call:
	call	print_directory

	mov	dl, [RUN_CDRV]  ; it is set at the beginning
	cmp	dl, [Current_Drv]
	je	short loc_print_dir_call_restore_cdir_retn
	call	change_current_drive 
	retn

loc_print_dir_call_restore_cdir_retn:
	cmp	byte [Restore_CDIR], 0
	jna	short pass_print_dir_call_restore_cdir_retn

	mov	esi, Logical_DOSDisks
	xor	eax, eax
	mov	ah, dl
	add	esi, eax

	call	restore_current_directory

pass_print_dir_call_restore_cdir_retn:
	retn

check_attr_s_cap:
	and	al, 0DFh
check_attr_s:
	cmp	al, 'S'
	jne	short pass_attr_s
	or	byte [AttributesMask], 4 ; system
	lodsb
	cmp	al, 20h
        je      get_dfname_fchar_attr
	jb	short loc_print_dir_call_all
	and	al, 0DFh
pass_attr_s:
	cmp	al, 'H'
	jne	short pass_attr_h
	or	byte [AttributesMask], 2 ; hidden
pass_attr_shr:
	lodsb
	cmp	al, 20h
        je      get_dfname_fchar_attr
	jb	short loc_print_dir_call_all
	jmp	short check_attr_s_cap

pass_attr_h:
	cmp	al, 'R'
	jne	short pass_attr_r
	or	byte [AttributesMask], 1 ; read only
	jmp	short pass_attr_shr

pass_attr_r:
	cmp	al, 'A'
        jne     loc_cmd_failed
	or	byte [AttributesMask], 20h ; archive
	jmp	short pass_attr_shr

print_directory:
	; 11/02/2016
	; 10/02/2016
	; 08/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 30/10/2010 ('proc_print_directory')	
	; 19/09/2009
	; 2005 
	; INPUT ->
	;	ESI = Asciiz File/Dir Name Address

	push	esi

	sub	eax, eax

	mov	word [Dir_Count], ax ; 0
	mov 	word [File_Count], ax ; 0
	mov 	dword [Total_FSize], eax ; 0

	call    clear_screen
	
	xor	ecx, ecx	
	mov     ch, [Current_Drv] ; DirBuff_Drv - 'A'
	mov     al, [Current_Dir_Drv] 
	mov     [Dir_Drive_Name], al
	mov	esi, Logical_DOSDisks
	add	esi, ecx

	call	move_volume_name_and_serial_no
	jnc	short print_dir_strlen_check

	pop	esi
	;call	beeper
	;retn
	jmp	beeper  ; beep ! and return

print_dir_strlen_check:
	mov	esi, Current_Dir_Root
	mov	edi, Dir_Str_Root
	
	;xor	ecx, ecx
        mov     cl, [Current_Dir_StrLen]
	inc	cl
	cmp	cl, 64
	jna	short pass_print_dir_strlen_shorting
	inc	esi
	add	esi, ecx
	sub	esi, 64 
	inc	edi
	mov	eax, '... ' 
	stosd
 
pass_print_dir_strlen_shorting:
	rep	movsb

	mov	esi, Dir_Drive_Str
	call	print_msg

	mov	esi, Vol_Serial_Header
	call	print_msg

	mov	esi, Dir_Str_Header
	call	print_msg
	
	mov	esi, next2line
	call	print_msg

loc_print_dir_first_file:
	mov	byte [PrintDir_RowCounter], 16
	mov	ax, [AttributesMask]
	pop	esi

	call	find_first_file
        jc      loc_dir_ok
	 
loc_dfname_use_this:
	; bl =	File Attributes (bh = Long Name Entry Length)
	test	bl, 10h  ; Is it a directory?
	jz	short loc_not_dir

	inc	word [Dir_Count]
	mov	edx, esi 	; FindFile_DirEntry address
 	mov	esi, Type_Dir	; '<DIR>     '
	mov	edi, Dir_Or_FileSize
	; move 10 bytes
	movsd
	movsd
	movsw	    	
	mov	esi, edx
	jmp     short loc_dir_attribute

loc_not_dir:
	inc	word [File_Count]
	add	[Total_FSize], eax

	mov	ecx, 10  ; 32 bit divisor
	mov	edi, ecx
	add	edi, Dir_Or_FileSize
loc_dir_rdivide:
	sub	edx, edx
	div	ecx 	 ; remainder in dl (< 10)
	add     dl, '0'	 ; to make visible (ascii)
	dec	edi
	mov     [edi], dl
	and	eax, eax
	jnz	short loc_dir_rdivide

loc_dir_fill_space:
	cmp     edi, Dir_Or_FileSize
	jna     short loc_dir_attribute
	dec     edi
	mov     byte [edi], 20h
	jmp     short loc_dir_fill_space

loc_dir_attribute:
	mov	dword [File_Attribute], 20202020h

	cmp	bl, 20h  ; Is it an archive file?
	jb	short loc_dir_pass_arch
	mov	byte [File_Attribute+3], 'A'

loc_dir_pass_arch:
	and	bl, 7
	jz	short loc_dir_file_name
	mov	bh, bl
	and	bl, 3
	cmp	bh, bl
	jna	short loc_dir_pass_s
	mov	byte [File_Attribute], 'S'

loc_dir_pass_s:
	and     bl,2
	jz      short loc_dir_pass_h
	mov     byte [File_Attribute+1], 'H'
loc_dir_pass_h:
	and     bh,1
	jz      short loc_dir_file_name
	mov     byte [File_Attribute+2], 'R'
loc_dir_file_name:
	;mov     bx, [esi+18h] ; Date
	;mov     dx, [esi+16h] ; Time
	mov	ebx, [esi+16h]
	mov	ecx, esi ; FindFile_DirEntry address
	mov     edi, File_Name
	; move 8 bytes
	movsd
	movsd
	mov	byte [edi], 20h
	inc	edi
	; move 3 bytes
	movsw
	movsb
	mov	esi, ecx

Dir_Time_start:
	;mov	ax, dx		; Time
	mov	ax, bx
	shr	ax, 5		; shift right 5 times
	and	ax, 0000111111b	; Minute Mask
	aam			; Q([AL]/10)->AH
				; R([AL]/10)->AL
				; [AL]+[AH]= Minute as BCD
	or	ax, '00'	; Convert to ASCII
	xchg	ah, al
	mov	[File_Minute], ax

	;mov	al, dh
	mov	al, bh
	shr	al, 3		; shift right 3 times
	aam			; [AL]+[AH]= Hours as BCD
	or	ax, '00'
	xchg	ah, al
	mov     [File_Hour], ax

	shr	ebx, 16		; BX = Date
	
Dir_Date_start:
	mov	ax, bx		; Date
	and	ax, 00011111b	; Day Mask
	aam			; Q([AL]/10)->AH
				; R([AL]/10)->AL
				; [AL]+[AH]= Day as BCD
	or	ax, '00'	; Convert to ASCII
	xchg	al, ah

	mov	[File_Day], ax

	mov	ax, bx
	shr	ax, 5		; shift right 5 times
	and	ax, 00001111b	; Month Mask
	aam
	or	ax, '00'
	xchg	ah, al
	mov	[File_Month], ax

	mov	ax, bx
	shr     ax, 9
	and	ax, 01111111b	; Result = Year - 1980
	add	ax, 1980

	mov	cl, 10
	div	cl		; Q -> AL, R -> AH 
	or	ah, '0'
	mov	[File_Year+3], ah
	aam
	xchg	ah, al
	or	ah, '0'	  ; Convert to ASCII
	mov	[File_Year+2], ah
	aam
	xchg	al, ah
	or	ax, '00'
	mov	[File_Year], ax

loc_show_line:
	push	esi
	mov     esi, File_Name
	call	print_msg
	mov	esi, nextline
	call	print_msg
	pop	esi

	dec	byte [PrintDir_RowCounter]
        jz      pause_dir_scroll

loc_next_entry:
	call	find_next_file
        jnc     loc_dfname_use_this

loc_dir_ok:
	mov     ecx, 10
	mov	ax, [Dir_Count]
	mov	edi, Decimal_Dir_Count
	cmp	ax, cx ; 10
	jb	short pass_ddc
	inc	edi
	cmp	ax, 100
	jb	short pass_ddc
	inc	edi
	cmp	ax, 1000
	jb	short pass_ddc
	inc	edi
	cmp	ax, 10000
	jb	short pass_ddc
	inc	edi
pass_ddc:
	mov     [edi+1], ch ; 0
loc_ddc_rediv:
	xor     edx, edx
	div     cx	; 10
	add     dl, '0'
	mov     [edi], dl
	dec     edi
	or	ax, ax
	jnz	short loc_ddc_rediv

	mov     ax, [File_Count]
	mov     edi, Decimal_File_Count
	cmp     ax, cx ; 10
	jb      short pass_dfc
	inc     edi
	cmp     ax, 100
	jb      short pass_dfc
	inc     edi
	cmp     ax, 1000
	jb      short pass_dfc
	inc     edi
	cmp     ax, 10000
	jb      short pass_dfc
	inc     edi
pass_dfc:
	;mov    cx, 10
	mov     [edi+1], ch ; 00
loc_dfc_rediv:
	;xor	dx, dx
	xor	dl, dl
	div	cx
	add	dl, '0'
	mov	[edi], dl
	dec	edi
	or	ax, ax
	jnz	short loc_dfc_rediv

	mov     edi, TFS_Dec_End
        ;mov    byte [edi], 0
	mov     eax, [Total_FSize]
	;mov    ecx, 10
rediv_tfs_hex:
	;sub	edx, edx
	sub	dl, dl
	div	ecx
	add	dl, '0'
	dec     edi
	mov     [edi], dl
	and	eax, eax
	jnz	short rediv_tfs_hex
	
	mov	[TFS_Dec_Begin], edi
	mov	esi, Decimal_File_Count_Header
	call	print_msg
	mov	esi, str_files
	call	print_msg
	mov	esi, str_dirs
	call	print_msg
	mov	esi, [TFS_Dec_Begin]
	call	print_msg
	mov	esi, str_bytes
	call	print_msg

	retn

pause_dir_scroll:
	sub	ah, ah           
	call	int16h
	cmp	al, 1Bh
        je      loc_dir_ok
	mov	byte [PrintDir_RowCounter], 16 ; Reset counter
        jmp     loc_next_entry

find_first_file:
	; 11/02/2015
	; 10/02/2016
	; 08/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 09/10/2011
	; 17/09/2009
	; 2005
	; INPUT ->
	;	ESI = ASCIIZ File/Dir/Path Name Address
	;	AL = Attributes AND mask (The AND result must be equal to AL)
	;	      bit 0 = Read Only
	;	      bir 1 = Hidden
	;	      bit 2 = System
	;	      bit 3 = Volume Label
	;	      bit 4 = Directory
	;	      bit 5 = Archive
	;	      bit 6 = Reserved, must be 0
	;	      bit 7 = Reserved, must be 0
	;       AH = Attributes Negative AND mask (The AND result must be ZERO)
	;
	; OUTPUT ->
	;	CF = 1 -> Error, Error Code in EAX (AL)
	;	CF = 0 ->
	;	     ESI = Directory Entry (FindFile_DirEntry) Location
	;	     EDI = Directory Buffer Directory Entry Location
	;	     EAX = File Size
	;	      BL = Attributes of The File/Directory
	;	      BH = Long Name Yes/No Status (>0 is YES)
	;             DX > 0 : Ambiguous filename chars are used
	;
	; (EAX, EBX, ECX, EDX, ESI, EDI will be changed)

	mov	[FindFile_AttributesMask], ax
	mov	edi, FindFile_DirEntry ; TR-DOS Fullfilename formatted buffer
	xor	eax, eax
	mov	ecx, 11
	rep	stosd	; 44 bytes
	;stosw		; +2 bytes 
	    
	mov	edi, FindFile_Name ; FFF structure, offset 66
	cmp	esi, edi
	je	short loc_fff_mfn_ok
	mov	edx, edi 
	 ; move 13 bytes
	movsd
	movsd
	movsd
	stosb
	mov	esi, edx
loc_fff_mfn_ok:
	mov	edi, Dir_Entry_Name ; Dir Entry Format File Name
	call	convert_file_name
	mov	esi, edi ; offset Dir_Entry_Name

	mov	ax, [FindFile_AttributesMask]
	;xor	ecx, ecx
	xor	cl, cl  
	call	locate_current_dir_file
	jc	short loc_fff_retn
	; EDI = Directory Entry
	; EBX = Directory Buffer Entry Index/Number

loc_fff_fnf_ln_check:
	xor	ch, ch 
	xor	dh, 0Fh
	jz	short loc_fff_longname_yes
	mov	[FindFile_LongNameYes], ch ; 0
	jmp	short loc_fff_longname_no

loc_fff_longname_yes:
	;inc	byte [FindFile_LongNameYes]
	mov	cl, [LFN_EntryLength]  
	mov	[FindFile_LongNameEntryLength], cl ; FindFile_LongNameYes

loc_fff_longname_no:
	;mov	bx, [DirBuff_CurrentEntry]
	mov	[FindFile_DirEntryNumber], bx
	mov	dx, ax ; Ambigouos Filename chars used sign > 0

	mov	al, [Current_Drv]
	mov	[FindFile_Drv], al 

	mov	eax, [Current_Dir_FCluster]
	mov	[FindFile_DirFirstCluster], eax

	mov	eax, [DirBuff_Cluster]
	mov	[FindFile_DirCluster], eax

	inc	word [FindFile_MatchCounter]

	mov	ebx, edi
	mov	esi, edi
	mov	edi, FindFile_DirEntry
	mov	eax, edi
	mov	cl, 8
	rep	movsd
	mov	esi, eax
	mov	edi, ebx

	mov	eax, [FindFile_DirEntry+28] ; File Size

	mov	bl, [FindFile_DirEntry+11] ; File Attributes 
	mov	bh, [FindFile_LongNameYes]

	;mov	cx, [DirBuff_EntryCounter]
	;mov	[FindFile_DirEntryNumber], cx
	;mov	cx, [FindFile_DirEntryNumber]
	; ecx = 0

loc_fff_retn:
	retn

find_next_file:
	; 10/02/2016
	; 08/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 06/02/2011
	; 17/09/2009
	; 2005
	; INPUT ->
	;	NONE, Find First File Parameters
	; OUTPUT ->
	;	CF = 1 -> Error, Error Code in EAX (AL)
	;	CF = 0 -> 
	;	    ESI = Directory Entry (FindFile_DirEntry) Location
	;	    EDI = Directory Buffer Directory Entry Location
	;	    EAX = File Size
	;	      BL = Attributes of The File/Directory
	;	      BH = Long Name Yes/No Status (>0 is YES)
	;             DX > 0 : Ambiguous filename chars are used 
	;
	; (EAX, EBX, ECX, EDX, ESI, EDI will be changed)

	cmp	word [FindFile_MatchCounter], 0
	ja	short loc_start_search_next_file

loc_fnf_stc_retn:
	stc
loc_fnf_ax12h_retn:
	mov	eax, 12h ; 18, No More files
;loc_fnf_retn:
	retn

loc_start_search_next_file:
	mov	bx, [FindFile_DirEntryNumber]
	inc	bx
	cmp	bx, [DirBuff_LastEntry]
	ja	short loc_cont_search_next_file

loc_fnf_search:
	mov	esi, Dir_Entry_Name
	mov	ax, [FindFile_AttributesMask]
	xor	cx, cx
	call	find_directory_entry
        jnc     loc_fff_fnf_ln_check

loc_cont_search_next_file:
	xor	ebx, ebx
	mov	bh, [Current_Drv]
	mov	esi, Logical_DOSDisks
	add	esi, ebx

	cmp	byte [Current_Dir_Level], 0
	jna	short loc_fnf_check_FAT_type
	cmp	byte [esi+LD_FATType], 1
	jb	short loc_fnf_ax12h_retn
	jmp	short loc_fnf_check_next_cluster
 
loc_fnf_check_FAT_type:
	cmp	byte [esi+LD_FATType], 3
	jb	short loc_fnf_ax12h_retn

loc_fnf_check_next_cluster:
	mov	eax, [DirBuff_Cluster]
	call	get_next_cluster
	jnc	short loc_fnf_load_next_dir_cluster
	or	eax, eax
	jz	short loc_fnf_stc_retn
	;mov	eax, 15h ;Drive not ready or read error
 	cmc	;stc
loc_fnf_retn:
	retn

loc_fnf_load_next_dir_cluster:
	call	load_FAT_sub_directory
	jc	short loc_fnf_retn
	xor	bx, bx
	mov	[FindFile_DirEntryNumber], bx
	jmp	short loc_fnf_search

get_and_print_longname:
	; 13/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 24/01/2010
	; 17/10/2009 (CMD_INTR.ASM, 'cmp_cmd_longname')
get_longname_fchar:
	cmp	byte [esi], 20h
	ja	short loc_find_longname
	;jb	short loc_longname_retn
	;inc	esi
	;je	short get_longname_fchar
;loc_longname_retn:
	retn
loc_find_longname:
	call	find_longname
	jnc	short loc_print_longname
               
	or	al, al
	jz	short loc_longname_not_found
	  
	cmp	al, 15h
	je	cd_drive_not_ready

loc_ln_file_dir_not_found:
	mov	esi, Msg_File_Directory_Not_Found
	;call	print_msg	
        ;retn
	jmp	print_msg

loc_longname_not_found:
        mov     esi, Msg_LongName_Not_Found
	;call	print_msg	
        ;retn
	jmp	print_msg

loc_print_longname:
	;mov	esi, LongFileName
	mov	edi, TextBuffer
	push	edi 
	cmp	al, 0
	ja	short loc_print_longname_1
loc_print_FS_longname: ; Singlix FS (64 byte ASCIIZ file name)
	lodsb
	stosb  
	or	al, al
	jnz	short loc_print_FS_longname
	jmp	short loc_print_longname_2
	;
loc_print_longname_1: ; MS Windows long name (UNICODE chars)
	lodsw
	stosb  
	or	al, al
	jnz	short loc_print_longname_1
	;
loc_print_longname_2:               
	pop	esi
	call	print_msg
  	mov	esi, nextline
	;call	print_msg
	;retn
	jmp	print_msg	

show_file:
	; 18/02/2016
	; 17/02/2016
	; 15/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 13/09/2011 (CMD_INTR.ASM, 'cmp_cmd_show')
	; 08/11/2009

loc_show_parse_path_name:
	mov	edi, FindFile_Drv
	call	parse_path_name
	jc	loc_cmd_failed

loc_show_check_filename_exists:
	mov	esi, FindFile_Name
	cmp	byte [esi], 20h
	jna	loc_cmd_failed

	; 15/02/2016 (invalid file name check)
	call	check_filename 	
	jnc	short loc_show_change_drv

	mov	esi, Msg_invalid_name_chars
	jmp	print_msg
   
loc_show_change_drv:
	mov	dh, [Current_Drv]
	mov	[RUN_CDRV], dh
	mov	dl, [FindFile_Drv]
	cmp	dl, dh
	je	short loc_show_change_directory
	call	change_current_drive
	;jc	loc_file_rw_cmd_failed
	jc	loc_run_cmd_failed

loc_show_change_directory:
	cmp	byte [FindFile_Directory], 20h
	jna	short loc_findload_showfile

	inc	byte [Restore_CDIR]
	mov	esi, FindFile_Directory
	xor	ah, ah ; CD_COMMAND sign -> 0 
	call	change_current_directory
	;jc	loc_file_rw_cmd_failed
	jc	loc_run_cmd_failed

;loc_show_change_prompt_dir_string:
	;call	change_prompt_dir_string

loc_findload_showfile:
	; 15/02/2016
	mov	esi, FindFile_Name
	mov	edi, Dir_Entry_Name ; Dir Entry Format File Name
	call	convert_file_name
	mov	esi, edi ; offset Dir_Entry_Name

	sub	al, al	; Attrib AND mask = 0
	; Directory attribute : 10h
	; Volume name attribute: 8h
	mov	ah, 00011000b ; 18h (Attrib NAND, AND --> zero mask)
	;
	xor	cx, cx  
	call	locate_current_dir_file
	;jc	loc_file_rw_cmd_failed
	jc	loc_run_cmd_failed

loc_show_load_file:
	; EDI = Directory Entry
	mov	ax, [edi+DirEntry_FstClusHI] ; First Cluster High Word
	shl	eax, 16
	mov	ax, [edi+DirEntry_FstClusLO] ; First Cluster Low Word
	mov	[Show_Cluster], eax
	mov	eax, [edi+DirEntry_FileSize] ; File Size
	and	eax, eax ; Empty file !
        jz      end_of_show_file 
	mov	[Show_FileSize], eax
	xor	eax, eax
	mov	[Show_FilePointer], eax ; 0
	mov	[Show_ClusterPointer], ax ; 0
	sub	ebx, ebx
	mov	bh, [Current_Drv]
	mov	esi, Logical_DOSDisks
	add	esi, ebx
	mov	[Show_LDDDT], esi ; Logical DOS Drv Description Table addr

	cmp	byte [esi+LD_FATType], 0	
	ja	short loc_show_calculate_cluster_size
	; Singlix FS
	; First Cluster Number is FDT number (in compatibility buffer)
	mov	edx, [Show_Cluster] ; Compatibility dir. buffer value (FDT)	
	mov	[Show_FDT], edx
	xor	eax, eax
	mov	[Show_Cluster], eax ; Sector index  = 0
				    ; (next time it will be 1)			
loc_show_calculate_cluster_size:
	mov	bx, [esi+LD_BPB+BPB_BytsPerSec] ; FAT 12-16-32 (512)
	; BX = 512 = [esi+LD_FS_BytesPerSec] ; Singlix FS	
	mov	al, [esi+LD_BPB+BPB_SecPerClust] ; FAT 12-16-32 (<= 128)
	; AL = 1 = [esi+LD_FS_Reserved2] ; SectPerClust for Singlix FS
	mul	ebx	

	;cmp	eax, 65536 ; non-compatible (very big) cluster size
	;ja	short end_of_show_file	
	mov	[Show_ClusterSize], ax

loc_start_show_file:
	mov	esi, nextline
	call	print_msg

	mov	eax, [Show_Cluster]
	mov	byte [Show_RowCount], 23

	; 17/02/2016
	mov	esi, [Show_LDDDT]

loc_show_next_cluster:
	; 15/02/2016
	mov	ebx, Cluster_Buffer ; 70000h (for current TRDOS 386 version)
	; ESI = Logical DOS drv description table address
	call	read_cluster
	;jc	loc_file_rw_cmd_failed
	jc	loc_run_cmd_failed

	xor 	ebx, ebx
loc_show_next_byte:
	cmp	byte [Show_RowCount], 0
	jne	short pass_show_wait_for_key
	xor	ah, ah
	call	int16h
	cmp	al, 1Bh
	jne	short pass_exit_show
end_of_show_file:
pass_show_file:
	mov	esi, nextline
	call	print_msg
	jmp	loc_file_rw_restore_retn

pass_exit_show:
	mov	byte [Show_RowCount], 20
pass_show_wait_for_key:
	add	ebx, Cluster_Buffer
	mov	al, [ebx]
	movzx	ebx, byte [ACTIVE_PAGE] ; [ptty]
	cmp	al, 0Dh
        jne     loc_show_check_tab_space
	dec	byte [Show_RowCount]
pass_show_dec_rowcount:
	mov	ah, 7 ; (light gray character color, black background)
	call	WRITE_TTY
loc_show_check_eof:
	inc	dword [Show_FilePointer]
	mov	eax, [Show_FilePointer]
	cmp	eax, [Show_FileSize]
	jnb	short end_of_show_file
	inc	word [Show_ClusterPointer]
	movzx	ebx, word [Show_ClusterPointer]

	; 17/02/2016
	; (sector boundary -9 bits- check, 512 = 0)
        test    bx, 1FFh ;  1 to 511
	jnz	short loc_show_next_byte

	; 16/02/2016
	mov	esi, [Show_LDDDT]
	;
	cmp	byte [esi+LD_FATType], 0
	ja	short loc_show_check_fat_cluster_size

	; Singlix FS
	; 1 sector, more... (cluster size = 1 sector)
	mov	eax, [Show_Cluster]
	inc	eax
	mov	[Show_Cluster], eax

	and	bx, bx ; 65536 -> 0
        jnz	loc_show_next_byte
	jmp     loc_show_next_cluster
	 
loc_show_check_fat_cluster_size:
	; 17/02/2016
	cmp	bx, [Show_ClusterSize] ; cluster size in bytes
        jb	loc_show_next_byte
	mov	word [Show_ClusterPointer], 0

	mov	eax, [Show_Cluster]
	;mov	esi, [Show_LDDDT]
loc_show_get_next_cluster:
	call	get_next_cluster
	;jc	loc_file_rw_cmd_failed
	jc	loc_run_cmd_failed
loc_show_update_ccluster:
	mov	[Show_Cluster], eax			
        jmp     loc_show_next_cluster

loc_show_check_tab_space:
	cmp	al, 09h
        jne     pass_show_dec_rowcount
loc_show_put_tab_space:
	;movzx	ebx, byte [ACTIVE_PAGE] ; [ptty]
	call	get_cpos
	; dl = cursor column
	and	dl, 7 ; 18/02/2016
loc_show_put_space_chars:
	;mov	al, 20h ; space
	;mov 	ah, 7	; color attribute
	mov	ax, 0720h ; 
	movzx	ebx, byte [ACTIVE_PAGE] ; [ptty]
	push	dx
	call	WRITE_TTY
	pop	dx
	; 18/02/2016
	cmp	dl, 7
	jnb	loc_show_check_eof
	inc	dl
	jmp	short loc_show_put_space_chars

check_filename:
	; 15/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 07/08/2010 (FILE.ASM, 'proc_check_filename')
	; 10/07/2010
	; Derived from 'proc_check_filename'
	; in the old TRDOS.ASM (09/02/2005).
	;
	; INPUT -> 
	;	ESI = Dot File Name Location
	; OUTPUT ->
	;	cf = 1 -> error code in AL
	;	     AL = 0Bh -> Invalid file name   
	;	cf = 0 -> valid file name
	; 
	;(EAX, ECX, EDI will be changed)

check_invalid_filename_chars:
	; 15/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 10/07/2010 (FILE.ASM, 'proc_check_invalid_filename_chars')
	; 10/02/2010
	; Derived from 'proc_check_invalid_filename_chars'
	; in the old TRDOS.ASM (09/02/2005).
	;
	; INPUT -> 
	;	ESI = ASCIIZ FileName
	; OUTPUT ->
	;	cf = 1 -> invalid
	;	cf = 0 -> valid
	; 
	;(EAX, ECX, ESI, EDI will be changed)
  
	push	esi

        mov     edi, invalid_fname_chars
	lodsb
check_filename_next_char:
	mov	ecx, sizeInvFnChars
	mov	edi, invalid_fname_chars
loc_scan_invalid_filename_char:
	scasb 
	je	short loc_invalid_filename_stc 
	loop	loc_scan_invalid_filename_char
	lodsb
	cmp	al, 1Fh  ; 20h and above 
	ja	short check_filename_next_char

check_filename_dot:
	mov	esi, [esp]

	mov	ah, 21h
	mov	ecx, 8
loc_check_filename_next_char:
	lodsb
	cmp	al, 2Eh
	jne	short pass_check_fn_dot_check
loc_check_filename_ext_0:
	lodsb
	cmp	al, ah ; 21h
	jb	short loc_invalid_filename
	cmp	al, 2Eh
	jne	short loc_check_filename_ext_1

loc_invalid_filename_stc:
loc_check_fn_stc_rtn:
	stc
loc_invalid_filename:
	mov	eax, 0Bh ; Invalid format
	; Invalid file name chars
loc_check_fn_rtn:
	pop	esi
	retn

pass_check_fn_dot_check:
	cmp	al, ah ; 21h
	jb	short loc_check_fn_clc_rtn
	loop	loc_check_filename_next_char
	lodsb
	cmp	al, ah ; 21h
	jb	short loc_check_fn_clc_rtn
	cmp	al, 2Eh
	jne	short loc_check_fn_stc_rtn
	jmp	short loc_check_filename_ext_0

loc_check_filename_ext_1:
	lodsb
	cmp	al, ah ; 21h
	jb	short loc_check_fn_clc_rtn
	cmp	al, 2Eh
	je	short loc_check_fn_stc_rtn
	lodsb
	cmp	al, ah ; 21h
	jb	short loc_check_fn_clc_rtn
	cmp	al, 2Eh
	je	short loc_check_fn_stc_rtn
	lodsb
	cmp	al, ah ; 21h
	jnb	short loc_check_fn_stc_rtn

loc_check_fn_clc_rtn:
	pop	esi
	clc
	retn

loc_print_deleted_message:
	mov	esi, Msg_Deleted
	call	print_msg

	;clc

loc_file_rw_restore_retn:
	; 15/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 28/02/2010 (CMD_INTR.ASM)
loc_file_rw_cmd_failed:
	pushf 
	call	restore_cdir_after_cmd_fail	
	popf
	jc	short loc_file_rw_check_write_fault
	retn

loc_permission_denied:
	; 27/02/2016
	mov	esi, Msg_Permission_Denied
	call	print_msg
	jmp	short loc_file_rw_restore_retn

loc_file_rw_check_write_fault:
	cmp	al, 1Dh ; Write Fault
        jne     loc_run_cmd_failed_cmp_al
	mov	esi, Msg_Not_Ready_Write_Err
	;call	print_msg
	;retn
	jmp	print_msg

make_directory:
	; 21/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 12/03/2011 (CMD_INTR.ASM, 'cmp_cmd_mkdir')
	; 14/08/2010
	; 10/07/2010
	; 29/11/2009
	;
get_mkdir_fchar:
	; esi = directory name
	cmp	byte [esi], 20h
	;je	short get_mkdir_fchar
        ;ja	short loc_mkdir_parse_path_name
        ja	short loc_mkdir_parse_path_name

loc_mkdir_nodirname_retn:
	retn

loc_mkdir_parse_path_name:
	mov	edi, FindFile_Drv
        call    parse_path_name
	jc	loc_cmd_failed

loc_mkdir_check_dirname_exists:
	mov	esi, FindFile_Name
	cmp	byte [esi], 20h
	jna	loc_cmd_failed
	mov	[DelFile_FNPointer], esi
	call	check_filename
	jc	short loc_mkdir_invalid_dir_name_chars

loc_mkdir_drv:
	mov	dh, [Current_Drv]
	mov	[RUN_CDRV], dh
	
	mov	dl, [FindFile_Drv]
	cmp	dl, dh
	je	short loc_mkdir_change_directory

	call	change_current_drive
	jc	loc_file_rw_cmd_failed

loc_mkdir_change_directory:
	cmp	byte [FindFile_Directory], 20h
	jna	short loc_mkdir_find_directory

	inc	byte [Restore_CDIR]
	mov	esi, FindFile_Directory
	xor	ah, ah ; CD_COMMAND sign -> 0 
	call	change_current_directory
	jc	short loc_mkdir_check_error_code

;loc_mkdir_change_prompt_dir_string:
	;call	change_prompt_dir_string

loc_mkdir_find_directory:
	;mov	esi, FindFile_Name
	mov	esi, [DelFile_FNPointer]
	;xor	eax, eax
	xor	ax, ax ; any name (dir, file, volume)
	call	find_first_file
	jc	short loc_mkdir_check_error_code

loc_mkdir_directory_found:
	mov	esi, Msg_Name_Exists
	call	print_msg

        jmp     loc_file_rw_restore_retn

loc_mkdir_invalid_dir_name_chars:
	mov	esi, Msg_invalid_name_chars
	call	print_msg

        jmp     loc_file_rw_restore_retn

loc_mkdir_check_error_code:
	cmp	al, 2
	;je	short loc_mkdir_directory_not_found
	je	short loc_mkdir_ask_for_yes_no
	stc
        jmp     loc_file_rw_cmd_failed

loc_mkdir_directory_not_found:
loc_mkdir_ask_for_yes_no:
	mov	esi, Msg_DoYouWantMkdir
	call	print_msg
	mov	esi, [DelFile_FNPointer]
	call	print_msg
	mov	esi, Msg_YesNo
	call	print_msg

	mov	byte [Y_N_nextline], 20h

loc_mkdir_ask_again:
	xor	ah, ah
	call	int16h
	cmp	al, 1Bh
	;je	short loc_do_not_make_directory
	je	short loc_mkdir_y_n_escape
	and	al, 0DFh ; y -> Y, n -> N
	cmp	al, 'Y' ; 'yes'
	je	short loc_mkdir_yes_make_directory
	cmp	al, 'N' ; 'no'
	jne	short loc_mkdir_ask_again

loc_do_not_make_directory:
loc_mkdir_yes_make_directory:
	mov	[Y_N_nextline], al
	push	ax
	mov	esi, Y_N_nextline
	call	print_msg
	pop	ax
	;cmp	al, 'Y' ; 'yes'
	;cmc
        ;jnc	loc_file_rw_restore_retn
	cmp	al, 'N' ; 'no'
        je	loc_file_rw_restore_retn  

loc_mkdir_call_make_sub_directory:
	mov	esi, [DelFile_FNPointer]
	mov	cl, 10h ; Directory attributes 
	call	make_sub_directory
        jc	loc_file_rw_cmd_failed

	mov	esi, Msg_OK
	call	print_msg
	jmp	loc_file_rw_restore_retn

loc_mkdir_y_n_escape:
	mov	al, 'N' ; 'no'
	jmp	short loc_do_not_make_directory

delete_directory:
	; 29/02/2016
	; 28/02/2016
	; 27/02/2016
	; 26/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 16/10/2010 (CMD_INTR.ASM, 'cmp_cmd_rmdir')
	; 05/06/2010
	;
get_rmdir_fchar:
	; esi = directory name
	cmp	byte [esi], 20h
	;je	short get_rmdir_fchar
        ;ja	short loc_rmdir_parse_path_name
        ja	short loc_rmdir_parse_path_name

loc_rmdir_nodirname_retn:
	retn

loc_rmdir_parse_path_name:
	mov	edi, FindFile_Drv
	call	parse_path_name
	jc	loc_cmd_failed

loc_rmdir_check_dirname_exists:
	mov	esi, FindFile_Name
	cmp	byte [esi], 20h
	jna	loc_cmd_failed
	mov	[DelFile_FNPointer], esi 

loc_rmdir_drv:
	mov	dh, [Current_Drv]
	mov	[RUN_CDRV], dh

	mov	dl, [FindFile_Drv]
	cmp	dl, dh
	je	short loc_rmdir_change_directory

	call	change_current_drive
	jc	loc_file_rw_cmd_failed

loc_rmdir_change_directory:
	cmp	byte [FindFile_Directory], 20h
	jna	short loc_rmdir_find_directory

	inc	byte [Restore_CDIR]
	mov	esi, FindFile_Directory
	xor	ah, ah ; CD_COMMAND sign -> 0 
	call	change_current_directory
	jc	short loc_rmdir_check_error_code

;loc_rmdir_change_prompt_dir_string:
	;call	change_prompt_dir_string

loc_rmdir_find_directory:
	;mov	esi, FindFile_Name
	mov	esi, [DelFile_FNPointer]
	mov	ax, 0810h ; Only directories
	call	find_first_file
	jnc	short loc_rmdir_ambgfn_check

loc_rmdir_check_error_code:
	cmp	al, 2
	je	short loc_rmdir_directory_not_found
	stc
	jmp	loc_file_rw_cmd_failed

loc_rmdir_ambgfn_check:
	and	dx, dx ; Ambiguous filename chars used sign (DX>0)
	jz	short loc_rmdir_directory_found

loc_rmdir_directory_not_found:
	mov	esi, Msg_Dir_Not_Found
	call	print_msg

	jmp	loc_file_rw_restore_retn

loc_rmdir_directory_found:
	and	bl, 07h ; Attributes
	jnz	loc_permission_denied

loc_rmdir_save_lnel: ; 28/02/2016
       ;mov	bh, [LongName_EntryLength]
	mov	[DelFile_LNEL], bh ; Long name entry length (if > 0)
	; edi = Directory Entry Offset (DirBuff)
	; esi = Directory Entry (FFF Structure)
	;mov	[DelFile_DirEntryAddr], edi ; not required
	;mov	ax, [edi+20] ; First Cluster High Word
        ;shl	eax, 16
	;mov	ax, [edi+26] ; First Cluster Low Word
	; ROOT Dir First Cluster = 0
        ;cmp	eax, 2
	;jb	loc_update_direntry_1

pass_rmdir_fc_check:
	push	edi ; * (29/02/2016)

	mov	esi, Msg_DoYouWantRmDir
	call	print_msg
	mov	esi, [DelFile_FNPointer]
	call	print_msg
	mov	esi, Msg_YesNo
	call	print_msg

loc_rmdir_ask_again:
	xor	ah, ah
	call	int16h
	cmp	al, 0Bh
	je	short loc_do_not_delete_directory
	and	al, 0DFh
	mov	[Y_N_nextline], al
	cmp	al, 'Y'
	je	short loc_rmdir_yes_delete_directory
	cmp	al, 'N'
	jne	short loc_rmdir_ask_again

loc_do_not_delete_directory:
loc_rmdir_yes_delete_directory:
	mov	[Y_N_nextline], al
	push	ax
	mov	esi, Y_N_nextline
	call	print_msg
	pop	ax
	pop	edi ; * (29/02/2016)
	;cmp	al, 'Y' ; 'yes'
	;cmc
        ;jnc	loc_file_rw_restore_retn
	cmp	al, 'N' ; 'no'
        je	loc_file_rw_restore_retn  

loc_rmdir_delete_short_name_check_dir_empty:
	; EDI = Directory buffer entry offset/address 
	mov	ax, [edi+20] ; First Cluster High Word
        shl	eax, 16
	mov	ax, [edi+26] ; First Cluster Low Word

	mov 	[DelFile_FCluster], eax

	;mov	bx, [DirBuff_EntryCounter]
	mov	bx, [FindFile_DirEntryNumber] ; 27/02/2016
	mov	[DelFile_EntryCounter], bx

    	sub	ebx, ebx
	mov	bh, [FindFile_Drv]
	mov	esi, Logical_DOSDisks
	add	esi, ebx

	cmp	word [edi+DirEntry_NTRes], 01A1h
	je	short loc_rmdir_delete_fs_directory

	;cmp	byte [esi+LD_FATType], 1
	;jnb	short loc_rmdir_get__last_cluster_0
	;mov	eax, 0Bh ; Invalid Format
	;jmp	loc_file_rw_cmd_failed
  
;loc_rmdir_get_last_cluster_0:
	mov	edx, [DirBuff_Cluster]
	mov	[RmDir_ParentDirCluster], edx

	mov	[RmDir_DirEntryOffset], edi

loc_rmdir_get_last_cluster:
	call	get_last_cluster
	jc	loc_file_rw_cmd_failed
	
	cmp	eax, [DelFile_FCluster]
	jne	short loc_rmdir_multi_dir_clusters

	mov	byte [RmDir_MultiClusters], 0
	jmp	short pass_rmdir_multi_dir_clusters

loc_rmdir_delete_fs_directory:
	cmp	byte [esi+LD_FSType], 0A1h
	jne	loc_permission_denied

	call	delete_fs_directory
	jnc	loc_print_deleted_message

	or	eax, eax
	jz	loc_rmdir_directory_not_empty_2         
	stc
	jmp	loc_file_rw_cmd_failed
 
loc_rmdir_multi_dir_clusters:
	mov	byte [RmDir_MultiClusters], 1

pass_rmdir_multi_dir_clusters:
	mov 	[RmDir_DirLastCluster], eax
	mov	[RmDir_PreviousCluster], ecx

loc_rmdir_load_fat_sub_directory:
	call	load_FAT_sub_directory
	jc	loc_file_rw_cmd_failed

loc_rmdir_find_last_dir_entry:
	push	esi
	mov	esi, Dir_File_Name
	mov	byte [esi], '*'
	mov	byte [esi+8], '*'
	xor	ebx, ebx ; Entry offset  = 0
loc_rmdir_find_last_dir_entry_next:
	mov	ax, 0800h ; Except volume/long names
	xor	cx, cx ; 0 = Find a valid file or dir name
	call	find_directory_entry
	jc	short loc_rmdir_empty_dir_cluster
	cmp	ebx, 1
	ja	short loc_rmdir_directory_not_empty_1
loc_rmdir_dot_entry_check:
	cmp	ch, '.' ; The first char of the dir entry
	jne	short loc_rmdir_directory_not_empty_1
	or	bl, bl
	jnz	short loc_rmdir_dotdot_entry_check
	cmp	byte [edi+1], 20h
	jmp	short pass_rmdir_dot_entry_check

loc_rmdir_dotdot_entry_check:
	cmp	word [edi+1], '. '
pass_rmdir_dot_entry_check:	
	jne	short loc_rmdir_directory_not_empty_1 
	inc	bl
	jmp	short loc_rmdir_find_last_dir_entry_next  

loc_rmdir_directory_not_empty_1:
	pop	eax ; pushed esi 
loc_rmdir_directory_not_empty_2:
	mov	esi, Msg_Dir_Not_Empty
	call	print_msg
	jmp	loc_file_rw_restore_retn

loc_rmdir_empty_dir_cluster:
	pop	esi

loc_rmdir_set_prev_cluster_dir_last_cluster:
	cmp	byte [RmDir_MultiClusters], 0
	jna	short loc_rmdir_unlink_dir_last_cluster

	mov	eax, [RmDir_PreviousCluster]
	xor	ecx, ecx
	dec	ecx ; FFFFFFFFh
	call	update_cluster
	jnc	short loc_rmdir_unlink_dir_last_cluster

loc_rmdir_unlink_stc_retn:
	cmp	eax, 1  ; eax = 0 -> end of cluster chain
	cmc 
	jc	loc_file_rw_cmd_failed
	jmp	short loc_rmdir_save_fat_buffer 

loc_rmdir_unlink_stc_retn_0Bh:
	mov	eax, 0Bh ; Invalid format
	stc
	jmp	loc_file_rw_cmd_failed
 
loc_rmdir_unlink_dir_last_cluster:
	mov	eax, [RmDir_DirLastCluster]
	xor	ecx, ecx ; 0
	call	update_cluster
	jnc	short loc_rmdir_unlink_stc_retn_0Bh 
	; Because of it is the last cluster
	; 'update_cluster' must return with eocc error 
	or	eax, eax
	jz	short loc_rmdir_save_fat_buffer ; eocc	
	stc
	jmp	loc_file_rw_cmd_failed
	 
loc_rmdir_save_fat_buffer:
	cmp	byte [FAT_BuffValidData], 2
	jne	short loc_rmdir_calculate_FAT_freespace
	call	save_fat_buffer
	jc	loc_file_rw_cmd_failed

loc_rmdir_calculate_FAT_freespace:
	mov	byte [CFS_error], 0
loc_rmdir_calculate_FAT_freespace_next:
	xor	eax, eax
	inc	al ; 1
	mov	bx, ax ; 1
	; BL = 1 -> Add EAX to free space count
	dec	bh 
	; BH = FFh ->
	; ESI = Logical DOS Drive Desc. Tbl. addr.
	call	calculate_fat_freespace

	and	ecx, ecx ; ecx = 0 -> valid free sector count
	jz 	short pass_rmdir_recalculate_FAT_freespace

	cmp	byte [CFS_error], 0
	ja	short loc_rmdir_calculate_FAT_freespace_err

loc_rmdir_recalculate_FAT_freespace:
	inc	byte [CFS_error]
	mov	bx, 0FF00h
	call	calculate_fat_freespace
	and	ecx, ecx
	jz	short loc_rmdir_calculate_FAT_freespace_next
              
loc_rmdir_calculate_FAT_freespace_err:
pass_rmdir_recalculate_FAT_freespace:

	cmp	byte [RmDir_MultiClusters], 0
	jna	short loc_rmdir_delete_short_name_continue

	mov	eax, [DelFile_FCluster]
        jmp     loc_rmdir_get_last_cluster

loc_rmdir_delete_short_name_invalid_data:
	mov	eax, 0Dh ; Invalid data
	stc
	jc	loc_file_rw_cmd_failed
	          
loc_rmdir_delete_short_name_continue:
	mov	eax, [RmDir_ParentDirCluster]
	cmp	eax, 2
	jnb	short loc_rmdir_del_short_name_load_sub_dir
	call	load_FAT_root_directory
	jc	loc_file_rw_cmd_failed
	jmp	short loc_rmdir_del_short_name_ld_chk_fclust

loc_rmdir_del_short_name_load_sub_dir:	
	call	load_FAT_sub_directory
	jc	loc_file_rw_cmd_failed

loc_rmdir_del_short_name_ld_chk_fclust:
	movzx	edi, word [RmDir_DirEntryOffset]
	add	edi, Directory_Buffer

	mov	ax, [edi+20] ; First Cluster High Word
	shl	eax, 16
	mov	ax, [edi+26] ; First Cluster Low Word
        ; Not necessary... 
	cmp	eax, [DelFile_FCluster]
	jne	short loc_rmdir_delete_short_name_invalid_data
	;
	mov	byte [edi], 0E5h ; 'Deleted' sign
	; 27/02/2016
	; TRDOS v1 has a bug here! it does not set
	; 'DirBuff_ValidData' to 2; as result of this bug,
	; 'save_directory_buffer' would not save the change ! 
  	mov	byte [DirBuff_ValidData], 2 ; change sign
	;
	call	save_directory_buffer
	jc	loc_file_rw_cmd_failed 

loc_rmdir_del_long_name:
	movzx	edx, byte [DelFile_LNEL]
	or	dl, dl
	jz	short loc_rmdir_update_parent_dir_lmdt
             
	movzx	eax, word [DelFile_EntryCounter]
	sub	eax, edx
	jc	loc_file_rw_cmd_failed
 
 	; EAX = Directory Entry Number of the long name last entry
	call	delete_longname
	;jc	short loc_file_rw_cmd_failed

loc_rmdir_update_parent_dir_lmdt:
	call	update_parent_dir_lmdt
	;jc	short loc_file_rw_cmd_failed

	mov	esi, Msg_OK
	call	print_msg
	jmp	loc_file_rw_restore_retn

delete_file:
	; 29/02/2016
	; 28/02/2016 (TRDOS 386 =  TRDOS v2.0)
	; 09/08/2010 (CMD_INTR.ASM, 'cmp_cmd_del')
	; 28/02/2010

get_delfile_fchar:
	; esi = file name
	cmp	byte [esi], 20h
	;je	short get_delfile_fchar
        ;ja	short loc_delfile_parse_path_name
        ja	short loc_delfile_parse_path_name

loc_delfile_nofilename_retn:
	retn

loc_delfile_parse_path_name:
	mov	edi, FindFile_Drv
	call	parse_path_name
	jc	loc_cmd_failed

loc_delfile_check_filename_exists:
	mov	esi, FindFile_Name
	cmp	byte [esi], 20h
	jna	loc_cmd_failed
	mov	[DelFile_FNPointer], esi 

loc_delfile_drv:
	mov	dl, [FindFile_Drv]
	mov	dh, [Current_Drv]
	mov	[RUN_CDRV], dh
	cmp	dl, dh
	je	short loc_delfile_change_directory

	call	change_current_drive
	jc	loc_file_rw_cmd_failed

loc_delfile_change_directory:
	cmp	byte [FindFile_Directory], 20h
	jna	short loc_delfile_find

	inc	byte [Restore_CDIR]
	mov	esi, FindFile_Directory
	xor	ah, ah ; CD_COMMAND sign -> 0 
	call	change_current_directory
	jc	loc_file_rw_cmd_failed

;loc_delfile_change_prompt_dir_string:
	;call	change_prompt_dir_string

loc_delfile_find:
	;mov	esi, FindFile_Name
	mov	esi, [DelFile_FNPointer]
	mov	ax, 1800h ; Except volume label and dirs
	call	find_first_file
	jc	loc_file_rw_cmd_failed

loc_delfile_ambgfn_check:
	and	dx, dx ; Ambiguous filename chars used sign (DX>0)
	jz	short loc_delfile_found

	mov	eax, 2 ; File not found sign
	stc
	jmp	loc_file_rw_cmd_failed   

loc_delfile_found:
	and	bl, 07h ; Attributes
        jnz     loc_permission_denied

;loc_delfile_found_save_lnel:
;	mov	[DelFile_LNEL], bh ; Long name entry length (if > 0)

loc_delfile_ask_for_delete:
	push	edi ; * (29/02/2016)

	mov	esi, Msg_DoYouWantDelete
	call	print_msg
	mov	esi, [DelFile_FNPointer]
	call	print_msg
	mov	esi, Msg_YesNo
	call	print_msg

loc_delfile_ask_again:
	xor	ah, ah
	call	int16h
	cmp	al, 0Bh
	je	short loc_do_not_delete_file
	and	al, 0DFh
	mov	[Y_N_nextline], al
	cmp	al, 'Y'
	je	short loc_yes_delete_file
	cmp	al, 'N'
	jne	short loc_delfile_ask_again

loc_do_not_delete_file:
loc_yes_delete_file:
	mov	[Y_N_nextline], al
	push	ax
	mov	esi, Y_N_nextline
	call	print_msg
	pop	ax
	pop	edi ; * (29/02/2016)
	;cmp	al, 'Y' ; 'yes'
	;cmc
        ;jnc	loc_file_rw_restore_retn
	cmp	al, 'N' ; 'no'
        je	loc_file_rw_restore_retn  

loc_delete_file:
	mov	bh, [FindFile_Drv]
	;mov	bl, [DelFile_LNEL]
	mov	bl, [FindFile_LongNameEntryLength]
	;mov	cx, [DirBuff_EntryCounter]
	mov	cx, [FindFile_DirEntryNumber]
	; (*) EDI = Directory buffer entry offset/address 
	call	remove_file ; (FILE.ASM, 'proc_delete_file')
	jnc	loc_print_deleted_message

	cmp	al, 05h
	je	loc_permission_denied
	stc
	jmp	loc_file_rw_cmd_failed

copy_file:
	retn
move_file:
	retn
set_get_path:
	retn
find_and_list_files:
	retn
set_get_env:
	retn
set_exec_arguments:
	retn
load_and_execute_file:
	retn
set_file_attributes:
	retn
rename_file:
	retn
delete_fs_directory:
	xor eax, eax
	retn