Debian Codegraph @

deps/curl/src/tool_homedir.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, 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.haxx.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 # include <pwd.h>
26 #endif
27
28 #include <curl/mprintf.h>
29
30 #include "tool_homedir.h"
31
32 #include "memdebug.h" /* keep this as LAST include */
33
34 static char *GetEnv(const char *variable)
35 {
36 char *dupe, *env;
37
38 env = curl_getenv(variable);
39 if(!env)
40 return NULL;
41
42 dupe = strdup(env);
43 curl_free(env);
44 return dupe;
45 }
46
47 /* return the home directory of the current user as an allocated string */
48 char *homedir(void)
49 {
50 char *home;
51
52 home = GetEnv("CURL_HOME");
53 if(home)
54 return home;
55
56 home = GetEnv("HOME");
57 if(home)
58 return home;
59
60 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
61 {
62 struct passwd *pw = getpwuid(geteuid());
63
64 if(pw) {
65 home = pw->pw_dir;
66 if(home && home[0])
67 home = strdup(home);
68 else
69 home = NULL;
70 }
71 }
72 #endif /* PWD-stuff */
73 #ifdef WIN32
74 home = GetEnv("APPDATA");
75 if(!home) {
76 char *env = GetEnv("USERPROFILE");
77 if(env) {
78 char *path = curl_maprintf("%s\\Application Data", env);
79 if(path) {
80 home = strdup(path);
81 curl_free(path);
82 }
83 free(env);
84 }
85 }
86 #endif /* WIN32 */
87 return home;
88 }