-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloc.c
287 lines (231 loc) · 5.6 KB
/
loc.c
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
typedef char int8;
typedef short int16;
typedef int int32;
typedef long long int64;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef _Bool bool;
enum { false = 0, true = 1 };
extern int64 system_call(int64 rax, int64 rdi, int64 rsi, int64 rdx, int64 r10, int64 r8, int64 r9);
int64 string_length(char* str) {
char* p = str;
for (; *p; p++);
return p-str;
}
#define STDIN 0
#define STDOUT 1
#define STDERR 2
enum Linux_Open {
OPEN_READ_ONLY = 0x0,
OPEN_WRITE_ONLY = 0x1,
OPEN_READ_WRITE = 0x2,
OPEN_CREATE = 0x40, // With an E!
OPEN_EXCLUSIVE = 0x80, // Error if we tried creating a file and it already exists.
OPEN_NO_TTY = 0x100, // Prevent obtaining a terminal handle (e.g. Ctrl+c stuff)
OPEN_TRUNCATE = 0x200,
OPEN_APPEND = 0x400,
OPEN_NON_BLOCK = 0x800,
OPEN_DIRECTORY = 0x10000,
OPEN_AUTO_CLOSE = 0x80000, // @RemoveMe?
};
int32 file_open(char* path, int64 flags) {
return system_call(2, (int64)path, flags, 0, 0, 0, 0);
}
void file_close(int32 file) {
system_call(3, file, 0, 0, 0, 0, 0);
}
int64 file_read(int32 file, char* buffer, int64 length) {
return system_call(0, (int64)buffer, length, 0, 0, 0, 0);
}
void file_write(int32 file, char* p, uint64 length) {
system_call(1, file, (int64)p, length, 0, 0, 0);
}
void print_str(char* s) {
file_write(STDOUT, s, string_length(s));
}
typedef struct DirectoryEntry64 {
uint64 inode;
int64 offset;
uint16 length;
uint8 type;
char name[];
} DirectoryEntry64;
bool compare(char* p0, char* p1, uint64 length) {
for (uint64 i = 0; i < length; i++) {
if (p0[i] != p1[i])
return false;
}
return true;
}
char* find_extension(char* str, int64 len) {
for (char* p = str+len-2; p > str; p--) {
if (*p == '.')
return p;
}
return str+len;
}
int64 get_file_size(int32 file) {
struct Status {
uint64 dev;
uint64 ino;
uint64 nlink;
uint32 mode;
uint32 uid;
uint32 gid;
uint32 padding0;
uint64 rdev;
int64 size;
int64 block_size;
int64 blocks;
uint64 atime;
uint64 atime_nsec;
uint64 mtime;
uint64 mtime_nsec;
uint64 ctime;
uint64 ctime_nsec;
int64 padding1[3];
} status;
system_call(5, file, (int64)&status, 0, 0, 0, 0);
return status.size;
}
bool is_code_file(char* path, int64 path_length) {
#define EXT(s) { s, sizeof(s)-1 }
struct Extension {
char* s;
int64 length;
};
static const struct Extension extension_table[] = {
EXT("asm"),
EXT("s"),
EXT("c"),
EXT("C"),
EXT("cc"),
EXT("cpp"),
EXT("cxx"),
EXT("h"),
EXT("H"),
EXT("hh"),
EXT("hpp"),
EXT("hxx"),
EXT("cs"),
EXT("css"),
EXT("html"),
EXT("js"),
EXT("swift"),
EXT("java"),
EXT("py"),
EXT("go"),
EXT("yaml"),
EXT("sh"),
EXT("bash"),
EXT("vim"),
EXT("xml"),
EXT("fs"),
EXT("ts"),
EXT("lua"),
EXT("f"),
EXT("i"),
EXT("f90"),
EXT("i90"),
EXT("for"),
EXT("ftn"),
EXT("fpp"),
};
static const uint64 extension_table_length = sizeof(extension_table)/sizeof(*extension_table);
char* path_ext = find_extension(path, path_length)+1;
int64 path_ext_length = (path+path_length)-path_ext;
if (!path_ext_length)
return false;
for (const struct Extension* ext = extension_table; ext < extension_table+extension_table_length; ext++) {
if (path_ext_length != ext->length)
continue;
if (!compare(ext->s, path_ext, path_ext_length))
continue;
return true;
}
return false;
}
char* load_file_virtual(int32 file, uint64 size) {
return (char*)system_call(9, 0, (size+4095)&-4096, 1, 0x02, file, 0);
}
#define MAX_BASE10_DIGITS 20
void digitize(uint64 n, char buffer[MAX_BASE10_DIGITS], char** out_str, int64* out_count) {
int32 digit_count = 0;
do buffer[MAX_BASE10_DIGITS-1-digit_count++] = 48 + (n % 10); while(n /= 10);
*out_str = buffer+MAX_BASE10_DIGITS-digit_count;
*out_count = digit_count;
}
void print_int(uint64 n) {
char s[MAX_BASE10_DIGITS];
char* begin;
int64 digit_count;
digitize(n, s, &begin, &digit_count);
file_write(STDOUT, begin, digit_count);
}
uint64 count_loc(char* s, uint64 size) {
uint64 counter = 0;
for (uint64 i = 0; i < size; i++) {
char c = s[i];
if (c == '\n')
counter++;
}
return counter;
}
enum FileType {
FT_UNKNOWN = 0,
FT_FIFO = 1,
FT_CHAR_DEVICE = 2,
FT_DIRECTORY = 4,
FT_BLOCK = 5,
FT_REGULAR = 8,
FT_LINK = 10,
FT_SOCKET = 12,
};
int main(int argc, char* args[]) {
int32 fdir = file_open(".", OPEN_READ_ONLY | OPEN_DIRECTORY);
if (fdir == -1) {
print_str("ERROR: Couldn't open cwd.\n");
return 1;
}
#define BUFFER_SIZE 8192
char buffer[BUFFER_SIZE] = { 0 };
char* p;
uint64 total_loc = 0;
while (true) {
p = buffer;
int64 read = system_call(217, fdir, (int64)buffer, BUFFER_SIZE, 0, 0, 0);
if (!read)
break;
if (read == -1) {
print_str("ERROR: Couldn't read dirs.\n");
return 1;
}
while (p < buffer+read) {
DirectoryEntry64* entry = (DirectoryEntry64*)p;
p += entry->length;
int64 namelen = string_length(entry->name);
if (entry->type != FT_REGULAR)
continue;
if (!is_code_file(entry->name, namelen))
continue;
// print_str("file: '");
// print_str(entry->name);
// print_str("'");
int32 file = file_open(entry->name, OPEN_READ_ONLY);
int64 file_size = get_file_size(file);
char* file_data = load_file_virtual(file, file_size);
uint64 loc = count_loc(file_data, file_size);
// file_close(file);
total_loc += loc;
// print_str(", loc: ");
// print_int(loc);
// print_str("\n");
}
}
// print_str("Directory loc: ");
print_int(total_loc);
print_str("\n");
return 0;
}