std: fix linux.get_errno

This commit is contained in:
Andrew Kelley 2016-05-13 09:53:30 -07:00
parent 8e3ab28be9
commit 5d2f86116f
2 changed files with 7 additions and 3 deletions

View File

@ -222,7 +222,7 @@ pub const AF_MAX = PF_MAX;
/// Get the errno from a syscall return value, or 0 for no error.
pub fn get_errno(r: isize) -> isize {
if (r > -4096) -r else 0
if (r > -4096 && r < 0) -r else 0
}
pub fn mmap(address: ?&u8, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {

View File

@ -7,8 +7,10 @@ pub error Unexpected;
pub error Io;
pub error TimedOut;
pub error ConnectionReset;
pub error ConnectionRefused;
pub error NoMem;
pub error NotSocket;
pub error BadFd;
struct Connection {
socket_fd: i32,
@ -27,16 +29,18 @@ struct Connection {
}
}
pub fn recv(c: Connection, buf: []u8) -> %isize {
pub fn recv(c: Connection, buf: []u8) -> %[]u8 {
const recv_ret = linux.recvfrom(c.socket_fd, buf.ptr, buf.len, 0, null, null);
const recv_err = linux.get_errno(recv_ret);
switch (recv_err) {
0 => return recv_ret,
0 => return buf[0...recv_ret],
errno.EINVAL => unreachable{},
errno.EFAULT => unreachable{},
errno.ENOTSOCK => return error.NotSocket,
errno.EINTR => return error.SigInterrupt,
errno.ENOMEM => return error.NoMem,
errno.ECONNREFUSED => return error.ConnectionRefused,
errno.EBADF => return error.BadFd,
// TODO more error values
else => return error.Unexpected,
}