summaryrefslogtreecommitdiff
path: root/win32/file.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-24 13:04:36 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-24 13:04:36 +0000
commita3b84cff1553cc51145531a1c05dd23b64806fd9 (patch)
tree162be0f481f40b6d22fb1d9c719bac4ea039eeef /win32/file.c
parent8fb654e4260453245204aff5bc078fc233ee5736 (diff)
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
Diffstat (limited to 'win32/file.c')
-rw-r--r--win32/file.c39
1 files changed, 16 insertions, 23 deletions
diff --git a/win32/file.c b/win32/file.c
index 556d9615b3e..6e7e5110ef5 100644
--- a/win32/file.c
+++ b/win32/file.c
@@ -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;
}
+ if (!home_type) return NULL;
+
/* allocate buffer */
- if (home_env)
- buffer = (wchar_t *)xmalloc(buffer_len * sizeof(wchar_t));
+ buffer = (wchar_t *)xmalloc(buffer_len * sizeof(wchar_t));
- switch (home_env) {
- case 1:
- /* HOME */
+ 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'/');
-
- return buffer;
- }
+ /* sanitize backslashes with forwardslashes */
+ replace_wchar(buffer, L'\\', L'/');
- return NULL;
+ return buffer;
}
/* Remove trailing invalid ':$DATA' of the path. */