2016-01-06 05:40:25 -08:00
|
|
|
use "syscall.zig";
|
2015-12-14 23:07:51 -08:00
|
|
|
|
2016-01-07 02:23:38 -08:00
|
|
|
const stdin_fileno : isize = 0;
|
2016-01-02 20:56:33 -08:00
|
|
|
const stdout_fileno : isize = 1;
|
|
|
|
const stderr_fileno : isize = 2;
|
|
|
|
|
2016-01-02 19:13:10 -08:00
|
|
|
// TODO error handling
|
2016-01-02 20:56:33 -08:00
|
|
|
pub fn os_get_random_bytes(buf: &u8, count: usize) -> isize {
|
|
|
|
getrandom(buf, count, 0)
|
2016-01-02 19:13:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO error handling
|
|
|
|
// TODO handle buffering and flushing (mutex protected)
|
2016-01-06 00:28:58 -08:00
|
|
|
pub fn print_str(str: []const u8) -> isize {
|
2016-01-02 20:56:33 -08:00
|
|
|
fprint_str(stdout_fileno, str)
|
|
|
|
}
|
2016-01-02 19:13:10 -08:00
|
|
|
|
|
|
|
// TODO error handling
|
|
|
|
// TODO handle buffering and flushing (mutex protected)
|
2016-01-06 00:28:58 -08:00
|
|
|
pub fn fprint_str(fd: isize, str: []const u8) -> isize {
|
2016-01-02 20:56:33 -08:00
|
|
|
write(fd, str.ptr, str.len)
|
2016-01-02 19:13:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO handle buffering and flushing (mutex protected)
|
|
|
|
// TODO error handling
|
|
|
|
pub fn print_u64(x: u64) -> isize {
|
|
|
|
// TODO use max_u64_base10_digits instead of hardcoding 20
|
2016-01-05 21:47:47 -08:00
|
|
|
var buf: [20]u8;
|
2016-01-02 19:13:10 -08:00
|
|
|
const len = buf_print_u64(buf.ptr, x);
|
|
|
|
return write(stdout_fileno, buf.ptr, len);
|
|
|
|
}
|
|
|
|
|
2016-01-05 05:30:49 -08:00
|
|
|
// TODO handle buffering and flushing (mutex protected)
|
|
|
|
// TODO error handling
|
|
|
|
pub fn print_i64(x: i64) -> isize {
|
|
|
|
// TODO use max_u64_base10_digits instead of hardcoding 20
|
2016-01-05 21:47:47 -08:00
|
|
|
var buf: [20]u8;
|
2016-01-05 05:30:49 -08:00
|
|
|
const len = buf_print_i64(buf.ptr, x);
|
|
|
|
return write(stdout_fileno, buf.ptr, len);
|
|
|
|
}
|
|
|
|
|
2016-01-07 02:23:38 -08:00
|
|
|
/*
|
|
|
|
// TODO error handling
|
|
|
|
pub fn readline(buf: []u8) -> ?[]u8 {
|
|
|
|
var index = 0;
|
|
|
|
while (index < buf.len) {
|
|
|
|
// TODO unknown size array indexing operator
|
|
|
|
const err = read(stdin_fileno, &buf.ptr[index], 1);
|
|
|
|
if (err != 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// TODO unknown size array indexing operator
|
|
|
|
if (buf.ptr[index] == '\n') {
|
|
|
|
return buf[0...index + 1];
|
|
|
|
}
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-01-02 20:56:33 -08:00
|
|
|
fn digit_to_char(digit: u64) -> u8 {
|
|
|
|
'0' + (digit as u8)
|
|
|
|
}
|
2016-01-02 02:38:45 -08:00
|
|
|
|
|
|
|
const max_u64_base10_digits: usize = 20;
|
|
|
|
|
2016-01-06 00:28:58 -08:00
|
|
|
// TODO use an array for out_buf instead of pointer. this should give bounds checking in
|
|
|
|
// debug mode and length can get optimized out in release mode. requires array slicing syntax
|
|
|
|
// for the buf_print_u64 call.
|
2016-01-05 05:30:49 -08:00
|
|
|
fn buf_print_i64(out_buf: &u8, x: i64) -> usize {
|
|
|
|
if (x < 0) {
|
|
|
|
out_buf[0] = '-';
|
|
|
|
return 1 + buf_print_u64(&out_buf[1], ((-(x + 1)) as u64) + 1);
|
|
|
|
} else {
|
|
|
|
return buf_print_u64(out_buf, x as u64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:28:58 -08:00
|
|
|
// TODO use an array for out_buf instead of pointer.
|
2016-01-02 02:38:45 -08:00
|
|
|
fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
|
2016-01-05 21:47:47 -08:00
|
|
|
var buf: [max_u64_base10_digits]u8;
|
2016-01-02 02:38:45 -08:00
|
|
|
var a = x;
|
2016-01-05 21:47:47 -08:00
|
|
|
var index = buf.len;
|
2016-01-02 02:38:45 -08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const digit = a % 10;
|
|
|
|
index -= 1;
|
|
|
|
buf[index] = digit_to_char(digit);
|
|
|
|
a /= 10;
|
|
|
|
if (a == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-05 21:47:47 -08:00
|
|
|
const len = buf.len - index;
|
2016-01-02 02:38:45 -08:00
|
|
|
|
|
|
|
// TODO memcpy intrinsic
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < len) {
|
|
|
|
out_buf[i] = buf[index + i];
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|