mruby-io (mrb_file__gethome): allow avoiding getpwnam(3); ref #5358

You have to define `MRB_IO_NO_PWNAM` to skip calling getpwname(3).
This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-09-03 15:27:29 +09:00
parent 96cf9ba230
commit 1d12097bae
2 changed files with 20 additions and 23 deletions
+5
View File
@@ -169,6 +169,11 @@ Add the line below to your build configuration.
| File#size | | |
| File#truncate | | |
## Porting Note
If your (non Windows) platform does not support `getpwnam(3)` for some reason, define `MRB_IO_NO_PWNAM`.
See [mruby#5358](https://github.com/mruby/mruby/issues/5358).
## License
Copyright (c) 2013 Internet Initiative Japan Inc.
+15 -23
View File
@@ -339,7 +339,6 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
mrb_value path;
mrb->c->ci->mid = 0;
#ifndef _WIN32
mrb_value username;
mrb_int argc = mrb_get_args(mrb, "|S", &username);
@@ -348,10 +347,21 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
if (home == NULL) {
return mrb_nil_value();
}
#ifdef _WIN32
home = getenv("USERPROFILE");
if (home == NULL) {
return mrb_nil_value();
}
#endif
if (!mrb_file_is_absolute_path(home)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
}
}
#if defined(_WIN32) || defined(MRB_IO_NO_PWNAM)
else {
return mrb_nil_value();
}
#else
else {
const char *cuser = RSTRING_CSTR(mrb, username);
struct passwd *pwd = getpwnam(cuser);
@@ -363,29 +373,11 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
mrb_raisef(mrb, E_ARGUMENT_ERROR, "non-absolute home of ~%v", username);
}
}
home = mrb_locale_from_utf8(home, -1);
path = mrb_str_new_cstr(mrb, home);
mrb_locale_free(home);
return path;
#else /* _WIN32 */
mrb_int argc = mrb_get_argc(mrb);
if (argc == 0) {
home = getenv("USERPROFILE");
if (home == NULL) {
return mrb_nil_value();
}
if (!mrb_file_is_absolute_path(home)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "non-absolute home");
}
}
else {
return mrb_nil_value();
}
home = mrb_locale_from_utf8(home, -1);
path = mrb_str_new_cstr(mrb, home);
mrb_locale_free(home);
return path;
#endif
home = mrb_locale_from_utf8(home, -1);
path = mrb_str_new_cstr(mrb, home);
mrb_locale_free(home);
return path;
}
#define TIME_OVERFLOW_P(a) (sizeof(time_t) >= sizeof(mrb_int) && ((a) > MRB_INT_MAX || (a) < MRB_INT_MIN))