unix: refactor uv_os_homedir to use uv_os_getenv

PR-URL: https://github.com/libuv/libuv/pull/1760
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Santiago Gimeno 2018-02-19 22:16:21 +01:00 committed by cjihrig
parent f60713f7cf
commit b7f649d308
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5

View File

@ -1048,29 +1048,16 @@ int uv__dup2_cloexec(int oldfd, int newfd) {
int uv_os_homedir(char* buffer, size_t* size) {
uv_passwd_t pwd;
char* buf;
size_t len;
int r;
if (buffer == NULL || size == NULL || *size == 0)
return UV_EINVAL;
/* Check if the HOME environment variable is set first. The task of
performing input validation on buffer and size is taken care of by
uv_os_getenv(). */
r = uv_os_getenv("HOME", buffer, size);
/* Check if the HOME environment variable is set first */
buf = getenv("HOME");
if (buf != NULL) {
len = strlen(buf);
if (len >= *size) {
*size = len + 1;
return UV_ENOBUFS;
}
memcpy(buffer, buf, len + 1);
*size = len;
return 0;
}
if (r != UV_ENOENT)
return r;
/* HOME is not set, so call uv__getpwuid_r() */
r = uv__getpwuid_r(&pwd);