2003-11-07 18:17:15 +01:00
|
|
|
/***************************************************************************
|
2004-10-06 09:50:18 +02:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2003-11-07 18:17:15 +01:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2011-04-25 22:44:39 +02:00
|
|
|
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2003-11-07 18:17:15 +01:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
2004-10-06 09:50:18 +02:00
|
|
|
*
|
2003-11-07 18:17:15 +01:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-12-30 18:59:56 +01:00
|
|
|
#ifdef __VMS
|
2003-11-07 18:17:15 +01:00
|
|
|
#include <unixlib.h>
|
|
|
|
#endif
|
|
|
|
|
2004-01-29 14:54:08 +01:00
|
|
|
#include "homedir.h"
|
|
|
|
|
2004-05-12 10:22:04 +02:00
|
|
|
#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
|
|
|
|
#include "memdebug.h"
|
2003-11-07 18:17:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static
|
2003-11-08 09:39:34 +01:00
|
|
|
char *GetEnv(const char *variable, char do_expand)
|
2003-11-07 18:17:15 +01:00
|
|
|
{
|
|
|
|
char *env = NULL;
|
|
|
|
#ifdef WIN32
|
|
|
|
char buf1[1024], buf2[1024];
|
|
|
|
DWORD rc;
|
|
|
|
|
|
|
|
/* Don't use getenv(); it doesn't find variable added after program was
|
|
|
|
* started. Don't accept truncated results (i.e. rc >= sizeof(buf1)). */
|
2004-10-06 09:50:18 +02:00
|
|
|
|
2003-11-07 18:17:15 +01:00
|
|
|
rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
|
2011-04-25 22:44:39 +02:00
|
|
|
if(rc > 0 && rc < sizeof(buf1)) {
|
2003-11-07 18:17:15 +01:00
|
|
|
env = buf1;
|
|
|
|
variable = buf1;
|
|
|
|
}
|
2011-04-25 22:44:39 +02:00
|
|
|
if(do_expand && strchr(variable,'%')) {
|
2003-11-07 18:17:15 +01:00
|
|
|
/* buf2 == variable if not expanded */
|
|
|
|
rc = ExpandEnvironmentStrings (variable, buf2, sizeof(buf2));
|
2011-04-25 22:44:39 +02:00
|
|
|
if(rc > 0 && rc < sizeof(buf2) &&
|
|
|
|
!strchr(buf2,'%')) /* no vars still unexpanded */
|
2003-11-07 18:17:15 +01:00
|
|
|
env = buf2;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)do_expand;
|
2009-12-30 18:59:56 +01:00
|
|
|
#ifdef __VMS
|
2003-11-07 18:17:15 +01:00
|
|
|
env = getenv(variable);
|
2011-04-25 22:44:39 +02:00
|
|
|
if(env && strcmp("HOME",variable) == 0) {
|
|
|
|
env = decc_translate_vms(env);
|
2003-11-07 18:17:15 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* no length control */
|
|
|
|
env = getenv(variable);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return (env && env[0])?strdup(env):NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the home directory of the current user as an allocated string */
|
|
|
|
char *homedir(void)
|
|
|
|
{
|
2005-04-27 23:24:58 +02:00
|
|
|
char *home;
|
|
|
|
|
|
|
|
home = GetEnv("CURL_HOME", FALSE);
|
|
|
|
if(home)
|
|
|
|
return home;
|
|
|
|
|
|
|
|
home = GetEnv("HOME", FALSE);
|
2003-11-07 18:17:15 +01:00
|
|
|
if(home)
|
|
|
|
return home;
|
2004-10-06 09:50:18 +02:00
|
|
|
|
2003-11-07 18:17:15 +01:00
|
|
|
#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
|
|
|
|
{
|
|
|
|
struct passwd *pw = getpwuid(geteuid());
|
2004-10-06 09:50:18 +02:00
|
|
|
|
2011-04-25 22:44:39 +02:00
|
|
|
if(pw) {
|
2009-12-30 18:59:56 +01:00
|
|
|
#ifdef __VMS
|
2008-09-12 13:18:17 +02:00
|
|
|
home = decc_translate_vms(pw->pw_dir);
|
2003-11-07 18:17:15 +01:00
|
|
|
#else
|
|
|
|
home = pw->pw_dir;
|
|
|
|
#endif
|
2011-04-25 22:44:39 +02:00
|
|
|
if(home && home[0])
|
2003-11-07 18:17:15 +01:00
|
|
|
home = strdup(home);
|
2007-11-01 22:49:59 +01:00
|
|
|
else
|
|
|
|
home = NULL;
|
2003-11-07 18:17:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* PWD-stuff */
|
|
|
|
#ifdef WIN32
|
|
|
|
home = GetEnv("APPDATA", TRUE);
|
|
|
|
if(!home)
|
|
|
|
home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
|
|
|
|
on Win-2K/XP */
|
|
|
|
#endif /* WIN32 */
|
|
|
|
return home;
|
|
|
|
}
|