#include "setup.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef __VMS
#include <unixlib.h>
#endif
#include "homedir.h"
#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
#include "memdebug.h"
#endif
static
char *GetEnv(const char *variable, char do_expand)
{
char *env = NULL;
#ifdef WIN32
char buf1[1024], buf2[1024];
DWORD rc;
rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
if (rc > 0 && rc < sizeof(buf1)) {
env = buf1;
variable = buf1;
}
if (do_expand && strchr(variable,'%')) {
rc = ExpandEnvironmentStrings (variable, buf2, sizeof(buf2));
if (rc > 0 && rc < sizeof(buf2) &&
!strchr(buf2,'%'))
env = buf2;
}
#else
(void)do_expand;
#ifdef __VMS
env = getenv(variable);
if (env && strcmp("HOME",variable) == 0) {
env = decc_translate_vms(env);
}
#else
env = getenv(variable);
#endif
#endif
return (env && env[0])?strdup(env):NULL;
}
char *homedir(void)
{
char *home;
home = GetEnv("CURL_HOME", FALSE);
if(home)
return home;
home = GetEnv("HOME", FALSE);
if(home)
return home;
#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
{
struct passwd *pw = getpwuid(geteuid());
if (pw) {
#ifdef __VMS
home = decc_translate_vms(pw->pw_dir);
#else
home = pw->pw_dir;
#endif
if (home && home[0])
home = strdup(home);
else
home = NULL;
}
}
#endif
#ifdef WIN32
home = GetEnv("APPDATA", TRUE);
if(!home)
home = GetEnv("%USERPROFILE%\\Application Data", TRUE);
#endif
return home;
}