Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [modules/] [basext/] [listdir.asm] - Blame information for rev 393

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 314 winston
;The MIT License
2
;
3
;Copyright (c) 2010 Dylan Smith
4
;
5
;Permission is hereby granted, free of charge, to any person obtaining a copy
6
;of this software and associated documentation files (the "Software"), to deal
7
;in the Software without restriction, including without limitation the rights
8
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
;copies of the Software, and to permit persons to whom the Software is
10
;furnished to do so, subject to the following conditions:
11
;
12
;The above copyright notice and this permission notice shall be included in
13
;all copies or substantial portions of the Software.
14
;
15
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
;THE SOFTWARE.
22 380 winston
.include        "stat.inc"
23
.include        "fcntl.inc"
24
.include        "spectranet.inc"
25
.include        "defs.inc"
26
.include        "zxrom.inc"
27
.include        "ctrlchars.inc"
28
.include        "sysvars.inc"
29
.text
30 314 winston
 
31
; Show the directory listing. HL = directory to open.
32 380 winston
.globl F_listdir
33
F_listdir:
34 314 winston
        call OPENDIR                    ; open the directory
35
        jp c, J_tbas_error
36
        ld (v_vfs_dirhandle), a         ; save the directory handle
37
        ld a, 2
38
        rst CALLBAS                     ; set channel to 2
39
        defw 0x1601
40 380 winston
.catloop1:
41 314 winston
        ld a, (v_vfs_dirhandle)         ; get the dir handle back
42
        ld de, INTERPWKSPC              ; location for result
43
        call READDIR                    ; read dir
44 380 winston
        jr c, .readdone1                 ; read is probably at EOF
45 314 winston
        call F_statentry                ; show some information about it
46
        ld hl, INTERPWKSPC
47
        call F_tbas_zxprint             ; print a C string to #2
48 393 winston
        ld a, ZXNEWLINE                 ; newline
49 314 winston
        rst CALLBAS
50
        defw 0x10
51 380 winston
        jr .catloop1
52
.readdone1:
53 314 winston
        push af                         ; save error code while
54
        ld a, (v_vfs_dirhandle)         ; we close the dir handle
55
        call CLOSEDIR
56
        pop hl                          ; pop into hl to not disturb flags
57
        jp c, J_tbas_error              ; report any error
58
        ld a, h                         ; get original error code
59
        cp EOF                          ; EOF is good
60
        jp nz, J_tbas_error             ; everything else is bad, report it
61
        jp EXIT_SUCCESS
62
 
63
; Expects the filename to be in INTERPWKSPC
64 380 winston
.globl F_statentry
65
F_statentry:
66 314 winston
        ld hl, INTERPWKSPC
67
        ld de, INTERPWKSPC+256          ; where to put the data
68
        call STAT
69 380 winston
        jr c, .staterr2
70 314 winston
        ld ix, INTERPWKSPC+256
71
        ld a, (ix+(STAT_MODE+1))        ; Check file mode MSB
72
        and S_IFDIR / 256               ; check directory flag
73 380 winston
        jr z, .isfile2
74 314 winston
        ld hl, STR_dir
75
        call F_tbas_zxprint
76 380 winston
        jr .continue2
77
.isfile2:
78 314 winston
        call F_showsize
79
        ld a, ' '
80
        rst CALLBAS
81
        defw 0x0010
82 380 winston
.continue2:
83 314 winston
        ret
84
 
85 380 winston
.staterr2:
86 314 winston
        ld hl, STR_staterr
87
        jp F_tbas_zxprint               ; print it and return.
88
 
89 380 winston
STR_staterr:    defb "  Err ",0
90
STR_dir:                defb "  Dir ",0
91 314 winston
 
92
;----------------------------------------------------------------------
93
; F_showsize
94
; Create a 4 digit decimal with the correct ending (b, k, M or G)
95
; TODO: This routine is particularly naive and hardly optimized
96
; for space (or anything else). (Low priority TODO).
97 380 winston
.globl F_showsize
98
F_showsize:
99 314 winston
        ld a, (ix+(STAT_SIZE+3))        ; MSB of size
