3

Functions like joinpath use the appropriate OS-dependent separator when joining two paths (ie / on Linux, \\ on Windows, etc). How do these functions know what separator to use?

Similarly, the root directory on Linux is /, but on Windows is probably C:\\. Is there a way to retrieve the OS-dependent root directory in Julia?

Note, I've had a look at the joinpath source on github, and it appears to use an undocumented function pathsep(a,b) and a global variable path_separator_re, but I can't see how either of these work.

1 Answer 1

1

It uses the Sys.isunix and Sys.iswindows functions in order to conditionally define the correct path_separator_re variables, etc.


if Sys.isunix()
    # ...
    const path_separator_re = r"/+"
    # ...

    splitdrive(path::String) = ("",path)
elseif Sys.iswindows()
    # ...
    const path_separator_re = r"[/\\]+"
    # ...

    function splitdrive(path::String)
        m = match(r"^([^\\]+:|\\\\[^\\]+\\[^\\]+|\\\\\?\\UNC\\[^\\]+\\[^\\]+|\\\\\?\\[^\\]+:|)(.*)$", path)
        String(m.captures[1]), String(m.captures[2])
    end
else
    error("path primitives for this OS need to be defined")
end

For the root directory, check out the homedir function, which uses libuv to determine it.

help?> homedir search: homedir

homedir() -> AbstractString Return the current user's home directory.

| Note | | homedir determines the home directory via libuv's uv_os_homedir. For details (for example on how to specify the home | directory via environment variables), see the uv_os_homedir documentation.

1
  • Very interesting answer. Thank you. Jan 22, 2018 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.