diff --git a/base/error.jl b/base/error.jl index 1687bc3db7dde..d5007eb558757 100644 --- a/base/error.jl +++ b/base/error.jl @@ -32,7 +32,8 @@ catch_backtrace() = ccall(:jl_get_backtrace, Array{Ptr{Void},1}, ()) ## system error handling ## -errno() = ccall(:jl_errno, Int32, ()) +errno() = ccall(:jl_errno, Cint, ()) +errno(e::Integer) = ccall(:jl_set_errno, Void, (Cint,), e) strerror(e::Integer) = bytestring(ccall(:strerror, Ptr{Uint8}, (Int32,), e)) strerror() = strerror(errno()) systemerror(p, b::Bool) = b ? throw(SystemError(string(p))) : nothing diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index d758e90e0d23f..65e6eb57941e9 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -4844,6 +4844,18 @@ C Interface Close shared library referenced by handle. +.. function:: find_library(names, locations) + + Searches for the first library in ``names`` in the paths in the ``locations`` list, ``DL_LOAD_PATH``, or system + library paths (in that order) which can successfully be dlopen'd. On success, the return value will be one of + the names (potentially prefixed by one of the paths in locations). This string can be assigned to a ``global const`` + and used as the library name in future ``ccall``'s. On failure, it returns the empty string. + +.. data:: DL_LOAD_PATH + + When calling ``dlopen``, the paths in this list will be searched first, in order, before searching the + system locations for a valid library handle. + .. function:: c_malloc(size::Integer) Call ``malloc`` from the C standard library. @@ -4923,17 +4935,22 @@ C Interface Re-enable Ctrl-C handler during execution of a function. Temporarily reverses the effect of ``disable_sigint``. -.. function:: find_library(names, locations) +.. function:: errno([code]) - Searches for the first library in ``names`` in the paths in the ``locations`` list, ``DL_LOAD_PATH``, or system - library paths (in that order) which can successfully be dlopen'd. On success, the return value will be one of - the names (potentially prefixed by one of the paths in locations). This string can be assigned to a ``global const`` - and used as the library name in future ``ccall``'s. On failure, it returns the empty string. + Get the value of the C library's ``errno``. If an argument is specified, it is + used to set the value of ``errno``. -.. data:: DL_LOAD_PATH + The value of ``errno`` is only valid immediately after a ``ccall`` to a C + library routine that sets it. Specifically, you cannot call ``errno`` at the next + prompt in a REPL, because lots of code is executed between prompts. - When calling ``dlopen``, the paths in this list will be searched first, in order, before searching the - system locations for a valid library handle. +.. function:: systemerror(sysfunc, iftrue) + + Raises a ``SystemError`` for ``errno`` with the descriptive string ``sysfunc`` if ``bool`` is true + +.. function:: strerror(n) + + Convert a system call error code to a descriptive string .. data:: Cchar @@ -5030,21 +5047,6 @@ Errors Get the backtrace of the current exception, for use within ``catch`` blocks. -.. function:: errno() - - Get the value of the C library's ``errno``. As in C, ``errno()`` must be called - directly after a function that uses ``errno`` for error reporting. Specifically - you can not call ``errno`` on the next prompt in a REPL, because lots of code - is executed between REPL prompts. - -.. function:: systemerror(sysfunc, iftrue) - - Raises a ``SystemError`` for ``errno`` with the descriptive string ``sysfunc`` if ``bool`` is true - -.. function:: strerror(n) - - Convert a system call error code to a descriptive string - .. function:: assert(cond, [text]) Raise an error if ``cond`` is false. Also available as the macro ``@assert expr``. diff --git a/src/julia.h b/src/julia.h index d7e568a1fb1a2..6d38533211246 100644 --- a/src/julia.h +++ b/src/julia.h @@ -822,6 +822,7 @@ DLLEXPORT jl_value_t *jl_eqtable_get(jl_array_t *h, void *key, jl_value_t *deflt // system information DLLEXPORT int jl_errno(void); +DLLEXPORT void jl_set_errno(int e); DLLEXPORT int32_t jl_stat(const char* path, char* statbuf); // environment entries diff --git a/src/sys.c b/src/sys.c index e471495e6b6c1..e7200fa98bf3d 100644 --- a/src/sys.c +++ b/src/sys.c @@ -297,6 +297,7 @@ void jl_free2(void *p, void *hint) // -- syscall utilities -- int jl_errno(void) { return errno; } +void jl_set_errno(int e) { errno = e; } // -- get the number of CPU cores --