aboutsummaryrefslogtreecommitdiff
path: root/std/std.zig
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2016-01-02 03:38:45 -0700
committerAndrew Kelley <superjoe30@gmail.com>2016-01-02 03:38:45 -0700
commit968b85ad77892da945d478799d4e775222248f1f (patch)
treed7ad6b71f1f80e27d5443c9faee41e480705757b /std/std.zig
parent724dcdd384c6c00b9e39ed67867d364287e45f0a (diff)
downloadzig-968b85ad77892da945d478799d4e775222248f1f.tar.gz
zig-968b85ad77892da945d478799d4e775222248f1f.zip
closer to guess number example working
Diffstat (limited to 'std/std.zig')
-rw-r--r--std/std.zig69
1 files changed, 66 insertions, 3 deletions
diff --git a/std/std.zig b/std/std.zig
index 132a22e996..42967e1c5f 100644
--- a/std/std.zig
+++ b/std/std.zig
@@ -1,6 +1,9 @@
const SYS_write : isize = 1;
const SYS_exit : isize = 60;
+const SYS_getrandom : isize = 278;
+
const stdout_fileno : isize = 1;
+const stderr_fileno : isize = 2;
fn syscall1(number: isize, arg1: isize) -> isize {
asm volatile ("syscall"
@@ -16,6 +19,12 @@ fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
: "rcx", "r11")
}
+/*
+pub fn getrandom(buf: &u8, count: usize, flags: u32) -> isize {
+ return syscall3(SYS_getrandom, buf as isize, count as isize, flags as isize);
+}
+*/
+
pub fn write(fd: isize, buf: &const u8, count: usize) -> isize {
return syscall3(SYS_write, fd, buf as isize, count as isize);
}
@@ -25,8 +34,62 @@ pub fn exit(status: i32) -> unreachable {
unreachable;
}
+/*
+fn digit_to_char(digit: u64) -> u8 { '0' + (digit as u8) }
+
+const max_u64_base10_digits: usize = 20;
+
+fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
+ // TODO use max_u64_base10_digits instead of hardcoding 20
+ var buf: [u8; 20];
+ var a = x;
+ var index = max_u64_base10_digits;
+
+ while (true) {
+ const digit = a % 10;
+ index -= 1;
+ buf[index] = digit_to_char(digit);
+ a /= 10;
+ if (a == 0)
+ break;
+ }
+
+ const len = max_u64_base10_digits - index;
+
+ // TODO memcpy intrinsic
+ var i: usize = 0;
+ while (i < len) {
+ out_buf[i] = buf[index + i];
+ i += 1;
+ }
+
+ return len;
+}
+
+// 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
+ var buf: [u8; 20];
+ const len = buf_print_u64(buf.ptr, x);
+ return write(stdout_fileno, buf.ptr, len);
+}
+*/
+
+
+// TODO error handling
+// TODO handle buffering and flushing (mutex protected)
+pub fn print_str(str: string) -> isize { fprint_str(stdout_fileno, str) }
+
+// TODO error handling
+// TODO handle buffering and flushing (mutex protected)
+pub fn fprint_str(fd: isize, str: string) -> isize {
+ return write(fd, str.ptr, str.len);
+}
+
+/*
// TODO error handling
-// TODO handle buffering and flushing
-pub fn print_str(str : string) -> isize {
- return write(stdout_fileno, str.ptr, str.len);
+pub fn os_get_random_bytes(buf: &u8, count: usize) -> isize {
+ return getrandom(buf, count, 0);
}
+*/