forked from qrush/unix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.s
584 lines (566 loc) · 6.5 KB
/
ls.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/ ls -- list file or directory
.globl flush
.globl fopen
.globl getw
.globl getc
.globl putc
.globl ctime
.globl end
sys break; end+512.
mov $1,obuf
mov sp,r5
mov (r5)+,count
tst (r5)+
dec count
mov count,ocount
bgt loop
mov $dotp,r5
loop:
mov (r5)+,r4
cmpb (r4)+,$'-
bne 1f
dec ocount
3:
movb (r4)+,r0
beq eloop
cmp r0,$'l
bne 4f
inc longf
br 3b
4:
cmpb r0,$'t
bne 4f
mov $14.,sortoff
br 3b
4:
cmpb r0,$'a
bne 4f
inc allflg
br 3b
4:
cmpb r0,$'s
bne 4f
incb longf+1
br 3b
4:
cmpb r0,$'d
bne 3b
inc dirflg
br 3b
1:
dec r4
jsr pc,do
eloop:
dec count
bgt loop
tst dnp
bne 1f
mov $dotp,r5
br loop
1:
jsr r5,flush; obuf
sys exit
do:
clr tblocks
mov $end,r1
mov $filnam,r3
mov r4,dnp
clr isadir
tst dirflg
bne nondir
sys stat; dnp: 0; statb
bec 1f
jsr r5,questf; < nonexistent\n\0>; .even
rts pc
1:
bit $40000,statb+2 /test directory
beq nondir
inc isadir
mov r4,r0
jsr r5,fopen; dbuf
bcc 1f
jsr r5,questf; < unreadable\n\0>; .even
rts pc
1:
movb (r4)+,(r3)+
bne 1b
dec r3
cmpb -1(r3),$'/
beq 1f
movb $'/,(r3)+
1:
jsr r5,getw; dbuf
bcs pass2
mov $4,-(sp)
tst r0
bne 2f
3:
jsr r5,getw; dbuf
dec (sp)
bne 3b
tst (sp)+
br 1b
2:
mov r3,r2
2:
jsr r5,getw; dbuf
mov r0,(r1)+
movb r0,(r2)+
swab r0
movb r0,(r2)+
dec (sp)
bne 2b
tst (sp)+
clrb (r2)+
tst allflg
bne 2f
cmpb (r3),$'.
bne 2f
sub $8.,r1
br 1b
2:
jsr r5,gstat
br 1b
nondir:
mov r3,r2
1:
movb (r4)+,(r2)+
bne 1b
1:
cmp r2,r3
blos 1f
cmpb -(r2),$'/
bne 1b
inc r2
1:
mov $8.,-(sp)
1:
movb (r2)+,(r1)+
bne 2f
dec r2
2:
dec (sp)
bne 1b
jsr r5,gstat
tst (sp)+
pass2:
mov dbuf,r0
sys close
mov $end,r2
cmp r1,r2
bne 1f
rts pc
1:
mov r5,-(sp)
mov r1,-(sp)
add sortoff,r2
1:
mov r2,(r1)+
add $20.,r2
cmp r2,(sp)
blo 1b
mov (sp),r2
tst -(r1)
1:
mov r2,r3
2:
tst (r3)+
cmp r3,r1
bhi 2f
mov (r2),r4
mov (r3),r5
tst sortoff
beq 4f
cmp (r4)+,(r5)+
blo 3f
bhi 2b
cmp (r4)+,(r5)+
blo 3f
br 2b
4:
4:
cmpb (r4)+,(r5)+
bhi 3f
blo 2b
dec r0
br 4b
3:
mov (r2),-(sp)
mov (r3),(r2)
mov (sp)+,(r3)
br 2b
2:
tst (r2)+
cmp r2,r1
blo 1b
1:
mov (sp)+,r2
mov (sp)+,r5
pass3:
cmp ocount,$1
ble 1f
tst isadir
beq 2f
mov dnp,0f
jsr r5,pstring; 0:..
jsr r5,pstring; colon
1:
tst longf
beq 1f
jsr r5,pstring; totmes
mov tblocks,r0
jsr r5,decimal; 4
jsr r5,pstring; nl
2:
tstb longf
beq 1f
mov $passwd,r0
jsr r5,fopen; iobuf
bes 1f
mov $uidbuf,r3
3:
2:
jsr r5,getc; iobuf
bes 3f
movb r0,(r3)+
cmpb r0,$':
bne 2b
2:
jsr r5,getc; iobuf
cmpb r0,$':
bne 2b
2:
jsr r5,getc; iobuf
cmpb r0,$':
beq 2f
movb r0,(r3)+
br 2b
2:
movb $'\n,(r3)+
cmp r3,$euidbuf
bhis 3f
2:
jsr r5,getc; iobuf
cmpb r0,$'\n
bne 2b
br 3b
3:
mov r3,euids
sys close
1:
cmp r2,r1
bhi 1f
mov (r2)+,r3
sub sortoff,r3
jsr r5,pentry
mov $8.,-(sp)
2:
movb (r3)+,r0
beq 2f
jsr r5,putc; obuf
dec (sp)
bne 2b
2:
tst (sp)+
jsr r5,pstring; nl
br 1b
1:
cmp ocount,$1
ble 1f
tst isadir
beq 1f
jsr r5,pstring; nl
1:
rts pc
pentry:
mov r2,-(sp)
tstb longf
bne listl
tstb longf+1
bne 2f
mov (sp)+,r2
rts r5
2:
mov 12.(r3),r0
jsr r5,calcb
jsr r5,decimal; 3
jsr r5,pstring; space
mov (sp)+,r2
rts r5
pstring:
mov r5,-(sp)
mov (r5),r5
1:
movb (r5)+,r0
beq 1f
jsr r5,putc; obuf
br 1b
1:
mov (sp)+,r5
tst (r5)+
rts r5
questf:
mov r4,0f
jsr r5,pstring; 0:..
mov r5,0f
jsr r5,pstring; 0:..
1:
tstb (r5)+
bne 1b
inc r5
bic $1,r5
rts r5
listl:
mov 18.(r3),r0 / inode
jsr r5,decimal; 4
jsr r5,pstring; space
mov r3,r4
add $8.,r4 / get to flags
bit $10000,(r4) /large
beq 2f
jsr r5,mode; 'l
br 3f
2:
jsr r5,mode; 's
3:
bit $40000,(r4) /directory
beq 2f
jsr r5,mode; 'd
br 3f
2:
bit $40,(r4) /set uid
beq 2f
jsr r5,mode; 'u
br 3f
2:
bit $20,(r4) /executable
beq 2f
jsr r5,mode; 'x
br 3f
2:
jsr r5,mode; '-
3:
bit $10,(r4) /read owner
beq 2f
jsr r5,mode; 'r
br 3f
2:
jsr r5, mode; '-
3:
bit $4,(r4) /write owner
beq 2f
jsr r5,mode; 'w
br 3f
2:
jsr r5,mode; '-
3:
bit $2,(r4) /read non-owner
beq 2f
jsr r5,mode; 'r
br 3f
2:
jsr r5,mode; '-
3:
bit $1,(r4)+ /write non-owner
beq 2f
jsr r5,mode; 'w
br 3f
2:
jsr r5,mode; '-
3:
jsr r5,pstring; space
movb (r4)+,r0
jsr r5,decimal; 2
movb (r4)+,r2
jsr pc,puid
mov (r4)+,r0
jsr r5,decimal; 5
jsr r5,pstring; space
mov r1,-(sp)
mov (r4)+,r0
mov (r4)+,r1
sub $16.,sp
mov sp,r2
jsr pc,ctime
mov sp,r2
mov $15.,-(sp)
1:
movb (r2)+,r0
jsr r5,putc; obuf
dec (sp)
bne 1b
add $18.,sp
mov (sp)+,r1
jsr r5,pstring; space
mov (sp)+,r2
rts r5
puid:
mov r1,-(sp)
mov $uidbuf,r1
1:
cmp r1,euids
bhis 1f
mov r1,0f
2:
tstb (r1)+
beq 3f
cmpb -1(r1),$':
bne 2b
clrb -1(r1)
3:
clr -(sp)
3:
movb (r1)+,r0
sub $'0,r0
cmp r0,$9.
bhi 3f
mov r1,-(sp)
mov 2(sp),r1
mpy $10.,r1
add r0,r1
mov r1,2(sp)
mov (sp)+,r1
br 3b
3:
mov (sp)+,r0
cmp r0,r2
bne 1b
jsr r5,pstring; space
jsr r5,pstring; 0:..
mov 0b,r1
mov $6,-(sp)
3:
tstb (r1)+
beq 3f
dec (sp)
br 3b
3:
jsr r5,pstring; space
dec (sp)
bgt 3b
tst (sp)+
mov (sp)+,r1
rts pc
1:
jsr r5,pstring; space
mov r2,r0
jsr r5,decimal; 3
jsr r5,pstring; space3
mov (sp)+,r1
rts pc
mode:
mov (r5)+,r0
jsr r5,putc; obuf
rts r5
gstat:
mov r1,-(sp)
add $512.,r1
cmp r1,0f
blo 1f
mov r1,0f
sys break; 0: end+512.
1:
mov (sp)+,r1
tst longf
bne 2f
tst sortoff
beq 1f
2:
sys stat; filnam; statb
bec 2f
mov r4,-(sp)
mov $filnam,r4
jsr r5,questf; < unstatable\n\0>; .even
mov (sp)+,r4
add $12.,r1
rts r5
2:
mov $statb+2,r0
mov (r0)+,(r1)+ /flags
mov (r0)+,(r1)+ /nlinks, uid
mov r0,-(sp)
mov (r0),r0
jsr r5,calcb
add r0,tblocks
mov (sp)+,r0
mov (r0)+,(r1)+ /size
add $20.,r0 /dska, ctim
mov (r0)+,(r1)+ /mtim
mov (r0)+,(r1)+
mov statb,(r1)+ /inode
rts r5
1:
add $12.,r1
rts r5
decimal:
mov r1,-(sp)
mov r2,-(sp)
mov r3,-(sp)
mov $6,r2
mov $numbuf+6,r3
1:
mov r0,r1
clr r0
dvd $10.,r0
add $'0,r1
movb r1,-(r3)
sob r2,1b
1:
cmp r3,$numbuf+5
beq 1f
cmpb (r3),$'0
bne 1f
movb $' ,(r3)+
br 1b
1:
mov $numbuf+6,r1
sub (r5),r1
mov (r5)+,-(sp)
1:
movb (r1)+,r0
jsr r5,putc; obuf
dec (sp)
bne 1b
tst (sp)+
mov (sp)+,r3
mov (sp)+,r2
mov (sp)+,r1
rts r5
calcb:
add $511.,r0
clrb r0
swab r0
asr r0
cmp r0,$8
blo 1f
inc r0
1:
1:
rts r5
dotp: dot
euids: uidbuf
dot: <.\0>
nl: <\n\0>
totmes: <total \0>
space3: < >
space: < \0>
passwd: </etc/passwd\0>
colon: <:\n\0>
.even
.bss
count: .=.+2
ocount: .=.+2
longf: .=.+2
sortoff: .=.+2
allflg: .=.+2
dirflg: .=.+2
isadir: .=.+2
filnam: .=.+32.
statb: .=.+34.
dbuf: .=.+518.
obuf: .=.+518.
numbuf: .=.+6
tblocks: .=.+2
uidbuf: .=.+1024.
euidbuf:
iobuf: .=.+518.