2003-11-07 12:17:15 -05:00
|
|
|
/***************************************************************************
|
2004-10-06 03:50:18 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2003-11-07 12:17:15 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2007-11-01 17:49:59 -04:00
|
|
|
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2003-11-07 12:17:15 -05: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 03:50:18 -04:00
|
|
|
*
|
2003-11-07 12:17:15 -05: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.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2004-01-29 08:54:08 -05:00
|
|
|
#include "homedir.h"
|
|
|
|
|
2004-05-12 04:22:04 -04:00
|
|
|
#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
|
|
|
|
#include "memdebug.h"
|
2003-11-07 12:17:15 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static
|
2003-11-08 03:39:34 -05:00
|
|
|
char *GetEnv(const char *variable, char do_expand)
|
2003-11-07 12:17:15 -05: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 03:50:18 -04:00
|
|
|
|
2003-11-07 12:17:15 -05:00
|
|
|
rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
|
|
|
|
if (rc > 0 && rc < sizeof(buf1)) {
|
|
|
|
env = buf1;
|
|
|
|
variable = buf1;
|
|
|
|
}
|
|
|
|
if (do_expand && strchr(variable,'%')) {
|
|
|
|
/* buf2 == variable if not expanded */
|
|
|
|
rc = ExpandEnvironmentStrings (variable, buf2, sizeof(buf2));
|
|
|
|
if (rc > 0 && rc < sizeof(buf2) &&
|
|
|
|
!strchr(buf2,'%')) /* no vars still unexpanded */
|
|
|
|
env = buf2;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)do_expand;
|
2004-10-06 03:50:18 -04:00
|
|
|
#ifdef VMS
|
2003-11-07 12:17:15 -05:00
|
|
|
env = getenv(variable);
|
|
|
|
if (env && strcmp("HOME",variable) == 0) {
|
2008-09-12 07:18:17 -04:00
|
|
|
env = decc_translate_vms(env);
|
2003-11-07 12:17:15 -05: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 17:24:58 -04:00
|
|
|
char *home;
|
|
|
|
|
|
|
|
home = GetEnv("CURL_HOME", FALSE);
|
|
|
|
if(home)
|
|
|
|
return home;
|
|
|
|
|
|
|
|
home = GetEnv("HOME", FALSE);
|
2003-11-07 12:17:15 -05:00
|
|
|
if(home)
|
|
|
|
return home;
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2003-11-07 12:17:15 -05:00
|
|
|
#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
|
|
|
|
{
|
|
|
|
struct passwd *pw = getpwuid(geteuid());
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2003-11-07 12:17:15 -05:00
|
|
|
if (pw) {
|
|
|
|
#ifdef VMS
|
2008-09-12 07:18:17 -04:00
|
|
|
home = decc_translate_vms(pw->pw_dir);
|
2003-11-07 12:17:15 -05:00
|
|
|
#else
|
|
|
|
home = pw->pw_dir;
|
|
|
|
#endif
|
|
|
|
if (home && home[0])
|
|
|
|
home = strdup(home);
|
2007-11-01 17:49:59 -04:00
|
|
|
else
|
|
|
|
home = NULL;
|
2003-11-07 12:17:15 -05: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;
|
|
|
|
}
|