1
/***************************************************************************
3
* Project ___| | | | _ \| |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2004, 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 http://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
* $Id: homedir.c,v 1.4 2004/01/29 13:54:08 bagder Exp $
22
***************************************************************************/
46
#include "../lib/memdebug.h"
50
char *GetEnv(const char *variable, char do_expand)
54
char buf1[1024], buf2[1024];
57
/* Don't use getenv(); it doesn't find variable added after program was
58
* started. Don't accept truncated results (i.e. rc >= sizeof(buf1)). */
60
rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
61
if (rc > 0 && rc < sizeof(buf1)) {
65
if (do_expand && strchr(variable,'%')) {
66
/* buf2 == variable if not expanded */
67
rc = ExpandEnvironmentStrings (variable, buf2, sizeof(buf2));
68
if (rc > 0 && rc < sizeof(buf2) &&
69
!strchr(buf2,'%')) /* no vars still unexpanded */
75
env = getenv(variable);
76
if (env && strcmp("HOME",variable) == 0) {
77
env = decc$translate_vms(env);
80
/* no length control */
81
env = getenv(variable);
84
return (env && env[0])?strdup(env):NULL;
87
/* return the home directory of the current user as an allocated string */
90
char *home = GetEnv("HOME", FALSE);
94
#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
96
struct passwd *pw = getpwuid(geteuid());
100
home = decc$translate_vms(pw->pw_dir);
108
#endif /* PWD-stuff */
110
home = GetEnv("APPDATA", TRUE);
112
home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only