Skip to content

Commit

Permalink
fix(pl.path): make expanduser more sturdy
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Apr 2, 2024
1 parent 675a0c2 commit 5b1c648
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lua/pl/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,29 @@ end
-- In windows, if HOME isn't set, then USERPROFILE is used in preference to
-- HOMEDRIVE HOMEPATH. This is guaranteed to be writeable on all versions of Windows.
-- @string P A file path
-- @treturn[1] string The file path with the `~` prefix substituted, or the input path if it had no prefix.
-- @treturn[2] nil
-- @treturn[2] string Error message if the environment variables were unavailable.
function path.expanduser(P)
assert_string(1,P)
if at(P,1) == '~' then
local home = getenv('HOME')
if not home then -- has to be Windows
home = getenv 'USERPROFILE' or (getenv 'HOMEDRIVE' .. getenv 'HOMEPATH')
if (not home) and path.is_windows then
home = getenv 'USERPROFILE'
if not home then
local hd = getenv 'HOMEDRIVE'
local hp = getenv 'HOMEPATH'
if hd and hp then
home = hd..hp
end
end
if not home then
return nil, "failed to expand '~' (HOME, USERPROFILE and HOMEDRIVE or HOMEPATH not set)"
end
else
if not home then
return nil, "failed to expand '~' (HOME not set)"
end
end
return home..sub(P,2)
else
Expand Down

0 comments on commit 5b1c648

Please sign in to comment.