kvisc/ka/usr/cmd-dir.k

165 lines
2.8 KiB
Plaintext
Raw Normal View History

2019-07-10 17:17:45 +02:00
; The OS/K Team licenses this file to you under the MIT license.
; See the LICENSE file in the project root for more information.
2019-08-21 19:10:26 +02:00
#define N 11
2019-08-05 14:56:22 +02:00
2019-08-21 16:57:32 +02:00
.handle_DIR:
2019-09-07 16:45:03 +02:00
push ebp
mov ebp, esp
2019-07-01 13:16:17 +02:00
2019-09-07 16:45:03 +02:00
push nx0, nx1
push nx2, nx3
2019-08-05 14:56:22 +02:00
2019-09-07 16:45:03 +02:00
nul nx0 ; no. of files found
nul nx1 ; no. of directories found
nul nx2 ; total amount of bytes found
2019-07-01 13:16:17 +02:00
2019-08-03 17:41:44 +02:00
call print, .dirmsg
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_first:
2019-09-07 16:45:03 +02:00
mov eax, Sys.FindFirst
2019-09-03 09:06:58 +02:00
mov ax0, .dir_buf
2019-08-03 18:20:36 +02:00
mov ax1, FNAME_MAX
2019-07-10 12:26:15 +02:00
trap 0
2019-09-03 09:06:58 +02:00
jmp .dir_list
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_next:
2019-09-07 16:45:03 +02:00
mov eax, Sys.FindNext
2019-09-03 09:06:58 +02:00
mov ax0, .dir_buf
2019-08-03 18:20:36 +02:00
mov ax1, FNAME_MAX
2019-07-10 12:26:15 +02:00
trap 0
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_list:
jraxz .dir_end
2019-07-01 13:16:17 +02:00
2019-09-07 16:45:03 +02:00
mov nx3, ecx ; file size
add nx2, ecx
2019-07-01 13:16:17 +02:00
2019-08-05 14:56:22 +02:00
; directory?
2019-09-07 16:45:03 +02:00
bnz edx, .dir_is_dir
2019-08-05 14:56:22 +02:00
2019-08-05 15:55:46 +02:00
; found a file
2019-09-07 16:45:03 +02:00
inc nx0
2019-08-05 15:55:46 +02:00
2019-07-01 13:16:17 +02:00
; separate extension from file name
2019-09-07 16:45:03 +02:00
mov ecx, FNAME_MAX
mov esi, .dir_buf
mov edi, esi
scasb esi, '.'
2019-07-01 13:16:17 +02:00
2019-07-02 20:13:05 +02:00
; print file name
2019-09-07 16:45:03 +02:00
sub ax1, esi, edi
2019-08-21 16:57:32 +02:00
dec ax1
2019-09-07 16:45:03 +02:00
call nprint, edi
2019-07-01 13:16:17 +02:00
; calculate where to put extension
2019-09-07 16:45:03 +02:00
sub edi, esi, .dir_buf
dec edi
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_ext_pad:
2019-08-05 14:56:22 +02:00
; print at least N non-space characters before extension
2019-09-07 16:45:03 +02:00
blte N, edi, .dir_print_ext
2019-07-01 13:16:17 +02:00
prn ' '
2019-09-07 16:45:03 +02:00
inc edi
2019-09-03 09:06:58 +02:00
jmp .dir_ext_pad
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_print_ext:
2019-08-21 16:57:32 +02:00
prn ' '
2019-07-01 13:16:17 +02:00
; here we print at least 4 characters excluding '.'
2019-09-07 16:45:03 +02:00
mov ecx, 4
2019-07-01 13:16:17 +02:00
2019-09-07 16:45:03 +02:00
bne b[esi], '.', .dir_print_ext.1
inc esi
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_print_ext.1:
2019-09-07 16:45:03 +02:00
bzr b[esi], .dir_print_ext.2
2019-07-01 13:16:17 +02:00
2019-09-07 16:45:03 +02:00
; print and decrease ecx, unless it's already 0
prn b[esi]
inc esi
2019-09-03 09:06:58 +02:00
jrcxz .dir_print_ext.1
2019-07-01 13:16:17 +02:00
2019-09-07 16:45:03 +02:00
dec ecx
2019-09-03 09:06:58 +02:00
jmp .dir_print_ext.1
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_print_ext.2:
2019-07-01 13:16:17 +02:00
; did we print at least 4 bytes?
2019-09-03 09:06:58 +02:00
jrcxz .dir_print_bytes ; yes, carry on
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_pe2.l:
2019-08-14 09:52:39 +02:00
prn ' '
2019-09-03 09:06:58 +02:00
loop .dir_pe2.l
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_print_bytes:
2019-07-01 13:16:17 +02:00
; print file size in bytes
2019-08-14 09:52:39 +02:00
prn ' '
prn ' '
prn ' '
2019-07-01 13:16:17 +02:00
2019-09-07 16:45:03 +02:00
shr eax, nx3, 10
and nx3, 1023
2019-07-01 13:16:17 +02:00
2019-09-07 16:45:03 +02:00
push nx3, eax
2019-09-03 09:06:58 +02:00
call printf, .dir_bytesstr
2019-09-07 16:45:03 +02:00
add esp, 16
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_prepare_next:
2019-07-01 13:16:17 +02:00
; go find next entry
prn 10
2019-09-03 09:06:58 +02:00
jmp .dir_next
2019-07-01 13:16:17 +02:00
2019-09-03 09:06:58 +02:00
.dir_end:
2019-09-07 16:45:03 +02:00
shr eax, nx2, 10
shr edx, eax, 10
and eax, 1023
and nx2, 1023
2019-08-05 15:55:46 +02:00
2019-09-07 16:45:03 +02:00
push nx2, eax, edx
2019-09-03 09:06:58 +02:00
call printf, .dir_endstr0
2019-09-07 16:45:03 +02:00
add esp, 24
2019-08-05 15:55:46 +02:00
2019-09-07 16:45:03 +02:00
push nx1, nx0
2019-09-03 09:06:58 +02:00
call printf, .dir_endstr1
2019-09-07 16:45:03 +02:00
add esp, 16
2019-07-01 13:16:17 +02:00
2019-09-07 16:45:03 +02:00
pop nx3, nx2
pop nx1, nx0
2019-07-22 13:18:13 +02:00
leave
2019-08-21 16:57:32 +02:00
jmp .print_prompt
2019-07-01 13:16:17 +02:00
2019-08-05 14:56:22 +02:00
; special case: direcory
2019-09-03 09:06:58 +02:00
.dir_is_dir:
2019-09-07 16:45:03 +02:00
inc nx1
2019-08-05 15:55:46 +02:00
2019-08-14 09:52:39 +02:00
; use printf instead of print
2019-08-21 19:10:26 +02:00
; because it returns the # of
; printed characters
2019-09-03 09:06:58 +02:00
call printf, .dir_buf
2019-08-14 09:52:39 +02:00
2019-09-07 16:45:03 +02:00
blte N, eax, .dir_no_pad
sub ecx, N, eax
dec ecx
2019-08-05 14:56:22 +02:00
2019-08-14 09:52:39 +02:00
.dir.l:
prn ' '
loop .dir.l
2019-08-05 14:56:22 +02:00
2019-08-14 09:52:39 +02:00
.dir_no_pad:
2019-08-05 14:56:22 +02:00
call print, .dir_ext
2019-09-03 09:06:58 +02:00
jmp .dir_print_bytes
2019-08-05 14:56:22 +02:00
2019-09-03 09:06:58 +02:00
.dir_buf = [FNAME_MAX]
2019-08-14 09:52:39 +02:00
.dir_ext = " <DIR> "
2019-09-03 09:06:58 +02:00
.dir_endstr0 = " total %dMB + %dKB + %dB\n"
.dir_endstr1 = " found %d file(s), %d dir(s)\n"
2019-08-05 14:56:22 +02:00
.dirmsg = "Directory of C:\\\n\n"
2019-08-05 15:55:46 +02:00
2019-09-03 09:06:58 +02:00
.dir_bytesstr = "%d kilobytes + %d bytes"
; .dir_bytesstr = "%dMB + %dKB + %dB" # too soon
2019-07-01 14:04:32 +02:00
2019-08-21 19:10:26 +02:00
#undef N