100
        ld b, a
101
        and 0xC0                        ; >= 2^30
102 380 winston
        jr nz, .gigs3
103 314 winston
        cpl                             ; check lower half
104
        and b                           ; of 4th stat byte for megs
105 380 winston
        jr nz, .megs3
106 314 winston
        ld a, (ix+(STAT_SIZE+2))
107
        ld b, a
108
        and 0xF0                        ; >= 2^20
109 380 winston
        jr nz, .megs3
110 314 winston
        cpl                             ; check lower half of
111
        and b                           ; 3rd stat byte for kilos
112 380 winston
        jr nz, .kilos3
113 314 winston
        ld a, (ix+(STAT_SIZE+1))
114
        and 0xFC                        ; >= 2^10
115 380 winston
        jr nz, .kilos3
116 314 winston
        ld l, (ix+STAT_SIZE)            ; less than 1K
117
        ld h, (ix+(STAT_SIZE+1))
118
        call F_decimal
119
        ld a, 'b'
120
        rst CALLBAS
121
        defw 0x0010
122
        ret
123 380 winston
.kilos3:
124 314 winston
        ld l, (ix+(STAT_SIZE+1))        ; 1K to 1023K
125
        ld h, (ix+(STAT_SIZE+2))
126
        srl h
127
        rr l
128
        srl h
129
        rr l
130
        call F_decimal
131
        ld a, 'k'
132
        rst CALLBAS
133
        defw 0x0010
134
        ret
135 380 winston
.megs3:
136 314 winston
        ld l, (ix+(STAT_SIZE+2))        ; 1M to 1023M
137
        ld h, (ix+(STAT_SIZE+3))
138
        ld b, 4
139 380 winston
.megloop3:
140 314 winston
        srl h
141
        rr l
142 380 winston
        djnz .megloop3
143 314 winston
        call F_decimal
144
        ld a, 'M'
145
        rst CALLBAS
146
        defw 0x0010
147
        ret
148 380 winston
.gigs3:
149 314 winston
        ld l, (ix+(STAT_SIZE+4))        ; 1G to 4G
150
        ld h, 0
151
        ld b, 6
152 380 winston
.gigloop3:
153 314 winston
        srl l
154 380 winston
        djnz .gigloop3
155 314 winston
        call F_decimal
156
        ld a, 'G'
157
        rst CALLBAS
158
        defw 0x0010
159
        ret
160
 
161
;----------------------------------------------------------------------
162
; F_decimal
163 380 winston
; Modified version of http://baze.au3.com3/misc/z80bits.html3#5.13
164 314 winston
; by baze.
165
; HL = number to convert
166 380 winston
.globl F_decimal
167
F_decimal:
168 314 winston
        ld e, 0
169
        ld bc, -1000            ; maximum value passed will be 9999
170 380 winston
        call .num14
171 314 winston
        ld bc, -100
172 380 winston
        call .num14
173 314 winston
        ld c, -10
174 380 winston
        call .num14
175 314 winston
        ld c, b
176
        ld e, 1                 ; print a zero if it's the last digit
177 380 winston
.num14:
178 314 winston
        ld a, '0'-1
179 380 winston
.num24:
180 314 winston
        inc a
181
        add hl, bc
182 380 winston
        jr c, .num24
183 314 winston
        sbc hl, bc
184
 
185
        bit 0, e
186 380 winston
        jr nz, .zerocont4
187 314 winston
        cp '0'
188 380 winston
        jr nz, .nonz4
189 314 winston
        ld a, ' '
190 380 winston
        jr .zerocont4
191
.nonz4:
192 314 winston
        ld e, 1
193 380 winston
.zerocont4:
194 314 winston
        rst CALLBAS
195
        defw 0x0010
196
        ret
197