1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

win32/file.c: use enum

* win32/file.c (home_dir): use enum instead of magic numbers.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56000 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-08-24 13:04:36 +00:00
parent 8fb654e426
commit a3b84cff15

View file

@ -60,7 +60,9 @@ home_dir(void)
{
wchar_t *buffer = NULL;
size_t buffer_len = 0, len = 0;
size_t home_env = 0;
enum {
HOME_NONE, ENV_HOME, ENV_DRIVEPATH, ENV_USERPROFILE
} home_type = HOME_NONE;
/*
GetEnvironmentVariableW when used with NULL will return the required
@ -70,53 +72,44 @@ home_dir(void)
if ((len = GetEnvironmentVariableW(L"HOME", NULL, 0)) != 0) {
buffer_len = len;
home_env = 1;
home_type = ENV_HOME;
}
else if ((len = GetEnvironmentVariableW(L"HOMEDRIVE", NULL, 0)) != 0) {
buffer_len = len;
if ((len = GetEnvironmentVariableW(L"HOMEPATH", NULL, 0)) != 0) {
buffer_len += len;
home_env = 2;
}
else {
buffer_len = 0;
home_type = ENV_DRIVEPATH;
}
}
else if ((len = GetEnvironmentVariableW(L"USERPROFILE", NULL, 0)) != 0) {
buffer_len = len;
home_env = 3;
home_type = ENV_USERPROFILE;
}
/* allocate buffer */
if (home_env)
buffer = (wchar_t *)xmalloc(buffer_len * sizeof(wchar_t));
if (!home_type) return NULL;
switch (home_env) {
case 1:
/* HOME */
/* allocate buffer */
buffer = (wchar_t *)xmalloc(buffer_len * sizeof(wchar_t));
switch (home_type) {
case ENV_HOME:
GetEnvironmentVariableW(L"HOME", buffer, buffer_len);
break;
case 2:
/* HOMEDRIVE + HOMEPATH */
case ENV_DRIVEPATH:
len = GetEnvironmentVariableW(L"HOMEDRIVE", buffer, buffer_len);
GetEnvironmentVariableW(L"HOMEPATH", buffer + len, buffer_len - len);
break;
case 3:
/* USERPROFILE */
case ENV_USERPROFILE:
GetEnvironmentVariableW(L"USERPROFILE", buffer, buffer_len);
break;
default:
break;
}
if (home_env) {
/* sanitize backslashes with forwardslashes */
replace_wchar(buffer, L'\\', L'/');
/* sanitize backslashes with forwardslashes */
replace_wchar(buffer, L'\\', L'/');
return buffer;
}
return NULL;
return buffer;
}
/* Remove trailing invalid ':$DATA' of the path. */