-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmyls.cl
203 lines (178 loc) · 5.64 KB
/
myls.cl
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
(headers
stdio
stdlib
"string"
pwd
grp
dirent
time
sys/types
sys/stat)
(syn true "True")
(syn false "False")
(define True 1)
(define False 0)
(define (macro !execinfop !x)
(.and !x (.or !s-ixusr !s-ixgrp !s-ixoth)))
(define (macro !chifelse !p !a !b)
(@putchar (? !p (paren !a) !b)))
(define (macro !buffer !x)
(? !x True False))
(define (macro !nop !x) !x)
(typedef (struct dirent) dirent)
(template incptr (x) (set x (addr (nth x 1))))
(func (pt parse-arg) char (
((pt argv 2) char)
((pt search 2) char)
(argp char)
((pt slot) int))
(var i int 0)
(while (ptr argv)
(var (pt start 2) char search)
(while (ptr start)
(if (not (@strcmp (pt argv) (pt start)))
(progn
(if argp
(return (nth argv 1)))
(if (lt (pt slot) i)
(set (pt slot) i))
(return (str !true))))
(incptr start))
(incptr argv)
(pinc i))
(return !null))
(const (arr (pt !classify) nil) char (arr-decl (str "-c") (str "-classify") !null))
(const (arr (pt !diskusage) nil) char (arr-decl (str "-d") (str "-disk-usage") !null))
(const (arr (pt !longlisting) nil) char (arr-decl (str "-l") (str "-long-listing") !null))
(const (arr (pt !followsymlinks) nil) char (arr-decl (str "-f") (str "-follow-symlinks") !null))
(const (arr (pt !humanreadable) nil) char (arr-decl (str "-h") (str "-human-readable") !null))
(const (arr (pt !recursive) nil) char (arr-decl (str "-r") (str "-recursive") !null))
(func atoi-if int (((pt c) char))
(if (not c)
(return -1))
(return (@atoi c)))
(define !bufsize 256)
(var (arr buffer !bufsize) char)
(func modeify void (x)
(var (arr ret 10) char (str "drwxrwxrwx"))
(var i int 9)
(if (not (call !s-isdir x))
(set (nth ret 0) (char "-")))
(while (ge i 1)
(if (same (mod x 2) 0)
(set (nth ret i) (char "-")))
(div-eq x 2)
(pdec i))
(for (set i 0) (lt i 10) (inc i)
(@putchar (nth ret i)))
(@putchar (char "\\t")))
(func printhumansize int (x)
(var size float (^x float))
(var (pt (arr prefixes nil)) char
(arr-decl (str !b) (str !kb) (str !mb) (str !tb) (str !pb)))
(var i int 0)
(while (gt size 1024)
(div-eq size 1024)
(pinc i))
(@printf (str "%.2g%s\\t") size (nth prefixes i)))
(func (pt stradd) char (((const (pt a) char) nil) ((const (pt b) char) nil))
(var (pt c) char (@malloc (add (@strlen a) (@strlen b) 1)))
(@memcpy c a (@strlen a))
(@memcpy (add c (@strlen a)) b (add (@strlen b) 1))
(return c))
(typedef (struct -ll (
((pt next) (struct -ll))
((pt data) void))) ll)
(func lsdir void (
((pt path) char)
(classify char)
(longlisting char)
(followsymlinks char)
(humanreadable char)
(recursive char))
(var (pt dir) !dir (@opendir path))
(if (not dir) (return))
(var dirstat (struct stat))
(var (pt dirinfo) dirent)
(var (pt nextlistings) ll)
(var (pt tmp) ll)
(while (set dirinfo (@readdir dir))
(@stat (slot dirinfo d-name) (addr dirstat))
(@printf (str "%-12s\\t") (slot dirinfo d-name))
(if recursive
(if (and (eq (slot dirinfo d-type) !dt-dir)
(or (eq (slot dirinfo d-type) !dt-lnk) followsymlinks))
(if (and
(@strcmp (slot dirinfo d-name) (str "."))
(@strcmp (slot dirinfo d-name) (str "..")))
(progn
(set tmp nextlistings)
(set nextlistings (@malloc (@sizeof ll)))
(set (slot nextlistings data)
(@stradd (@stradd path (str /)) (slot dirinfo d-name)))
(set (slot nextlistings next) tmp)))))
(if classify
(progn
(@!chifelse (eq (slot dirinfo d-type) !dt-dir)
(char /) (char " "))
(@!chifelse (@!execinfop (mem dirstat st-mode))
(char *) (char " "))
(@!chifelse (eq (slot dirinfo d-type) !dt-lnk)
(char @) (char " "))))
(if longlisting
(progn
(@printf (str %d\\t) (slot dirinfo d-ino))
(@modeify (mem dirstat st-mode))
(var (pt uid) (struct passwd) (@getpwuid (mem dirstat st-uid)))
(var (pt gid) (struct group) (@getgrgid (mem dirstat st-uid)))
(@printf (str %s\\t) (? uid (slot uid pw-name) (str root)))
(@printf (str %s\\t) (? gid (slot gid gr-name) (str root)))
(if humanreadable
(@printhumansize (mem dirstat st-size))
(@printf (str %d\\t) (mem dirstat st-size)))
(var (pt yearboundary) (struct tm))
(var rawtime time_t (@time 0))
(set yearboundary (@localtime (addr rawtime)))
(set (slot yearboundary tm-yday) 0)
(set (slot yearboundary tm-mon ) 0)
(set (slot yearboundary tm-mday) 0)
(set (slot yearboundary tm-sec ) 0)
(set (slot yearboundary tm-min ) 0)
(set (slot yearboundary tm-hour) 0)
(if (lt (mem dirstat st-mtime) (@mktime yearboundary))
(@strftime
buffer
!bufsize
(str "%b %d %Y")
(@localtime (addr (mem dirstat st-mtime))))
(@strftime
buffer
!bufsize
(str "%b %d %H:%M")
(@localtime (addr (mem dirstat st-mtime)))))
(@printf (str "%s ") buffer)))
(@putchar (char \\n))
(while nextlistings
(@printf (str "\\n%s:\\n")
(cast (slot nextlistings data) (typ* char 2)))
(@lsdir
(cast (slot nextlistings data) (typ* char 2))
classify longlisting followsymlinks humanreadable recursive)
(set nextlistings (slot nextlistings next)))))
(main
(var pos int 0)
(var diskusage int -1)
(set diskusage
(@atoi-if
(@parse-arg argv (^!diskusage (typ* char 2)) true (addr pos))))
(templates buffers-set (x y)
(var x char (@!buffer (@parse-arg argv (cast y (typ* char 2)) false (addr pos)))))
(buffers-set
(classify !classify)
(longlisting !longlisting)
(followsymlinks !followsymlinks)
(humanreadable !humanreadable)
(recursive !recursive))
(var (pt path) char (? (eq pos (- argc 1)) (str ".") (nth argv (- argc 1))))
(@lsdir path classify longlisting followsymlinks humanreadable recursive)
(return 0))