From: Lucas Bajolet Date: Mon, 7 Apr 2014 17:56:57 +0000 (-0400) Subject: lib/standard: Added errno and strerror bindings to stdlib. X-Git-Tag: v0.6.6~125^2~5 X-Git-Url: http://nitlanguage.org lib/standard: Added errno and strerror bindings to stdlib. Signed-off-by: Lucas Bajolet --- diff --git a/examples/socket_server.nit b/examples/socket_server.nit index 574d0a2..9df9d79 100644 --- a/examples/socket_server.nit +++ b/examples/socket_server.nit @@ -38,7 +38,7 @@ loop for c in clients do fs.readset.set(c) if fs.select(max, 4, 0) == 0 then - print "Error occured in select {socket.errno.to_s}" + print "Error occured in select {sys.errno.strerror}" break end diff --git a/lib/socket/socket.nit b/lib/socket/socket.nit index b8f4acc..52094cc 100644 --- a/lib/socket/socket.nit +++ b/lib/socket/socket.nit @@ -146,7 +146,6 @@ class Socket return new Socket.primitive_init(socket.accept) end - fun errno: Int do return socket.errno end class SocketSet diff --git a/lib/socket/socket_c.nit b/lib/socket/socket_c.nit index d576332..11bda16 100644 --- a/lib/socket/socket_c.nit +++ b/lib/socket/socket_c.nit @@ -28,7 +28,6 @@ in "C Header" `{ #include #include #include - #include typedef int S_DESCRIPTOR; typedef struct sockaddr_in S_ADDR_IN; @@ -116,7 +115,6 @@ extern FFSocket `{ S_DESCRIPTOR* `} fun destroy `{ free(recv); `} fun close: Int `{ return close( *recv ); `} fun descriptor: Int `{ return *recv; `} - fun errno: Int `{ return errno; `} fun gethostbyname(n: String): FFSocketHostent import String.to_cstring `{ return gethostbyname(String_to_cstring(n)); `} fun connect(addrIn: FFSocketAddrIn): Int `{ return connect( *recv, (S_ADDR*)addrIn, sizeof(*addrIn) ); `} diff --git a/lib/standard/kernel.nit b/lib/standard/kernel.nit index 19da7f8..e2f8f1f 100644 --- a/lib/standard/kernel.nit +++ b/lib/standard/kernel.nit @@ -17,6 +17,10 @@ module kernel import end # Mark this module is a top level one. (must be only one) +`{ +#include +`} + ############################################################################### # System Classes # ############################################################################### @@ -84,6 +88,11 @@ end class Sys # Instructions outside classes implicitly redefine this method. fun main do end + + # Number of the last error + fun errno: Int is extern `{ + return errno; + `} end ############################################################################### diff --git a/lib/standard/string.nit b/lib/standard/string.nit index 29a25ff..2ba5772 100644 --- a/lib/standard/string.nit +++ b/lib/standard/string.nit @@ -19,6 +19,7 @@ intrude import collection # FIXME should be collection::array `{ #include +#include `} ############################################################################### @@ -1341,6 +1342,15 @@ redef class Bool end redef class Int + + # Wrapper of strerror C function + private fun strerror_ext: NativeString is extern `{ + return strerror(recv); + `} + + # Returns a string describing error number + fun strerror: String do return strerror_ext.to_s + # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative). # assume < to_c max const of char fun fill_buffer(s: Buffer, base: Int, signed: Bool) diff --git a/src/naive_interpreter.nit b/src/naive_interpreter.nit index 4bd3267..7b42204 100644 --- a/src/naive_interpreter.nit +++ b/src/naive_interpreter.nit @@ -869,6 +869,8 @@ redef class AExternMethPropdef return v.int_instance(res) else if pname == "native_int_to_s" then return v.native_string_instance(recvval.to_s) + else if pname == "strerror_ext" then + return v.native_string_instance(recvval.strerror) end else if cname == "NativeFile" then var recvval = args.first.val @@ -966,6 +968,8 @@ redef class AExternMethPropdef return v.int_instance(parser_action(args[1].to_i, args[2].to_i)) else if pname == "file_getcwd" then return v.native_string_instance(getcwd) + else if pname == "errno" then + return v.int_instance(sys.errno) end fatal(v, "NOT YET IMPLEMENTED extern {mpropdef}") abort