tool_homedir.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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.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 "tool_homedir.h"
29 
30 #include "memdebug.h" /* keep this as LAST include */
31 
32 static char *GetEnv(const char *variable, char do_expand)
33 {
34  char *env = NULL;
35 #ifdef WIN32
36  char buf1[1024], buf2[1024];
37  DWORD rc;
38 
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)). */
41 
42  rc = GetEnvironmentVariableA(variable, buf1, sizeof(buf1));
43  if(rc > 0 && rc < sizeof(buf1)) {
44  env = buf1;
45  variable = buf1;
46  }
47  if(do_expand && strchr(variable, '%')) {
48  /* buf2 == variable if not expanded */
49  rc = ExpandEnvironmentStringsA(variable, buf2, sizeof(buf2));
50  if(rc > 0 && rc < sizeof(buf2) &&
51  !strchr(buf2, '%')) /* no vars still unexpanded */
52  env = buf2;
53  }
54 #else
55  (void)do_expand;
56  /* no length control */
57  env = getenv(variable);
58 #endif
59  return (env && env[0]) ? strdup(env) : NULL;
60 }
61 
62 /* return the home directory of the current user as an allocated string */
63 char *homedir(void)
64 {
65  char *home;
66 
67  home = GetEnv("CURL_HOME", FALSE);
68  if(home)
69  return home;
70 
71  home = GetEnv("HOME", FALSE);
72  if(home)
73  return home;
74 
75 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
76  {
77  struct passwd *pw = getpwuid(geteuid());
78 
79  if(pw) {
80  home = pw->pw_dir;
81  if(home && home[0])
82  home = strdup(home);
83  else
84  home = NULL;
85  }
86  }
87 #endif /* PWD-stuff */
88 #ifdef WIN32
89  home = GetEnv("APPDATA", TRUE);
90  if(!home)
91  home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
92  on Win-2K/XP */
93 #endif /* WIN32 */
94  return home;
95 }
#define getenv
Definition: setup-vms.h:52
#define strdup(ptr)
Definition: curl_memory.h:122
#define getpwuid
Definition: setup-vms.h:53
#define FALSE
UNITTEST_START int rc
Definition: unit1301.c:31
static char * GetEnv(const char *variable, char do_expand)
Definition: tool_homedir.c:32
char * homedir(void)
Definition: tool_homedir.c:63
#define TRUE


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16