Subject: Re: [msysGit] [PATCH v2] Add a Windows-specific fallback to getenv("HOME");

From: Torsten Bögershausen <[email-masked]>
Date: Thu, 05 Jun 2014 10:32:44 +0200

On 2014-06-05 10.03, Stepan Kasal wrote:
> From: Johannes Schindelin <[email-masked]>
> Date: Wed, 2 Jun 2010 00:41:33 +0200
> 
> If HOME is not set, use $HOMEDRIVE$HOMEPATH
> 
> Signed-off-by: Johannes Schindelin <[email-masked]>
> Signed-off-by: Stepan Kasal <[email-masked]>
> ---
> 
> Hello Karsten,
> thanks for your explanation.  There are more things to be done, but
> I hope you can ack this patch as a step forward.
> 
> Hello Dscho,
> I hope you can ack this as well: it is basically equivalent with your
> patch, tailored according to current upstream fashion,  ;-)
> 
> Stepan
> 
>  compat/mingw.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/compat/mingw.c b/compat/mingw.c
> index a0e13bc..e108388 100644
> --- a/compat/mingw.c
> +++ b/compat/mingw.c
> @@ -1181,6 +1181,11 @@ char *mingw_getenv(const char *name)
>  		if (!result)
>  			result = getenv_cs("TEMP");
>  	}
> +	if (!result && !strcmp(name, "HOME")) {
> +		struct strbuf buf = STRBUF_INIT;
> +		strbuf_addf(&buf, "%s%s", getenv_cs("HOMEDRIVE"), getenv_cs("HOMEPATH"));
should we have a NULL pointer check here?
What happens if %HOMEPATH% is not set for any reason ?
getenv_cs will return NULL, and strbuf_addf() does not like that, as far as I know.
And even if it converts a NULL pointer into "<NULL>" or "NULL", the result is not what we want.
If HOMEDRIVE is set, but not HOMEPATH, we can fall back into the root of HOMEDRIVE:

	if (!result && !strcmp(name, "HOME")) {
		const char *homedrive = getenv_cs("HOMEDRIVE");
		const char *homepath = getenv_cs("HOMEPATH");
		if (!homepath)
			homepath = "";
        	if (homedrive) {
			struct strbuf buf = STRBUF_INIT;
			strbuf_addf(&buf, "%s%s", homedrive, homepath);
			result = strbuf_detach(&buf, NULL);
		}
	}
	return result;
}