mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Fix File.expand_path for Windows
- The inner method `File._gethome` could be read as intending to use the `USERPROFILE` environment variable instead on Windows when the `HOME` environment variable is not available.
In reality, however, this was not the case.
- The result of `File.expand_path` should unify path separators with `/`, but it did not.
This commit is contained in:
@@ -149,7 +149,7 @@ class File < IO
|
||||
if drive_prefix.empty?
|
||||
expanded_path
|
||||
else
|
||||
drive_prefix + expanded_path.gsub("/", File::ALT_SEPARATOR)
|
||||
drive_prefix + expanded_path
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -345,15 +345,14 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
|
||||
mrb_int argc = mrb_get_args(mrb, "|S", &username);
|
||||
if (argc == 0) {
|
||||
home = getenv("HOME");
|
||||
if (home == NULL) {
|
||||
return mrb_nil_value();
|
||||
}
|
||||
#ifdef _WIN32
|
||||
home = getenv("USERPROFILE");
|
||||
if (home == NULL) {
|
||||
return mrb_nil_value();
|
||||
home = getenv("USERPROFILE");
|
||||
}
|
||||
#endif
|
||||
if (home == NULL) {
|
||||
return mrb_nil_value();
|
||||
}
|
||||
if (!mrb_file_is_absolute_path(home)) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
|
||||
}
|
||||
@@ -378,6 +377,15 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
|
||||
home = mrb_utf8_from_locale(home, -1);
|
||||
path = mrb_str_new_cstr(mrb, home);
|
||||
mrb_utf8_free(home);
|
||||
#ifdef _WIN32
|
||||
char *pathp = RSTRING_PTR(path);
|
||||
const char *const pathend = pathp + RSTRING_LEN(path);
|
||||
for (;;) {
|
||||
pathp = memchr(pathp, '\\', pathend - pathp);
|
||||
if (!pathp) break;
|
||||
*pathp++ = '/';
|
||||
}
|
||||
#endif
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
@@ -204,7 +204,7 @@ assert('File.expand_path') do
|
||||
assert_equal "/", File.expand_path("../../../..", "/")
|
||||
if File._getwd[1] == ":"
|
||||
drive_letter = File._getwd[0]
|
||||
assert_equal drive_letter + ":\\", File.expand_path(([".."] * 100).join("/"))
|
||||
assert_equal drive_letter + ":/", File.expand_path(([".."] * 100).join("/"))
|
||||
else
|
||||
assert_equal "/", File.expand_path(([".."] * 100).join("/"))
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user