Update tag value for tizen 2.0 build
[external/curl.git] / src / homedir.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2009, 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 http://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
23 #include "setup.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifdef HAVE_PWD_H
30 #include <pwd.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #ifdef __VMS
36 #include <unixlib.h>
37 #endif
38
39 #include "homedir.h"
40
41 #if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
42 #include "memdebug.h"
43 #endif
44
45 static
46 char *GetEnv(const char *variable, char do_expand)
47 {
48   char *env = NULL;
49 #ifdef WIN32
50   char  buf1[1024], buf2[1024];
51   DWORD rc;
52
53   /* Don't use getenv(); it doesn't find variable added after program was
54    * started. Don't accept truncated results (i.e. rc >= sizeof(buf1)).  */
55
56   rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
57   if (rc > 0 && rc < sizeof(buf1)) {
58     env = buf1;
59     variable = buf1;
60   }
61   if (do_expand && strchr(variable,'%')) {
62     /* buf2 == variable if not expanded */
63     rc = ExpandEnvironmentStrings (variable, buf2, sizeof(buf2));
64     if (rc > 0 && rc < sizeof(buf2) &&
65         !strchr(buf2,'%'))    /* no vars still unexpanded */
66       env = buf2;
67   }
68 #else
69   (void)do_expand;
70 #ifdef __VMS
71   env = getenv(variable);
72   if (env && strcmp("HOME",variable) == 0) {
73         env = decc_translate_vms(env);
74   }
75 #else
76   /* no length control */
77   env = getenv(variable);
78 #endif
79 #endif
80   return (env && env[0])?strdup(env):NULL;
81 }
82
83 /* return the home directory of the current user as an allocated string */
84 char *homedir(void)
85 {
86   char *home;
87
88   home = GetEnv("CURL_HOME", FALSE);
89   if(home)
90     return home;
91
92   home = GetEnv("HOME", FALSE);
93   if(home)
94     return home;
95
96 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
97  {
98    struct passwd *pw = getpwuid(geteuid());
99
100    if (pw) {
101 #ifdef __VMS
102      home = decc_translate_vms(pw->pw_dir);
103 #else
104      home = pw->pw_dir;
105 #endif
106      if (home && home[0])
107        home = strdup(home);
108      else
109        home = NULL;
110    }
111  }
112 #endif /* PWD-stuff */
113 #ifdef WIN32
114   home = GetEnv("APPDATA", TRUE);
115   if(!home)
116     home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
117                                                                on Win-2K/XP */
118 #endif /* WIN32 */
119   return home;
120 }