Skip to content

Commit 48c3318

Browse files
authored
Merge pull request #20 from v420v/issue/18
read files by size of the file instead of fixed size
2 parents e64806b + 7f725e2 commit 48c3318

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

lib/std/header.ibu

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,36 @@
33
#define STDIN 0
44
#define STDIO 1
55
#define STDERR 2
6-
#define PROGRAM_MAX_SIZE 170000
76

87
struct Vec {
98
ptr **u8,
109
len i32,
1110
cap i32,
1211
}
1312

13+
struct TimeSpec {
14+
tv_sec i64,
15+
tv_nsec i64,
16+
}
17+
18+
struct Stat {
19+
st_dev u64,
20+
st_ino u64,
21+
st_nlink u64,
22+
st_mode u32,
23+
st_uid u32,
24+
st_gid u32,
25+
pad0 i32,
26+
st_rdev u64,
27+
st_size i64,
28+
st_blksize i64,
29+
st_blocks i64,
30+
st_atim TimeSpec,
31+
st_mtim TimeSpec,
32+
st_ctim TimeSpec,
33+
unused [3]i64,
34+
}
35+
1436
func alloc(size i32) *u8;
1537
func strcmp(a *u8, b *u8) bool;
1638
func new_vec(cap i32) *Vec;
@@ -39,3 +61,4 @@ func printf(fmt *u8, ...) i32;
3961
func eprintf(fmt *u8, ...) i32;
4062
func sprintf(buf *u8, fmt *u8, ...) i32;
4163
func strndup(str *u8, n i32) *u8;
64+
func fstat(fd i32, buf *Stat) i64;

lib/std/std.ibu

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func heap_init() u0 {
2020
}
2121
}
2222

23+
func fstat(fd i32, buf *Stat) i64 {
24+
return syscall(SYS_FSTAT, fd, buf, 0);
25+
}
26+
2327
func memset(ptr *u8, val u8, n i32) u0 {
2428
for let i i32 = 0; i < n; i++ {
2529
ptr[i] = val;

src/ibu.ibu

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ func main(argc i32, argv **u8) i32 {
2525
}
2626
}
2727

28-
let program *u8 = alloc(PROGRAM_MAX_SIZE);
28+
let stat Stat = {};
29+
let fstat_result i64 = fstat(fd, &stat);
30+
if fstat_result < 0 {
31+
eprintf("error: fstat failed\n");
32+
exit(1);
33+
}
34+
35+
let program *u8 = alloc(stat.st_size+1);
2936
if program == nil {
3037
eprintf("memory allocation failed\n");
3138
exit(1);
3239
}
33-
read(fd, program, PROGRAM_MAX_SIZE);
40+
read(fd, program, stat.st_size);
3441
close(fd);
42+
program[stat.st_size] = '\0';
3543

3644
let t *Tokenizer = new_tokenizer(file_name, program);
3745
let tokens *Token = tokenize(t);

src/preprocessor/preprocessor.ibu

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,20 @@ func preprocess(tok *Token, p *Preprocessor) u0 {
168168
if !already_imported {
169169
vec_append(p.included_file_full_path, file_full_path);
170170

171-
let buf *u8 = alloc(PROGRAM_MAX_SIZE);
171+
let stat Stat = {};
172+
let fstat_result i64 = fstat(fd, &stat);
173+
if fstat_result < 0 {
174+
eprintf("error: fstat failed\n");
175+
exit(1);
176+
}
177+
178+
let buf *u8 = alloc(stat.st_size+1);
172179
if buf == nil {
173180
eprintf("memory allocation failed\n");
174181
exit(1);
175182
}
176-
read(fd, buf, PROGRAM_MAX_SIZE);
183+
read(fd, buf, stat.st_size);
184+
buf[stat.st_size] = '\0';
177185
close(fd);
178186

179187
let tokens *Token = tokenize(new_tokenizer(new_file_name, buf));
@@ -182,6 +190,7 @@ func preprocess(tok *Token, p *Preprocessor) u0 {
182190
*get_eof_from_tokens(tokens) = *tok.next.next.next;
183191
*tok = *tokens;
184192
} else {
193+
close(fd);
185194
*tok = *tok.next.next.next;
186195
}
187196
} else {

0 commit comments

Comments
 (0)