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
|
#ifndef BASE_UNIX_IO_H
#define BASE_UNIX_IO_H
//-
internal void unix_file_write(String8 path);
internal String8 unix_file_load(MemArena *arena, String8 path);
internal b32 unix_file_exists(String8 path);
internal s64 unix_read(int fd, const void *buf, u64 count);
internal s64 unix_log(int fd, const void *buf, u64 count);
internal void unix_log_with_args(char *format, ...);
//- implementations
internal b32
unix_file_exists(String8 path)
{
return access((const char *)path.data, F_OK) == 0;
}
//- file stuff
internal String8
unix_file_load(MemArena *arena, String8 path)
{
struct stat sbuf = {0};
s32 file = open((const char *)path.data, O_RDONLY);
if(file == -1)
{
Log("An error occured. File was not loaded.");
return (String8){0};
}
if(fstat(file, &sbuf) == -1)
{
close(file);
return str8_zero();
}
String8 *result = PushString8(arena, (u64)sbuf.st_size);
if(result.size != 0)
{
result.data = (u8 *)mmap(
0,
result.size,
PROT_READ,
MAP_PRIVATE,
file,
0);
}
close(file);
return result;
}
internal void
unix_file_write(String8 path)
{
String8 result = {0};
s32 file = open((const char *)path.data, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if(file == -1)
{
return (String8){0};
u64 written = 0;
s64 err = write(file, data.data + written, data.size - written);
while(written < data.size)
{
if(err == -1)
{
close(file);
return str8_zero();
}
}
written += err;
}
close(file);
}
//- stdout stuff etc
//
// TODO: improve the API interface for a better linux logging system
// and prepare to seperate it from the windows api system
//
// TODO: can this be replaced with the regular open?
// and then just passing a refernce to the STDIN File
internal s64
unix_read(int fd, const void *buf, u64 count)
{
return (u64)syscall(SYS_read, fd, buf, count);
}
internal void
unix_log_with_args(char *format, ...)
{
va_list arguments;
va_start(arguments, format);
vprintf(format, arguments);
va_end(arguments);
}
internal s64
unix_log(int fd, const void *buf, u64 count)
{
return (u64)syscall(SYS_write, (int)fd, buf, count);
}
#endif /* BASE_UNIX_IO_H */
|