1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22 #include "tool_setup.h"
23
24 #ifdef HAVE_PWD_H
25 # undef __NO_NET_API /* required for building for AmigaOS */
26 # include <pwd.h>
27 #endif
28
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif
35
36 #include <curl/mprintf.h>
37
38 #include "tool_homedir.h"
39
40 #include "memdebug.h" /* keep this as LAST include */
41
GetEnv(const char * variable)42 static char *GetEnv(const char *variable)
43 {
44 char *dupe, *env;
45
46 env = curl_getenv(variable);
47 if(!env)
48 return NULL;
49
50 dupe = strdup(env);
51 curl_free(env);
52 return dupe;
53 }
54
55 /* return the home directory of the current user as an allocated string */
56
57 /*
58 * The original logic found a home dir to use (by checking a range of
59 * environment variables and last using getpwuid) and returned that for the
60 * parent to use.
61 *
62 * With the XDG_CONFIG_HOME support (added much later than the other), this
63 * variable is treated differently in order to not ruin existing installations
64 * even if this environment variable is set. If this variable is set, and a
65 * file name is set to check, then only if that file name exists in that
66 * directory will it be returned as a "home directory".
67 *
68 * 1. use CURL_HOME if set
69 * 2. use XDG_CONFIG_HOME if set and fname is present
70 * 3. use HOME if set
71 * 4. Non-windows: use getpwuid
72 * 5. Windows: use APPDATA if set
73 * 6. Windows: use "USERPROFILE\Application Data" is set
74 */
75
homedir(const char * fname)76 char *homedir(const char *fname)
77 {
78 char *home;
79
80 home = GetEnv("CURL_HOME");
81 if(home)
82 return home;
83
84 if(fname) {
85 home = GetEnv("XDG_CONFIG_HOME");
86 if(home) {
87 char *c = curl_maprintf("%s" DIR_CHAR "%s", home, fname);
88 if(c) {
89 int fd = open(c, O_RDONLY);
90 curl_free(c);
91 if(fd >= 0) {
92 close(fd);
93 return home;
94 }
95 }
96 free(home);
97 }
98 }
99
100 home = GetEnv("HOME");
101 if(home)
102 return home;
103
104 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
105 {
106 struct passwd *pw = getpwuid(geteuid());
107
108 if(pw) {
109 home = pw->pw_dir;
110 if(home && home[0])
111 home = strdup(home);
112 else
113 home = NULL;
114 }
115 }
116 #endif /* PWD-stuff */
117 #ifdef WIN32
118 home = GetEnv("APPDATA");
119 if(!home) {
120 char *env = GetEnv("USERPROFILE");
121 if(env) {
122 char *path = curl_maprintf("%s\\Application Data", env);
123 if(path) {
124 home = strdup(path);
125 curl_free(path);
126 }
127 free(env);
128 }
129 }
130 #endif /* WIN32 */
131 return home;
132 }
133