2019-10-07 01:31:01 +03:00
|
|
|
module os2
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
struct File {
|
|
|
|
fd int
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn C.perror(charptr)
|
|
|
|
|
2019-10-07 01:31:01 +03:00
|
|
|
|
|
|
|
fn C.open(byteptr, int, int) int
|
2019-12-27 19:59:04 +03:00
|
|
|
|
|
|
|
|
2019-10-07 01:31:01 +03:00
|
|
|
fn C.write(voidptr, byteptr, int) int
|
|
|
|
|
2019-12-27 19:59:04 +03:00
|
|
|
|
|
|
|
fn C.close(int) int
|
|
|
|
|
|
|
|
|
2019-10-07 01:31:01 +03:00
|
|
|
pub fn create(path string) ?File {
|
2019-12-27 19:59:04 +03:00
|
|
|
fd := C.open(path.str, C.O_CREAT | C.O_TRUNC | C.O_WRONLY, 0644) // 511
|
2019-10-07 01:31:01 +03:00
|
|
|
if fd == -1 {
|
|
|
|
return error('failed to create "$path":')
|
2019-12-27 19:59:04 +03:00
|
|
|
// os.print_c_errno()
|
2019-10-07 01:31:01 +03:00
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
return File{
|
|
|
|
fd}
|
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
|
|
|
|
pub fn (f File) writeln(s string) {
|
|
|
|
ss := s + '\n'
|
|
|
|
ret := C.write(f.fd, ss.str, s.len + 1)
|
|
|
|
if ret == -1 {
|
|
|
|
C.perror('failed to write')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (f File) close() {
|
|
|
|
C.close(f.fd)
|
|
|
|
}
|