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
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-02-12 19:40:08 -05:00
|
|
|
* Copyright (C) 1998 - 2020, 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
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2003-11-07 12:17:15 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_PWD_H
|
2011-10-06 11:39:00 -04:00
|
|
|
# include <pwd.h>
|
2003-11-07 12:17:15 -05:00
|
|
|
#endif
|
|
|
|
|
2020-08-21 17:40:12 -04:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2020-01-29 03:23:55 -05:00
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
2011-10-06 11:39:00 -04:00
|
|
|
#include "tool_homedir.h"
|
2004-01-29 08:54:08 -05:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2003-11-07 12:17:15 -05:00
|
|
|
|
2020-01-29 03:23:55 -05:00
|
|
|
static char *GetEnv(const char *variable)
|
2003-11-07 12:17:15 -05:00
|
|
|
{
|
2020-01-29 03:23:55 -05:00
|
|
|
char *dupe, *env;
|
2003-11-07 12:17:15 -05:00
|
|
|
|
2020-01-29 03:23:55 -05:00
|
|
|
env = curl_getenv(variable);
|
|
|
|
if(!env)
|
|
|
|
return NULL;
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2020-01-29 03:23:55 -05:00
|
|
|
dupe = strdup(env);
|
|
|
|
curl_free(env);
|
|
|
|
return dupe;
|
2003-11-07 12:17:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return the home directory of the current user as an allocated string */
|
2020-08-21 17:40:12 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The original logic found a home dir to use (by checking a range of
|
|
|
|
* environment variables and last using getpwuid) and returned that for the
|
|
|
|
* parent to use.
|
|
|
|
*
|
|
|
|
* With the XDG_CONFIG_HOME support (added much later than the other), this
|
|
|
|
* variable is treated differently in order to not ruin existing installations
|
|
|
|
* even if this environment variable is set. If this variable is set, and a
|
|
|
|
* file name is set to check, then only if that file name exists in that
|
|
|
|
* directory will it be returned as a "home directory".
|
|
|
|
*
|
|
|
|
* 1. use CURL_HOME if set
|
|
|
|
* 2. use XDG_CONFIG_HOME if set and fname is present
|
|
|
|
* 3. use HOME if set
|
|
|
|
* 4. Non-windows: use getpwuid
|
|
|
|
* 5. Windows: use APPDATA if set
|
|
|
|
* 6. Windows: use "USERPROFILE\Application Data" is set
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *homedir(const char *fname)
|
2003-11-07 12:17:15 -05:00
|
|
|
{
|
2005-04-27 17:24:58 -04:00
|
|
|
char *home;
|
|
|
|
|
2020-01-29 03:23:55 -05:00
|
|
|
home = GetEnv("CURL_HOME");
|
2005-04-27 17:24:58 -04:00
|
|
|
if(home)
|
|
|
|
return home;
|
|
|
|
|
2020-08-21 17:40:12 -04:00
|
|
|
if(fname) {
|
|
|
|
home = GetEnv("XDG_CONFIG_HOME");
|
|
|
|
if(home) {
|
|
|
|
char *c = curl_maprintf("%s" DIR_CHAR "%s", home, fname);
|
|
|
|
if(c) {
|
|
|
|
int fd = open(c, O_RDONLY);
|
|
|
|
curl_free(c);
|
|
|
|
if(fd >= 0) {
|
|
|
|
close(fd);
|
|
|
|
return home;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(home);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 03:23:55 -05:00
|
|
|
home = GetEnv("HOME");
|
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
|
|
|
|
2011-04-25 16:44:39 -04:00
|
|
|
if(pw) {
|
2003-11-07 12:17:15 -05:00
|
|
|
home = pw->pw_dir;
|
2011-04-25 16:44:39 -04:00
|
|
|
if(home && home[0])
|
2003-11-07 12:17:15 -05:00
|
|
|
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
|
2020-01-29 03:23:55 -05:00
|
|
|
home = GetEnv("APPDATA");
|
|
|
|
if(!home) {
|
|
|
|
char *env = GetEnv("USERPROFILE");
|
|
|
|
if(env) {
|
|
|
|
char *path = curl_maprintf("%s\\Application Data", env);
|
|
|
|
if(path) {
|
|
|
|
home = strdup(path);
|
|
|
|
curl_free(path);
|
|
|
|
}
|
|
|
|
free(env);
|
|
|
|
}
|
|
|
|
}
|
2003-11-07 12:17:15 -05:00
|
|
|
#endif /* WIN32 */
|
|
|
|
return home;
|
|
|
|
}
|