1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
28 #include "tool_homedir.h"
30 #include "memdebug.h" /* keep this as LAST include */
32 static char *GetEnv(const char *variable, char do_expand)
36 char buf1[1024], buf2[1024];
39 /* Don't use getenv(); it doesn't find variable added after program was
40 * started. Don't accept truncated results (i.e. rc >= sizeof(buf1)). */
42 rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
43 if(rc > 0 && rc < sizeof(buf1)) {
47 if(do_expand && strchr(variable, '%')) {
48 /* buf2 == variable if not expanded */
49 rc = ExpandEnvironmentStrings (variable, buf2, sizeof(buf2));
50 if(rc > 0 && rc < sizeof(buf2) &&
51 !strchr(buf2, '%')) /* no vars still unexpanded */
56 /* no length control */
57 env = getenv(variable);
59 return (env && env[0]) ? strdup(env) : NULL;
62 /* return the home directory of the current user as an allocated string */
67 home = GetEnv("CURL_HOME", FALSE);
71 home = GetEnv("HOME", FALSE);
75 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
77 struct passwd *pw = getpwuid(geteuid());
87 #endif /* PWD-stuff */
89 home = GetEnv("APPDATA", TRUE);
91 home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only